go-gin-prometheus

Gin Web Framework Prometheus metrics exporter

Github星跟踪图

go-gin-prometheus

License: MIT

Gin Web Framework Prometheus metrics exporter

Installation

$ go get github.com/zsais/go-gin-prometheus

Usage

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/zsais/go-gin-prometheus"
)

func main() {
	r := gin.New()

	p := ginprometheus.NewPrometheus("gin")
	p.Use(r)

	r.GET("/", func(c *gin.Context) {
		c.JSON(200, "Hello world!")
	})

	r.Run(":29090")
}

See the example.go file

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:

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/zsais/go-gin-prometheus"
)

func main() {
	r := gin.New()

	p := ginprometheus.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(":29090")
}

which would map /customer/alice and /customer/bob to their
template /customer/:name, and thus preserve a low cardinality for
our metrics.

主要指标

概览
名称与所有者zsais/go-gin-prometheus
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2015-09-01 23:08:26
推送于2025-07-22 04:35:09
最后一次提交2020-02-17 10:04:48
发布数3
最新版本名称v1.0.1 (发布于 )
第一版名称v0.1.0 (发布于 )
用户参与
星数456
关注者数2
派生数137
提交数57
已启用问题?
问题数23
打开的问题数11
拉请求数23
打开的拉请求数10
关闭的拉请求数7
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?