| 1 | package caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ |
| 2 | |
| 3 | import ( |
| 4 | "contextscip-go gomod github.com/golang/go/src go1.25.1 context/" |
| 5 | "errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/" |
| 6 | "fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/" |
| 7 | "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/" |
| 8 | "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/" |
| 9 | "syncscip-go gomod github.com/golang/go/src go1.25.1 sync/" |
| 10 | "timescip-go gomod github.com/golang/go/src go1.25.1 time/" |
| 11 | |
| 12 | "github.com/prometheus/client_golang/prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/" |
| 13 | "github.com/prometheus/client_golang/prometheus/promautoscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/" |
| 14 | otelpromscip-go gomod go.opentelemetry.io/contrib/bridges/prometheus v0.68.0 `go.opentelemetry.io/contrib/bridges/prometheus`/ "go.opentelemetry.io/contrib/bridges/prometheusscip-go gomod go.opentelemetry.io/contrib/bridges/prometheus v0.68.0 `go.opentelemetry.io/contrib/bridges/prometheus`/" |
| 15 | "go.opentelemetry.io/contrib/exporters/autoexportscip-go gomod go.opentelemetry.io/contrib/exporters/autoexport v0.68.0 `go.opentelemetry.io/contrib/exporters/autoexport`/" |
| 16 | sdkmetricscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/ "go.opentelemetry.io/otel/sdk/metricscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/" |
| 17 | "go.opentelemetry.io/otel/sdk/resourcescip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/resource`/" |
| 18 | semconvscip-go gomod go.opentelemetry.io/otel v1.43.0 `go.opentelemetry.io/otel/semconv/v1.26.0`/ "go.opentelemetry.io/otel/semconv/v1.26.0scip-go gomod go.opentelemetry.io/otel v1.43.0 `go.opentelemetry.io/otel/semconv/v1.26.0`/" |
| 19 | |
| 20 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 21 | caddymetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/metrics`/ "github.com/caddyserver/caddy/v2/internal/metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/metrics`/" |
| 22 | ) |
| 23 | |
| 24 | // Metrics configures metrics observations. |
| 25 | // EXPERIMENTAL and subject to change or removal. |
| 26 | // |
| 27 | // Example configuration: |
| 28 | // |
| 29 | // { |
| 30 | // "apps": { |
| 31 | // "http": { |
| 32 | // "metrics": { |
| 33 | // "per_host": true, |
| 34 | // "observe_catchall_hosts": false |
| 35 | // }, |
| 36 | // "servers": { |
| 37 | // "srv0": { |
| 38 | // "routes": [{ |
| 39 | // "match": [{"host": ["example.com", "www.example.com"]}], |
| 40 | // "handle": [{"handler": "static_response", "body": "Hello"}] |
| 41 | // }] |
| 42 | // } |
| 43 | // } |
| 44 | // } |
| 45 | // } |
| 46 | // } |
| 47 | // |
| 48 | // In this configuration: |
| 49 | // - Requests to example.com and www.example.com get individual host labels |
| 50 | // - All other hosts (e.g., attacker.com) are aggregated under "_other" label |
| 51 | // - This prevents unlimited cardinality from arbitrary Host headers |
| 52 | type Metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics# struct { |
| 53 | // Enable per-host metrics. Enabling this option may |
| 54 | // incur high-memory consumption, depending on the number of hosts |
| 55 | // managed by Caddy. |
| 56 | // |
| 57 | // CARDINALITY PROTECTION: To prevent unbounded cardinality attacks, |
| 58 | // only explicitly configured hosts (via host matchers) are allowed |
| 59 | // by default. Other hosts are aggregated under the "_other" label. |
| 60 | // See AllowCatchAllHosts to change this behavior. |
| 61 | PerHostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#PerHost. bool `json:"per_host,omitempty"` |
| 62 | |
| 63 | // Allow metrics for catch-all hosts (hosts without explicit configuration). |
| 64 | // When false (default), only hosts explicitly configured via host matchers |
| 65 | // will get individual metrics labels. All other hosts will be aggregated |
| 66 | // under the "_other" label to prevent cardinality explosion. |
| 67 | // |
| 68 | // This is automatically enabled for HTTPS servers (since certificates provide |
| 69 | // some protection against unbounded cardinality), but disabled for HTTP servers |
| 70 | // by default to prevent cardinality attacks from arbitrary Host headers. |
| 71 | // |
| 72 | // Set to true to allow all hosts to get individual metrics (NOT RECOMMENDED |
| 73 | // for production environments exposed to the internet). |
| 74 | ObserveCatchallHostsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#ObserveCatchallHosts. bool `json:"observe_catchall_hosts,omitempty"` |
| 75 | |
| 76 | // Enable pushing metrics via OTLP in addition to the existing Prometheus |
| 77 | // scrape endpoints. When set, a PeriodicReader is attached to the shared |
| 78 | // Prometheus registry (via a Prometheus -> OpenTelemetry bridge), and the |
| 79 | // exporter is autoconfigured from the standard OTEL_* environment |
| 80 | // variables (OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_PROTOCOL, |
| 81 | // OTEL_METRICS_EXPORTER, ...). Set OTEL_METRICS_EXPORTER=none or simply |
| 82 | // keep this field false to disable OTLP export. |
| 83 | OTLPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#OTLP. bool `json:"otlp,omitempty"` |
| 84 | |
| 85 | initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#init. syncscip-go gomod github.com/golang/go/src go1.25.1 sync/.Oncescip-go gomod github.com/golang/go/src go1.25.1 sync/Once# |
| 86 | httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics. *httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics# |
| 87 | allowedHostsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#allowedHosts. map[string]struct{} |
| 88 | hasHTTPSServerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#hasHTTPSServer. bool |
| 89 | meterProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#meterProvider. *sdkmetricscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/.MeterProviderscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/MeterProvider# |
| 90 | } |
| 91 | |
| 92 | type httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics# struct { |
| 93 | requestInFlightscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestInFlight. *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.GaugeVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/GaugeVec# |
| 94 | requestCountscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestCount. *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.CounterVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterVec# |
| 95 | requestErrorsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestErrors. *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.CounterVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterVec# |
| 96 | requestDurationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestDuration. *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.HistogramVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramVec# |
| 97 | requestSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestSize. *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.HistogramVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramVec# |
| 98 | responseSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#responseSize. *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.HistogramVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramVec# |
| 99 | responseDurationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#responseDuration. *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.HistogramVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramVec# |
| 100 | } |
| 101 | |
| 102 | func initHTTPMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/initHTTPMetrics().(ctx caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#, metrics *Metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#) { |
| 103 | const ns, sub = "caddy", "http" |
| 104 | registry := ctx.GetMetricsRegistryscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#GetMetricsRegistry().() |
| 105 | basicLabels := []string{"server", "handler"} |
| 106 | if metrics.PerHostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#PerHost. { |
| 107 | basicLabels = append(basicLabels, "host") |
| 108 | } |
| 109 | metrics.httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..requestInFlightscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestInFlight. = promautoscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/.Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/With().(registry).NewGaugeVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/Factory#NewGaugeVec().(prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.GaugeOptsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/GaugeOpts#{ |
| 110 | Namespacescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Namespace.: ns, |
| 111 | Subsystemscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Subsystem.: sub, |
| 112 | Namescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Name.: "requests_in_flight", |
| 113 | Helpscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Help.: "Number of requests currently handled by this server.", |
| 114 | }, basicLabels) |
| 115 | metrics.httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..requestErrorsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestErrors. = promautoscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/.Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/With().(registry).NewCounterVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/Factory#NewCounterVec().(prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.CounterOptsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#{ |
| 116 | Namespacescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Namespace.: ns, |
| 117 | Subsystemscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Subsystem.: sub, |
| 118 | Namescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Name.: "request_errors_total", |
| 119 | Helpscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Help.: "Number of requests resulting in middleware errors.", |
| 120 | }, basicLabels) |
| 121 | metrics.httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..requestCountscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestCount. = promautoscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/.Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/With().(registry).NewCounterVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/Factory#NewCounterVec().(prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.CounterOptsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#{ |
| 122 | Namespacescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Namespace.: ns, |
| 123 | Subsystemscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Subsystem.: sub, |
| 124 | Namescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Name.: "requests_total", |
| 125 | Helpscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterOpts#Help.: "Counter of HTTP(S) requests made.", |
| 126 | }, basicLabels) |
| 127 | |
| 128 | // TODO: allow these to be customized in the config |
| 129 | durationBuckets := prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.DefBucketsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/DefBuckets. |
| 130 | sizeBuckets := prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.ExponentialBucketsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/ExponentialBuckets().(256, 4, 8) |
| 131 | |
| 132 | httpLabels := []string{"server", "handler", "code", "method"} |
| 133 | if metrics.PerHostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#PerHost. { |
| 134 | httpLabels = append(httpLabels, "host") |
| 135 | } |
| 136 | metrics.httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..requestDurationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestDuration. = promautoscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/.Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/With().(registry).NewHistogramVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/Factory#NewHistogramVec().(prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.HistogramOptsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#{ |
| 137 | Namespacescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Namespace.: ns, |
| 138 | Subsystemscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Subsystem.: sub, |
| 139 | Namescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Name.: "request_duration_seconds", |
| 140 | Helpscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Help.: "Histogram of round-trip request durations.", |
| 141 | Bucketsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Buckets.: durationBuckets, |
| 142 | }, httpLabels) |
| 143 | metrics.httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..requestSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestSize. = promautoscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/.Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/With().(registry).NewHistogramVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/Factory#NewHistogramVec().(prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.HistogramOptsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#{ |
| 144 | Namespacescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Namespace.: ns, |
| 145 | Subsystemscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Subsystem.: sub, |
| 146 | Namescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Name.: "request_size_bytes", |
| 147 | Helpscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Help.: "Total size of the request. Includes body", |
| 148 | Bucketsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Buckets.: sizeBuckets, |
| 149 | }, httpLabels) |
| 150 | metrics.httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..responseSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#responseSize. = promautoscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/.Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/With().(registry).NewHistogramVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/Factory#NewHistogramVec().(prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.HistogramOptsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#{ |
| 151 | Namespacescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Namespace.: ns, |
| 152 | Subsystemscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Subsystem.: sub, |
| 153 | Namescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Name.: "response_size_bytes", |
| 154 | Helpscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Help.: "Size of the returned response.", |
| 155 | Bucketsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Buckets.: sizeBuckets, |
| 156 | }, httpLabels) |
| 157 | metrics.httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..responseDurationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#responseDuration. = promautoscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/.Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/With().(registry).NewHistogramVecscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus/promauto`/Factory#NewHistogramVec().(prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.HistogramOptsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#{ |
| 158 | Namespacescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Namespace.: ns, |
| 159 | Subsystemscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Subsystem.: sub, |
| 160 | Namescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Name.: "response_duration_seconds", |
| 161 | Helpscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Help.: "Histogram of times to first byte in response bodies.", |
| 162 | Bucketsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramOpts#Buckets.: durationBuckets, |
| 163 | }, httpLabels) |
| 164 | } |
| 165 | |
| 166 | // provisionOTLP wires a MeterProvider that periodically reads the process-wide |
| 167 | // Prometheus registry and pushes the result via OTLP. The exporter and reader |
| 168 | // are autoconfigured from the standard OTEL_* environment variables, matching |
| 169 | // the ergonomics of the existing `tracing` directive. It is a no-op when |
| 170 | // m.OTLP is false, and honors OTEL_METRICS_EXPORTER=none (autoexport |
| 171 | // short-circuits to a no-op reader in that case). |
| 172 | func (m *Metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#) provisionOTLPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#provisionOTLP().(ctx caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#) error { |
| 173 | if !m.OTLPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#OTLP. { |
| 174 | return nil |
| 175 | } |
| 176 | |
| 177 | // Register a Prometheus -> OpenTelemetry bridge against the process-wide |
| 178 | // Prometheus registry as the *default* source the NewMetricReader below |
| 179 | // will read from. |
| 180 | // |
| 181 | // NB: despite the "With*" naming, autoexport.WithFallbackMetricProducer is |
| 182 | // a package-level setter (it returns nothing) — it mutates autoexport's |
| 183 | // internal producer registry and takes effect on the very next call to |
| 184 | // NewMetricReader. It is NOT a MetricOption and must not be passed as one. |
| 185 | // Users can still override the source by setting OTEL_METRICS_PRODUCERS. |
| 186 | reg := ctx.GetMetricsRegistryscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#GetMetricsRegistry().() |
| 187 | autoexportscip-go gomod go.opentelemetry.io/contrib/exporters/autoexport v0.68.0 `go.opentelemetry.io/contrib/exporters/autoexport`/.WithFallbackMetricProducerscip-go gomod go.opentelemetry.io/contrib/exporters/autoexport v0.68.0 `go.opentelemetry.io/contrib/exporters/autoexport`/WithFallbackMetricProducer().(func(contextscip-go gomod github.com/golang/go/src go1.25.1 context/.Contextscip-go gomod github.com/golang/go/src go1.25.1 context/Context#) (sdkmetricscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/.Producerscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/Producer#, error) { |
| 188 | return otelpromscip-go gomod go.opentelemetry.io/contrib/bridges/prometheus v0.68.0 `go.opentelemetry.io/contrib/bridges/prometheus`/.NewMetricProducerscip-go gomod go.opentelemetry.io/contrib/bridges/prometheus v0.68.0 `go.opentelemetry.io/contrib/bridges/prometheus`/NewMetricProducer().(otelpromscip-go gomod go.opentelemetry.io/contrib/bridges/prometheus v0.68.0 `go.opentelemetry.io/contrib/bridges/prometheus`/.WithGathererscip-go gomod go.opentelemetry.io/contrib/bridges/prometheus v0.68.0 `go.opentelemetry.io/contrib/bridges/prometheus`/WithGatherer().(reg)), nil |
| 189 | }) |
| 190 | |
| 191 | reader, err := autoexportscip-go gomod go.opentelemetry.io/contrib/exporters/autoexport v0.68.0 `go.opentelemetry.io/contrib/exporters/autoexport`/.NewMetricReaderscip-go gomod go.opentelemetry.io/contrib/exporters/autoexport v0.68.0 `go.opentelemetry.io/contrib/exporters/autoexport`/NewMetricReader().(ctx) |
| 192 | if err != nil { |
| 193 | return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("creating OTLP metric reader: %w", err) |
| 194 | } |
| 195 | |
| 196 | version, _ := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Versionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Version().() |
| 197 | res, err := resourcescip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/resource`/.Mergescip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/resource`/Merge().(resourcescip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/resource`/.Defaultscip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/resource`/Default().(), resourcescip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/resource`/.NewSchemalessscip-go gomod go.opentelemetry.io/otel/sdk v1.43.0 `go.opentelemetry.io/otel/sdk/resource`/NewSchemaless().( |
| 198 | semconvscip-go gomod go.opentelemetry.io/otel v1.43.0 `go.opentelemetry.io/otel/semconv/v1.26.0`/.WebEngineNamescip-go gomod go.opentelemetry.io/otel v1.43.0 `go.opentelemetry.io/otel/semconv/v1.26.0`/WebEngineName().(ServerHeaderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ServerHeader.), |
| 199 | semconvscip-go gomod go.opentelemetry.io/otel v1.43.0 `go.opentelemetry.io/otel/semconv/v1.26.0`/.WebEngineVersionscip-go gomod go.opentelemetry.io/otel v1.43.0 `go.opentelemetry.io/otel/semconv/v1.26.0`/WebEngineVersion().(version), |
| 200 | )) |
| 201 | if err != nil { |
| 202 | return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("building OTLP metrics resource: %w", err) |
| 203 | } |
| 204 | |
| 205 | m.meterProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#meterProvider. = sdkmetricscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/.NewMeterProviderscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/NewMeterProvider().( |
| 206 | sdkmetricscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/.WithResourcescip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/WithResource().(res), |
| 207 | sdkmetricscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/.WithReaderscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/WithReader().(reader), |
| 208 | ) |
| 209 | |
| 210 | return nil |
| 211 | } |
| 212 | |
| 213 | // shutdown flushes and tears down the OTLP MeterProvider if one was provisioned. |
| 214 | // Both ForceFlush and Shutdown are always attempted so that a flush failure |
| 215 | // does not prevent the reader goroutines from being stopped; errors from both |
| 216 | // are returned joined. |
| 217 | func (m *Metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#) shutdownscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#shutdown().(ctx contextscip-go gomod github.com/golang/go/src go1.25.1 context/.Contextscip-go gomod github.com/golang/go/src go1.25.1 context/Context#) error { |
| 218 | if m == nil || m.meterProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#meterProvider. == nil { |
| 219 | return nil |
| 220 | } |
| 221 | |
| 222 | // ForceFlush gives the final collection a chance to reach the collector |
| 223 | // before the reader goroutine is stopped by Shutdown. |
| 224 | return errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Joinscip-go gomod github.com/golang/go/src go1.25.1 errors/Join().( |
| 225 | m.meterProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#meterProvider..ForceFlushscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/MeterProvider#ForceFlush().(ctx), |
| 226 | m.meterProviderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#meterProvider..Shutdownscip-go gomod go.opentelemetry.io/otel/sdk/metric v1.43.0 `go.opentelemetry.io/otel/sdk/metric`/MeterProvider#Shutdown().(ctx), |
| 227 | ) |
| 228 | } |
| 229 | |
| 230 | // scanConfigForHosts scans the HTTP app configuration to build a set of allowed hosts |
| 231 | // for metrics collection, similar to how auto-HTTPS scans for domain names. |
| 232 | func (m *Metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#) scanConfigForHostsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#scanConfigForHosts().(app *Appscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/App#) { |
| 233 | if !m.PerHostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#PerHost. { |
| 234 | return |
| 235 | } |
| 236 | |
| 237 | m.allowedHostsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#allowedHosts. = make(map[string]struct{}) |
| 238 | m.hasHTTPSServerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#hasHTTPSServer. = false |
| 239 | |
| 240 | for _, srv := range app.Serversscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/App#Servers. { |
| 241 | // Check if this server has TLS enabled |
| 242 | serverHasTLS := len(srv.TLSConnPoliciesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Server#TLSConnPolicies.) > 0 |
| 243 | if serverHasTLS { |
| 244 | m.hasHTTPSServerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#hasHTTPSServer. = true |
| 245 | } |
| 246 | |
| 247 | // Collect hosts from route matchers |
| 248 | for _, route := range srv.Routesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Server#Routes. { |
| 249 | for _, matcherSet := range route.MatcherSetsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Route#MatcherSets. { |
| 250 | for _, matcher := range matcherSet { |
| 251 | if hm, ok := matcher.(*MatchHostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchHost#); ok { |
| 252 | for _, host := range *hm { |
| 253 | // Only allow non-fuzzy hosts to prevent unbounded cardinality |
| 254 | if !hm.fuzzyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/MatchHost#fuzzy().(host) { |
| 255 | m.allowedHostsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#allowedHosts.[stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.25.1 strings/ToLower().(host)] = struct{}{} |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // shouldAllowHostMetrics determines if metrics should be collected for the given host. |
| 266 | // This implements the cardinality protection by only allowing metrics for: |
| 267 | // 1. Explicitly configured hosts |
| 268 | // 2. Catch-all requests on HTTPS servers (if AllowCatchAllHosts is true or auto-enabled) |
| 269 | // 3. Catch-all requests on HTTP servers only if explicitly allowed |
| 270 | func (m *Metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#) shouldAllowHostMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#shouldAllowHostMetrics().(host string, isHTTPS bool) bool { |
| 271 | if !m.PerHostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#PerHost. { |
| 272 | return true // host won't be used in labels anyway |
| 273 | } |
| 274 | |
| 275 | normalizedHost := stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.25.1 strings/ToLower().(host) |
| 276 | |
| 277 | // Always allow explicitly configured hosts |
| 278 | if _, exists := m.allowedHostsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#allowedHosts.[normalizedHost]; exists { |
| 279 | return true |
| 280 | } |
| 281 | |
| 282 | // For catch-all requests (not in allowed hosts) |
| 283 | allowCatchAll := m.ObserveCatchallHostsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#ObserveCatchallHosts. || (isHTTPS && m.hasHTTPSServerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#hasHTTPSServer.) |
| 284 | return allowCatchAll |
| 285 | } |
| 286 | |
| 287 | // serverNameFromContext extracts the current server name from the context. |
| 288 | // Returns "UNKNOWN" if none is available (should probably never happen). |
| 289 | func serverNameFromContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/serverNameFromContext().(ctx contextscip-go gomod github.com/golang/go/src go1.25.1 context/.Contextscip-go gomod github.com/golang/go/src go1.25.1 context/Context#) string { |
| 290 | srv, ok := ctx.Valuescip-go gomod github.com/golang/go/src go1.25.1 context/Context#Value().(ServerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ServerCtxKey.).(*Serverscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Server#) |
| 291 | if !ok || srv == nil || srv.namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Server#name. == "" { |
| 292 | return "UNKNOWN" |
| 293 | } |
| 294 | return srv.namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Server#name. |
| 295 | } |
| 296 | |
| 297 | // metricsInstrumentedRoute wraps a compiled route Handler with metrics |
| 298 | // instrumentation. It wraps the entire compiled route chain once, |
| 299 | // collecting metrics only once per route match. |
| 300 | type metricsInstrumentedRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute# struct { |
| 301 | handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#handler. string |
| 302 | nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#next. Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler# |
| 303 | metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#metrics. *Metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics# |
| 304 | } |
| 305 | |
| 306 | func newMetricsInstrumentedRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/newMetricsInstrumentedRoute().(ctx caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#, handler string, next Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#, m *Metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#) *metricsInstrumentedRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute# { |
| 307 | m.initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#init..Doscip-go gomod github.com/golang/go/src go1.25.1 sync/Once#Do().(func() { |
| 308 | initHTTPMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/initHTTPMetrics().(ctx, m) |
| 309 | }) |
| 310 | |
| 311 | return &metricsInstrumentedRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#{handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#handler.: handler, nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#next.: next, metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#metrics.: m} |
| 312 | } |
| 313 | |
| 314 | func (h *metricsInstrumentedRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#) ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#ServeHTTP().(w httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error { |
| 315 | server := serverNameFromContextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/serverNameFromContext().(r.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().()) |
| 316 | labels := prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Labelsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Labels#{"server": server, "handler": h.handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#handler.} |
| 317 | method := caddymetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/metrics`/.SanitizeMethodscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/metrics`/SanitizeMethod().(r.Methodscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Method.) |
| 318 | // the "code" value is set later, but initialized here to eliminate the possibility |
| 319 | // of a panic |
| 320 | statusLabels := prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Labelsscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Labels#{"server": server, "handler": h.handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#handler., "method": method, "code": ""} |
| 321 | |
| 322 | // Determine if this is an HTTPS request |
| 323 | isHTTPS := r.TLSscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#TLS. != nil |
| 324 | |
| 325 | if h.metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#metrics..PerHostscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#PerHost. { |
| 326 | // Apply cardinality protection for host metrics |
| 327 | if h.metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#metrics..shouldAllowHostMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#shouldAllowHostMetrics().(r.Hostscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Host., isHTTPS) { |
| 328 | labels["host"] = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.25.1 strings/ToLower().(r.Hostscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Host.) |
| 329 | statusLabels["host"] = stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.25.1 strings/ToLower().(r.Hostscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Host.) |
| 330 | } else { |
| 331 | // Use a catch-all label for unallowed hosts to prevent cardinality explosion |
| 332 | labels["host"] = "_other" |
| 333 | statusLabels["host"] = "_other" |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | inFlight := h.metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#metrics..httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..requestInFlightscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestInFlight..Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/GaugeVec#With().(labels) |
| 338 | inFlight.Incscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Gauge#Inc().() |
| 339 | defer inFlight.Decscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Gauge#Dec().() |
| 340 | |
| 341 | start := timescip-go gomod github.com/golang/go/src go1.25.1 time/.Nowscip-go gomod github.com/golang/go/src go1.25.1 time/Now().() |
| 342 | |
| 343 | // This is a _bit_ of a hack - it depends on the ShouldBufferFunc always |
| 344 | // being called when the headers are written. |
| 345 | // Effectively the same behaviour as promhttp.InstrumentHandlerTimeToWriteHeader. |
| 346 | writeHeaderRecorder := ShouldBufferFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ShouldBufferFunc#(func(status int, header httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#) bool { |
| 347 | statusLabels["code"] = caddymetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/metrics`/.SanitizeCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/metrics`/SanitizeCode().(status) |
| 348 | ttfb := timescip-go gomod github.com/golang/go/src go1.25.1 time/.Sincescip-go gomod github.com/golang/go/src go1.25.1 time/Since().(start).Secondsscip-go gomod github.com/golang/go/src go1.25.1 time/Duration#Seconds().() |
| 349 | h.metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#metrics..httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..responseDurationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#responseDuration..Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramVec#With().(statusLabels).Observescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Observer#Observe().(ttfb) |
| 350 | return false |
| 351 | }) |
| 352 | wrec := NewResponseRecorderscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/NewResponseRecorder().(w, nil, writeHeaderRecorder) |
| 353 | err := h.nextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#next..ServeHTTPscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Handler#ServeHTTP.(wrec, r) |
| 354 | dur := timescip-go gomod github.com/golang/go/src go1.25.1 time/.Sincescip-go gomod github.com/golang/go/src go1.25.1 time/Since().(start).Secondsscip-go gomod github.com/golang/go/src go1.25.1 time/Duration#Seconds().() |
| 355 | h.metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#metrics..httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..requestCountscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestCount..Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterVec#With().(labels).Incscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Counter#Inc().() |
| 356 | |
| 357 | observeRequest := func(status int) { |
| 358 | // If the code hasn't been set yet, and we didn't encounter an error, we're |
| 359 | // probably falling through with an empty handler. |
| 360 | if statusLabels["code"] == "" { |
| 361 | // we still sanitize it, even though it's likely to be 0. A 200 is |
| 362 | // returned on fallthrough so we want to reflect that. |
| 363 | statusLabels["code"] = caddymetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/metrics`/.SanitizeCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/internal/metrics`/SanitizeCode().(status) |
| 364 | } |
| 365 | |
| 366 | h.metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#metrics..httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..requestDurationscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestDuration..Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramVec#With().(statusLabels).Observescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Observer#Observe().(dur) |
| 367 | h.metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#metrics..httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..requestSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestSize..Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramVec#With().(statusLabels).Observescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Observer#Observe().(float64(computeApproximateRequestSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/computeApproximateRequestSize().(r))) |
| 368 | h.metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#metrics..httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..responseSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#responseSize..Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/HistogramVec#With().(statusLabels).Observescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Observer#Observe().(float64(wrec.Sizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder#Size.())) |
| 369 | } |
| 370 | |
| 371 | if err != nil { |
| 372 | var handlerErr HandlerErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError# |
| 373 | if errorsscip-go gomod github.com/golang/go/src go1.25.1 errors/.Asscip-go gomod github.com/golang/go/src go1.25.1 errors/As().(err, &handlerErr) { |
| 374 | observeRequest(handlerErr.StatusCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/HandlerError#StatusCode.) |
| 375 | } |
| 376 | |
| 377 | h.metricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/metricsInstrumentedRoute#metrics..httpMetricsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/Metrics#httpMetrics..requestErrorsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/httpMetrics#requestErrors..Withscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/CounterVec#With().(labels).Incscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Counter#Inc().() |
| 378 | |
| 379 | return err |
| 380 | } |
| 381 | |
| 382 | observeRequest(wrec.Statusscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ResponseRecorder#Status.()) |
| 383 | |
| 384 | return nil |
| 385 | } |
| 386 | |
| 387 | // taken from https://github.com/prometheus/client_golang/blob/6007b2b5cae01203111de55f753e76d8dac1f529/prometheus/promhttp/instrument_server.go#L298 |
| 388 | func computeApproximateRequestSizescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/computeApproximateRequestSize().(r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) int { |
| 389 | s := 0 |
| 390 | if r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL. != nil { |
| 391 | s += len(r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Stringscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#String().()) |
| 392 | } |
| 393 | |
| 394 | s += len(r.Methodscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Method.) |
| 395 | s += len(r.Protoscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Proto.) |
| 396 | for name, values := range r.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Header. { |
| 397 | s += len(name) |
| 398 | for _, value := range values { |
| 399 | s += len(value) |
| 400 | } |
| 401 | } |
| 402 | s += len(r.Hostscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Host.) |
| 403 | |
| 404 | // N.B. r.Form and r.MultipartForm are assumed to be included in r.URL. |
| 405 | |
| 406 | if r.ContentLengthscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#ContentLength. != -1 { |
| 407 | s += int(r.ContentLengthscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#ContentLength.) |
| 408 | } |
| 409 | return s |
| 410 | } |