Skip to content

Commit

Permalink
stats/prometheus: normalize labels for single-label implementations (#…
Browse files Browse the repository at this point in the history
…1466)

Signed-off-by: Vicent Marti <[email protected]>

Signed-off-by: Vicent Marti <[email protected]>
  • Loading branch information
vmg authored Jan 9, 2023
1 parent 5b1c399 commit 5f2209f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions go/stats/prometheusbackend/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func newCountersWithSingleLabelCollector(c *stats.CountersWithSingleLabel, name
desc: prometheus.NewDesc(
name,
c.Help(),
[]string{labelName},
[]string{normalizeMetric(labelName)},
nil),
vt: vt}

Expand Down Expand Up @@ -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}

Expand Down Expand Up @@ -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),
}

Expand Down
20 changes: 20 additions & 0 deletions go/stats/prometheusbackend/prometheusbackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 5f2209f

Please sign in to comment.