mirror of
https://github.com/tailscale/tailscale.git
synced 2025-08-11 21:27:31 +00:00
util/usermetrics: make usermetrics non-global
this commit changes usermetrics to be non-global, this is a building block for correct metrics if a go process runs multiple tsnets or in tests. Updates #13420 Updates tailscale/corp#22075 Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:

committed by
Kristoffer Dalby

parent
e1bbe1bf45
commit
0e0e53d3b3
@@ -97,7 +97,12 @@ type KeyValue[T comparable] struct {
|
||||
}
|
||||
|
||||
func (v *MultiLabelMap[T]) String() string {
|
||||
return `"MultiLabelMap"`
|
||||
var sb strings.Builder
|
||||
sb.WriteString("MultiLabelMap:\n")
|
||||
v.Do(func(kv KeyValue[T]) {
|
||||
fmt.Fprintf(&sb, "\t%v: %v\n", kv.Key, kv.Value)
|
||||
})
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// WritePrometheus writes v to w in Prometheus exposition format.
|
||||
@@ -281,3 +286,16 @@ func (v *MultiLabelMap[T]) Do(f func(KeyValue[T])) {
|
||||
f(KeyValue[T]{e.key, e.val})
|
||||
}
|
||||
}
|
||||
|
||||
// ResetAllForTest resets all values for metrics to zero.
|
||||
// Should only be used in tests.
|
||||
func (v *MultiLabelMap[T]) ResetAllForTest() {
|
||||
v.Do(func(kv KeyValue[T]) {
|
||||
switch v := kv.Value.(type) {
|
||||
case *expvar.Int:
|
||||
v.Set(0)
|
||||
case *expvar.Float:
|
||||
v.Set(0)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user