Skip to content

Commit 7b0adca

Browse files
authored
fix: High-cardinality metrics and updates to middleware (#221)
* Fix high-cardinality metrics. Add New() with default registry. Expand unit-tests * Add missing buckets
1 parent 35b71cc commit 7b0adca

File tree

6 files changed

+404
-199
lines changed

6 files changed

+404
-199
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
Build:
1010
strategy:
1111
matrix:
12-
go-version: [1.21.x, 1.22.x]
12+
go-version: [1.22.x, 1.23.x]
1313
platform: [ubuntu-latest, windows-latest]
1414
runs-on: ${{ matrix.platform }}
1515
steps:

README.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
# fiberprometheus
66

7-
Prometheus middleware for gofiber.
7+
Prometheus middleware for [Fiber](https://github.com/gofiber/fiber)).
88

9-
**Note: Requires Go 1.21 and above**
9+
**Note: Requires Go 1.22 and above**
1010

1111
![Release](https://img.shields.io/github/release/ansrivas/fiberprometheus.svg)
1212
[![Discord](https://img.shields.io/badge/discord-join%20channel-7289DA)](https://gofiber.io/discord)
@@ -16,16 +16,15 @@ Prometheus middleware for gofiber.
1616

1717
Following metrics are available by default:
1818

19-
```
19+
```text
2020
http_requests_total
2121
http_request_duration_seconds
2222
http_requests_in_progress_total
23-
http_cache_results
2423
```
2524

2625
### Install v2
2726

28-
```
27+
```console
2928
go get -u github.com/gofiber/fiber/v2
3029
go get -u github.com/ansrivas/fiberprometheus/v2
3130
```
@@ -36,8 +35,8 @@ go get -u github.com/ansrivas/fiberprometheus/v2
3635
package main
3736

3837
import (
39-
"github.com/ansrivas/fiberprometheus/v2"
40-
"github.com/gofiber/fiber/v2"
38+
"github.com/ansrivas/fiberprometheus/v2"
39+
"github.com/gofiber/fiber/v2"
4140
)
4241

4342
func main() {
@@ -74,6 +73,6 @@ func main() {
7473
- Hit the default url at http://localhost:3000
7574
- Navigate to http://localhost:3000/metrics
7675

77-
### Grafana Board
76+
### Grafana Dashboard
7877

7978
- https://grafana.com/grafana/dashboards/14331

go.mod

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
module github.com/ansrivas/fiberprometheus/v2
22

3-
go 1.21
3+
go 1.22
44

55
require (
6-
github.com/gofiber/fiber/v2 v2.52.4
7-
github.com/prometheus/client_golang v1.19.0
8-
github.com/valyala/fasthttp v1.52.0
6+
github.com/gofiber/fiber/v2 v2.52.5
7+
github.com/prometheus/client_golang v1.20.5
8+
github.com/valyala/fasthttp v1.57.0
99
)
1010

1111
require (
12-
github.com/andybalholm/brotli v1.1.0 // indirect
12+
github.com/andybalholm/brotli v1.1.1 // indirect
1313
github.com/beorn7/perks v1.0.1 // indirect
1414
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1515
github.com/google/uuid v1.6.0 // indirect
16-
github.com/klauspost/compress v1.17.8 // indirect
16+
github.com/klauspost/compress v1.17.11 // indirect
1717
github.com/mattn/go-colorable v0.1.13 // indirect
1818
github.com/mattn/go-isatty v0.0.20 // indirect
19-
github.com/mattn/go-runewidth v0.0.15 // indirect
20-
github.com/philhofer/fwd v1.1.2 // indirect
19+
github.com/mattn/go-runewidth v0.0.16 // indirect
20+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
2121
github.com/prometheus/client_model v0.6.1 // indirect
22-
github.com/prometheus/common v0.53.0 // indirect
23-
github.com/prometheus/procfs v0.14.0 // indirect
22+
github.com/prometheus/common v0.60.1 // indirect
23+
github.com/prometheus/procfs v0.15.1 // indirect
2424
github.com/rivo/uniseg v0.4.7 // indirect
25-
github.com/tinylib/msgp v1.1.9 // indirect
2625
github.com/valyala/bytebufferpool v1.0.0 // indirect
2726
github.com/valyala/tcplisten v1.0.0 // indirect
28-
golang.org/x/sys v0.19.0 // indirect
29-
google.golang.org/protobuf v1.33.0 // indirect
27+
golang.org/x/sys v0.26.0 // indirect
28+
google.golang.org/protobuf v1.35.1 // indirect
3029
)

go.sum

+41-22
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,69 @@
11
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
22
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
3+
github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA=
4+
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
35
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
46
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
57
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
68
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
79
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
810
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9-
github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM=
10-
github.com/gofiber/fiber/v2 v2.52.4/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
11+
github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo=
12+
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
1113
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1214
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1315
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1416
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
15-
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
16-
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
17+
github.com/klauspost/compress v1.17.10 h1:oXAz+Vh0PMUvJczoi+flxpnBEPxoER1IaAnU/NMPtT0=
18+
github.com/klauspost/compress v1.17.10/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
19+
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
20+
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
21+
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
22+
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
1723
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
1824
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
1925
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
2026
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
2127
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
22-
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
23-
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
24-
github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw=
25-
github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0=
26-
github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU=
27-
github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k=
28+
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
29+
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
30+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
31+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
32+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
33+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
34+
github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI=
35+
github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
36+
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
37+
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
2838
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
2939
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
30-
github.com/prometheus/common v0.53.0 h1:U2pL9w9nmJwJDa4qqLQ3ZaePJ6ZTwt7cMD3AG3+aLCE=
31-
github.com/prometheus/common v0.53.0/go.mod h1:BrxBKv3FWBIGXw89Mg1AeBq7FSyRzXWI3l3e7W3RN5U=
32-
github.com/prometheus/procfs v0.14.0 h1:Lw4VdGGoKEZilJsayHf0B+9YgLGREba2C6xr+Fdfq6s=
33-
github.com/prometheus/procfs v0.14.0/go.mod h1:XL+Iwz8k8ZabyZfMFHPiilCniixqQarAy5Mu67pHlNQ=
40+
github.com/prometheus/common v0.60.0 h1:+V9PAREWNvJMAuJ1x1BaWl9dewMW4YrHZQbx0sJNllA=
41+
github.com/prometheus/common v0.60.0/go.mod h1:h0LYf1R1deLSKtD4Vdg8gy4RuOvENW2J/h19V5NADQw=
42+
github.com/prometheus/common v0.60.1 h1:FUas6GcOw66yB/73KC+BOZoFJmbo/1pojoILArPAaSc=
43+
github.com/prometheus/common v0.60.1/go.mod h1:h0LYf1R1deLSKtD4Vdg8gy4RuOvENW2J/h19V5NADQw=
44+
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
45+
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
3446
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
3547
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
3648
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
37-
github.com/tinylib/msgp v1.1.9 h1:SHf3yoO2sGA0veCJeCBYLHuttAVFHGm2RHgNodW7wQU=
38-
github.com/tinylib/msgp v1.1.9/go.mod h1:BCXGB54lDD8qUEPmiG0cQQUANC4IUQyB2ItS2UDlO/k=
49+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
50+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3951
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
4052
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
41-
github.com/valyala/fasthttp v1.52.0 h1:wqBQpxH71XW0e2g+Og4dzQM8pk34aFYlA1Ga8db7gU0=
42-
github.com/valyala/fasthttp v1.52.0/go.mod h1:hf5C4QnVMkNXMspnsUlfM3WitlgYflyhHYoKol/szxQ=
53+
github.com/valyala/fasthttp v1.56.0 h1:bEZdJev/6LCBlpdORfrLu/WOZXXxvrUQSiyniuaoW8U=
54+
github.com/valyala/fasthttp v1.56.0/go.mod h1:sReBt3XZVnudxuLOx4J/fMrJVorWRiWY2koQKgABiVI=
55+
github.com/valyala/fasthttp v1.57.0 h1:Xw8SjWGEP/+wAAgyy5XTvgrWlOD1+TxbbvNADYCm1Tg=
56+
github.com/valyala/fasthttp v1.57.0/go.mod h1:h6ZBaPRlzpZ6O3H5t2gEk1Qi33+TmLvfwgLLp0t9CpE=
4357
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
4458
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
59+
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
4560
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4661
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
47-
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
48-
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
49-
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
50-
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
62+
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
63+
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
64+
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
65+
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
66+
google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA=
67+
google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
68+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
69+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

middleware.go

+53-49
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ package fiberprometheus
2323

2424
import (
2525
"strconv"
26+
"strings"
2627
"time"
2728

2829
"github.com/gofiber/fiber/v2"
@@ -39,10 +40,8 @@ type FiberPrometheus struct {
3940
requestsTotal *prometheus.CounterVec
4041
requestDuration *prometheus.HistogramVec
4142
requestInFlight *prometheus.GaugeVec
42-
cacheHeaderKey string
43-
cacheCounter *prometheus.CounterVec
4443
defaultURL string
45-
skipPaths map[string]struct{}
44+
skipPaths map[string]bool
4645
}
4746

4847
func create(registry prometheus.Registerer, serviceName, namespace, subsystem string, labels map[string]string) *FiberPrometheus {
@@ -67,15 +66,6 @@ func create(registry prometheus.Registerer, serviceName, namespace, subsystem st
6766
[]string{"status_code", "method", "path"},
6867
)
6968

70-
cacheCounter := promauto.With(registry).NewCounterVec(
71-
prometheus.CounterOpts{
72-
Name: prometheus.BuildFQName(namespace, subsystem, "cache_results"),
73-
Help: "Counts all cache hits by status code, method, and path",
74-
ConstLabels: constLabels,
75-
},
76-
[]string{"status_code", "method", "path", "cache_result"},
77-
)
78-
7969
histogram := promauto.With(registry).NewHistogramVec(prometheus.HistogramOpts{
8070
Name: prometheus.BuildFQName(namespace, subsystem, "request_duration_seconds"),
8171
Help: "Duration of all HTTP requests by status code, method and path.",
@@ -115,6 +105,7 @@ func create(registry prometheus.Registerer, serviceName, namespace, subsystem st
115105
15.0,
116106
20.0,
117107
30.0,
108+
60.0, // 1m
118109
},
119110
},
120111
[]string{"status_code", "method", "path"},
@@ -138,18 +129,10 @@ func create(registry prometheus.Registerer, serviceName, namespace, subsystem st
138129
requestsTotal: counter,
139130
requestDuration: histogram,
140131
requestInFlight: gauge,
141-
cacheHeaderKey: "X-Cache",
142-
cacheCounter: cacheCounter,
143132
defaultURL: "/metrics",
144133
}
145134
}
146135

147-
// CustomCacheKey allows to set a custom header key for caching
148-
// By default it is set to "X-Cache", the fiber default
149-
func (ps *FiberPrometheus) CustomCacheKey(cacheHeaderKey string) {
150-
ps.cacheHeaderKey = cacheHeaderKey
151-
}
152-
153136
// New creates a new instance of FiberPrometheus middleware
154137
// serviceName is available as a const label
155138
func New(serviceName string) *FiberPrometheus {
@@ -191,6 +174,11 @@ func NewWithRegistry(registry prometheus.Registerer, serviceName, namespace, sub
191174
return create(registry, serviceName, namespace, subsystem, labels)
192175
}
193176

177+
// NewWithDefaultRegistry creates a new instance of FiberPrometheus middleware using the default prometheus registry
178+
func NewWithDefaultRegistry(serviceName string) *FiberPrometheus {
179+
return create(prometheus.DefaultRegisterer, serviceName, "http", "", nil)
180+
}
181+
194182
// RegisterAt will register the prometheus handler at a given URL
195183
func (ps *FiberPrometheus) RegisterAt(app fiber.Router, url string, handlers ...fiber.Handler) {
196184
ps.defaultURL = url
@@ -201,61 +189,77 @@ func (ps *FiberPrometheus) RegisterAt(app fiber.Router, url string, handlers ...
201189

202190
// SetSkipPaths allows to set the paths that should be skipped from the metrics
203191
func (ps *FiberPrometheus) SetSkipPaths(paths []string) {
204-
ps.skipPaths = make(map[string]struct{})
192+
if ps.skipPaths == nil {
193+
ps.skipPaths = make(map[string]bool)
194+
}
205195
for _, path := range paths {
206-
ps.skipPaths[path] = struct{}{}
196+
ps.skipPaths[path] = true
207197
}
208198
}
209199

210200
// Middleware is the actual default middleware implementation
211201
func (ps *FiberPrometheus) Middleware(ctx *fiber.Ctx) error {
212-
path := string(ctx.Request().RequestURI())
213-
214-
if path == ps.defaultURL {
215-
return ctx.Next()
216-
}
202+
// Retrieve the request method
203+
method := utils.CopyString(ctx.Method())
217204

218-
// Check if the path is in the map of skipped paths
219-
if _, exists := ps.skipPaths[path]; exists {
220-
return ctx.Next() // Skip metrics collection
221-
}
222-
223-
// Start metrics timer
224-
start := time.Now()
225-
method := ctx.Route().Method
205+
// Increment the in-flight gauge
226206
ps.requestInFlight.WithLabelValues(method).Inc()
227207
defer func() {
228208
ps.requestInFlight.WithLabelValues(method).Dec()
229209
}()
230210

211+
// Start metrics timer
212+
start := time.Now()
213+
214+
// Continue stack
231215
err := ctx.Next()
232-
// initialize with default error code
233-
// https://docs.gofiber.io/guide/error-handling
216+
217+
// Get the route path
218+
routePath := utils.CopyString(ctx.Route().Path)
219+
220+
// If the route path is empty, use the current path
221+
if routePath == "/" {
222+
routePath = utils.CopyString(ctx.Path())
223+
}
224+
225+
// Normalize the path
226+
if routePath != "" && routePath != "/" {
227+
routePath = normalizePath(routePath)
228+
}
229+
230+
// Check if the normalized path should be skipped
231+
if ps.skipPaths[routePath] {
232+
return nil
233+
}
234+
235+
// Determine status code from stack
234236
status := fiber.StatusInternalServerError
235237
if err != nil {
236238
if e, ok := err.(*fiber.Error); ok {
237-
// Get correct error code from fiber.Error type
238239
status = e.Code
239240
}
240241
} else {
241242
status = ctx.Response().StatusCode()
242243
}
243244

244-
// Get status as string
245+
// Convert status code to string
245246
statusCode := strconv.Itoa(status)
246247

247-
// Update total requests counter
248-
ps.requestsTotal.WithLabelValues(statusCode, method, path).Inc()
249-
250-
// Update the cache counter
251-
cacheResult := utils.CopyString(ctx.GetRespHeader(ps.cacheHeaderKey, ""))
252-
if cacheResult != "" {
253-
ps.cacheCounter.WithLabelValues(statusCode, method, path, cacheResult).Inc()
254-
}
248+
// Update metrics
249+
ps.requestsTotal.WithLabelValues(statusCode, method, routePath).Inc()
255250

256-
// Update the request duration histogram
251+
// Observe the Request Duration
257252
elapsed := float64(time.Since(start).Nanoseconds()) / 1e9
258-
ps.requestDuration.WithLabelValues(statusCode, method, path).Observe(elapsed)
253+
ps.requestDuration.WithLabelValues(statusCode, method, routePath).Observe(elapsed)
259254

260255
return err
261256
}
257+
258+
// normalizePath will remove the trailing slash from the route path
259+
func normalizePath(routePath string) string {
260+
normalized := strings.TrimRight(routePath, "/")
261+
if normalized == "" {
262+
return "/"
263+
}
264+
return normalized
265+
}

0 commit comments

Comments
 (0)