From 9e4603ed991f7705e9eb4dc75722e0f8a941e1aa Mon Sep 17 00:00:00 2001 From: Jim Lambert Date: Tue, 18 Sep 2018 10:02:33 -0400 Subject: [PATCH] support aggregate label for prometheus metrics --- middleware.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/middleware.go b/middleware.go index c5a1924..4ededae 100644 --- a/middleware.go +++ b/middleware.go @@ -95,6 +95,9 @@ type Prometheus struct { MetricsPath string ReqCntURLLabelMappingFn RequestCounterURLLabelMappingFn + + // gin.Context string to use as a prometheus URL label + URLLabelFromContext string } // PrometheusPushGateway contains the configuration for pushing to a Prometheus pushgateway (optional) @@ -366,6 +369,14 @@ func (p *Prometheus) handlerFunc() gin.HandlerFunc { p.reqDur.Observe(elapsed) url := p.ReqCntURLLabelMappingFn(c) + // jlambert Oct 2018 - sidecar specific mod + if len(p.URLLabelFromContext) > 0 { + u, found := c.Get(p.URLLabelFromContext) + if !found { + u = "unknown" + } + url = u.(string) + } p.reqCnt.WithLabelValues(status, c.Request.Method, c.HandlerName(), c.Request.Host, url).Inc() p.reqSz.Observe(float64(reqSz)) p.resSz.Observe(resSz)