From 45851d93718cfb9f044853c40f5c01230d93b104 Mon Sep 17 00:00:00 2001 From: Will Hegedus Date: Wed, 5 Nov 2025 13:20:27 -0500 Subject: [PATCH 1/5] Fix pprof endpoints not working with --web.route-prefix When using --web.route-prefix or --web.external-url with a path component, the pprof debug endpoints were not accessible. The handlers were directly passing requests to http.DefaultServeMux without adjusting the URL path to match what the pprof handlers expect. For example, with --web.route-prefix=/prometheus/alertmanager, a request to /prometheus/alertmanager/debug/pprof/ was being forwarded with the full path intact, but http.DefaultServeMux only has handlers registered for paths starting with /debug/pprof/. This fix reconstructs the request path by extracting the subpath parameter from the route and prepending /debug to it, ensuring the path matches what http.DefaultServeMux expects. The fix also preserves trailing slashes which are required by some pprof handlers. Added unit tests to verify pprof endpoints work correctly both with and without route prefix configuration. Co-authored-by: Claude Signed-off-by: Will Hegedus --- ui/web.go | 14 +++++++++-- ui/web_test.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 ui/web_test.go diff --git a/ui/web.go b/ui/web.go index 3b81135237..1c57671ebd 100644 --- a/ui/web.go +++ b/ui/web.go @@ -19,6 +19,7 @@ import ( "net/http" _ "net/http/pprof" // Comment this line to disable pprof endpoint. "path" + "strings" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/route" @@ -87,8 +88,17 @@ func Register(r *route.Router, reloadCh chan<- chan error, logger *slog.Logger) w.WriteHeader(http.StatusOK) }) - r.Get("/debug/*subpath", http.DefaultServeMux.ServeHTTP) - r.Post("/debug/*subpath", http.DefaultServeMux.ServeHTTP) + debugHandlerFunc := func(w http.ResponseWriter, req *http.Request) { + subpath := route.Param(req.Context(), "subpath") + req.URL.Path = path.Join("/debug", subpath) + // path.Join removes trailing slashes, but some pprof handlers expect them. + if strings.HasSuffix(subpath, "/") && !strings.HasSuffix(req.URL.Path, "/") { + req.URL.Path += "/" + } + http.DefaultServeMux.ServeHTTP(w, req) + } + r.Get("/debug/*subpath", debugHandlerFunc) + r.Post("/debug/*subpath", debugHandlerFunc) } func disableCaching(w http.ResponseWriter) { diff --git a/ui/web_test.go b/ui/web_test.go new file mode 100644 index 0000000000..d1551d8562 --- /dev/null +++ b/ui/web_test.go @@ -0,0 +1,67 @@ +// Copyright 2015 Prometheus Team +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ui + +import ( + "log/slog" + "net/http" + "net/http/httptest" + "os" + "testing" + + "github.com/prometheus/common/route" +) + +func TestDebugHandlersWithRoutePrefix(t *testing.T) { + logger := slog.New(slog.NewTextHandler(os.Stderr, nil)) + reloadCh := make(chan chan error) + + // Test with route prefix + routePrefix := "/prometheus/alertmanager" + router := route.New().WithPrefix(routePrefix) + Register(router, reloadCh, logger) + + // Test GET request to pprof index (note: pprof index returns text/html) + req := httptest.NewRequest("GET", routePrefix+"/debug/pprof/", nil) + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Errorf("Expected status %d for %s, got %d. Body: %s", http.StatusOK, req.URL.Path, w.Code, w.Body.String()) + } + if w.Body.Len() == 0 { + t.Error("Expected non-empty response body for pprof index") + } + + // Test GET request to pprof heap endpoint + req = httptest.NewRequest("GET", routePrefix+"/debug/pprof/heap", nil) + w = httptest.NewRecorder() + router.ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Errorf("Expected status %d for %s, got %d", http.StatusOK, req.URL.Path, w.Code) + } + + // Test without route prefix (should also work) + router2 := route.New() + Register(router2, reloadCh, logger) + + req = httptest.NewRequest("GET", "/debug/pprof/", nil) + w = httptest.NewRecorder() + router2.ServeHTTP(w, req) + + if w.Code != http.StatusOK { + t.Errorf("Expected status %d for %s without prefix, got %d", http.StatusOK, req.URL.Path, w.Code) + } +} From b057160188e271717bc690ba59f835d31a619acf Mon Sep 17 00:00:00 2001 From: Will Hegedus Date: Thu, 13 Nov 2025 11:33:50 -0500 Subject: [PATCH 2/5] chore: update ui/web_test.go copyright header Co-authored-by: Siavash Safi Signed-off-by: Will Hegedus --- ui/web_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/web_test.go b/ui/web_test.go index d1551d8562..0a382b7800 100644 --- a/ui/web_test.go +++ b/ui/web_test.go @@ -1,4 +1,4 @@ -// Copyright 2015 Prometheus Team +// Copyright The Prometheus Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at From 81053dbcd5f97cbbbbefc2d42bce18b539a459c1 Mon Sep 17 00:00:00 2001 From: Will Hegedus Date: Thu, 13 Nov 2025 11:38:05 -0500 Subject: [PATCH 3/5] chore: update ui/web.go copyright header Signed-off-by: Will Hegedus --- ui/web.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/web.go b/ui/web.go index 1c57671ebd..4c6d189463 100644 --- a/ui/web.go +++ b/ui/web.go @@ -1,4 +1,4 @@ -// Copyright 2015 Prometheus Team +// Copyright The Prometheus Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at From b1f68caec5712c7c0be680008f26f046dd036756 Mon Sep 17 00:00:00 2001 From: Will Hegedus Date: Mon, 17 Nov 2025 10:13:11 -0500 Subject: [PATCH 4/5] test: Use testify for new ui/web_test.go tests Updated based on PR comments to conform with existing testing patterns Signed-off-by: Will Hegedus --- ui/web_test.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/ui/web_test.go b/ui/web_test.go index 0a382b7800..528c4e8456 100644 --- a/ui/web_test.go +++ b/ui/web_test.go @@ -21,6 +21,7 @@ import ( "testing" "github.com/prometheus/common/route" + "github.com/stretchr/testify/require" ) func TestDebugHandlersWithRoutePrefix(t *testing.T) { @@ -37,21 +38,15 @@ func TestDebugHandlersWithRoutePrefix(t *testing.T) { w := httptest.NewRecorder() router.ServeHTTP(w, req) - if w.Code != http.StatusOK { - t.Errorf("Expected status %d for %s, got %d. Body: %s", http.StatusOK, req.URL.Path, w.Code, w.Body.String()) - } - if w.Body.Len() == 0 { - t.Error("Expected non-empty response body for pprof index") - } + require.Equal(t, http.StatusOK, w.Code) + require.NotEqual(t, 0, w.Body.Len()) // Test GET request to pprof heap endpoint req = httptest.NewRequest("GET", routePrefix+"/debug/pprof/heap", nil) w = httptest.NewRecorder() router.ServeHTTP(w, req) - if w.Code != http.StatusOK { - t.Errorf("Expected status %d for %s, got %d", http.StatusOK, req.URL.Path, w.Code) - } + require.Equal(t, http.StatusOK, w.Code) // Test without route prefix (should also work) router2 := route.New() @@ -61,7 +56,5 @@ func TestDebugHandlersWithRoutePrefix(t *testing.T) { w = httptest.NewRecorder() router2.ServeHTTP(w, req) - if w.Code != http.StatusOK { - t.Errorf("Expected status %d for %s without prefix, got %d", http.StatusOK, req.URL.Path, w.Code) - } + require.Equal(t, http.StatusOK, w.Code) } From 7317f2d47ebe1949fe30f6392e833cd90637a5d8 Mon Sep 17 00:00:00 2001 From: Will Hegedus Date: Tue, 25 Nov 2025 13:31:50 -0500 Subject: [PATCH 5/5] test: modify test for pprof handler to check response body Signed-off-by: Will Hegedus --- ui/web_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ui/web_test.go b/ui/web_test.go index 528c4e8456..e6293d1834 100644 --- a/ui/web_test.go +++ b/ui/web_test.go @@ -39,7 +39,7 @@ func TestDebugHandlersWithRoutePrefix(t *testing.T) { router.ServeHTTP(w, req) require.Equal(t, http.StatusOK, w.Code) - require.NotEqual(t, 0, w.Body.Len()) + require.Contains(t, w.Body.String(), "/debug/pprof/", "pprof page did not load with expected content when using a route prefix") // Test GET request to pprof heap endpoint req = httptest.NewRequest("GET", routePrefix+"/debug/pprof/heap", nil) @@ -57,4 +57,5 @@ func TestDebugHandlersWithRoutePrefix(t *testing.T) { router2.ServeHTTP(w, req) require.Equal(t, http.StatusOK, w.Code) + require.Contains(t, w.Body.String(), "/debug/pprof/", "pprof page did not load with expected content") }