From c6749494ae5b7b369d89b4fc1ad9975949d0ea39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Gon=C3=A7alves?= Date: Fri, 8 Jul 2022 18:44:54 -0300 Subject: [PATCH] Improve metrics name convention (#490) * Change metrics to be compliant to prometheus name convention --- internal/core/monitoring/metrics.go | 12 ++++++++++-- internal/core/monitoring/metrics_test.go | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/internal/core/monitoring/metrics.go b/internal/core/monitoring/metrics.go index e24aaf243..4a9456cad 100644 --- a/internal/core/monitoring/metrics.go +++ b/internal/core/monitoring/metrics.go @@ -41,14 +41,22 @@ type MetricOpts struct { Name string Help string Labels []string + + // MetricUnit will only be used by Gauge metric to fill which unit the metric is related. + MetricUnit string } func CreateCounterMetric(options *MetricOpts) *prometheus.CounterVec { + name := options.Name + if len(options.MetricUnit) > 0 { + name = options.Name + "_" + options.MetricUnit + } + return promauto.NewCounterVec( prometheus.CounterOpts{ Namespace: options.Namespace, Subsystem: options.Subsystem, - Name: options.Name + "_counter", + Name: name + "_total", Help: options.Help + " (counter)", }, options.Labels, @@ -73,7 +81,7 @@ func CreateGaugeMetric(options *MetricOpts) *prometheus.GaugeVec { prometheus.GaugeOpts{ Namespace: options.Namespace, Subsystem: options.Subsystem, - Name: options.Name + "_gauge", + Name: options.Name, Help: options.Help + " (gauge)", }, options.Labels, diff --git a/internal/core/monitoring/metrics_test.go b/internal/core/monitoring/metrics_test.go index b14e759c2..14968015c 100644 --- a/internal/core/monitoring/metrics_test.go +++ b/internal/core/monitoring/metrics_test.go @@ -38,17 +38,18 @@ func TestCounterCreation(t *testing.T) { t.Run("successfully fetch counter metric", func(t *testing.T) { counter := CreateCounterMetric(&MetricOpts{ - Namespace: "maestro", - Subsystem: "test", - Name: "counter_test", - Help: "Test Counter", - Labels: []string{"success"}, + Namespace: "maestro", + Subsystem: "test", + Name: "counter_test", + Help: "Test Counter", + Labels: []string{"success"}, + MetricUnit: "unit", }) counter.WithLabelValues("true").Inc() metrics, _ := prometheus.DefaultGatherer.Gather() - metricFamily := FilterMetric(metrics, "maestro_test_counter_test_counter") + metricFamily := FilterMetric(metrics, "maestro_test_counter_test_unit_total") trueMetric := metricFamily.GetMetric()[0] require.Equal(t, float64(1), trueMetric.GetCounter().GetValue()) @@ -60,7 +61,7 @@ func TestCounterCreation(t *testing.T) { counter.WithLabelValues("true").Inc() metrics, _ = prometheus.DefaultGatherer.Gather() - metricFamily = FilterMetric(metrics, "maestro_test_counter_test_counter") + metricFamily = FilterMetric(metrics, "maestro_test_counter_test_unit_total") trueMetric = metricFamily.GetMetric()[1] require.Equal(t, float64(2), trueMetric.GetCounter().GetValue()) @@ -91,7 +92,7 @@ func TestCounterCreation(t *testing.T) { gauge.WithLabelValues("true").Inc() metrics, _ := prometheus.DefaultGatherer.Gather() - metricFamily := FilterMetric(metrics, "maestro_test_gauge_test_gauge") + metricFamily := FilterMetric(metrics, "maestro_test_gauge_test") trueMetric := metricFamily.GetMetric()[0] require.Equal(t, float64(1), trueMetric.GetGauge().GetValue()) @@ -103,7 +104,7 @@ func TestCounterCreation(t *testing.T) { gauge.WithLabelValues("true").Dec() metrics, _ = prometheus.DefaultGatherer.Gather() - metricFamily = FilterMetric(metrics, "maestro_test_gauge_test_gauge") + metricFamily = FilterMetric(metrics, "maestro_test_gauge_test") trueMetric = metricFamily.GetMetric()[1] require.Equal(t, float64(0), trueMetric.GetGauge().GetValue())