metricsmw

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

README

Metrics

The package core/metrics 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(metrics.MeasureRequestsMiddleware)

Using standard net/http library.

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

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 (
	// RequestCounter counts the number of requests for a combination of method, code and path.
	RequestCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "http_request_total",
			Help: "A running count for HTTP requests",
		},
		[]string{"method", "code", "path"},
	)

	// RequestDurationHistogram measures the duration in nanoseconds for requests.
	RequestDurationHistogram = prometheus.NewHistogramVec(
		prometheus.HistogramOpts{
			Name: "http_request_duration_nanoseconds",
			Help: "Duration in nanoseconds of each request",
		},
		[]string{"method", "code", "path"},
	)

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

	// All represents a combination of all HTTP metric collectors.
	All = []prometheus.Collector{
		RequestCounter,
		RequestDurationHistogram,
		ResponseSizeHistogram,
	}
)
View Source
var DefaultServerMetrics = grpc_prometheus.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 = grpc_prometheus.StreamClientInterceptor

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

View Source
var StreamServerInterceptor = grpc_prometheus.StreamServerInterceptor

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

View Source
var UnaryClientInterceptor = grpc_prometheus.UnaryClientInterceptor

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

View Source
var UnaryServerInterceptor = grpc_prometheus.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.

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