From 199de5ffca7f066b3939022e827fb14777598e2f Mon Sep 17 00:00:00 2001 From: AlkaidChan <362774405@qq.com> Date: Tue, 31 Oct 2023 21:21:20 +0800 Subject: [PATCH] fix: duplicated register bedis client metrics --- bcs-services/bcs-bscp/pkg/dal/bedis/metric.go | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/bcs-services/bcs-bscp/pkg/dal/bedis/metric.go b/bcs-services/bcs-bscp/pkg/dal/bedis/metric.go index 20c653beaa..73dc11b4f8 100644 --- a/bcs-services/bcs-bscp/pkg/dal/bedis/metric.go +++ b/bcs-services/bcs-bscp/pkg/dal/bedis/metric.go @@ -13,35 +13,46 @@ package bedis import ( + "sync" + "github.com/prometheus/client_golang/prometheus" "bscp.io/pkg/metrics" ) +var ( + metricInstance *metric + once sync.Once +) + func initMetric() *metric { - m := new(metric) - labels := prometheus.Labels{} - m.cmdLagMS = prometheus.NewHistogramVec(prometheus.HistogramOpts{ - Namespace: metrics.Namespace, - Subsystem: metrics.BedisCmdSubSys, - Name: "lag_milliseconds", - Help: "the lags(milliseconds) to exec a bedis command", - ConstLabels: labels, - Buckets: []float64{1, 2, 3, 4, 5, 7, 9, 12, 14, 16, 18, 20, 40, 60, 80, 100, 150, 200, 500}, - }, []string{"cmd"}) - metrics.Register().MustRegister(m.cmdLagMS) - - m.errCounter = prometheus.NewCounterVec( - prometheus.CounterOpts{ + once.Do(func() { + m := new(metric) + labels := prometheus.Labels{} + m.cmdLagMS = prometheus.NewHistogramVec(prometheus.HistogramOpts{ Namespace: metrics.Namespace, Subsystem: metrics.BedisCmdSubSys, - Name: "total_err_count", - Help: "the total error count when exec a bedis command", + Name: "lag_milliseconds", + Help: "the lags(milliseconds) to exec a bedis command", ConstLabels: labels, + Buckets: []float64{1, 2, 3, 4, 5, 7, 9, 12, 14, 16, 18, 20, 40, 60, 80, 100, 150, 200, 500}, }, []string{"cmd"}) - metrics.Register().MustRegister(m.errCounter) + metrics.Register().MustRegister(m.cmdLagMS) + + m.errCounter = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Namespace: metrics.Namespace, + Subsystem: metrics.BedisCmdSubSys, + Name: "total_err_count", + Help: "the total error count when exec a bedis command", + ConstLabels: labels, + }, []string{"cmd"}) + metrics.Register().MustRegister(m.errCounter) + + metricInstance = m - return m + }) + return metricInstance } type metric struct {