From 3b6a66f8fec430f834884983c07a73e6033b90b4 Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Tue, 30 Sep 2025 17:41:36 +0200 Subject: [PATCH] chore: add "http.server.requests" metric This commit adds a new metric to test UTF-8 metrics with Prometheus. Example ``` $ curl -H "Accept: text/plain;version=1.0.0;escaping=allow-utf-8" localhost:8080/metrics (following the OpenTelemetry semantic conventions) {"http.server.requests","http.request.method"="get"} 171 {"http.server.requests","http.request.method"="post"} 167 ... ``` --- main.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/main.go b/main.go index dfcd6c1..896f4d6 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "os" + "strings" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" @@ -33,6 +34,11 @@ var ( Name: "http_request_duration_seconds", Help: "Duration of all HTTP requests", }, []string{"code", "handler", "method"}) + + httpRequestsTotalOtel = prometheus.NewCounterVec(prometheus.CounterOpts{ + Name: "http.server.requests", + Help: "Count of HTTP requests per HTTP method (following the OpenTelemetry semantic conventions)", + }, []string{"http.request.method"}) ) func main() { @@ -47,14 +53,17 @@ func main() { r := prometheus.NewRegistry() r.MustRegister(httpRequestsTotal) r.MustRegister(httpRequestDuration) + r.MustRegister(httpRequestsTotalOtel) r.MustRegister(version) // Initialize the most likely labels. httpRequestDuration.WithLabelValues("200", foundHandlerName, "get") + httpRequestsTotalOtel.WithLabelValues("get") httpRequestsTotal.WithLabelValues("200", "get") httpRequestsTotal.WithLabelValues("404", "get") foundHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + httpRequestsTotalOtel.WithLabelValues(strings.ToLower(r.Method)).Inc() w.WriteHeader(http.StatusOK) w.Write([]byte("Hello from example application.")) })