Categorygithub.com/banzaicloud/go-gin-prometheus
modulepackage
0.1.0
Repository: https://github.com/banzaicloud/go-gin-prometheus.git
Documentation: pkg.go.dev

# README

go-gin-prometheus

License: MIT

Gin Web Framework Prometheus metrics exporter

Installation

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

Usage

package main

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

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

	p := ginprometheus.NewPrometheus("gin", []string{})
	p.SetListenAddress(":9900")
	p.Use(r, "/metrics")

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

	r.Run(":29090")
}

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 want to generate a time series for every possible customer name:

package main

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

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

	p := ginprometheus.NewPrometheus("gin", []string{"name"})
	p.SetListenAddress(":9900")
	p.Use(r, "/metrics")

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

	r.Run(":29090")
}

# Functions

NewMetric associates prometheus.Collector based on Metric.Type.
NewPrometheus generates a new set of metrics with a certain subsystem name.

# Structs

Metric is a definition for the name, description, type, ID, and prometheus.Collector type (i.e.
Prometheus contains the metrics gathered by the instance and its path.
PrometheusPushGateway contains the configuration for pushing to a Prometheus pushgateway (optional).

# Type aliases

RequestCounterURLLabelMappingFn is a function which can be supplied to the middleware to control the cardinality of the request counter's "url" label, which might be required in some contexts.