ginprometheus

package module
v0.0.0-...-1f1c6d7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2020 License: MIT Imports: 13 Imported by: 2

README

go-gin-prometheus

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.String()
		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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Prometheus

type Prometheus struct {
	MetricsPath string
	// contains filtered or unexported fields
}

Prometheus contains the metrics gathered by the instance and its path

func NewBuilder

func NewBuilder() *Prometheus

func (*Prometheus) Auth

func (*Prometheus) Counter

func (*Prometheus) Duration

func (*Prometheus) Job

func (p *Prometheus) Job(j string)

Job job name, defaults to "gin"

func (*Prometheus) ListenAddress

func (p *Prometheus) ListenAddress(address string)

ListenAddress for exposing metrics on address. If not set, it will be exposed at the same address of the gin engine that is being used

func (*Prometheus) PushGateway

func (p *Prometheus) PushGateway(pushGatewayURL, metricsURL string, pushIntervalSeconds time.Duration) PrometheusPushGatewayMiddlewareBuilder

PushGateway sends metrics to a remote pushgateway exposed on pushGatewayURL every pushIntervalSeconds. Metrics are fetched from metricsURL

func (*Prometheus) RequestSize

func (*Prometheus) ResponseSize

func (*Prometheus) UrlMapping

func (*Prometheus) Use

func (p *Prometheus) Use(e *gin.Engine)

Use adds the middleware to a gin engine.

type PrometheusMiddlerwareBuilder

type PrometheusMiddlerwareBuilder interface {
	Auth(accounts gin.Accounts) PrometheusMiddlerwareBuilder
	UrlMapping(function URLLabelMappingFn) PrometheusMiddlerwareBuilder
	Counter(counter *prometheus.CounterVec) PrometheusMiddlerwareBuilder
	Duration(observer prometheus.ObserverVec) PrometheusMiddlerwareBuilder
	RequestSize(observer prometheus.ObserverVec) PrometheusMiddlerwareBuilder
	ResponseSize(observer prometheus.ObserverVec) PrometheusMiddlerwareBuilder
	PushGateway(pushGatewayURL, metricsURL string, pushIntervalSeconds time.Duration) PrometheusPushGatewayMiddlewareBuilder
	Use(e *gin.Engine)
}

type PrometheusPushGateway

type PrometheusPushGateway struct {
	// contains filtered or unexported fields
}

PrometheusPushGateway contains the configuration for pushing to a Prometheus pushgateway (optional)

type PrometheusPushGatewayMiddlewareBuilder

type PrometheusPushGatewayMiddlewareBuilder interface {
	Job(job string)
	ListenAddress(address string)
	Use(e *gin.Engine)
}

type URLLabelMappingFn

type URLLabelMappingFn func(c *gin.Context) string

URLLabelMappingFn 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. For instance, if for a "/customer/:name" route you don't want to generate a time series for every possible customer name, you could use this function:

func(c *gin.Context) string {
	url := c.Request.URL.String()
	for _, p := range c.Params {
		if p.Key == "name" {
			url = strings.Replace(url, p.Value, ":name", 1)
			break
		}
	}
	return url
}

which would map "/customer/alice" and "/customer/bob" to their template "/customer/:name".

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL