diff --git a/middleware/metrics.go b/middleware/metrics.go index 36e1c3a9..cbc8060f 100644 --- a/middleware/metrics.go +++ b/middleware/metrics.go @@ -1,19 +1,31 @@ package middleware import ( + "context" "fmt" "github.com/gin-gonic/gin" "github.com/prometheus/client_golang/prometheus" ) +type RouteKey struct{} + +func Route(route string) gin.HandlerFunc { + return func(c *gin.Context) { + c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), RouteKey{}, route)) + c.Next() + } +} + func Metrics(c *gin.Context) { c.Next() - customMetrics.histogram.WithLabelValues( - c.Request.Method, - c.Request.URL.String(), - fmt.Sprint(c.Writer.Status())) - + route, ok := c.Request.Context().Value(RouteKey{}).(string) + if ok { + customMetrics.histogram.WithLabelValues( + c.Request.Method, + route, + fmt.Sprint(c.Writer.Status())) + } } var customMetrics = newMetrics() diff --git a/server/http.go b/server/http.go index b5090a1d..ecaf40fb 100644 --- a/server/http.go +++ b/server/http.go @@ -53,16 +53,19 @@ func (s *HTTPServer) Init() error { pattern: "redirect", handler: failure.Handle(handlers.redirect), method: router.GET, + route: "redirect", }, { pattern: "display", handler: failure.Handle(handlers.display), method: router.GET, + route: "display", }, { pattern: "get", handler: failure.Handle(handlers.get), method: router.GET, + route: "get", }, } ) @@ -146,6 +149,7 @@ func (s *HTTPServer) Init() error { middleware.URLParser(s.config.Options.MimetypeDetector, s.processor), middleware.OperationParser(), middleware.RestrictSizes(s.config.Options.AllowedSizes), + middleware.Route(e.route), e.handler, } diff --git a/server/types.go b/server/types.go index dfc4ccbc..1e7af3d4 100644 --- a/server/types.go +++ b/server/types.go @@ -7,4 +7,5 @@ type endpoint struct { handler gin.HandlerFunc method handlerMethod pattern string + route string }