work on flags
This commit is contained in:
		| @@ -20,15 +20,15 @@ var promDbHeartbeat = promauto.NewHistogramVec(prom.HistogramOpts{ | ||||
| 	Buckets: []float64{0.1, 0.2, 0.5, 1, 5, 10, 50}, | ||||
| }, []string{"connection_id"}) | ||||
|  | ||||
| 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) | ||||
| 			}, | ||||
| 		}) | ||||
| 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 | ||||
| } | ||||
|   | ||||
| @@ -4,12 +4,20 @@ import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"net/url" | ||||
| 	"time" | ||||
|  | ||||
| 	"git.k-space.ee/k-space/logmower-shipper/pkg/globals" | ||||
| 	"go.mongodb.org/mongo-driver/mongo" | ||||
| 	mongoOpt "go.mongodb.org/mongo-driver/mongo/options" | ||||
| ) | ||||
|  | ||||
| func Initialize(ctx context.Context, uri string) (*mongo.Collection, error) { | ||||
| const CommandTimeout = 10 * time.Second | ||||
|  | ||||
| func GlobalTimeout(ctx context.Context) context.Context { | ||||
| 	ctx, _ = context.WithTimeout(ctx, CommandTimeout) //nolint:lostcancel (cancelled by mongo, should be bug on them //TODO) | ||||
| 	return ctx | ||||
| } | ||||
|  | ||||
| func Initialize(ctx context.Context, uri string, opts *mongoOpt.ClientOptions) (*mongo.Collection, error) { | ||||
| 	uriParsed, err := url.ParseRequestURI(uri) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("parsing URI for database name: %w", err) | ||||
| @@ -20,20 +28,20 @@ func Initialize(ctx context.Context, uri string) (*mongo.Collection, error) { | ||||
| 		return nil, fmt.Errorf("URI must include database name (as database to authenticate against)") | ||||
| 	} | ||||
|  | ||||
| 	dbOpt := monitoredClientOptions().ApplyURI(uri) | ||||
| 	dbOpt := attachMetrics(opts).ApplyURI(uri) | ||||
|  | ||||
| 	dbClient, err := mongo.Connect(globals.MongoTimeout(ctx), dbOpt) | ||||
| 	dbClient, err := mongo.Connect(GlobalTimeout(ctx), dbOpt) | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("connecting to %q: %w", dbOpt.GetURI(), err) | ||||
| 	} | ||||
|  | ||||
| 	if err := dbClient.Ping(globals.MongoTimeout(ctx), nil); err != nil { | ||||
| 	if err := dbClient.Ping(GlobalTimeout(ctx), nil); err != nil { | ||||
| 		return nil, fmt.Errorf("first ping to database: %w", err) | ||||
| 	} | ||||
|  | ||||
| 	col := dbClient.Database(uriParsed.Path).Collection("logs") | ||||
|  | ||||
| 	if err := InitializeIndexes(globals.MongoTimeout(ctx), col); err != nil { | ||||
| 	if err := InitializeIndexes(GlobalTimeout(ctx), col); err != nil { | ||||
| 		return nil, fmt.Errorf("initializing indexes: %w", err) | ||||
| 	} | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user