|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/prometheus/client_golang/prometheus" |
| 7 | +) |
| 8 | + |
| 9 | +// ManagerMetricsCollector implements ManagerCollector interface and prometheus.Collector interface |
| 10 | +type ManagerMetricsCollector struct { |
| 11 | + // Metrics |
| 12 | + reloadsTotal prometheus.Counter |
| 13 | + reloadsError prometheus.Counter |
| 14 | + configStale prometheus.Gauge |
| 15 | + reloadsDuration prometheus.Histogram |
| 16 | +} |
| 17 | + |
| 18 | +// NewManagerMetricsCollector creates a new ManagerMetricsCollector |
| 19 | +func NewManagerMetricsCollector(constLabels map[string]string) *ManagerMetricsCollector { |
| 20 | + nc := &ManagerMetricsCollector{ |
| 21 | + reloadsTotal: prometheus.NewCounter( |
| 22 | + prometheus.CounterOpts{ |
| 23 | + Name: "nginx_reloads_total", |
| 24 | + Namespace: metricsNamespace, |
| 25 | + Help: "Number of successful NGINX reloads", |
| 26 | + ConstLabels: constLabels, |
| 27 | + }), |
| 28 | + reloadsError: prometheus.NewCounter( |
| 29 | + prometheus.CounterOpts{ |
| 30 | + Name: "nginx_reload_errors_total", |
| 31 | + Namespace: metricsNamespace, |
| 32 | + Help: "Number of unsuccessful NGINX reloads", |
| 33 | + ConstLabels: constLabels, |
| 34 | + }, |
| 35 | + ), |
| 36 | + configStale: prometheus.NewGauge( |
| 37 | + prometheus.GaugeOpts{ |
| 38 | + Name: "nginx_stale_config", |
| 39 | + Namespace: metricsNamespace, |
| 40 | + Help: "Indicates if NGINX is not serving the latest configuration.", |
| 41 | + ConstLabels: constLabels, |
| 42 | + }, |
| 43 | + ), |
| 44 | + reloadsDuration: prometheus.NewHistogram( |
| 45 | + prometheus.HistogramOpts{ |
| 46 | + Name: "nginx_reloads_milliseconds", |
| 47 | + Namespace: metricsNamespace, |
| 48 | + Help: "Duration in milliseconds of NGINX reloads", |
| 49 | + ConstLabels: constLabels, |
| 50 | + Buckets: []float64{500, 1000, 5000, 10000, 30000}, |
| 51 | + }, |
| 52 | + ), |
| 53 | + } |
| 54 | + return nc |
| 55 | +} |
| 56 | + |
| 57 | +// IncNginxReloadCount increments the counter of successful NGINX reloads and sets the stale config status to false. |
| 58 | +func (mc *ManagerMetricsCollector) IncReloadCount() { |
| 59 | + mc.reloadsTotal.Inc() |
| 60 | + mc.updateConfigStaleStatus(false) |
| 61 | +} |
| 62 | + |
| 63 | +// IncNginxReloadErrors increments the counter of NGINX reload errors and sets the stale config status to true. |
| 64 | +func (mc *ManagerMetricsCollector) IncReloadErrors() { |
| 65 | + mc.reloadsError.Inc() |
| 66 | + mc.updateConfigStaleStatus(true) |
| 67 | +} |
| 68 | + |
| 69 | +// updateConfigStaleStatus updates the last NGINX reload status metric. |
| 70 | +func (mc *ManagerMetricsCollector) updateConfigStaleStatus(stale bool) { |
| 71 | + var status float64 |
| 72 | + if stale { |
| 73 | + status = 1.0 |
| 74 | + } |
| 75 | + mc.configStale.Set(status) |
| 76 | +} |
| 77 | + |
| 78 | +// ObserveLastReloadTime adds the last NGINX reload time to the histogram. |
| 79 | +func (mc *ManagerMetricsCollector) ObserveLastReloadTime(duration time.Duration) { |
| 80 | + mc.reloadsDuration.Observe(float64(duration / time.Millisecond)) |
| 81 | +} |
| 82 | + |
| 83 | +// Describe implements prometheus.Collector interface Describe method. |
| 84 | +func (mc *ManagerMetricsCollector) Describe(ch chan<- *prometheus.Desc) { |
| 85 | + mc.reloadsTotal.Describe(ch) |
| 86 | + mc.reloadsError.Describe(ch) |
| 87 | + mc.configStale.Describe(ch) |
| 88 | + mc.reloadsDuration.Describe(ch) |
| 89 | +} |
| 90 | + |
| 91 | +// Collect implements the prometheus.Collector interface Collect method. |
| 92 | +func (mc *ManagerMetricsCollector) Collect(ch chan<- prometheus.Metric) { |
| 93 | + mc.reloadsTotal.Collect(ch) |
| 94 | + mc.reloadsError.Collect(ch) |
| 95 | + mc.configStale.Collect(ch) |
| 96 | + mc.reloadsDuration.Collect(ch) |
| 97 | +} |
| 98 | + |
| 99 | +// ManagerNoopCollector is a no-op collector that will implement ManagerCollector interface. |
| 100 | +// Used to initialize the ManagerCollector when metrics are disabled to avoid nil pointer errors. |
| 101 | +type ManagerNoopCollector struct{} |
| 102 | + |
| 103 | +// NewManagerNoopCollector creates a no-op collector that implements ManagerCollector interface. |
| 104 | +func NewManagerNoopCollector() *ManagerNoopCollector { |
| 105 | + return &ManagerNoopCollector{} |
| 106 | +} |
| 107 | + |
| 108 | +// IncReloadCount implements a no-op IncReloadCount. |
| 109 | +func (mc *ManagerNoopCollector) IncReloadCount() {} |
| 110 | + |
| 111 | +// IncReloadErrors implements a no-op IncReloadErrors. |
| 112 | +func (mc *ManagerNoopCollector) IncReloadErrors() {} |
| 113 | + |
| 114 | +// ObserveLastReloadTime implements a no-op ObserveLastReloadTime. |
| 115 | +func (mc *ManagerNoopCollector) ObserveLastReloadTime(_ time.Duration) {} |
0 commit comments