57 lines
2.2 KiB
Go
57 lines
2.2 KiB
Go
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"
|
|
)
|
|
|
|
func mongoMonitoredClientOptions(l *zap.Logger) *mongoOpt.ClientOptions {
|
|
promMongoHeartbeat := promauto.NewHistogramVec(prom.HistogramOpts{
|
|
Subsystem: "mongo",
|
|
Name: "heartbeat",
|
|
Help: "Time in ns for succeeded heartbeat, or 0 on failure",
|
|
Buckets: []float64{1},
|
|
}, []string{"connection_id"})
|
|
|
|
promMongoCmd := promauto.NewHistogramVec(prom.HistogramOpts{
|
|
Subsystem: "mongo",
|
|
Name: "command",
|
|
Help: "Time in ns of commands",
|
|
Buckets: prom.DefBuckets,
|
|
}, []string{"connection_id", "command_name"})
|
|
|
|
promMongoCmdErr := promauto.NewCounterVec(prom.CounterOpts{
|
|
Subsystem: "mongo",
|
|
Name: "errors_count",
|
|
Help: "Count of failed commands",
|
|
}, []string{"connection_id", "command_name"})
|
|
|
|
return mongoOpt.Client().
|
|
SetServerMonitor(&mongoEvent.ServerMonitor{
|
|
ServerHeartbeatSucceeded: func(ev *mongoEvent.ServerHeartbeatSucceededEvent) {
|
|
promMongoHeartbeat.WithLabelValues(ev.ConnectionID).Observe(float64(ev.DurationNanos))
|
|
},
|
|
ServerHeartbeatFailed: func(ev *mongoEvent.ServerHeartbeatFailedEvent) {
|
|
promMongoHeartbeat.WithLabelValues(ev.ConnectionID).Observe(0)
|
|
l.Error("mongo heartbeat", zap.Error(ev.Failure), zap.String("connection_id", ev.ConnectionID))
|
|
},
|
|
}).
|
|
SetMonitor(&mongoEvent.CommandMonitor{
|
|
Succeeded: func(_ context.Context, ev *mongoEvent.CommandSucceededEvent) {
|
|
promMongoCmd.WithLabelValues(ev.ConnectionID, ev.CommandName).Observe(float64(ev.DurationNanos))
|
|
},
|
|
Failed: func(_ context.Context, ev *mongoEvent.CommandFailedEvent) {
|
|
promMongoCmd.WithLabelValues(ev.ConnectionID, ev.CommandName).Observe(float64(ev.DurationNanos))
|
|
|
|
promMongoCmdErr.WithLabelValues(ev.ConnectionID, ev.CommandName).Add(1)
|
|
l.Error("mongo 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
|
|
},
|
|
})
|
|
}
|