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 ¶
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
deprecated: moved to github.com/clambin/go-common/http/middleware
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() }
Output:
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)
deprecated: moved to github.com/clambin/go-common/http/middleware
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
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 RequestLogFormatter ¶ added in v0.11.0
A RequestLogFormatter takes the HTTP request, the resulting HTTP status code and latency and formats the log entry.
var DefaultRequestLogFormatter RequestLogFormatter = &defaultRequestLogFormatter{}
DefaultRequestLogFormatter is the default RequestLogFormatter. It logs the request's HTTP method and the path.
type RequestLogFormatterFunc ¶ added in v0.11.0
type RequestLogFormatterFunc func(r *http.Request, statusCode int, latency time.Duration) []slog.Attr
The RequestLogFormatterFunc type is an adapter that allows an ordinary function to be used as a RequestLogFormatter.
func (RequestLogFormatterFunc) FormatRequest ¶ added in v0.11.0
func (f RequestLogFormatterFunc) FormatRequest(r *http.Request, statusCode int, latency time.Duration) []slog.Attr
FormatRequest calls f(r, statusCode, latency)