Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/config/instrumentation/config_metrics.go
4096 views
1
package instrumentation
2
3
import (
4
"crypto/sha256"
5
"fmt"
6
"sync"
7
8
"github.com/prometheus/client_golang/prometheus"
9
"github.com/prometheus/client_golang/prometheus/promauto"
10
)
11
12
// configMetrics exposes metrics related to configuration loading
13
type configMetrics struct {
14
configHash *prometheus.GaugeVec
15
configLoadSuccess prometheus.Gauge
16
configLoadSuccessSeconds prometheus.Gauge
17
configLoadFailures prometheus.Counter
18
}
19
20
var confMetrics *configMetrics
21
var configMetricsInitializer sync.Once
22
23
func initializeConfigMetrics() {
24
confMetrics = newConfigMetrics()
25
}
26
27
func newConfigMetrics() *configMetrics {
28
var m configMetrics
29
30
m.configHash = promauto.NewGaugeVec(
31
prometheus.GaugeOpts{
32
Name: "agent_config_hash",
33
Help: "Hash of the currently active config file.",
34
},
35
[]string{"sha256"},
36
)
37
m.configLoadSuccess = promauto.NewGauge(prometheus.GaugeOpts{
38
Name: "agent_config_last_load_successful",
39
Help: "Config loaded successfully.",
40
})
41
m.configLoadSuccessSeconds = promauto.NewGauge(prometheus.GaugeOpts{
42
Name: "agent_config_last_load_success_timestamp_seconds",
43
Help: "Timestamp of the last successful configuration load.",
44
})
45
m.configLoadFailures = promauto.NewCounter(prometheus.CounterOpts{
46
Name: "agent_config_load_failures_total",
47
Help: "Configuration load failures.",
48
})
49
return &m
50
}
51
52
// Create a sha256 hash of the config before expansion and expose it via
53
// the agent_config_hash metric.
54
func InstrumentConfig(buf []byte) {
55
configMetricsInitializer.Do(initializeConfigMetrics)
56
hash := sha256.Sum256(buf)
57
confMetrics.configHash.Reset()
58
confMetrics.configHash.WithLabelValues(fmt.Sprintf("%x", hash)).Set(1)
59
}
60
61
// Expose metrics for load success / failures.
62
func InstrumentLoad(success bool) {
63
configMetricsInitializer.Do(initializeConfigMetrics)
64
if success {
65
confMetrics.configLoadSuccessSeconds.SetToCurrentTime()
66
confMetrics.configLoadSuccess.Set(1)
67
} else {
68
confMetrics.configLoadSuccess.Set(0)
69
confMetrics.configLoadFailures.Inc()
70
}
71
}
72
73