logmower-shipper/pkg/mongo/metrics.go

35 lines
1.2 KiB
Go
Raw Normal View History

2022-11-09 16:07:28 +00:00
package mongo
import (
"log"
"time"
2022-11-09 16:07:28 +00:00
"git.k-space.ee/k-space/logmower-shipper/pkg/globals"
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"
)
2022-11-09 16:07:28 +00:00
const promSubsystem = "database"
2022-11-09 19:55:54 +00:00
var promDbHeartbeat = promauto.NewHistogramVec(prom.HistogramOpts{
Namespace: globals.PrometheusPrefix, Subsystem: promSubsystem,
Name: "heartbeat_time",
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"})
2022-11-09 16:07:28 +00:00
func monitoredClientOptions() *mongoOpt.ClientOptions {
return mongoOpt.Client().
SetServerMonitor(&mongoEvent.ServerMonitor{
ServerHeartbeatSucceeded: func(ev *mongoEvent.ServerHeartbeatSucceededEvent) {
promDbHeartbeat.WithLabelValues(ev.ConnectionID).Observe(time.Duration(ev.DurationNanos).Seconds())
},
ServerHeartbeatFailed: func(ev *mongoEvent.ServerHeartbeatFailedEvent) {
promDbHeartbeat.WithLabelValues(ev.ConnectionID).Observe(0)
log.Printf("database heartbeat failed on connection %q: %e", ev.ConnectionID, ev.Failure)
},
})
}