Introducing a new way to review your PRs.

Read the announcement

basecamp / kamal-proxy

Lightweight proxy server for Kamal

View on GitHub

15 documents · Updated Nov 18, 2026

docs / proxy-metrics

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.

MeasurementMeaningLabels
kamal_proxy_http_requests_totalTotal completed HTTP requestsservice, method, status, rollout
kamal_proxy_http_request_duration_secondsRequest duration in secondsservice, method, status, rollout
kamal_proxy_http_in_flight_requestsCurrent requests being handledservice

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

TriggerConsequenceGuard
Metrics port is left at 0No Prometheus endpoint or exported proxy measurements are availableSet --metrics-port and configure the scraper for that listener
Scraper targets the public HTTP or HTTPS portPrometheus cannot reach the separate metrics handlerScrape the configured metrics port
Exclusion uses the client path while routing strips a prefixIntended health-check traffic remains in the measurementsConfigure the path as seen by the upstream target
A path is excluded by exact matchNested paths are still countedAdd each required path explicitly

Key files

FileWhy to read it
internal/…/metrics.goDefines the exported measurements, labels, method grouping, and no-op behavior.
internal/…/server.goShows how the separate metrics listener starts, serves, and stops.
internal/…/service.goConnects service routing and path exclusions to in-flight tracking.
internal/…/logging_middleware.goRecords completed request status and duration for metrics and logs.
internal/…/run.goDefines the runtime switch that enables the metrics listener.
internal/…/deploy.goDefines the service-level path exclusion setting.
example/prometheus.ymlShows a minimal Prometheus scrape configuration.