diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/fiber-prometheus-middleware.iml b/.idea/fiber-prometheus-middleware.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/fiber-prometheus-middleware.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0e40ce8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/prometheus.go b/prometheus.go index c5dd540..3f58770 100644 --- a/prometheus.go +++ b/prometheus.go @@ -1,13 +1,14 @@ package fiberprom import ( + "log" + "strconv" + "time" + "github.com/gofiber/adaptor/v2" "github.com/gofiber/fiber/v2" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" - "log" - "strconv" - "time" ) var defaultMetricPath = "/metrics" @@ -17,6 +18,8 @@ type Prometheus struct { reqDur *prometheus.HistogramVec router fiber.Router MetricsPath string + // urlMapper is a map of url to be mapped to a different url to avoid too many labels + urlMapper map[string]string } // NewPrometheus generates a new set of metrics with a certain subsystem name @@ -29,6 +32,10 @@ func NewPrometheus(subsystem string) *Prometheus { return p } +func (p *Prometheus) SetURLMapper(mapper map[string]string) { + p.urlMapper = mapper +} + func (p *Prometheus) registerMetrics(subsystem string) { p.reqDur = prometheus.NewHistogramVec( prometheus.HistogramOpts{ @@ -52,7 +59,7 @@ func (p *Prometheus) Use(r fiber.Router) { r.Get(p.MetricsPath, prometheusHandler()) } -// HandlerFunc is onion or wraper to handler for fasthttp listenandserve +// HandlerFunc is onion or wrapper to handler for fasthttp listenandserve func (p *Prometheus) HandlerFunc() fiber.Handler { return func(ctx *fiber.Ctx) error { uri := string(ctx.Request().URI().Path()) @@ -61,6 +68,12 @@ func (p *Prometheus) HandlerFunc() fiber.Handler { return ctx.Next() } + if len(p.urlMapper) > 0 { + if mappedPath, ok := p.urlMapper[uri]; ok { + uri = mappedPath + } + } + start := time.Now() defer func() {