Skip to content

Commit 95f2763

Browse files
committed
telemetry: Fix atomic.Pointer initialisation responsibility
Return `atomic.pointer`, so that initialisation is not the responsibility of another package.
1 parent 7cf54c1 commit 95f2763

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

cmd/icingadb/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func run() int {
101101
// the heartbeat is not read while HA gets stuck when updating the instance table.
102102
var heartbeat *icingaredis.Heartbeat
103103
var ha *icingadb.HA
104+
var telemetrySyncStats *atomic.Pointer[telemetry.SuccessfulSync]
104105
{
105106
rc, err := cmd.Redis(logs.GetChildLogger("redis"))
106107
if err != nil {
@@ -116,8 +117,7 @@ func run() int {
116117
ha = icingadb.NewHA(ctx, db, heartbeat, logs.GetChildLogger("high-availability"))
117118

118119
telemetryLogger := logs.GetChildLogger("telemetry")
119-
telemetry.LastSuccessfulSync.Store(&telemetry.SuccessfulSync{})
120-
telemetry.StartHeartbeat(ctx, rc, telemetryLogger, ha, heartbeat)
120+
telemetrySyncStats = telemetry.StartHeartbeat(ctx, rc, telemetryLogger, ha, heartbeat)
121121
telemetry.WriteStats(ctx, rc, telemetryLogger)
122122
}
123123
// Closing ha on exit ensures that this instance retracts its heartbeat
@@ -251,7 +251,7 @@ func run() int {
251251
logger := logs.GetChildLogger("config-sync")
252252

253253
if synctx.Err() == nil {
254-
telemetry.LastSuccessfulSync.Store(&telemetry.SuccessfulSync{
254+
telemetrySyncStats.Store(&telemetry.SuccessfulSync{
255255
FinishMilli: syncEnd.UnixMilli(),
256256
DurationMilli: elapsed.Milliseconds(),
257257
})

pkg/icingaredis/telemetry/heartbeat.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,17 @@ func GetCurrentDbConnErr() (string, int64) {
7979
// OngoingSyncStartMilli is to be updated by the main() function.
8080
var OngoingSyncStartMilli int64
8181

82-
// LastSuccessfulSync is to be updated by the main() function.
83-
var LastSuccessfulSync atomic.Pointer[SuccessfulSync]
84-
8582
var boolToStr = map[bool]string{false: "0", true: "1"}
8683
var startTime = time.Now().UnixMilli()
8784

8885
// StartHeartbeat periodically writes heartbeats to Redis for being monitored by Icinga 2.
86+
// It returns an atomic pointer to SuccessfulSync,
87+
// which contains synchronisation statistics that the caller should update.
8988
func StartHeartbeat(
9089
ctx context.Context, client *redis.Client, logger *logging.Logger, ha ha, heartbeat *icingaredis.Heartbeat,
91-
) {
90+
) *atomic.Pointer[SuccessfulSync] {
91+
var syncStats atomic.Pointer[SuccessfulSync]
92+
syncStats.Store(&SuccessfulSync{})
9293
goMetrics := NewGoMetrics()
9394

9495
const interval = time.Second
@@ -100,7 +101,7 @@ func StartHeartbeat(
100101
heartbeat := heartbeat.LastReceived()
101102
responsibleTsMilli, responsible, otherResponsible := ha.State()
102103
ongoingSyncStart := atomic.LoadInt64(&OngoingSyncStartMilli)
103-
lastSync := LastSuccessfulSync.Load()
104+
lastSync := syncStats.Load()
104105
dbConnErr, dbConnErrSinceMilli := GetCurrentDbConnErr()
105106
now := time.Now()
106107

@@ -144,6 +145,8 @@ func StartHeartbeat(
144145
silenceUntil = time.Time{}
145146
}
146147
})
148+
149+
return &syncStats
147150
}
148151

149152
type goMetrics struct {

0 commit comments

Comments
 (0)