diff options
| author | Anton Tolchanov <anton@tailscale.com> | 2024-08-13 07:37:47 +0100 |
|---|---|---|
| committer | Anton Tolchanov <anton@tailscale.com> | 2024-08-14 10:17:43 +0100 |
| commit | b615a1d4db9f117f9b09ba5139a83a956c7bd6c0 (patch) | |
| tree | 4bc3c371546b384cb937949f6de7ba13614340ae | |
| parent | 10662c4282aa7c137ec582f24ab98116fb0de14e (diff) | |
| download | tailscale-knyar/metrictype.tar.xz tailscale-knyar/metrictype.zip | |
metrics: use constants for metric typesknyar/metrictype
NewMultiLabelMap does not return an error so it's easy for someone to
pass an incorrect/unsupported metric type and serve invalid metrics to
Prometheus.
This would read as `metrics.Counter` and `metrics.Gauge` when used,
which seems readable enough; but if you think bare strings are better,
an alternative to this approach might be to verify string value in
`NewMultiLabelMap` and panic?
Updates tailscale/corp#22075
Signed-off-by: Anton Tolchanov <anton@tailscale.com>
| -rw-r--r-- | metrics/multilabelmap.go | 13 | ||||
| -rw-r--r-- | metrics/multilabelmap_test.go | 2 |
2 files changed, 11 insertions, 4 deletions
diff --git a/metrics/multilabelmap.go b/metrics/multilabelmap.go index c0f312e7d..e60d08cd2 100644 --- a/metrics/multilabelmap.go +++ b/metrics/multilabelmap.go @@ -13,6 +13,13 @@ import ( "sync" ) +// MetricType is a Prometheus metric type. At the moment we only support +// counter and gauge. +type MetricType string + +const Counter MetricType = "counter" +const Gauge MetricType = "gauge" + // MultiLabelMap is a struct-value-to-Var map variable that satisfies the // [expvar.Var] interface but also allows for multiple Prometheus labels to be // associated with each value. @@ -22,7 +29,7 @@ import ( // The struct fields must all be strings, and the string values must be valid // Prometheus label values without requiring quoting. type MultiLabelMap[T comparable] struct { - Type string // optional Prometheus type ("counter", "gauge") + Type MetricType Help string // optional Prometheus help string m sync.Map // map[T]expvar.Var @@ -33,7 +40,7 @@ type MultiLabelMap[T comparable] struct { // NewMultiLabelMap creates and publishes (via expvar.Publish) a new // MultiLabelMap[T] variable with the given name and returns it. -func NewMultiLabelMap[T comparable](name string, promType, helpText string) *MultiLabelMap[T] { +func NewMultiLabelMap[T comparable](name string, promType MetricType, helpText string) *MultiLabelMap[T] { m := &MultiLabelMap[T]{ Type: promType, Help: helpText, @@ -105,7 +112,7 @@ func (v *MultiLabelMap[T]) WritePrometheus(w io.Writer, name string) { io.WriteString(w, "# TYPE ") io.WriteString(w, name) io.WriteString(w, " ") - io.WriteString(w, v.Type) + io.WriteString(w, string(v.Type)) io.WriteString(w, "\n") } if v.Help != "" { diff --git a/metrics/multilabelmap_test.go b/metrics/multilabelmap_test.go index 9a1340a3c..911f7bb51 100644 --- a/metrics/multilabelmap_test.go +++ b/metrics/multilabelmap_test.go @@ -82,7 +82,7 @@ func TestMultiLabelMapTypes(t *testing.T) { } m := new(MultiLabelMap[LabelTypes]) - m.Type = "counter" + m.Type = Counter m.Help = "some good stuff" m.Add(LabelTypes{"a", true, -1, 2}, 3) var buf bytes.Buffer |
