mirror of
https://github.com/kopia/kopia.git
synced 2026-05-11 16:25:13 -04:00
We can't enable checklocks on CI yet until https://github.com/google/gvisor/pull/8807 is merged upstream. This was tested with private build of checklocks with this patch applied and the results were clean.
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package metrics
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
"golang.org/x/exp/maps"
|
|
)
|
|
|
|
const (
|
|
prometheusCounterSuffix = "_total"
|
|
prometheusPrefix = "kopia_"
|
|
)
|
|
|
|
//nolint:gochecknoglobals
|
|
var (
|
|
promCacheMutex sync.Mutex
|
|
// +checklocks:promCacheMutex
|
|
promCounters = map[string]*prometheus.CounterVec{}
|
|
// +checklocks:promCacheMutex
|
|
promHistograms = map[string]*prometheus.HistogramVec{}
|
|
)
|
|
|
|
func getPrometheusCounter(opts prometheus.CounterOpts, labels map[string]string) prometheus.Counter {
|
|
promCacheMutex.Lock()
|
|
defer promCacheMutex.Unlock()
|
|
|
|
prom := promCounters[opts.Name]
|
|
if prom == nil {
|
|
prom = promauto.NewCounterVec(opts, maps.Keys(labels))
|
|
|
|
promCounters[opts.Name] = prom
|
|
}
|
|
|
|
return prom.WithLabelValues(maps.Values(labels)...)
|
|
}
|
|
|
|
func getPrometheusHistogram(opts prometheus.HistogramOpts, labels map[string]string) prometheus.Observer {
|
|
promCacheMutex.Lock()
|
|
defer promCacheMutex.Unlock()
|
|
|
|
prom := promHistograms[opts.Name]
|
|
if prom == nil {
|
|
prom = promauto.NewHistogramVec(opts, maps.Keys(labels))
|
|
|
|
promHistograms[opts.Name] = prom
|
|
}
|
|
|
|
return prom.WithLabelValues(maps.Values(labels)...)
|
|
}
|