middleware

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2019 License: Apache-2.0 Imports: 5 Imported by: 245

Documentation

Overview

Package middleware will measure metrics of a Go net/http handler using a `metrics.Recorder`. The metrics measured are based on RED and/or Four golden signals and try to be measured in a efficient way.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Recorder is the way the metrics will be recorder in the different backends.
	Recorder metrics.Recorder
	// Service is an optional identifier for the metrics, this can be useful if
	// a same service has multiple servers (e.g API, metrics and healthchecks).
	Service string
	// GroupedStatus will group the status label in the form of `\dxx`, for example,
	// 200, 201, and 203 will have the label `code="2xx"`. This impacts on the cardinality
	// of the metrics and also improves the performance of queries that are grouped by
	// status code because there are already aggregated in the metric.
	// By default will be false.
	GroupedStatus bool
	// DisableMeasureSize will disable the recording metrics about the response size,
	// by default measuring size is enabled (`DisableMeasureSize` is false).
	DisableMeasureSize bool
	// DisableMeasureInflight will disable the recording metrics about the inflight requests number,
	// by default measuring inflights is enabled (`DisableMeasureInflight` is false).
	DisableMeasureInflight bool
}

Config is the configuration for the middleware factory.

type Middleware

type Middleware interface {
	// Handler wraps the received handler with the Prometheus middleware.
	// The first argument receives the handlerID, all the metrics will have
	// that handler ID as the handler label on the metrics, if an empty
	// string is passed then it will get the handlerID from the request
	// path.
	Handler(handlerID string, h http.Handler) http.Handler
}

Middleware is a factory that creates middlewares or wrappers that measure requests to the wrapped handler using different metrics backends using a `metrics.Recorder` implementation.

Example (PrometheusBackendMiddleware)

PrometheusBackendMiddleware shows how you would create a middleware factory for standard go library `http.Handler` and wrap a handler to measure with the default settings using Prometheus as the metrics recorder backend, the Prometheus will use the default settings so it will measure using the default Prometheus registry.

package main

import (
	"log"
	"net/http"

	"github.com/prometheus/client_golang/prometheus/promhttp"
	metrics "github.com/slok/go-http-metrics/metrics/prometheus"
	"github.com/slok/go-http-metrics/middleware"
)

func main() {
	// Create our middleware factory with the default settings.
	mdlw := middleware.New(middleware.Config{
		Recorder: metrics.NewRecorder(metrics.Config{}),
	})

	// Create our handler.
	myHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("hello world!"))
	})

	// Wrap our handler with the middleware.
	h := mdlw.Handler("", myHandler)

	// Serve metrics from the default prometheus registry.
	log.Printf("serving metrics at: %s", ":8081")
	go http.ListenAndServe(":8081", promhttp.Handler())

	// Serve our handler.
	log.Printf("listening at: %s", ":8080")
	if err := http.ListenAndServe(":8080", h); err != nil {
		log.Panicf("error while serving: %s", err)
	}
}
Output:

func New

func New(cfg Config) Middleware

New returns the a Middleware factory.

Directories

Path Synopsis
Package gin is a helper package to get a gin compatible handler/middleware from the standard net/http Middleware factory.
Package gin is a helper package to get a gin compatible handler/middleware from the standard net/http Middleware factory.
Package gorestful is a helper package to get a gorestful compatible handler/middleware from the standard net/http Middleware factory.
Package gorestful is a helper package to get a gorestful compatible handler/middleware from the standard net/http Middleware factory.
Package httprouter is a helper package to get a httprouter compatible handler/middleware from the standatd net/http Middleware factory.
Package httprouter is a helper package to get a httprouter compatible handler/middleware from the standatd net/http Middleware factory.
Package negroni is a helper package to get a negroni compatible handler/middleware from the standard net/http Middleware factory.
Package negroni is a helper package to get a negroni compatible handler/middleware from the standard net/http Middleware factory.

Jump to

Keyboard shortcuts

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