Browse Source

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 <[email protected]>
Brad Fitzpatrick 1 year ago
parent
commit
9699bb0a20
1 changed files with 3 additions and 1 deletions
  1. 3 1
      metrics/multilabelmap.go

+ 3 - 1
metrics/multilabelmap.go

@@ -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
 }