metrics: fix outdated docs on MultiLabelMap

And make NewMultiLabelMap panic earlier (at construction time)
if the comparable struct type T violates the documented rules,
rather than panicking at Add time.

Updates #cleanup

Change-Id: Ib1a03babdd501b8d699c4f18b1097a56c916c6d5
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2024-04-04 20:47:27 -07:00 committed by Brad Fitzpatrick
parent fe0cfec4ad
commit 9699bb0a20
1 changed files with 3 additions and 1 deletions

View File

@ -17,7 +17,7 @@ import (
// [expvar.Var] interface but also allows for multiple Prometheus labels to be
// associated with each value.
//
// T must be a struct type with only string fields. The struct field names
// T must be a struct type with scalar fields. The struct field names
// (lowercased) are used as the labels, unless a "prom" struct tag is present.
// The struct fields must all be strings, and the string values must be valid
// Prometheus label values without requiring quoting.
@ -38,6 +38,8 @@ func NewMultiLabelMap[T comparable](name string, promType, helpText string) *Mul
Type: promType,
Help: helpText,
}
var zero T
_ = labelString(zero) // panic early if T is invalid
expvar.Publish(name, m)
return m
}