From 32109d0db8861ea4a1aff04ef03ea145da8d571e Mon Sep 17 00:00:00 2001 From: miyuko Date: Thu, 16 Oct 2025 00:31:18 +0100 Subject: [PATCH] Monitor how long it takes us to respond to HTTP requests. --- src/observe.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/observe.go b/src/observe.go index 0522808..603c5b6 100644 --- a/src/observe.go +++ b/src/observe.go @@ -22,6 +22,17 @@ import ( sentryslog "github.com/getsentry/sentry-go/slog" ) +var ( + httpRequestDurationSeconds = promauto.NewHistogramVec(prometheus.HistogramOpts{ + Name: "git_pages_http_request_duration_seconds", + Help: "Time to respond to incoming HTTP requests", + + NativeHistogramBucketFactor: 1.1, + NativeHistogramMaxBucketNumber: 100, + NativeHistogramMinResetDuration: 10 * time.Minute, + }, []string{"method"}) +) + func hasSentry() bool { return os.Getenv("SENTRY_DSN") != "" } @@ -129,6 +140,18 @@ func ObserveHTTPHandler(handler http.Handler) http.Handler { }(handler) } + handler = func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + next.ServeHTTP(w, r) + duration := time.Since(start) + + httpRequestDurationSeconds. + With(prometheus.Labels{"method": r.Method}). + Observe(duration.Seconds()) + }) + }(handler) + return handler }