2022-11-09 16:07:28 +00:00
|
|
|
package mongo
|
2022-10-11 07:55:22 +00:00
|
|
|
|
|
|
|
import (
|
2022-11-09 12:19:56 +00:00
|
|
|
"log"
|
2022-11-06 18:37:43 +00:00
|
|
|
"time"
|
2022-10-11 07:55:22 +00:00
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
"git.k-space.ee/k-space/logmower-shipper/pkg/globals"
|
2022-10-11 07:55:22 +00:00
|
|
|
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-10-11 07:55:22 +00:00
|
|
|
|
2022-11-09 16:07:28 +00:00
|
|
|
func monitoredClientOptions() *mongoOpt.ClientOptions {
|
2022-10-11 07:55:22 +00:00
|
|
|
return mongoOpt.Client().
|
|
|
|
SetServerMonitor(&mongoEvent.ServerMonitor{
|
|
|
|
ServerHeartbeatSucceeded: func(ev *mongoEvent.ServerHeartbeatSucceededEvent) {
|
2022-11-06 18:37:43 +00:00
|
|
|
promDbHeartbeat.WithLabelValues(ev.ConnectionID).Observe(time.Duration(ev.DurationNanos).Seconds())
|
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)
|
2022-11-09 12:19:56 +00:00
|
|
|
log.Printf("database heartbeat failed on connection %q: %e", ev.ConnectionID, ev.Failure)
|
2022-10-11 07:55:22 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|