From 95f2763a73dc9a711d39db2acfa044e7ca362475 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 24 Oct 2024 11:43:55 +0200 Subject: [PATCH] `telemetry`: Fix `atomic.Pointer` initialisation responsibility Return `atomic.pointer`, so that initialisation is not the responsibility of another package. --- cmd/icingadb/main.go | 6 +++--- pkg/icingaredis/telemetry/heartbeat.go | 13 ++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/cmd/icingadb/main.go b/cmd/icingadb/main.go index dc0098ab..4c0bb8f7 100644 --- a/cmd/icingadb/main.go +++ b/cmd/icingadb/main.go @@ -101,6 +101,7 @@ func run() int { // the heartbeat is not read while HA gets stuck when updating the instance table. var heartbeat *icingaredis.Heartbeat var ha *icingadb.HA + var telemetrySyncStats *atomic.Pointer[telemetry.SuccessfulSync] { rc, err := cmd.Redis(logs.GetChildLogger("redis")) if err != nil { @@ -116,8 +117,7 @@ func run() int { ha = icingadb.NewHA(ctx, db, heartbeat, logs.GetChildLogger("high-availability")) telemetryLogger := logs.GetChildLogger("telemetry") - telemetry.LastSuccessfulSync.Store(&telemetry.SuccessfulSync{}) - telemetry.StartHeartbeat(ctx, rc, telemetryLogger, ha, heartbeat) + telemetrySyncStats = telemetry.StartHeartbeat(ctx, rc, telemetryLogger, ha, heartbeat) telemetry.WriteStats(ctx, rc, telemetryLogger) } // Closing ha on exit ensures that this instance retracts its heartbeat @@ -251,7 +251,7 @@ func run() int { logger := logs.GetChildLogger("config-sync") if synctx.Err() == nil { - telemetry.LastSuccessfulSync.Store(&telemetry.SuccessfulSync{ + telemetrySyncStats.Store(&telemetry.SuccessfulSync{ FinishMilli: syncEnd.UnixMilli(), DurationMilli: elapsed.Milliseconds(), }) diff --git a/pkg/icingaredis/telemetry/heartbeat.go b/pkg/icingaredis/telemetry/heartbeat.go index 61bf44ad..c29b1533 100644 --- a/pkg/icingaredis/telemetry/heartbeat.go +++ b/pkg/icingaredis/telemetry/heartbeat.go @@ -79,16 +79,17 @@ func GetCurrentDbConnErr() (string, int64) { // OngoingSyncStartMilli is to be updated by the main() function. var OngoingSyncStartMilli int64 -// LastSuccessfulSync is to be updated by the main() function. -var LastSuccessfulSync atomic.Pointer[SuccessfulSync] - var boolToStr = map[bool]string{false: "0", true: "1"} var startTime = time.Now().UnixMilli() // StartHeartbeat periodically writes heartbeats to Redis for being monitored by Icinga 2. +// It returns an atomic pointer to SuccessfulSync, +// which contains synchronisation statistics that the caller should update. func StartHeartbeat( ctx context.Context, client *redis.Client, logger *logging.Logger, ha ha, heartbeat *icingaredis.Heartbeat, -) { +) *atomic.Pointer[SuccessfulSync] { + var syncStats atomic.Pointer[SuccessfulSync] + syncStats.Store(&SuccessfulSync{}) goMetrics := NewGoMetrics() const interval = time.Second @@ -100,7 +101,7 @@ func StartHeartbeat( heartbeat := heartbeat.LastReceived() responsibleTsMilli, responsible, otherResponsible := ha.State() ongoingSyncStart := atomic.LoadInt64(&OngoingSyncStartMilli) - lastSync := LastSuccessfulSync.Load() + lastSync := syncStats.Load() dbConnErr, dbConnErrSinceMilli := GetCurrentDbConnErr() now := time.Now() @@ -144,6 +145,8 @@ func StartHeartbeat( silenceUntil = time.Time{} } }) + + return &syncStats } type goMetrics struct {