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 (發布於 )
用户参与
星數458
關注者數2
派生數137
提交數57
已啟用問題?
問題數23
打開的問題數11
拉請求數23
打開的拉請求數10
關閉的拉請求數7
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?