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 <anton@tailscale.com>
pull/6069/head
Anton Tolchanov 2 years ago committed by Anton Tolchanov
parent dd50dcd067
commit 3c27632ffe

@ -474,7 +474,7 @@ func writePromExpVar(w io.Writer, prefix string, kv expvar.KeyValue) {
label, key = a, b label, key = a, b
} }
} }
name := prefix + key name := strings.ReplaceAll(prefix+key, "-", "_")
switch v := kv.Value.(type) { switch v := kv.Value.(type) {
case PrometheusVar: case PrometheusVar:

@ -359,6 +359,12 @@ func TestVarzHandler(t *testing.T) {
new(expvar.Int), new(expvar.Int),
"# TYPE foo counter\nfoo 0\n", "# 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", "int_with_type_counter",
"counter_foo", "counter_foo",

Loading…
Cancel
Save