From 79ee6d6e1e68177446374bb524db3a74aafeecef Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 30 Jun 2023 08:55:57 -0700 Subject: [PATCH] tsweb/varz: use default metrics.LabelMap.Label on serialization To not break Prometheus if the label is unset. Updates tailscale/corp#12830 Signed-off-by: Brad Fitzpatrick --- tsweb/varz/varz.go | 2 +- tsweb/varz/varz_test.go | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/tsweb/varz/varz.go b/tsweb/varz/varz.go index ed88d5ed6..12922106e 100644 --- a/tsweb/varz/varz.go +++ b/tsweb/varz/varz.go @@ -189,7 +189,7 @@ func writePromExpVar(w io.Writer, prefix string, kv expvar.KeyValue) { // IntMap uses expvar.Map on the inside, which presorts // keys. The output ordering is deterministic. v.Do(func(kv expvar.KeyValue) { - fmt.Fprintf(w, "%s{%s=%q} %v\n", name, v.Label, kv.Key, kv.Value) + fmt.Fprintf(w, "%s{%s=%q} %v\n", name, cmpx.Or(v.Label, "label"), kv.Key, kv.Value) }) case *expvar.Map: if label != "" && typ != "" { diff --git a/tsweb/varz/varz_test.go b/tsweb/varz/varz_test.go index e46caeccf..4fe91479b 100644 --- a/tsweb/varz/varz_test.go +++ b/tsweb/varz/varz_test.go @@ -165,6 +165,16 @@ func TestVarzHandler(t *testing.T) { })(), "control_save_config{reason=\"fun\"} 1\ncontrol_save_config{reason=\"new\"} 1\ncontrol_save_config{reason=\"updated\"} 1\n", }, + { + "metrics_label_map_unlabeled", + "foo", + (func() *metrics.LabelMap { + m := &metrics.LabelMap{Label: ""} + m.Add("a", 1) + return m + })(), + "foo{label=\"a\"} 1\n", + }, { "expvar_label_map", "counter_labelmap_keyname_m",