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 / observability

Proxy observability

Kamal Proxy emits two main runtime signals: structured request logs and Prometheus metrics. Together they show what traffic the proxy received, how each request ended, which service and target handled it, and how much work is active. Use Proxy metrics for metric names and Request logging for the log record contract.

Signal model

The proxy creates its operational signals around the request lifecycle. A request gets an identity before routing, gains service and target context while it is handled, and produces a log record and metric observation when processing ends.

The following flow shows how request behavior becomes operational data.

flowchart LR
    A[Incoming request] --> B[Request identity and start time]
    B --> C[Host and path routing]
    C --> D[Service and target handling]
    D --> E[Response status and duration]
    E --> F[Structured request log]
    E --> G[Request count and duration metrics]
    D --> H[In-flight request gauge]

Request context

Request context links signals from one request. The proxy keeps an existing X-Request-ID or creates one, and it adds X-Request-Start when the request does not already have that header. The request log uses the request ID so an operator can match proxy activity with downstream application logs.

The context also records the selected service, target, and rollout group. These values describe the route that served the request rather than only the listener that accepted it. A request that is redirected or rejected before a target is selected can therefore have an empty target value.

References: internal/…/request_id_middleware.go• L8-30 internal/…/request_start_middleware.go• L9-27 internal/…/logging_middleware.go• L20-50 internal/…/service.go• L612-639

Service and target signals

Service and target state explains why traffic changes over time. The proxy logs target health transitions and records whether a request used the active group or a rollout group. Deployment, pause, resume, drain, and target health events are separate service signals from the per-request record.

These signals must be read with routing and lifecycle state. A target can be healthy before it receives traffic, while a draining target can still appear in logs for requests that started before the drain. See Request routing and middleware and Services and upstream targets for those relationships.

References: internal/…/target.go• L218-275 internal/…/service.go• L258-320 internal/…/service.go• L527-535 internal/…/router.go• L151-176

Request logging

Request logging is the detailed view of proxy behavior. Each completed request produces one structured Request event with the listener, request, response, route, client, and timing data needed to inspect an individual exchange.

Request records

The record includes the host, path, method, query, protocol, scheme, status, duration, request and response sizes, content types, client address, forwarded address, user agent, request ID, service, target, and rollout marker. Operators can add selected request or response headers when an application-specific value is needed for diagnosis.

The logger captures the response status and bytes written by the proxy response path. This makes the record useful for client-visible outcomes, including redirects and proxy-generated errors, even when no upstream response exists.

Read Request logging for the field-level contract and header options.

References: internal/…/logging_middleware.go• L53-127 internal/…/logging_middleware.go• L129-174 internal/…/deploy.go• L64-68

Request identity

The request ID is preserved when a caller supplies X-Request-ID; otherwise the proxy creates a UUID. The same value is available to the request log and to the upstream request path, which provides the main link between proxy and application visibility.

The proxy also preserves an incoming X-Request-Start value. If none exists, it adds the current time in milliseconds. This gives systems that use that header a common start marker without replacing a value supplied by an earlier proxy.

References: internal/…/request_id_middleware.go• L22-30 internal/…/request_start_middleware.go• L23-27 internal/…/server.go• L218-229

Proxy metrics

Metrics provide an aggregate view for dashboards and alerts. When enabled, the proxy exposes Prometheus data on its separate metrics listener. When disabled, request tracking has no effect and the proxy does not publish that endpoint.

Request measurements

The proxy measures request count and request duration by service, HTTP method, response status, and rollout status. It also measures the current number of in-flight requests by service. These dimensions connect traffic volume and latency to the service group that handled the request.

Methods outside the standard HTTP method set are grouped as OTHER. Duration values use seconds, and the in-flight value represents current work rather than a total over time. Read Proxy metrics for the metric contract and Prometheus use.

References: internal/…/metrics.go• L33-105 internal/…/logging_middleware.go• L60-113 internal/…/service.go• L314-323

Metrics scope

