Skip to content

Commit ea3ba12

Browse files
committed
use gin.Context.FullPath method instead
1 parent 2199a42 commit ea3ba12

File tree

2 files changed

+4
-17
lines changed

2 files changed

+4
-17
lines changed

README.md

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,7 @@ func main() {
6161
p := ginprometheus.NewPrometheus("gin")
6262

6363
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
64+
return c.FullPath()
7265
}
7366

7467
p.Use(r)

middleware.go

Lines changed: 3 additions & 9 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{
@@ -59,16 +60,9 @@ the cardinality of the request counter's "url" label, which might be required in
5960
For instance, if for a "/customer/:name" route you don't want to generate a time series for every
6061
possible customer name, you could use this function:
6162
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-
}
63+
func(c *gin.Context) string {
64+
return c.FullPath()
6965
}
70-
return url
71-
}
7266
7367
which would map "/customer/alice" and "/customer/bob" to their template "/customer/:name".
7468
*/

0 commit comments

Comments
 (0)