diff --git a/go/stats/prometheusbackend/collectors.go b/go/stats/prometheusbackend/collectors.go index 206e8dfcec3..a1126c0d211 100644 --- a/go/stats/prometheusbackend/collectors.go +++ b/go/stats/prometheusbackend/collectors.go @@ -74,7 +74,7 @@ func newCountersWithSingleLabelCollector(c *stats.CountersWithSingleLabel, name desc: prometheus.NewDesc( name, c.Help(), - []string{labelName}, + []string{normalizeMetric(labelName)}, nil), vt: vt} @@ -111,7 +111,7 @@ func newGaugesWithSingleLabelCollector(g *stats.GaugesWithSingleLabel, name stri desc: prometheus.NewDesc( name, g.Help(), - []string{labelName}, + []string{normalizeMetric(labelName)}, nil), vt: vt} @@ -266,7 +266,7 @@ func newTimingsCollector(t *stats.Timings, name string) { desc: prometheus.NewDesc( name, t.Help(), - []string{t.Label()}, + []string{normalizeMetric(t.Label())}, nil), } diff --git a/go/stats/prometheusbackend/prometheusbackend_test.go b/go/stats/prometheusbackend/prometheusbackend_test.go index 438e678bb3e..888dd630941 100644 --- a/go/stats/prometheusbackend/prometheusbackend_test.go +++ b/go/stats/prometheusbackend/prometheusbackend_test.go @@ -357,6 +357,26 @@ func testMetricsHandler(t *testing.T) *httptest.ResponseRecorder { return response } +func TestPrometheusLabels(t *testing.T) { + m1 := stats.NewCountersWithSingleLabel("ThisIsMetric1", "helpstring1", "ThisIsALabel") + m1.Add("labelvalue1", 420) + + m2 := stats.NewCountersWithMultiLabels("ThisIsMetric2", "helpstring2", []string{"ThisIsALabel"}) + m2.Add([]string{"labelvalue2"}, 420) + + response := testMetricsHandler(t) + + expect := []string{ + "namespace_this_is_metric1{this_is_a_label=\"labelvalue1\"} 420", + "namespace_this_is_metric2{this_is_a_label=\"labelvalue2\"} 420", + } + for _, line := range expect { + if !strings.Contains(response.Body.String(), line) { + t.Fatalf("Expected result to contain %s, got %s", line, response.Body.String()) + } + } +} + func TestMain(m *testing.M) { Init(namespace) os.Exit(m.Run())