Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New metric holder #411

Merged
merged 35 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e158638
New metric holder
Jul 6, 2023
1697b7e
Rename metric.Ctl constructor
Jul 6, 2023
77027c1
Merge branch 'master' into 410-metricholder
Jul 6, 2023
794754a
Fix prometheus import name
Jul 6, 2023
b2efef4
Fix build
Jul 6, 2023
9187373
Add tests
Jul 7, 2023
fba676d
Refactor metric controller
Jul 7, 2023
7dde5dd
Merge branch 'master' into 410-metricholder
Jul 7, 2023
039fdf4
Merge branch 'master' into 410-metricholder
Jul 10, 2023
2c19e3b
Remove updateTime & beautify
Jul 28, 2023
cd3836a
Merge branch 'master' into 410-metricholder
Jul 28, 2023
8a10adf
Fix nowTime & tests
Jul 31, 2023
0252a2e
Refactor metric controller
Aug 25, 2023
be4f1c5
Merge branch 'master' into 410-metricholder
Aug 25, 2023
87556d1
Merge branch 'master' into 410-metricholder
Aug 25, 2023
0d6f7b0
Add xtime package
Aug 28, 2023
e5aff94
Use xtime in metric holder
Aug 28, 2023
46e3b4d
Merge branch 'master' into 410-metricholder
Aug 28, 2023
08f0a3e
Rework holder & wrappers
Sep 18, 2023
f98cb0d
Merge branch 'master' into 410-metricholder
Oct 6, 2023
6203975
Rework
Oct 17, 2023
49774ae
Merge branch 'master' into 410-metricholder
Oct 17, 2023
a163e8c
Fix go mod
Oct 17, 2023
a6262ef
Merge branch 'master' into 410-metricholder
Nov 1, 2023
ea087c0
Merge branch 'master' into 410-metricholder
Dec 13, 2023
400d909
Some performance optimizations to the MetricHolder (#568)
vadimalekseev Jan 12, 2024
07e8749
Add metric_hold_duration to pipeline settings
Jan 12, 2024
16484e0
go mod tidy
Jan 12, 2024
2210454
Merge branch 'master' into 410-metricholder
Jan 12, 2024
27f1340
Fix tests
Jan 12, 2024
0afaadf
Merge branch 'master' into 410-metricholder
Jan 25, 2024
e2485ca
Fix after merge
Jan 25, 2024
fdaa4d0
Merge branch 'master' into 410-metricholder
Jan 29, 2024
0b34b6d
Merge branch 'master' into 410-metricholder
Jan 31, 2024
7cd0ab8
Fix tests
Jan 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fd/file.d.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (f *FileD) Start() {
}

func (f *FileD) initMetrics() {
f.metricCtl = metric.New("file_d", f.registry)
f.versionMetric = f.metricCtl.RegisterCounter("version", "", "version")
f.metricCtl = metric.NewCtl("file_d", f.registry)
f.versionMetric = f.metricCtl.RegisterCounterVec("version", "", "version")
f.versionMetric.WithLabelValues(buildinfo.Version).Inc()
}

Expand Down
181 changes: 118 additions & 63 deletions metric/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,90 +19,145 @@ type Ctl struct {
subsystem string
register *prometheus.Registry

counters map[string]*prometheus.CounterVec
counterMx *sync.Mutex
counters map[string]prometheus.Counter
counterMx *sync.RWMutex
vadimalekseev marked this conversation as resolved.
Show resolved Hide resolved

gauges map[string]*prometheus.GaugeVec
gaugeMx *sync.Mutex
counterVecs map[string]*prometheus.CounterVec
counterVecMx *sync.RWMutex

histograms map[string]*prometheus.HistogramVec
histogramMx *sync.Mutex
gauges map[string]prometheus.Gauge
gaugeMx *sync.RWMutex

gaugeVecs map[string]*prometheus.GaugeVec
gaugeVecMx *sync.RWMutex

histograms map[string]prometheus.Histogram
histogramMx *sync.RWMutex

histogramVecs map[string]*prometheus.HistogramVec
histogramVecMx *sync.RWMutex
}

func New(subsystem string, registry *prometheus.Registry) *Ctl {
func NewCtl(subsystem string, registry *prometheus.Registry) *Ctl {
ctl := &Ctl{
subsystem: subsystem,
counters: make(map[string]*prometheus.CounterVec),
counterMx: new(sync.Mutex),
gauges: make(map[string]*prometheus.GaugeVec),
gaugeMx: new(sync.Mutex),
histograms: make(map[string]*prometheus.HistogramVec),
histogramMx: new(sync.Mutex),
register: registry,
subsystem: subsystem,
register: registry,
counters: make(map[string]prometheus.Counter),
counterMx: new(sync.RWMutex),
counterVecs: make(map[string]*prometheus.CounterVec),
counterVecMx: new(sync.RWMutex),
gauges: make(map[string]prometheus.Gauge),
gaugeMx: new(sync.RWMutex),
gaugeVecs: make(map[string]*prometheus.GaugeVec),
gaugeVecMx: new(sync.RWMutex),
histograms: make(map[string]prometheus.Histogram),
histogramMx: new(sync.RWMutex),
histogramVecs: make(map[string]*prometheus.HistogramVec),
histogramVecMx: new(sync.RWMutex),
}
return ctl
}

func (mc *Ctl) RegisterCounter(name, help string, labels ...string) *prometheus.CounterVec {
mc.counterMx.Lock()
defer mc.counterMx.Unlock()
func (mc *Ctl) RegisterCounter(name, help string) prometheus.Counter {
newCounter := func() prometheus.Counter {
return prometheus.NewCounter(prometheus.CounterOpts{
Namespace: PromNamespace,
Subsystem: mc.subsystem,
Name: name,
Help: help,
})
}

if metric, hasCounter := mc.counters[name]; hasCounter {
return metric
return registerMetric(mc.register, name, mc.counters, mc.counterMx, newCounter)
}

func (mc *Ctl) RegisterCounterVec(name, help string, labels ...string) *prometheus.CounterVec {
newCounterVec := func() *prometheus.CounterVec {
return prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: PromNamespace,
Subsystem: mc.subsystem,
Name: name,
Help: help,
}, labels)
}

promCounter := prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: PromNamespace,
Subsystem: mc.subsystem,
Name: name,
Help: help,
}, labels)

mc.counters[name] = promCounter
mc.register.Unregister(promCounter)
mc.register.MustRegister(promCounter)
return promCounter
return registerMetric(mc.register, name, mc.counterVecs, mc.counterVecMx, newCounterVec)
}

