2022-10-11 07:55:22 +00:00
|
|
|
package logmower
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-09 12:19:56 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/url"
|
2022-11-06 18:37:43 +00:00
|
|
|
"time"
|
2022-10-11 07:55:22 +00:00
|
|
|
|
2022-11-09 14:00:44 +00:00
|
|
|
ms "git.k-space.ee/k-space/logmower-shipper/pkg/mongo_struct"
|
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"
|
2022-11-09 12:19:56 +00:00
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
2022-10-11 07:55:22 +00:00
|
|
|
mongoOpt "go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
)
|
|
|
|
|
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-11-06 18:37:43 +00:00
|
|
|
Help: "Time in seconds for succeeded heartbeat, or 0 on failure",
|
|
|
|
Buckets: []float64{0.1, 0.2, 0.5, 1, 5, 10, 50},
|
2022-10-11 07:55:22 +00:00
|
|
|
}, []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-11-06 18:37:43 +00:00
|
|
|
Help: "Time in seconds 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-09 12:19:56 +00:00
|
|
|
func mongoMonitoredClientOptions() *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
|
|
|
},
|
|
|
|
}).
|
|
|
|
SetMonitor(&mongoEvent.CommandMonitor{
|
|
|
|
Succeeded: func(_ context.Context, ev *mongoEvent.CommandSucceededEvent) {
|
2022-11-06 18:37:43 +00:00
|
|
|
promDbCmd.WithLabelValues(ev.ConnectionID, ev.CommandName).Observe(time.Duration(ev.DurationNanos).Seconds())
|
2022-10-11 07:55:22 +00:00
|
|
|
},
|
|
|
|
Failed: func(_ context.Context, ev *mongoEvent.CommandFailedEvent) {
|
2022-11-06 18:37:43 +00:00
|
|
|
promDbCmd.WithLabelValues(ev.ConnectionID, ev.CommandName).Observe(time.Duration(ev.DurationNanos).Seconds())
|
2022-10-11 07:55:22 +00:00
|
|
|
|
2022-11-06 16:41:10 +00:00
|
|
|
promDbCmdErr.WithLabelValues(ev.ConnectionID, ev.CommandName).Add(1)
|
2022-10-11 07:55:22 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2022-11-09 12:19:56 +00:00
|
|
|
|
|
|
|
func initDatabase(ctx context.Context, uri string) (*mongo.Collection, error) {
|
|
|
|
uriParsed, err := url.ParseRequestURI(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing URI for database name: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
uriParsed.Path = uriParsed.Path[1:] // remove leading slash
|
|
|
|
if uriParsed.Path == "" {
|
|
|
|
return nil, fmt.Errorf("URI must include database name (as database to authenticate against)")
|
|
|
|
}
|
|
|
|
|
|
|
|
dbOpt := mongoMonitoredClientOptions().ApplyURI(uri)
|
|
|
|
|
|
|
|
dbClient, err := mongo.Connect(mongoTimeoutCtx(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("connecting to %q: %w", dbOpt.GetURI(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
col := dbClient.Database(uriParsed.Path).Collection("logs")
|
|
|
|
|
|
|
|
if err := ms.InitializeIndexes(mongoTimeoutCtx(ctx), col); err != nil {
|
|
|
|
return nil, fmt.Errorf("initializing indexes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return col, nil
|
|
|
|
}
|