Debian Codegraph @

README.md

raw
1 # go-gin-prometheus
2 [![](https://godoc.org/github.com/zsais/go-gin-prometheus?status.svg)](https://godoc.org/github.com/zsais/go-gin-prometheus) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
3
4 Gin Web Framework Prometheus metrics exporter
5
6 ## Installation
7
8 `$ go get github.com/zsais/go-gin-prometheus`
9
10 ## Usage
11
12 ```go
13 package main
14
15 import (
16 "github.com/gin-gonic/gin"
17 "github.com/zsais/go-gin-prometheus"
18 )
19
20 func main() {
21 r := gin.New()
22
23 p := ginprometheus.NewPrometheus("gin")
24 p.Use(r)
25
26 r.GET("/", func(c *gin.Context) {
27 c.JSON(200, "Hello world!")
28 })
29
30 r.Run(":29090")
31 }
32 ```
33
34 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.