Skip to content

Commit 3b9cfce

Browse files
authored
Merge metric controller and holder (#925)
* merge metric controller and holder * decrease SecondsBucketsDetailedNano buckets count
1 parent e1d8885 commit 3b9cfce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+401
-355
lines changed

fd/file.d.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type FileD struct {
3636

3737
// file_d metrics
3838

39-
versionMetric *prometheus.GaugeVec
39+
versionMetric *metric.GaugeVec
4040
}
4141

4242
func New(config *cfg.Config, httpAddr string) *FileD {
@@ -63,7 +63,7 @@ func (f *FileD) Start() {
6363
}
6464

6565
func (f *FileD) initMetrics() {
66-
f.metricCtl = metric.NewCtl("file_d", f.registry)
66+
f.metricCtl = metric.NewCtl("file_d", f.registry, 0)
6767
f.versionMetric = f.metricCtl.RegisterGaugeVec("version", "", "version")
6868
f.versionMetric.WithLabelValues(buildinfo.Version).Inc()
6969
}

metric/controller.go

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package metric
22

33
import (
44
"sync"
5+
"time"
56

67
"github.com/prometheus/client_golang/prometheus"
78
)
@@ -11,73 +12,95 @@ const (
1112
)
1213

1314
var (
14-
SecondsBucketsDetailedNano = prometheus.ExponentialBuckets(0.000005, 2, 19) // covers range from 5ns to 1.3ms
15-
SecondsBucketsDetailed = prometheus.ExponentialBuckets(0.0005, 2, 16) // covers range from 500us to 16.384s
16-
SecondsBucketsLong = prometheus.ExponentialBuckets(0.005, 2, 16) // covers range from 5ms to 163.84s
15+
SecondsBucketsDetailedNano = prometheus.ExponentialBuckets(0.000005, 2.18, 17) // covers range from 5µs to 1.3s
16+
SecondsBucketsDetailed = prometheus.ExponentialBuckets(0.0005, 2, 16) // covers range from 500µs to 16.384s
17+
SecondsBucketsLong = prometheus.ExponentialBuckets(0.005, 2, 16) // covers range from 5000µs to 163.84s
1718
)
1819

1920
type Ctl struct {
2021
subsystem string
2122
register *prometheus.Registry
2223

24+
holder *Holder
2325
metrics map[string]prometheus.Collector
2426
mu sync.RWMutex
2527
}
2628

27-
func NewCtl(subsystem string, registry *prometheus.Registry) *Ctl {
29+
func NewCtl(subsystem string, registry *prometheus.Registry, metricHoldDuration time.Duration) *Ctl {
2830
ctl := &Ctl{
2931
subsystem: subsystem,
3032
register: registry,
3133
metrics: make(map[string]prometheus.Collector),
3234
}
35+
36+
if metricHoldDuration != 0 {
37+
ctl.holder = NewHolder(metricHoldDuration)
38+
}
39+
3340
return ctl
3441
}
3542

36-
func (mc *Ctl) RegisterCounter(name, help string) prometheus.Counter {
43+
func (mc *Ctl) Maintenance() {
44+
if mc.holder == nil {
45+
return
46+
}
47+
48+
mc.holder.maintenance()
49+
}
50+
51+
func (mc *Ctl) AddToHolder(mv heldMetricVec) {
52+
if mc.holder == nil {
53+
return
54+
}
55+
56+
mc.holder.addMetricVec(mv)
57+
}
58+
59+
func (mc *Ctl) RegisterCounter(name, help string) *Counter {
3760
counter := prometheus.NewCounter(prometheus.CounterOpts{
3861
Namespace: PromNamespace,
3962
Subsystem: mc.subsystem,
4063
Name: name,
4164
Help: help,
4265
})
4366

44-
return mc.registerMetric(name, counter).(prometheus.Counter)
67+
return newCounter(mc.registerMetric(name, counter).(prometheus.Counter))
4568
}
4669

47-
func (mc *Ctl) RegisterCounterVec(name, help string, labels ...string) *prometheus.CounterVec {
70+
func (mc *Ctl) RegisterCounterVec(name, help string, labels ...string) *CounterVec {
4871
counterVec := prometheus.NewCounterVec(prometheus.CounterOpts{
4972
Namespace: PromNamespace,
5073
Subsystem: mc.subsystem,
5174
Name: name,
5275
Help: help,
5376
}, labels)
5477

55-
return mc.registerMetric(name, counterVec).(*prometheus.CounterVec)
78+
return newCounterVec(mc.registerMetric(name, counterVec).(*prometheus.CounterVec))
5679
}
5780

58-
func (mc *Ctl) RegisterGauge(name, help string) prometheus.Gauge {
81+
func (mc *Ctl) RegisterGauge(name, help string) *Gauge {
5982
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
6083
Namespace: PromNamespace,
6184
Subsystem: mc.subsystem,
6285
Name: name,
6386
Help: help,
6487
})
6588

66-
return mc.registerMetric(name, gauge).(prometheus.Gauge)
89+
return newGauge(mc.registerMetric(name, gauge).(prometheus.Gauge))
6790
}
6891

69-
func (mc *Ctl) RegisterGaugeVec(name, help string, labels ...string) *prometheus.GaugeVec {
92+
func (mc *Ctl) RegisterGaugeVec(name, help string, labels ...string) *GaugeVec {
7093
gaugeVec := prometheus.NewGaugeVec(prometheus.GaugeOpts{
7194
Namespace: PromNamespace,
7295
Subsystem: mc.subsystem,
7396
Name: name,
7497
Help: help,
7598
}, labels)
7699

77-
return mc.registerMetric(name, gaugeVec).(*prometheus.GaugeVec)
100+
return newGaugeVec(mc.registerMetric(name, gaugeVec).(*prometheus.GaugeVec))
78101
}
79102

80-
func (mc *Ctl) RegisterHistogram(name, help string, buckets []float64) prometheus.Histogram {
103+
func (mc *Ctl) RegisterHistogram(name, help string, buckets []float64) *Histogram {
81104
histogram := prometheus.NewHistogram(prometheus.HistogramOpts{
82105
Namespace: PromNamespace,
83106
Subsystem: mc.subsystem,
@@ -86,10 +109,10 @@ func (mc *Ctl) RegisterHistogram(name, help string, buckets []float64) prometheu
86109
Buckets: buckets,
87110
})
88111

89-
return mc.registerMetric(name, histogram).(prometheus.Histogram)
112+
return newHistogram(mc.registerMetric(name, histogram).(prometheus.Histogram))
90113
}
91114

92-
func (mc *Ctl) RegisterHistogramVec(name, help string, buckets []float64, labels ...string) *prometheus.HistogramVec {
115+
func (mc *Ctl) RegisterHistogramVec(name, help string, buckets []float64, labels ...string) *HistogramVec {
93116
histogramVec := prometheus.NewHistogramVec(prometheus.HistogramOpts{
94117
Namespace: PromNamespace,
95118
Subsystem: mc.subsystem,
@@ -98,7 +121,7 @@ func (mc *Ctl) RegisterHistogramVec(name, help string, buckets []float64, labels
98121
Buckets: buckets,
99122
}, labels)
100123

101-
return mc.registerMetric(name, histogramVec).(*prometheus.HistogramVec)
124+
return newHistogramVec(mc.registerMetric(name, histogramVec).(*prometheus.HistogramVec))
102125
}
103126

104127
func (mc *Ctl) registerMetric(name string, newMetric prometheus.Collector) prometheus.Collector {

metric/counter.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package metric
2+
3+
import (
4+
"time"
5+
6+
"github.com/prometheus/client_golang/prometheus"
7+
"github.com/prometheus/client_golang/prometheus/testutil"
8+
)
9+
10+
type Counter struct {
11+
*heldMetric[prometheus.Counter]
12+
}
13+
14+
func newCounter(c prometheus.Counter) *Counter {
15+
return &Counter{
16+
heldMetric: newHeldMetric(nil, c),
17+
}
18+
}
19+
20+
func (c *Counter) Inc() {
21+
c.metric.Inc()
22+
c.updateUsage()
23+
}
24+
25+
func (c *Counter) Add(v float64) {
26+
c.metric.Add(v)
27+
c.updateUsage()
28+
}
29+
30+
// should only be used in tests
31+
func (c *Counter) ToFloat64() float64 {
32+
return testutil.ToFloat64(c.metric)
33+
}
34+
35+
type CounterVec struct {
36+
store *heldMetricsStore[prometheus.Counter]
37+
vec *prometheus.CounterVec
38+
}
39+
40+
func newCounterVec(cv *prometheus.CounterVec) *CounterVec {
41+
return &CounterVec{
42+
vec: cv,
43+
store: newHeldMetricsStore[prometheus.Counter](),
44+
}
45+
}
46+
47+
func (cv *CounterVec) WithLabelValues(lvs ...string) *Counter {
48+
return &Counter{
49+
heldMetric: cv.store.GetOrCreate(lvs, cv.vec.WithLabelValues),
50+
}
51+
}
52+
53+
func (cv *CounterVec) DeleteLabelValues(lvs ...string) bool {
54+
return cv.store.Delete(lvs, cv.vec)
55+
}
56+
57+
func (cv *CounterVec) DeleteOldMetrics(holdDuration time.Duration) {
58+
cv.store.DeleteOldMetrics(holdDuration, cv.vec)
59+
}

metric/gauge.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package metric
2+
3+
import (
4+
"time"
5+
6+
"github.com/prometheus/client_golang/prometheus"
7+
"github.com/prometheus/client_golang/prometheus/testutil"
8+
)
9+
10+
type Gauge struct {
11+
*heldMetric[prometheus.Gauge]
12+
}
13+
14+
func newGauge(c prometheus.Gauge) *Gauge {
15+
return &Gauge{
16+
heldMetric: newHeldMetric(nil, c),
17+
}
18+
}
19+
20+
func (g *Gauge) Set(v float64) {
21+
g.metric.Set(v)
22+
g.updateUsage()
23+
}
24+
25+
func (g *Gauge) Inc() {
26+
g.metric.Inc()
27+
g.updateUsage()
28+
}
29+
30+
func (g *Gauge) Dec() {
31+
g.metric.Dec()
32+
g.updateUsage()
33+
}
34+
35+
func (g *Gauge) Add(v float64) {
36+
g.metric.Add(v)
37+
g.updateUsage()
38+
}
39+
40+
func (g *Gauge) Sub(v float64) {
41+
g.metric.Sub(v)
42+
g.updateUsage()
43+
}
44+
45+
// should only be used in tests
46+
func (c *Gauge) ToFloat64() float64 {
47+
return testutil.ToFloat64(c.metric)
48+
}
49+
50+
type GaugeVec struct {
51+
store *heldMetricsStore[prometheus.Gauge]
52+
vec *prometheus.GaugeVec
53+
}
54+
55+
func newGaugeVec(gv *prometheus.GaugeVec) *GaugeVec {
56+
return &GaugeVec{
57+
vec: gv,
58+
store: newHeldMetricsStore[prometheus.Gauge](),
59+
}
60+
}
61+
62+
func (gv *GaugeVec) WithLabelValues(lvs ...string) *Gauge {
63+
return &Gauge{
64+
heldMetric: gv.store.GetOrCreate(lvs, gv.vec.WithLabelValues),
65+
}
66+
}
67+
68+
func (cv *GaugeVec) DeleteLabelValues(lvs ...string) bool {
69+
return cv.store.Delete(lvs, cv.vec)
70+
}
71+
72+
func (gv *GaugeVec) DeleteOldMetrics(holdDuration time.Duration) {
73+
gv.store.DeleteOldMetrics(holdDuration, gv.vec)
74+
}

metric/held_counter.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

metric/held_gauge.go

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)