aboutsummaryrefslogtreecommitdiff
path: root/internal/middleware/metrics.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/middleware/metrics.go')
-rw-r--r--internal/middleware/metrics.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/internal/middleware/metrics.go b/internal/middleware/metrics.go
new file mode 100644
index 0000000..f9d7e0f
--- /dev/null
+++ b/internal/middleware/metrics.go
@@ -0,0 +1,43 @@
+package middleware
+
+import (
+ "net/http"
+
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promhttp"
+)
+
+var (
+ requestCounter = prometheus.NewCounterVec(
+ prometheus.CounterOpts{
+ Name: "handler_requests_total",
+ Help: "Total number of request/responses by HTTP status code.",
+ }, []string{"handler", "code"})
+
+ requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
+ Name: "handler_request_duration",
+ Help: "Handler request duration.",
+ }, []string{"handler", "method"})
+
+ requestInFlight = prometheus.NewGaugeVec(prometheus.GaugeOpts{
+ Name: "handler_requests_in_flight",
+ Help: "Current number of requests being served.",
+ }, []string{"handler"})
+)
+
+func init() {
+ _ = prometheus.Register(requestCounter)
+ _ = prometheus.Register(requestDuration)
+ _ = prometheus.Register(requestInFlight)
+}
+
+// Metrics captures request duration, request count and in-flight request count
+// metrics for HTTP handlers. The family field is used to discriminate handlers.
+func Metrics(family string, next http.Handler) http.Handler {
+ return promhttp.InstrumentHandlerDuration(
+ requestDuration.MustCurryWith(prometheus.Labels{"handler": family}),
+ promhttp.InstrumentHandlerCounter(requestCounter.MustCurryWith(prometheus.Labels{"handler": family}),
+ promhttp.InstrumentHandlerInFlight(requestInFlight.With(prometheus.Labels{"handler": family}), next),
+ ),
+ )
+}