tsweb: sort top-level expvars after removing type prefixes

Fixes #5778

Change-Id: I56c367338fa5686da288cc6545209ef4d6b88549
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2022-10-10 19:52:07 -07:00
committed by Brad Fitzpatrick
parent 0475ed4a7e
commit 614a24763b
2 changed files with 51 additions and 1 deletions

View File

@ -22,6 +22,7 @@ import (
"github.com/google/go-cmp/cmp"
"tailscale.com/metrics"
"tailscale.com/tstest"
"tailscale.com/version"
)
type noopHijacker struct {
@ -743,3 +744,30 @@ func TestSortedStructAllocs(t *testing.T) {
t.Errorf("allocs = %v; want 0", n)
}
}
func TestVarzHandlerSorting(t *testing.T) {
defer func() { expvarDo = expvar.Do }()
expvarDo = func(f func(expvar.KeyValue)) {
f(expvar.KeyValue{Key: "counter_zz", Value: new(expvar.Int)})
f(expvar.KeyValue{Key: "gauge_aa", Value: new(expvar.Int)})
}
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
VarzHandler(rec, req)
got := rec.Body.Bytes()
const want = "# TYPE aa gauge\naa 0\n# TYPE zz counter\nzz 0\n"
if string(got) != want {
t.Errorf("got %q; want %q", got, want)
}
rec = new(httptest.ResponseRecorder) // without a body
// Lock in the current number of allocs, to prevent it from growing.
if !version.IsRace() {
allocs := int(testing.AllocsPerRun(1000, func() {
VarzHandler(rec, req)
}))
if max := 13; allocs > max {
t.Errorf("allocs = %v; want max %v", allocs, max)
}
}
}