middleware

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2020 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Overview

Example
package main

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

func main() {
	middleware := middleware.New()

	gin.Default().Use(middleware)
}
Output:

Index

Examples

Constants

View Source
const (
	// StatusCodeLabelName The default label name for the request status code.
	StatusCodeLabelName = "status_code"
	// MethodLabelName The default label name for the request method.
	MethodLabelName = "method"
	// PathLabelName The default label name for the request path.
	PathLabelName = "path"
)

Variables

This section is empty.

Functions

func New

func New() gin.HandlerFunc

New creates new gin middleware with the default options.

Example
package main

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

func main() {
	middleware := middleware.New()

	gin.Default().Use(middleware)
}
Output:

func NewWithOptions

func NewWithOptions(options *Options) gin.HandlerFunc

NewWithOptions creates new gin middleware with the provided options.

Example (Blacklist)
package main

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

func main() {
	// Do not collect metrics for requests to the /blacklisted path.
	blacklister := func(c *gin.Context) bool {
		if c.Request.URL.Path == "/blacklisted" {
			return true
		}

		return false
	}

	options := &middleware.Options{Blacklister: blacklister}
	middleware := middleware.NewWithOptions(options)

	gin.Default().Use(middleware)
}
Output:

Example (Options)
package main

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

func main() {
	options := &middleware.Options{
		// Overwrite the http metric name.
		HTTPMetricName: "my_request_duration_seconds",
		// Overwrite the http metric histogram buckets.
		HTTPMetricBuckets: []float64{0.1, 0.5, 1, 2, 5},
		// Don't use normalized paths for the path label.
		// IMPORTANT: If you have ids in the paths this can lead to huge amount of metrics.
		HTTPMetricUseNotNormalizedPaths: true,
		// Overwrite the metrics path. Metrics for the metrics path won't be collected.
		MetricsPath: "/my-metrics",
		// Add static labels to the http metrics histogram.
		AdditionalHTTPMetricDefaultLabelsNames: map[string]string{"custom": "my-value"},
		// Change the name of the default labels.
		HTTPMetricDefaultLabelsNames: map[string]string{middleware.StatusCodeLabelName: "status"},
	}

	middleware := middleware.NewWithOptions(options)

	gin.Default().Use(middleware)
}
Output:

Example (Registry)
package main

import (
	"github.com/TsvetanMilanov/go-gin-prometheus-middleware/middleware"
	"github.com/gin-gonic/gin"
	"github.com/prometheus/client_golang/prometheus"
)

func main() {
	myCustomRegistry := prometheus.NewRegistry()
	options := &middleware.Options{
		// Use custom registry for the metrics.
		Registry: myCustomRegistry,
	}

	middleware := middleware.NewWithOptions(options)

	gin.Default().Use(middleware)
}
Output:

Types

type Blacklister added in v0.2.0

type Blacklister = func(c *gin.Context) bool

Blacklister is a function which will be used to decide which requests should not be included in the metrics.

type Options

type Options struct {
	HTTPMetricName                  string
	HTTPMetricBuckets               []float64
	HTTPMetricUseNotNormalizedPaths bool
	MetricsPath                     string

	AdditionalHTTPMetricDefaultLabelsNames map[string]string
	HTTPMetricDefaultLabelsNames           map[string]string

	Registry    prometheus.Registerer
	Blacklister Blacklister
}

Options the options which will be used to create the middleware.

Jump to

Keyboard shortcuts

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