go-gin-prometheus

Gin Web Framework Prometheus metrics exporter

Github stars Tracking Chart

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.

Main metrics

Overview
Name With Ownerzsais/go-gin-prometheus
Primary LanguageGo
Program languageGo (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2015-09-01 23:08:26
Pushed At2025-07-22 04:35:09
Last Commit At2020-02-17 10:04:48
Release Count3
Last Release Namev1.0.1 (Posted on )
First Release Namev0.1.0 (Posted on )
用户参与
Stargazers Count456
Watchers Count2
Fork Count137
Commits Count57
Has Issues Enabled
Issues Count23
Issue Open Count11
Pull Requests Count23
Pull Requests Open Count10
Pull Requests Close Count7
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private