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 <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2023-06-30 08:55:57 -07:00 committed by Brad Fitzpatrick
parent 2e19790f61
commit 79ee6d6e1e
2 changed files with 11 additions and 1 deletions

View File

@ -189,7 +189,7 @@ funcRet = fmt.Sprintf(" returning %T", v)
// IntMap uses expvar.Map on the inside, which presorts // IntMap uses expvar.Map on the inside, which presorts
// keys. The output ordering is deterministic. // keys. The output ordering is deterministic.
v.Do(func(kv expvar.KeyValue) { 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: case *expvar.Map:
if label != "" && typ != "" { if label != "" && typ != "" {

View File

@ -165,6 +165,16 @@ func() *expvar.Map {
})(), })(),
"control_save_config{reason=\"fun\"} 1\ncontrol_save_config{reason=\"new\"} 1\ncontrol_save_config{reason=\"updated\"} 1\n", "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", "expvar_label_map",
"counter_labelmap_keyname_m", "counter_labelmap_keyname_m",