2022-10-11 07:55:22 +00:00
|
|
|
package logmower
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
prom "github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
mongoEvent "go.mongodb.org/mongo-driver/event"
|
|
|
|
mongoOpt "go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-11-06 17:04:03 +00:00
|
|
|
var (
|
|
|
|
promDbHeartbeat = promauto.NewHistogramVec(prom.HistogramOpts{
|
2022-11-06 16:41:10 +00:00
|
|
|
Namespace: PrometheusPrefix,
|
|
|
|
Subsystem: "database",
|
2022-11-06 13:57:05 +00:00
|
|
|
Name: "heartbeat_time",
|
2022-10-11 07:55:22 +00:00
|
|
|
Help: "Time in ns for succeeded heartbeat, or 0 on failure",
|
|
|
|
Buckets: []float64{1},
|
|
|
|
}, []string{"connection_id"})
|
|
|
|
|
2022-11-06 17:04:03 +00:00
|
|
|
promDbCmd = promauto.NewHistogramVec(prom.HistogramOpts{
|
2022-11-06 16:41:10 +00:00
|
|
|
Namespace: PrometheusPrefix,
|
|
|
|
Subsystem: "database",
|
|
|
|
Name: "operation_latency", // "command_time",
|
2022-10-11 07:55:22 +00:00
|
|
|
Help: "Time in ns of commands",
|
2022-11-06 16:41:10 +00:00
|
|
|
Buckets: []float64{0.1, 0.2, 0.5, 1, 5, 10, 50},
|
2022-10-11 07:55:22 +00:00
|
|
|
}, []string{"connection_id", "command_name"})
|
|
|
|
|
2022-11-06 17:04:03 +00:00
|
|
|
promDbCmdErr = promauto.NewCounterVec(prom.CounterOpts{
|
2022-11-06 16:41:10 +00:00
|
|
|
Namespace: PrometheusPrefix,
|
|
|
|
Subsystem: "database",
|
2022-11-06 13:57:05 +00:00
|
|
|
Name: "errors",
|
2022-11-06 16:41:10 +00:00
|
|
|
Help: "Failed commands (also reflected elsewhere)",
|
2022-10-11 07:55:22 +00:00
|
|
|
}, []string{"connection_id", "command_name"})
|
2022-11-06 17:04:03 +00:00
|
|
|
)
|
2022-10-11 07:55:22 +00:00
|
|
|
|
2022-11-06 17:04:03 +00:00
|
|
|
func mongoMonitoredClientOptions(l *zap.Logger) *mongoOpt.ClientOptions {
|
2022-10-11 07:55:22 +00:00
|
|
|
return mongoOpt.Client().
|
|
|
|
SetServerMonitor(&mongoEvent.ServerMonitor{
|
|
|
|
ServerHeartbeatSucceeded: func(ev *mongoEvent.ServerHeartbeatSucceededEvent) {
|
2022-11-06 16:41:10 +00:00
|
|
|
promDbHeartbeat.WithLabelValues(ev.ConnectionID).Observe(float64(ev.DurationNanos))
|
2022-10-11 07:55:22 +00:00
|
|
|
},
|
|
|
|
ServerHeartbeatFailed: func(ev *mongoEvent.ServerHeartbeatFailedEvent) {
|
2022-11-06 16:41:10 +00:00
|
|
|
promDbHeartbeat.WithLabelValues(ev.ConnectionID).Observe(0)
|
|
|
|
l.Error("database heartbeat", zap.Error(ev.Failure), zap.String("connection_id", ev.ConnectionID))
|
2022-10-11 07:55:22 +00:00
|
|
|
},
|
|
|
|
}).
|
|
|
|
SetMonitor(&mongoEvent.CommandMonitor{
|
|
|
|
Succeeded: func(_ context.Context, ev *mongoEvent.CommandSucceededEvent) {
|
2022-11-06 16:41:10 +00:00
|
|
|
promDbCmd.WithLabelValues(ev.ConnectionID, ev.CommandName).Observe(float64(ev.DurationNanos))
|
2022-10-11 07:55:22 +00:00
|
|
|
},
|
|
|
|
Failed: func(_ context.Context, ev *mongoEvent.CommandFailedEvent) {
|
2022-11-06 16:41:10 +00:00
|
|
|
promDbCmd.WithLabelValues(ev.ConnectionID, ev.CommandName).Observe(float64(ev.DurationNanos))
|
2022-10-11 07:55:22 +00:00
|
|
|
|
2022-11-06 16:41:10 +00:00
|
|
|
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
|
2022-10-11 07:55:22 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|