Path: blob/main/pkg/config/instrumentation/config_metrics.go
4096 views
package instrumentation12import (3"crypto/sha256"4"fmt"5"sync"67"github.com/prometheus/client_golang/prometheus"8"github.com/prometheus/client_golang/prometheus/promauto"9)1011// configMetrics exposes metrics related to configuration loading12type configMetrics struct {13configHash *prometheus.GaugeVec14configLoadSuccess prometheus.Gauge15configLoadSuccessSeconds prometheus.Gauge16configLoadFailures prometheus.Counter17}1819var confMetrics *configMetrics20var configMetricsInitializer sync.Once2122func initializeConfigMetrics() {23confMetrics = newConfigMetrics()24}2526func newConfigMetrics() *configMetrics {27var m configMetrics2829m.configHash = promauto.NewGaugeVec(30prometheus.GaugeOpts{31Name: "agent_config_hash",32Help: "Hash of the currently active config file.",33},34[]string{"sha256"},35)36m.configLoadSuccess = promauto.NewGauge(prometheus.GaugeOpts{37Name: "agent_config_last_load_successful",38Help: "Config loaded successfully.",39})40m.configLoadSuccessSeconds = promauto.NewGauge(prometheus.GaugeOpts{41Name: "agent_config_last_load_success_timestamp_seconds",42Help: "Timestamp of the last successful configuration load.",43})44m.configLoadFailures = promauto.NewCounter(prometheus.CounterOpts{45Name: "agent_config_load_failures_total",46Help: "Configuration load failures.",47})48return &m49}5051// Create a sha256 hash of the config before expansion and expose it via52// the agent_config_hash metric.53func InstrumentConfig(buf []byte) {54configMetricsInitializer.Do(initializeConfigMetrics)55hash := sha256.Sum256(buf)56confMetrics.configHash.Reset()57confMetrics.configHash.WithLabelValues(fmt.Sprintf("%x", hash)).Set(1)58}5960// Expose metrics for load success / failures.61func InstrumentLoad(success bool) {62configMetricsInitializer.Do(initializeConfigMetrics)63if success {64confMetrics.configLoadSuccessSeconds.SetToCurrentTime()65confMetrics.configLoadSuccess.Set(1)66} else {67confMetrics.configLoadSuccess.Set(0)68confMetrics.configLoadFailures.Inc()69}70}717273