middleware

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 5 Imported by: 3

Documentation

Overview

Package middleware contains HTTP middleware. Some of these are used by httpserver, but can be used by any http router that supports net/http-compatible middleware.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Logger added in v0.4.1

func Logger(requestWriter RequestLogger) func(http.Handler) http.Handler

Logger logs incoming HTTP requests. If requestWriter is nil, the logger defaults to DefaultRequestLogger.

Types

type PrometheusMetrics

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

PrometheusMetrics implements a net/http middleware that records HTTP request statistics as Prometheus metrics

Example
package main

import (
	"github.com/clambin/go-common/httpserver/middleware"
	"github.com/prometheus/client_golang/prometheus"
	"net/http"
)

func main() {
	handler := func(w http.ResponseWriter, _ *http.Request) {
		_, _ = w.Write([]byte("Hello"))
	}

	// This will create the following metrics:
	//   foo_bar_http_requests_total
	//   foo_bar_http_requests_duration_seconds
	//
	// Both will have a label "handler" set to "example".
	// The duration metrics will be a Summary (i.e. the default)
	m := middleware.NewPrometheusMetrics(middleware.PrometheusMetricsOptions{
		Namespace:   "foo",
		Subsystem:   "bar",
		Application: "example",
	})
	prometheus.MustRegister(m)

	s := &http.Server{
		Addr:    ":8080",
		Handler: m.Handle(http.HandlerFunc(handler)),
	}

	_ = s.ListenAndServe()
}

func NewPrometheusMetrics

func NewPrometheusMetrics(o PrometheusMetricsOptions) *PrometheusMetrics

NewPrometheusMetrics creates a new PrometheusMetrics middleware for the provided PrometheusMetricsOptions options.

Note: NewPrometheusMetrics will create the required Prometheus metrics, but will not register these with any prometheus registry. This is left up to the caller:

m := NewPrometheusMetrics(options)
prometheus.MustRegister(m)

func (*PrometheusMetrics) Collect

func (m *PrometheusMetrics) Collect(c chan<- prometheus.Metric)

Collect implements the prometheus.Collector interface

func (*PrometheusMetrics) Describe

func (m *PrometheusMetrics) Describe(descs chan<- *prometheus.Desc)

Describe implements the prometheus.Collector interface

func (*PrometheusMetrics) Handle

func (m *PrometheusMetrics) Handle(next http.Handler) http.Handler

Handle returns an http.Handler that instruments the call to the next http.Handler

type PrometheusMetricsDurationType

type PrometheusMetricsDurationType int

PrometheusMetricsDurationType specifies the type of metrics to record for request duration. Use Summary if you are only interested in the average latency. Use Histogram if you want to use a histogram to measure a service level indicator (eg latency of 95% of all requests).

const (
	// Summary measures the average duration.
	Summary PrometheusMetricsDurationType = iota
	// Histogram measures the latency in buckets and can be used to calculate a service level indicator. PrometheusMetricsOptions.Buckets
	// specify the buckets to be used. If none are provided, prometheus.DefBuckets will be used.
	Histogram
)

type PrometheusMetricsOptions

type PrometheusMetricsOptions struct {
	// Namespace is used as namespace to create the metrics' fully qualified name
	Namespace string
	// Subsystem is used as namespace to create the metrics' fully qualified name
	Subsystem string
	// Application will be added as a 'handler' label to all metrics
	Application string
	// MetricsType specifies if the duration metrics should be recorded as a Summary or a Histogram
	MetricsType PrometheusMetricsDurationType
	// Buckets specifies the buckets to use for a Histogram duration metric.
	// If left blank, prometheus.DefBuckets will be used
	Buckets []float64
}

PrometheusMetricsOptions configure the metrics to be captured by a PrometheusMetrics middleware.

type RequestLogger added in v0.8.0

type RequestLogger interface {
	Log(r *http.Request, code int, latency time.Duration)
}

A RequestLogger takes an incoming request, the resulting HTTP status code and the latency and logs it to a logger.

var DefaultRequestLogger RequestLogger = defaultRequestLogger{}

DefaultRequestLogger logs the incoming request, the resulting HTTP status code and the latency to slog at Info log level.

Jump to

Keyboard shortcuts

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