Proxy metrics
Proxy metrics provide a Prometheus view of traffic handled by Kamal Proxy. They show request volume, request time, response status, rollout traffic, and current work in progress so operators can assess service health and proxy load.
Metrics endpoint
The metrics endpoint is a separate HTTP server. Set --metrics-port when starting the proxy to expose it; port 0 keeps metrics disabled. Prometheus can then scrape that port directly, as shown by example/prometheus.yml.
The metrics server uses the proxy bind address and follows the normal server shutdown path. It does not share the public HTTP or HTTPS listener, so monitoring access can be controlled separately from application traffic. See observability for the wider signal model, request-logging for request records, and request-routing for the routing context behind service labels.
References: internal/…/run.go• L20-35 internal/…/server.go• L48-65 internal/…/server.go• L68-98 internal/…/server.go• L183-208 example/prometheus.yml• L1-7
Request measurements
The proxy records completed requests after routing and response handling finish. Each request contributes to a total count and a duration histogram. Both measurements use the same labels so operators can compare traffic volume with latency for one service and response class.
| Measurement | Meaning | Labels |
|---|---|---|
kamal_proxy_http_requests_total | Total completed HTTP requests | service, method, status, rollout |
kamal_proxy_http_request_duration_seconds | Request duration in seconds | service, method, status, rollout |
kamal_proxy_http_in_flight_requests | Current requests being handled | service |
The method label uses standard HTTP method names. Unknown methods use OTHER, which keeps method groups bounded. The status label is the response status captured by the proxy. The rollout label separates traffic sent through rollout targets from other service traffic.
References: internal/…/metrics.go• L33-85 internal/…/metrics.go• L97-106 internal/…/logging_middleware.go• L53-117
In-flight request tracking
The in-flight gauge measures active service requests rather than completed traffic. The proxy increments it when a request enters a service and decrements it when service handling ends. This makes it useful for spotting current pressure even when completed-request totals have not yet changed.
The gauge is labeled only by service. Requests that match an excluded path do not enter this gauge, so the value reflects only traffic included in the service's metrics view.
References: internal/…/service.go• L314-323 internal/…/metrics.go• L62-70 internal/…/metrics.go• L87-93
Metrics exclusions
Service deployment settings can remove selected paths from Prometheus measurements. Use --exclude-metrics-path one or more times when deploying a service. Matching is exact and uses the path as the upstream target sees it, which matters when a route strips a path prefix.
Excluded requests still produce request logs. They skip the request counter, duration histogram, and in-flight gauge, so health checks or load-balancer probes do not distort user-traffic measures.
References: internal/…/deploy.go• L62-69 internal/…/service.go• L100-122 internal/…/service.go• L314-323 internal/…/logging_middleware.go• L107-113 README.md• L128-148
Metrics lifecycle
Metrics are inactive until the metrics server starts. The active tracker is then connected to the Prometheus collector and the endpoint serves the collector output. When metrics are disabled, request paths still run normally because a no-op tracker accepts instrumentation calls without exporting data.
The request path measures in-flight work at service entry and records completion data in the request logging boundary. This links each metric to the routed service, final status, rollout state, and total elapsed time.
flowchart LR
R[Request] --> S[Matched service]
S --> I[In-flight gauge]
I --> H[Proxy handling]
H --> C[Completion status and duration]
C --> P[Prometheus counter and histogram]
P --> E[Metrics endpoint]
E --> Q[Prometheus scrape]
References: internal/…/metrics.go• L12-31 internal/…/metrics.go• L20-23 internal/…/server.go• L183-204 internal/…/service.go• L314-323 internal/…/logging_middleware.go• L60-114
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the metric names and label sets aligned between the tracker and any dashboards or scrape consumers.
- Preserve the distinction between the target path used for exclusion and the original client path when routing changes strip prefixes.
- Keep metrics exclusion state attached to the request context until request completion so logs remain enabled while measurements are skipped.
- Keep the metrics listener lifecycle aligned with startup and graceful shutdown when server listeners change.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
Metrics port is left at 0 | No Prometheus endpoint or exported proxy measurements are available | Set --metrics-port and configure the scraper for that listener |
| Scraper targets the public HTTP or HTTPS port | Prometheus cannot reach the separate metrics handler | Scrape the configured metrics port |
| Exclusion uses the client path while routing strips a prefix | Intended health-check traffic remains in the measurements | Configure the path as seen by the upstream target |
| A path is excluded by exact match | Nested paths are still counted | Add each required path explicitly |
Key files
| File | Why to read it |
|---|---|
| internal/…/metrics.go | Defines the exported measurements, labels, method grouping, and no-op behavior. |
| internal/…/server.go | Shows how the separate metrics listener starts, serves, and stops. |
| internal/…/service.go | Connects service routing and path exclusions to in-flight tracking. |
| internal/…/logging_middleware.go | Records completed request status and duration for metrics and logs. |
| internal/…/run.go | Defines the runtime switch that enables the metrics listener. |
| internal/…/deploy.go | Defines the service-level path exclusion setting. |
| example/prometheus.yml | Shows a minimal Prometheus scrape configuration. |