metricsmw

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2020 License: Apache-2.0 Imports: 7 Imported by: 0

README

Metrics Middleware

The package core/workers/metricsmw is used to record and expose metrics for an application. The metrics server is be provided over HTTP using the prometheus extraction protocol.

You can read more about using prometheus in go on the their offical website.

gRPC server metrics
server := grpc.NewServer(
    grpc.StreamInterceptor(metrics.StreamServerInterceptor),
    grpc.UnaryInterceptor(metrics.UnaryServerInterceptor),
)
gRPC client metrics
conn, err = grpc.Dial(
    address,
    grpc.WithUnaryInterceptor(metrics.UnaryClientInterceptor),
    grpc.WithStreamInterceptor(metrics.StreamClientInterceptor)
)
HTTP server metrics

Using gorilla mux.

r := mux.NewRouter()
r.Use(mux.MiddlewareFunc(metrics.MeasureRequestsMiddleware))

Using standard net/http library.

http.Handle("/check", metrics.MeasureRequests(func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(200)
}))
Time series buckets

The collection of histogram metrics must put data into predefined buckets. These have been set at: 0.2, 0.4, 0.6, 0.8, 1.0, 1.5, 2.0, 5.0, 10.0, 15.0 seconds. more information can be found here: Prometheus Documentation

Documentation

Overview

Package metricsmw is used to record and expose metrics for an application.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// RequestDurationHistogram measures the duration in seconds for requests.
	RequestDurationHistogram = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Name: "http_request_duration_seconds",
			Help: "Duration in seconds of each request",
		},
		[]string{"method", "code"},
	)

	// ResponseSizeHistogram measures the size in bytes for responses.
	ResponseSizeHistogram = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Name: "http_response_byte_size",
			Help: "Size in bytes of each response",
		},
		[]string{"method", "code"},
	)

	// All represents a combination of all HTTP metric collectors.
	// TODO: Remove once we move to v1.0 since we no longer need to register the collectors manually.
	All = []prometheus.Collector{
		RequestDurationHistogram,
		ResponseSizeHistogram,
	}
)
View Source
var DefaultServerMetrics = grpcprometheus.DefaultServerMetrics

DefaultServerMetrics is the default instance of ServerMetrics. It is intended to be used in conjunction the default Prometheus metrics registry.

View Source
var MeasureRequestsMiddleware = MiddlewareFunc(MeasureRequestsHandler)

MeasureRequestsMiddleware wraps the measure requests handler in a gorilla mux middleware.

View Source
var StreamClientInterceptor = grpcprometheus.StreamClientInterceptor

StreamClientInterceptor is a gRPC client-side interceptor that provides Prometheus monitoring for Streaming RPCs.

View Source
var StreamServerInterceptor = grpcprometheus.StreamServerInterceptor

StreamServerInterceptor is a gRPC server-side interceptor that provides Prometheus monitoring for Streaming RPCs.

View Source
var UnaryClientInterceptor = grpcprometheus.UnaryClientInterceptor

UnaryClientInterceptor is a gRPC client-side interceptor that provides Prometheus monitoring for Unary RPCs.

View Source
var UnaryServerInterceptor = grpcprometheus.UnaryServerInterceptor

UnaryServerInterceptor is a gRPC server-side interceptor that provides Prometheus monitoring for Unary RPCs.

Functions

func MeasureRequests

func MeasureRequests(next http.HandlerFunc) http.HandlerFunc

MeasureRequests returns a middleware for collecting metrics on http requests.

Example
package main

import (
	"net/http"

	"github.com/LUSHDigital/core/middleware/metricsmw"
)

func main() {
	http.Handle("/check", metricsmw.MeasureRequests(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(200)
	}))
}
Output:

func MeasureRequestsHandler

func MeasureRequestsHandler(next http.Handler) http.Handler

MeasureRequestsHandler wraps the measure requests handler in a http handler.

func Register

func Register()

Register registers all the metric collectors with prometheus. DEPRECATED: metricsmw.Register() does not need to be called since registering of metrics now happens automatically. TODO: Remove once we move to v1.0 since we no longer need to register the collectors manually.

Types

type MiddlewareFunc

type MiddlewareFunc func(http.Handler) http.Handler

MiddlewareFunc represents a middleware func for use with gorilla mux.

func (MiddlewareFunc) Middleware

func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler

Middleware allows MiddlewareFunc to implement the middleware interface.

Jump to

Keyboard shortcuts

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