From 80ea6b78211c03ed8f31968f752607fda0031b00 Mon Sep 17 00:00:00 2001 From: rasmus Date: Sun, 6 Nov 2022 19:04:03 +0200 Subject: [PATCH] globalize mongo metrics multiple calls for options would duplicate/override --- cmd/mongo.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cmd/mongo.go b/cmd/mongo.go index 7a70c60..5948d34 100644 --- a/cmd/mongo.go +++ b/cmd/mongo.go @@ -11,8 +11,8 @@ import ( "go.uber.org/zap" ) -func mongoMonitoredClientOptions(l *zap.Logger) *mongoOpt.ClientOptions { - promDbHeartbeat := promauto.NewHistogramVec(prom.HistogramOpts{ +var ( + promDbHeartbeat = promauto.NewHistogramVec(prom.HistogramOpts{ Namespace: PrometheusPrefix, Subsystem: "database", Name: "heartbeat_time", @@ -20,7 +20,7 @@ func mongoMonitoredClientOptions(l *zap.Logger) *mongoOpt.ClientOptions { Buckets: []float64{1}, }, []string{"connection_id"}) - promDbCmd := promauto.NewHistogramVec(prom.HistogramOpts{ + promDbCmd = promauto.NewHistogramVec(prom.HistogramOpts{ Namespace: PrometheusPrefix, Subsystem: "database", Name: "operation_latency", // "command_time", @@ -28,13 +28,15 @@ func mongoMonitoredClientOptions(l *zap.Logger) *mongoOpt.ClientOptions { Buckets: []float64{0.1, 0.2, 0.5, 1, 5, 10, 50}, }, []string{"connection_id", "command_name"}) - promDbCmdErr := promauto.NewCounterVec(prom.CounterOpts{ + promDbCmdErr = promauto.NewCounterVec(prom.CounterOpts{ Namespace: PrometheusPrefix, Subsystem: "database", Name: "errors", Help: "Failed commands (also reflected elsewhere)", }, []string{"connection_id", "command_name"}) +) +func mongoMonitoredClientOptions(l *zap.Logger) *mongoOpt.ClientOptions { return mongoOpt.Client(). SetServerMonitor(&mongoEvent.ServerMonitor{ ServerHeartbeatSucceeded: func(ev *mongoEvent.ServerHeartbeatSucceededEvent) {