summaryrefslogtreecommitdiffstats
path: root/pkg/icingaredis/telemetry/heartbeat.go
blob: ee476a15d1db5664830dafd2579b0da7935bcbe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package telemetry

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	"github.com/icinga/icingadb/internal"
	"github.com/icinga/icingadb/pkg/com"
	"github.com/icinga/icingadb/pkg/icingaredis"
	"github.com/icinga/icingadb/pkg/logging"
	"github.com/icinga/icingadb/pkg/periodic"
	"github.com/icinga/icingadb/pkg/utils"
	"github.com/pkg/errors"
	"go.uber.org/zap"
	"regexp"
	"runtime/metrics"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
	"time"
)

// ha represents icingadb.HA to avoid import cycles.
type ha interface {
	State() (weResponsibleMilli int64, weResponsible, otherResponsible bool)
}

type SuccessfulSync struct {
	FinishMilli   int64
	DurationMilli int64
}

// currentDbConnErr stores ongoing errors from database connections.
var currentDbConnErr struct {
	mu         sync.Mutex
	message    string
	sinceMilli int64
}

// UpdateCurrentDbConnErr updates the current error information stored in currentDbConnErr.
func UpdateCurrentDbConnErr(err error) {
	now := time.Now().UnixMilli()

	currentDbConnErr.mu.Lock()
	defer currentDbConnErr.mu.Unlock()

	if currentDbConnErr.sinceMilli >= now {
		// Already updated with a more recent error, ignore this one.
		return
	}

	message := ""
	if err != nil {
		message = err.Error()
	}

	if currentDbConnErr.message == message {
		// Error stayed the same, no update needed, keeping the old timestamp.
		return
	}

	if currentDbConnErr.message == "" || message == "" {
		// Either first error or recovery from an error, update timestamp.
		currentDbConnErr.sinceMilli = now
	}

	currentDbConnErr.message = message
}

// GetCurrentDbConnErr returns the last error message (or the empty string if not in an error state) and a timestamp in
// milliseconds of the last change from OK to error or from error to OK.
func GetCurrentDbConnErr() (string, int64) {
	currentDbConnErr.mu.Lock()
	defer currentDbConnErr.mu.Unlock()

	return currentDbConnErr.message, currentDbConnErr.sinceMilli
}

// OngoingSyncStartMilli is to be updated by the main() function.
var OngoingSyncStartMilli int64

// LastSuccessfulSync is to be updated by the main() function.
var LastSuccessfulSync com.Atomic[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.
func StartHeartbeat(
	ctx context.Context, client *icingaredis.Client, logger *logging.Logger, ha ha, heartbeat *icingaredis.Heartbeat,
) {
	goMetrics := NewGoMetrics()

	const interval = time.Second

	var lastErr string
	var silenceUntil time.Time

	periodic.Start(ctx, interval, func(tick periodic.Tick) {
		heartbeat := heartbeat.LastReceived()
		responsibleTsMilli, responsible, otherResponsible := ha.State()
		ongoingSyncStart := atomic.LoadInt64(&OngoingSyncStartMilli)
		sync, _ := LastSuccessfulSync.Load()
		dbConnErr, dbConnErrSinceMilli := GetCurrentDbConnErr()
		now := time.Now()

		values := map[string]string{
			"version":                 internal.Version.Version,
			"time":                    strconv.FormatInt(now.UnixMilli(), 10),
			"start-time":              strconv.FormatInt(startTime, 10),
			"error":                   dbConnErr,
			"error-since":             strconv.FormatInt(dbConnErrSinceMilli, 10),
			"performance-data":        goMetrics.PerformanceData(),
			"last-heartbeat-received": strconv.FormatInt(heartbeat, 10),
			"ha-responsible":          boolToStr[responsible],
			"ha-responsible-ts":       strconv.FormatInt(responsibleTsMilli, 10),
			"ha-other-responsible":    boolToStr[otherResponsible],
			"sync-ongoing-since":      strconv.FormatInt(ongoingSyncStart, 10),
			"sync-success-finish":     strconv.FormatInt(sync.FinishMilli, 10),
			"sync-success-duration":   strconv.FormatInt(sync.DurationMilli, 10),
		}

		ctx, cancel := context.WithDeadline(ctx, tick.Time.Add(interval))
		defer cancel()

		cmd := client.XAdd(ctx, &redis.XAddArgs{
			Stream: "icingadb:telemetry:heartbeat",
			MaxLen: 1,
			Values: values,
		})
		if err := cmd.Err(); err != nil && !utils.IsContextCanceled(err) && !errors.Is(err, context.DeadlineExceeded) {
			logw := logger.Debugw
			currentErr := err.Error()

			if currentErr != lastErr || now.After(silenceUntil) {
				logw = logger.Warnw
				lastErr = currentErr
				silenceUntil = now.Add(time.Minute)
			}

			logw("Can't update own heartbeat", zap.Error(icingaredis.WrapCmdErr(cmd)))
		} else {
			lastErr = ""
			silenceUntil = time.Time{}
		}
	})
}

type goMetrics struct {
	names   []string
	units   []string
	samples []metrics.Sample
}

func NewGoMetrics() *goMetrics {
	m := &goMetrics{}

	forbiddenRe := regexp.MustCompile(`\W`)

	for _, d := range metrics.All() {
		switch d.Kind {
		case metrics.KindUint64, metrics.KindFloat64:
			name := "go_" + strings.TrimLeft(forbiddenRe.ReplaceAllString(d.Name, "_"), "_")

			unit := ""
			if strings.HasSuffix(d.Name, ":bytes") {
				unit = "B"
			} else if strings.HasSuffix(d.Name, ":seconds") {
				unit = "s"
			} else if d.Cumulative {
				unit = "c"
			}

			m.names = append(m.names, name)
			m.units = append(m.units, unit)
			m.samples = append(m.samples, metrics.Sample{Name: d.Name})
		}
	}

	return m
}

func (g *goMetrics) PerformanceData() string {
	metrics.Read(g.samples)

	var buf strings.Builder

	for i, sample := range g.samples {
		if i > 0 {
			buf.WriteByte(' ')
		}

		switch sample.Value.Kind() {
		case metrics.KindUint64:
			_, _ = fmt.Fprintf(&buf, "%s=%d%s", g.names[i], sample.Value.Uint64(), g.units[i])
		case metrics.KindFloat64:
			_, _ = fmt.Fprintf(&buf, "%s=%f%s", g.names[i], sample.Value.Float64(), g.units[i])
		}
	}

	return buf.String()
}