35 lines
1.2 KiB
Go
35 lines
1.2 KiB
Go
package mongo
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
|
|
"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"
|
|
)
|
|
|
|
const promSubsystem = "database"
|
|
|
|
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"})
|
|
|
|
func attachMetrics(opts *mongoOpt.ClientOptions) *mongoOpt.ClientOptions {
|
|
opts.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)
|
|
},
|
|
})
|
|
return opts
|
|
}
|