prom mongo: use seconds instead of nanoseconds

This commit is contained in:
rasmus 2022-11-06 20:37:43 +02:00
parent 80ea6b7821
commit 532575a87b
1 changed files with 7 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package logmower
import (
"context"
"fmt"
"time"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
@ -16,15 +17,15 @@ var (
Namespace: PrometheusPrefix,
Subsystem: "database",
Name: "heartbeat_time",
Help: "Time in ns for succeeded heartbeat, or 0 on failure",
Buckets: []float64{1},
Help: "Time in seconds for succeeded heartbeat, or 0 on failure",
Buckets: []float64{0.1, 0.2, 0.5, 1, 5, 10, 50},
}, []string{"connection_id"})
promDbCmd = promauto.NewHistogramVec(prom.HistogramOpts{
Namespace: PrometheusPrefix,
Subsystem: "database",
Name: "operation_latency", // "command_time",
Help: "Time in ns of commands",
Help: "Time in seconds of commands",
Buckets: []float64{0.1, 0.2, 0.5, 1, 5, 10, 50},
}, []string{"connection_id", "command_name"})
@ -40,7 +41,7 @@ func mongoMonitoredClientOptions(l *zap.Logger) *mongoOpt.ClientOptions {
return mongoOpt.Client().
SetServerMonitor(&mongoEvent.ServerMonitor{
ServerHeartbeatSucceeded: func(ev *mongoEvent.ServerHeartbeatSucceededEvent) {
promDbHeartbeat.WithLabelValues(ev.ConnectionID).Observe(float64(ev.DurationNanos))
promDbHeartbeat.WithLabelValues(ev.ConnectionID).Observe(time.Duration(ev.DurationNanos).Seconds())
},
ServerHeartbeatFailed: func(ev *mongoEvent.ServerHeartbeatFailedEvent) {
promDbHeartbeat.WithLabelValues(ev.ConnectionID).Observe(0)
@ -49,10 +50,10 @@ func mongoMonitoredClientOptions(l *zap.Logger) *mongoOpt.ClientOptions {
}).
SetMonitor(&mongoEvent.CommandMonitor{
Succeeded: func(_ context.Context, ev *mongoEvent.CommandSucceededEvent) {
promDbCmd.WithLabelValues(ev.ConnectionID, ev.CommandName).Observe(float64(ev.DurationNanos))
promDbCmd.WithLabelValues(ev.ConnectionID, ev.CommandName).Observe(time.Duration(ev.DurationNanos).Seconds())
},
Failed: func(_ context.Context, ev *mongoEvent.CommandFailedEvent) {
promDbCmd.WithLabelValues(ev.ConnectionID, ev.CommandName).Observe(float64(ev.DurationNanos))
promDbCmd.WithLabelValues(ev.ConnectionID, ev.CommandName).Observe(time.Duration(ev.DurationNanos).Seconds())
promDbCmdErr.WithLabelValues(ev.ConnectionID, ev.CommandName).Add(1)
l.Error("database command", zap.Error(fmt.Errorf("%s", ev.Failure)), zap.String("connection_id", ev.ConnectionID), zap.String("command_name", ev.CommandName)) // TODO: https://github.com/mongodb/mongo-go-driver/pull/1105