metrics,syncs: add ShardedInt support to metrics.LabelMap

metrics.LabelMap grows slightly more heavy, needing a lock to ensure
proper ordering for newly initialized ShardedInt values. An Add method
enables callers to use .Add for both expvar.Int and syncs.ShardedInt
values, but retains the original behavior of defaulting to initializing
expvar.Int values.

Updates tailscale/corp#25450

Co-Authored-By: Andrew Dunham <andrew@du.nham.ca>
Signed-off-by: James Tucker <james@tailscale.com>
This commit is contained in:
James Tucker
2024-12-19 16:32:40 -08:00
committed by James Tucker
parent 72b278937b
commit 68b12a74ed
3 changed files with 46 additions and 0 deletions

View File

@ -21,6 +21,15 @@ func TestLabelMap(t *testing.T) {
if g, w := m.Get("bar").Value(), int64(2); g != w {
t.Errorf("bar = %v; want %v", g, w)
}
m.GetShardedInt("sharded").Add(5)
if g, w := m.GetShardedInt("sharded").Value(), int64(5); g != w {
t.Errorf("sharded = %v; want %v", g, w)
}
m.Add("sharded", 1)
if g, w := m.GetShardedInt("sharded").Value(), int64(6); g != w {
t.Errorf("sharded = %v; want %v", g, w)
}
m.Add("neverbefore", 1)
}
func TestCurrentFileDescriptors(t *testing.T) {