From 3c27632ffea86f880265c7561f571e25af7b77c1 Mon Sep 17 00:00:00 2001 From: Anton Tolchanov Date: Thu, 17 Nov 2022 09:34:32 -0800 Subject: [PATCH] tsweb: avoid dashes in Prometheus metric names Ideally we should strip other invalid characters too, but that would call for a regexp replacement which increases the number of allocations and makes `TestVarzHandlerSorting` fail. Signed-off-by: Anton Tolchanov --- tsweb/tsweb.go | 2 +- tsweb/tsweb_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tsweb/tsweb.go b/tsweb/tsweb.go index 8ba1a52a7..a42f59dcd 100644 --- a/tsweb/tsweb.go +++ b/tsweb/tsweb.go @@ -474,7 +474,7 @@ func writePromExpVar(w io.Writer, prefix string, kv expvar.KeyValue) { label, key = a, b } } - name := prefix + key + name := strings.ReplaceAll(prefix+key, "-", "_") switch v := kv.Value.(type) { case PrometheusVar: diff --git a/tsweb/tsweb_test.go b/tsweb/tsweb_test.go index a38b671c9..d571a3773 100644 --- a/tsweb/tsweb_test.go +++ b/tsweb/tsweb_test.go @@ -359,6 +359,12 @@ func TestVarzHandler(t *testing.T) { new(expvar.Int), "# TYPE foo counter\nfoo 0\n", }, + { + "dash_in_metric_name", + "counter_foo-bar", + new(expvar.Int), + "# TYPE foo_bar counter\nfoo_bar 0\n", + }, { "int_with_type_counter", "counter_foo",