Skip to content

Commit 3b6a66f

Browse files
committed
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 ... ```
1 parent df36b8f commit 3b6a66f

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66
"net/http"
77
"os"
8+
"strings"
89

910
"github.com/prometheus/client_golang/prometheus"
1011
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -33,6 +34,11 @@ var (
3334
Name: "http_request_duration_seconds",
3435
Help: "Duration of all HTTP requests",
3536
}, []string{"code", "handler", "method"})
37+
38+
httpRequestsTotalOtel = prometheus.NewCounterVec(prometheus.CounterOpts{
39+
Name: "http.server.requests",
40+
Help: "Count of HTTP requests per HTTP method (following the OpenTelemetry semantic conventions)",
41+
}, []string{"http.request.method"})
3642
)
3743

3844
func main() {
@@ -47,14 +53,17 @@ func main() {
4753
r := prometheus.NewRegistry()
4854
r.MustRegister(httpRequestsTotal)
4955
r.MustRegister(httpRequestDuration)
56+
r.MustRegister(httpRequestsTotalOtel)
5057
r.MustRegister(version)
5158

5259
// Initialize the most likely labels.
5360
httpRequestDuration.WithLabelValues("200", foundHandlerName, "get")
61+
httpRequestsTotalOtel.WithLabelValues("get")
5462
httpRequestsTotal.WithLabelValues("200", "get")
5563
httpRequestsTotal.WithLabelValues("404", "get")
5664

5765
foundHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
66+
httpRequestsTotalOtel.WithLabelValues(strings.ToLower(r.Method)).Inc()
5867
w.WriteHeader(http.StatusOK)
5968
w.Write([]byte("Hello from example application."))
6069
})

0 commit comments

Comments
 (0)