From 328d910aee822178f55442c76bc7e35ce34d68cc Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Wed, 5 Aug 2026 20:35:17 +0200 Subject: [PATCH] fix(api): correctly return metrics, support bundle (fixes #10847) (#10849) Our faked request wasn't good enough; improve it, adding a test. Signed-off-by: Jakob Borg --- lib/api/api.go | 18 +++++++++++++----- lib/api/api_test.go | 8 ++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/api/api.go b/lib/api/api.go index b82e12ca8..5fa3e4335 100644 --- a/lib/api/api.go +++ b/lib/api/api.go @@ -1184,10 +1184,7 @@ func (s *service) getSupportBundle(w http.ResponseWriter, r *http.Request) { } // Metrics data as text - var metricsBuf bytes.Buffer - wr := bufferedResponseWriter{Writer: &metricsBuf} - promhttp.Handler().ServeHTTP(wr, &http.Request{Method: http.MethodGet}) - files = append(files, fileEntry{name: "metrics.txt", data: metricsBuf.Bytes()}) + files = append(files, fileEntry{name: "metrics.txt", data: prometheusMetrics()}) // Connection data as JSON connStats := s.model.ConnectionStats() @@ -1258,6 +1255,16 @@ func (s *service) getSupportBundle(w http.ResponseWriter, r *http.Request) { io.Copy(w, &zipFilesBuffer) } +func prometheusMetrics() []byte { + var metricsBuf bytes.Buffer + wr := bufferedResponseWriter{Writer: &metricsBuf} + promhttp.Handler().ServeHTTP(wr, &http.Request{ + Method: http.MethodGet, + URL: &url.URL{Scheme: "http://", Host: "localhost", Path: "/metrics"}, + }) + return metricsBuf.Bytes() +} + func (s *service) getSystemDiscovery(w http.ResponseWriter, _ *http.Request) { devices := make(map[string]discover.CacheEntry) @@ -1946,7 +1953,8 @@ func sanitizedHostname(name string) (string, error) { return r > unicode.MaxASCII || !unicode.IsLetter(r) && !unicode.IsNumber(r) && r != '.' && r != '-' - }))) + })), + ) name, _, err := transform.String(t, name) if err != nil { return "", err diff --git a/lib/api/api_test.go b/lib/api/api_test.go index a1e298f0a..bbf902e47 100644 --- a/lib/api/api_test.go +++ b/lib/api/api_test.go @@ -1829,6 +1829,14 @@ func TestSanitizedHostname(t *testing.T) { } } +func TestPrometheusMetrics(t *testing.T) { + // We should get some form of reasonable metrics response + bs := prometheusMetrics() + if !bytes.Contains(bs, []byte("TYPE go_info gauge")) { + t.Error("metrics should include go_info gauge") + } +} + // runningInContainer returns true if we are inside Docker or LXC. It might // be prone to false negatives if things change in the future, but likely // not false positives.