Skip to content

Commit

Permalink
Improve metrics name convention (#490)
Browse files Browse the repository at this point in the history
* Change metrics to be compliant to prometheus name convention
  • Loading branch information
arthur29 authored Jul 8, 2022
1 parent 998925f commit c674949
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
12 changes: 10 additions & 2 deletions internal/core/monitoring/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
19 changes: 10 additions & 9 deletions internal/core/monitoring/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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())
Expand Down Expand Up @@ -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())

Expand All @@ -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())
Expand Down

0 comments on commit c674949

Please sign in to comment.