The metrics listener is separate from the HTTP and HTTPS traffic listeners. Operators enable it with --metrics-port; a zero port leaves it disabled. Prometheus can scrape the listener as a normal target, as shown in example/prometheus.yml.

Services can exclude exact upstream paths from request metrics. Excluded requests remain in request logs, but they do not change request counts, duration observations, or the in-flight gauge. This is useful for health checks and other high-volume traffic that should not shape user-traffic views.

References: internal/…/server.go• L183-208 internal/…/run.go• L28-35 internal/…/deploy.go• L64-66 internal/…/service.go• L120-122 example/prometheus.yml• L1-7

Signal interpretation

Logs and metrics answer different operational questions. Use metrics to detect changes in rate, latency, status, rollout share, and active work. Use request logs to explain a selected request and to connect its result to a service, target, request ID, or client.

Request outcomes

The response status in both signals is the status observed at the proxy boundary. It can come from the upstream service, a redirect, a paused or stopped service, a timeout, a client cancellation, or another proxy error path. A status change therefore shows the client-visible result, while the service and target fields help identify where the result came from.

A missing target does not by itself mean that routing failed. The proxy can finish a request before target selection, such as for a redirect or a proxy-level rejection. Combine the status, route fields, and nearby service or target events before deciding which layer failed.

References: internal/…/service.go• L612-665 internal/…/target.go• L361-399 internal/…/logging_middleware.go• L83-113

Deployment and rollout views

Deployment changes appear in service and target event logs, while request metrics show the traffic result of those changes. The rollout label separates requests sent to the rollout group from requests sent to the active group. The in-flight gauge helps show when old traffic has drained during a replacement.

For a complete view, compare request signals with target health and drain state. See Services and upstream targets for target availability and Request routing and middleware for the request path.

References: internal/…/router.go• L151-205 internal/…/service.go• L448-481 internal/…/service.go• L527-533 internal/…/metrics.go• L78-93

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the request log status, service, target, rollout, and duration fields aligned with the metric labels that describe the same request.
  • Preserve the rule that excluded paths remain visible in logs while leaving all request metric measurements unchanged.
  • Keep the metrics listener separate from client traffic so scrape access does not alter request routing or request counts.
  • If request identity or forwarded-client handling changes, review the link between proxy logs and upstream application logs.
  • If routing, rollout, draining, or error behavior changes, review the meaning of target, rollout, status, and in-flight signals.

Important failure modes

TriggerConsequenceGuard
Metrics port is zero or the metrics listener cannot startPrometheus has no usable proxy endpointSet a dedicated metrics port and treat startup failure as an availability issue for monitoring
Health or monitor paths are not excluded when they dominate trafficRequest rate, latency, and error views describe monitor traffic instead of user trafficConfigure exact upstream paths with --exclude-metrics-path
A request is rejected before target selectionThe log can have an empty target and metrics still show a service-level outcomeRead status and service fields with routing and service state
A target drains while requests are activeRequests can end with timeout or cancellation results during replacementCompare request outcomes with target drain and deployment events
A caller reuses an incorrect X-Request-IDLogs from different requests can appear to belong to one traceGenerate IDs at the edge when the caller does not provide a trusted ID, and validate caller trust outside the proxy

Key files

FileWhy to read it
internal/…/server.goShows how traffic handlers and the separate metrics listener start and stop.
internal/…/logging_middleware.goDefines the request event and the point where request metrics are recorded.
internal/…/metrics.goDefines the aggregate request measurements and their labels.
internal/…/service.goConnects service routing, rollout choice, in-flight tracking, and metric exclusion.
internal/…/target.goConnects target selection, health, draining, proxy errors, and target fields in logs.
internal/…/request_id_middleware.goDefines request ID creation and preservation.
internal/…/run.goDefines the operator switch that enables the metrics listener and JSON logging mode.
internal/…/deploy.goDefines per-service metric exclusions and optional logged headers.
example/prometheus.ymlShows a basic Prometheus scrape target for the proxy.