Skip to content

Commit 56b5623

Browse files
committed
use gin.Context.FullPath by default
1 parent 2199a42 commit 56b5623

File tree

2 files changed

+4
-68
lines changed

2 files changed

+4
-68
lines changed

README.md

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -32,55 +32,3 @@ func main() {
3232
```
3333

3434
See the [example.go file](https://github.com/zsais/go-gin-prometheus/blob/master/example/example.go)
35-
36-
## Preserving a low cardinality for the request counter
37-
38-
The request counter (`requests_total`) has a `url` label which,
39-
although desirable, can become problematic in cases where your
40-
application uses templated routes expecting a great number of
41-
variations, as Prometheus explicitly recommends against metrics having
42-
high cardinality dimensions:
43-
44-
https://prometheus.io/docs/practices/naming/#labels
45-
46-
If you have for instance a `/customer/:name` templated route and you
47-
don't want to generate a time series for every possible customer name,
48-
you could supply this mapping function to the middleware:
49-
50-
```go
51-
package main
52-
53-
import (
54-
"github.com/gin-gonic/gin"
55-
"github.com/zsais/go-gin-prometheus"
56-
)
57-
58-
func main() {
59-
r := gin.New()
60-
61-
p := ginprometheus.NewPrometheus("gin")
62-
63-
p.ReqCntURLLabelMappingFn = func(c *gin.Context) string {
64-
url := c.Request.URL.Path
65-
for _, p := range c.Params {
66-
if p.Key == "name" {
67-
url = strings.Replace(url, p.Value, ":name", 1)
68-
break
69-
}
70-
}
71-
return url
72-
}
73-
74-
p.Use(r)
75-
76-
r.GET("/", func(c *gin.Context) {
77-
c.JSON(200, "Hello world!")
78-
})
79-
80-
r.Run(":29090")
81-
}
82-
```
83-
84-
which would map `/customer/alice` and `/customer/bob` to their
85-
template `/customer/:name`, and thus preserve a low cardinality for
86-
our metrics.

middleware.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
var defaultMetricPath = "/metrics"
1818

1919
// Standard default metrics
20+
//
2021
// counter, counter_vec, gauge, gauge_vec,
2122
// histogram, histogram_vec, summary, summary_vec
2223
var reqCnt = &Metric{
@@ -56,21 +57,6 @@ var standardMetrics = []*Metric{
5657
/*
5758
RequestCounterURLLabelMappingFn is a function which can be supplied to the middleware to control
5859
the cardinality of the request counter's "url" label, which might be required in some contexts.
59-
For instance, if for a "/customer/:name" route you don't want to generate a time series for every
60-
possible customer name, you could use this function:
61-
62-
func(c *gin.Context) string {
63-
url := c.Request.URL.Path
64-
for _, p := range c.Params {
65-
if p.Key == "name" {
66-
url = strings.Replace(url, p.Value, ":name", 1)
67-
break
68-
}
69-
}
70-
return url
71-
}
72-
73-
which would map "/customer/alice" and "/customer/bob" to their template "/customer/:name".
7460
*/
7561
type RequestCounterURLLabelMappingFn func(c *gin.Context) string
7662

@@ -140,7 +126,9 @@ func NewPrometheus(subsystem string, customMetricsList ...[]*Metric) *Prometheus
140126
MetricsList: metricsList,
141127
MetricsPath: defaultMetricPath,
142128
ReqCntURLLabelMappingFn: func(c *gin.Context) string {
143-
return c.Request.URL.Path // i.e. by default do nothing, i.e. return URL as is
129+
// return route full path
130+
// map "/customer/alice" and "/customer/bob" to their template "/customer/:name"
131+
return c.FullPath()
144132
},
145133
}
146134

0 commit comments

Comments
 (0)