mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-26 11:36:26 -08:00
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 258b087998 (#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 <[email protected]>
This commit is contained in:
1 parent
3ffce79f7b
commit
72a7f41564
3 files changed
+39
-1
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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()))
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user