{"modulePath":"github.com/clambin/go-common/httpserver","version":"v0.13.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package middleware\n\nPackage 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.\n\n## Functions\n\n```go\nfunc RequestLogger(logger *slog.Logger, logLevel slog.Level, formatter RequestLogFormatter) func(http.Handler) http.Handler\n```\nRequestLogger logs incoming HTTP requests.\n\ndeprecated: moved to github.com/clambin/go-common/http/middleware\n\n## Types\n\n```go\ntype PrometheusMetrics struct {\n\t// contains filtered or unexported fields\n}\n```\nPrometheusMetrics implements a net/http middleware that records HTTP request statistics as Prometheus metrics\n\ndeprecated: moved to github.com/clambin/go-common/http/middleware\n\n```go\nfunc NewPrometheusMetrics(o PrometheusMetricsOptions) *PrometheusMetrics\n```\nNewPrometheusMetrics creates a new PrometheusMetrics middleware for the provided PrometheusMetricsOptions options.\n\nNote: NewPrometheusMetrics will create the required Prometheus metrics, but will not register these with any prometheus registry. This is left up to the caller:\n\n\tm := NewPrometheusMetrics(options)\n\tprometheus.MustRegister(m)\n\ndeprecated: moved to github.com/clambin/go-common/http/middleware\n\n```go\nfunc (m *PrometheusMetrics) Collect(c chan\u003c- prometheus.Metric)\n```\nCollect implements the prometheus.Collector interface\n\n```go\nfunc (m *PrometheusMetrics) Describe(descs chan\u003c- *prometheus.Desc)\n```\nDescribe implements the prometheus.Collector interface\n\n```go\nfunc (m *PrometheusMetrics) Handle(next http.Handler) http.Handler\n```\nHandle returns an http.Handler that instruments the call to the next http.Handler\n\n```go\ntype PrometheusMetricsDurationType int\n```\nPrometheusMetricsDurationType 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).\n\n```go\nconst (\n\t// Summary measures the average duration.\n\tSummary PrometheusMetricsDurationType = iota\n\t// Histogram measures the latency in buckets and can be used to calculate a service level indicator. PrometheusMetricsOptions.Buckets\n\t// specify the buckets to be used. If none are provided, prometheus.DefBuckets will be used.\n\tHistogram\n)\n```\n\n```go\ntype PrometheusMetricsOptions struct {\n\t// Namespace is used as namespace to create the metrics' fully qualified name\n\tNamespace string\n\t// Subsystem is used as namespace to create the metrics' fully qualified name\n\tSubsystem string\n\t// Application will be added as a 'handler' label to all metrics\n\tApplication string\n\t// MetricsType specifies if the duration metrics should be recorded as a Summary or a Histogram\n\tMetricsType PrometheusMetricsDurationType\n\t// Buckets specifies the buckets to use for a Histogram duration metric.\n\t// If left blank, prometheus.DefBuckets will be used\n\tBuckets []float64\n}\n```\nPrometheusMetricsOptions configure the metrics to be captured by a PrometheusMetrics middleware.\n\n```go\ntype RequestLogFormatter interface {\n\tFormatRequest(*http.Request, int, time.Duration) []slog.Attr\n}\n```\nA RequestLogFormatter takes the HTTP request, the resulting HTTP status code and latency and formats the log entry.\n\n```go\nvar DefaultRequestLogFormatter RequestLogFormatter = \u0026defaultRequestLogFormatter{}\n```\nDefaultRequestLogFormatter is the default RequestLogFormatter. It logs the request's HTTP method and the path.\n\n```go\ntype RequestLogFormatterFunc func(r *http.Request, statusCode int, latency time.Duration) []slog.Attr\n```\nThe RequestLogFormatterFunc type is an adapter that allows an ordinary function to be used as a RequestLogFormatter.\n\n```go\nfunc (f RequestLogFormatterFunc) FormatRequest(r *http.Request, statusCode int, latency time.Duration) []slog.Attr\n```\nFormatRequest calls f(r, statusCode, latency)\n\n","path":"github.com/clambin/go-common/httpserver/middleware","name":"middleware","synopsis":"Package middleware contains HTTP middleware.","isRedistributable":true}
