From 72a7f41564816f6e3070c3bc87764ab54167e2cd Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 26 Nov 2026 15:01:43 +0000 Subject: [PATCH] tsweb/promvarz, cmd/tailscaled: don't mix other metrics into filtered varz The promvarz and tailscaled /debug/varz handlers wrap varz.Handler with metrics it doesn't know about: promvarz writes the Prometheus client's metrics first, and tailscaled appends clientmetrics. With the match and interval parameters that varz got in 258b0879984b (#22636), that meant unfiltered metrics around filtered ones, a bad parameter's error after a 200 and a full dump, and streams that didn't start with a frame. Now requests with a query string get only varz's own output. Updates tailscale/corp#47429 Change-Id: I617969401b1ff269db32481b2656841b70355d57 Signed-off-by: Brad Fitzpatrick --- cmd/tailscaled/debug.go | 9 ++++++++- tsweb/promvarz/promvarz.go | 12 ++++++++++++ tsweb/promvarz/promvarz_test.go | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cmd/tailscaled/debug.go b/cmd/tailscaled/debug.go index 360075f5b..47a21a6e7 100644 --- a/cmd/tailscaled/debug.go +++ b/cmd/tailscaled/debug.go @@ -63,7 +63,14 @@ func newDebugMux() *http.ServeMux { func servePrometheusMetrics(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") varz.Handler(w, r) - clientmetric.WritePrometheusExpositionFormat(w) + + // varz's "match" and "interval" parameters cover only its own + // metrics, so leave out clientmetrics when there's a query string. + // TODO(bradfitz): cover clientmetrics too, perhaps as an expvar + // implementing [varz.PrometheusWriter]. + if r.URL.RawQuery == "" { + clientmetric.WritePrometheusExpositionFormat(w) + } } func debugMode(args []string) error { diff --git a/tsweb/promvarz/promvarz.go b/tsweb/promvarz/promvarz.go index 4fdf394d0..7c5e6c3a3 100644 --- a/tsweb/promvarz/promvarz.go +++ b/tsweb/promvarz/promvarz.go @@ -25,7 +25,19 @@ func registerVarz(debug *tsweb.DebugHandler) { // handler returns Prometheus metrics exported by our expvar converter // and the official Prometheus client. +// +// Requests with a query string, such as varz's "match" and "interval" +// parameters, get only the expvar metrics, so that varz can reject bad +// parameters before writing anything and can stream. +// +// TODO(bradfitz): make match and interval cover the native metrics too, +// perhaps by publishing them as an expvar implementing +// [varz.PrometheusWriter] so varz handles them like any other var. func handler(w http.ResponseWriter, r *http.Request) { + if r.URL != nil && r.URL.RawQuery != "" { + varz.Handler(w, r) + return + } if err := gatherNativePrometheusMetrics(w); err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) diff --git a/tsweb/promvarz/promvarz_test.go b/tsweb/promvarz/promvarz_test.go index 123330d6e..e0ad44fb5 100644 --- a/tsweb/promvarz/promvarz_test.go +++ b/tsweb/promvarz/promvarz_test.go @@ -37,3 +37,22 @@ func TestHandler(t *testing.T) { t.Error(err) } } + +// TestHandlerQuery checks that requests with varz query parameters +// skip the native metrics, so bad parameters get a clean error and +// filtered output has only what matched. +func TestHandlerQuery(t *testing.T) { + testVar1.Set(42) + + rec := httptest.NewRecorder() + handler(rec, httptest.NewRequest("GET", "/debug/varz?match=(", nil)) + if rec.Code != http.StatusBadRequest || strings.Contains(rec.Body.String(), "promvarz_test_native") { + t.Errorf("bad match: code %d, body:\n%s", rec.Code, rec.Body.Bytes()) + } + + rec = httptest.NewRecorder() + handler(rec, httptest.NewRequest("GET", "/debug/varz?match=^promvarz_", nil)) + if got, want := rec.Body.String(), "# TYPE promvarz_test_expvar gauge\npromvarz_test_expvar 42\n"; got != want { + t.Errorf("match: got %q; want %q", got, want) + } +}