func (mc *Ctl) RegisterGauge(name, help string, labels ...string) *prometheus.GaugeVec {
mc.gaugeMx.Lock()
defer mc.gaugeMx.Unlock()
func (mc *Ctl) RegisterGauge(name, help string) prometheus.Gauge {
newGauge := func() prometheus.Gauge {
return prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: PromNamespace,
Subsystem: mc.subsystem,
Name: name,
Help: help,
})
}

if metric, hasGauge := mc.gauges[name]; hasGauge {
return metric
return registerMetric(mc.register, name, mc.gauges, mc.gaugeMx, newGauge)
}

func (mc *Ctl) RegisterGaugeVec(name, help string, labels ...string) *prometheus.GaugeVec {
newGaugeVec := func() *prometheus.GaugeVec {
return prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: PromNamespace,
Subsystem: mc.subsystem,
Name: name,
Help: help,
}, labels)
}

promGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: PromNamespace,
Subsystem: mc.subsystem,
Name: name,
Help: help,
}, labels)

mc.gauges[name] = promGauge
mc.register.Unregister(promGauge)
mc.register.MustRegister(promGauge)
return promGauge
return registerMetric(mc.register, name, mc.gaugeVecs, mc.gaugeVecMx, newGaugeVec)
}

func (mc *Ctl) RegisterHistogram(name, help string, buckets []float64, labels ...string) *prometheus.HistogramVec {
mc.histogramMx.Lock()
defer mc.histogramMx.Unlock()
func (mc *Ctl) RegisterHistogram(name, help string, buckets []float64) prometheus.Histogram {
newHistogram := func() prometheus.Histogram {
return prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: PromNamespace,
Subsystem: mc.subsystem,
Name: name,
Help: help,
Buckets: buckets,
})
}

return registerMetric(mc.register, name, mc.histograms, mc.histogramMx, newHistogram)
}

func (mc *Ctl) RegisterHistogramVec(name, help string, buckets []float64, labels ...string) *prometheus.HistogramVec {
newHistogramVec := func() *prometheus.HistogramVec {
return prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: PromNamespace,
Subsystem: mc.subsystem,
Name: name,
Help: help,
Buckets: buckets,
}, labels)
}

if metric, hasHistogram := mc.histograms[name]; hasHistogram {
return registerMetric(mc.register, name, mc.histogramVecs, mc.histogramVecMx, newHistogramVec)
}

func registerMetric[T prometheus.Collector](register *prometheus.Registry, name string,
metrics map[string]T, mx *sync.RWMutex, newMetric func() T,
) T {
mx.RLock()
metric, has := metrics[name]
mx.RUnlock()
if has {
return metric
}

promHistogram := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: PromNamespace,
Subsystem: mc.subsystem,
Name: name,
Help: help,
Buckets: buckets,
}, labels)

mc.histograms[name] = promHistogram
mc.register.Unregister(promHistogram)
mc.register.MustRegister(promHistogram)
return promHistogram
mx.Lock()
defer mx.Unlock()
metric, has = metrics[name]
if !has {
metric = newMetric()

metrics[name] = metric
register.Unregister(metric)
register.MustRegister(metric)
}

return metric
}
Loading