Skip to content

Commit

Permalink
support url mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
hirosumee committed Sep 21, 2022
1 parent 485fff8 commit b8b7e60
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/fiber-prometheus-middleware.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions prometheus.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand All @@ -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{
Expand All @@ -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())
Expand All @@ -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() {
Expand Down

0 comments on commit b8b7e60

Please sign in to comment.