util/clientmetric: allow client metric values to be provided by a function

Adds NewGaugeFunc and NewCounterFunc (inspired by expvar.Func) which
change the current value to be reported by a function. This allows
some client metric values to be computed on-demand during uploading (at
most every 15 seconds), instead of being continuously updated.

clientmetric uploading had a bunch of micro-optimizations for memory
access (#3331) which are not possible with this approach. However, any
performance hit from function-based metrics is contained to those metrics
only, and we expect to have very few.

Also adds a DisableDeltas() option for client metrics, so that absolute
values are always reported. This makes server-side processing of some
metrics easier to reason about.

Updates tailscale/corp#9230

Signed-off-by: Mihai Parparita <mihai@tailscale.com>
This commit is contained in:
Mihai Parparita
2023-04-05 14:19:40 -07:00
committed by Mihai Parparita
parent c0e0a5458f
commit f49b9f75b8
2 changed files with 105 additions and 14 deletions

View File

@ -72,3 +72,38 @@ func TestEncodeLogTailMetricsDelta(t *testing.T) {
t.Errorf("with increments = %q; want %q", got, want)
}
}
func TestDisableDeltas(t *testing.T) {
clearMetrics()
c := NewCounter("foo")
c.DisableDeltas()
c.Set(123)
if got, want := EncodeLogTailMetricsDelta(), "N06fooS02f601"; got != want {
t.Errorf("first = %q; want %q", got, want)
}
c.Set(456)
advanceTime()
if got, want := EncodeLogTailMetricsDelta(), "S029007"; got != want {
t.Errorf("second = %q; want %q", got, want)
}
}
func TestWithFunc(t *testing.T) {
clearMetrics()
v := int64(123)
NewCounterFunc("foo", func() int64 { return v })
if got, want := EncodeLogTailMetricsDelta(), "N06fooS02f601"; got != want {
t.Errorf("first = %q; want %q", got, want)
}
v = 456
advanceTime()
if got, want := EncodeLogTailMetricsDelta(), "I029a05"; got != want {
t.Errorf("second = %q; want %q", got, want)
}
}