Skip to content

Commit

Permalink
using handler pattern instead of url mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
hirosumee committed Sep 21, 2022
1 parent 8a31b44 commit cac9cfe
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fiberprom
import (
"log"
"strconv"
"strings"
"time"

"github.com/gofiber/adaptor/v2"
Expand All @@ -19,8 +18,6 @@ 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 @@ -33,10 +30,6 @@ 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 Down Expand Up @@ -69,25 +62,25 @@ func (p *Prometheus) HandlerFunc() fiber.Handler {
return ctx.Next()
}

if len(p.urlMapper) > 0 {
for prefix, val := range p.urlMapper {
if strings.HasPrefix(uri, prefix) {
uri = val
break
}
start := time.Now()
// next
err := ctx.Next()

status := fiber.StatusInternalServerError
if err != nil {
if e, ok := err.(*fiber.Error); ok {
// Get correct error code from fiber.Error type
status = e.Code
}
} else {
status = ctx.Response().StatusCode()
}
uri = ctx.Route().Path
elapsed := float64(time.Since(start)) / float64(time.Second)
ep := ctx.Method() + "_" + uri
p.reqDur.WithLabelValues(strconv.Itoa(status), ep).Observe(elapsed)

start := time.Now()

defer func() {
status := strconv.Itoa(ctx.Response().StatusCode())
elapsed := float64(time.Since(start)) / float64(time.Second)
ep := ctx.Method() + "_" + uri
p.reqDur.WithLabelValues(status, ep).Observe(elapsed)
}()
// next
return ctx.Next()
return err
}
}

Expand Down

0 comments on commit cac9cfe

Please sign in to comment.