https://github.com/paradeum-team/gin-prometheus-ext
https://github.com/paradeum-team/gin-prometheus-ext
Last synced: 1 day ago
JSON representation
- Host: GitHub
- URL: https://github.com/paradeum-team/gin-prometheus-ext
- Owner: paradeum-team
- Created: 2020-12-16T08:37:41.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2022-06-14T10:53:10.000Z (about 4 years ago)
- Last Synced: 2026-02-15T06:24:12.499Z (4 months ago)
- Language: Go
- Size: 10.7 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# gin-prometheus-ext
在[go-gin-prometheus](https://github.com/zsais/go-gin-prometheus) 的基础上,增加了几个metrics ,兼容api的分位数和耗时统计
# go-gin-prometheus
[](https://godoc.org/github.com/zsais/go-gin-prometheus) [](https://opensource.org/licenses/MIT)
Gin Web Framework Prometheus metrics exporter
## Installation
`$ go get github.com/paradeum-team/gin-prometheus-ext`
## Usage
```go
package main
import (
"github.com/gin-gonic/gin"
"github.com/paradeum-team/gin-prometheus-ext"
"strings"
)
func main() {
r := gin.New()
p := ginprometheusext.NewPrometheus("gin")
p.Use(r)
r.GET("/", func(c *gin.Context) {
c.JSON(200, "Hello world!")
})
r.Run(":8090")
}
```
## Preserving a low cardinality for the request counter
The request counter (`requests_total`) has a `url` label which,
although desirable, can become problematic in cases where your
application uses templated routes expecting a great number of
variations, as Prometheus explicitly recommends against metrics having
high cardinality dimensions:
https://prometheus.io/docs/practices/naming/#labels
If you have for instance a `/customer/:name` templated route and you
don't want to generate a time series for every possible customer name,
you could supply this mapping function to the middleware:
```go
package main
import (
"github.com/gin-gonic/gin"
"github.com/paradeum-team/gin-prometheus-ext"
"strings"
)
func main() {
r := gin.New()
p := ginprometheusext.NewPrometheus("gin")
p.ReqCntURLLabelMappingFn = func(c *gin.Context) string {
url := c.Request.URL.Path
for _, p := range c.Params {
if p.Key == "name" {
url = strings.Replace(url, p.Value, ":name", 1)
break
}
}
return url
}
p.Use(r)
r.GET("/", func(c *gin.Context) {
c.JSON(200, "Hello world!")
})
r.Run(":8090")
}
```
which would map `/customer/alice` and `/customer/bob` to their
template `/customer/:name`, and thus preserve a low cardinality for
our metrics.