ADR 0004: Adopt the fleet observability standard (logs, metrics, traces)
Status
Accepted (2026-10-26).
Context
customer-payments logged JSON with log/slog and nothing else: no trace or span id on a log line,
no request id, no request log, no metrics, no traces and no traceparent on the events it publishes.
The warehouse-systems services (inventory-storage ADR 0016, wes-work-planning ADR 0013, "standard
metrics convention") and customer-ordering (ADR 0007) already share one pipeline and one convention:
- pods export OTLP/gRPC to the in-cluster Collector
(
otel-collector.observability.svc.cluster.local:4317), which feeds Jaeger (traces) and a Prometheus exporter (metrics); - logs stay on stdout as JSON for Alloy and Loki.
Without the same wiring a payment could not be followed from the authorize call through the Kafka
events to the consumers, a log line could not be joined to its trace, and no dashboard could show
authorization rate, declines or latency. A service-specific setup was rejected (every repo should answer
the same query the same way); so were OTel auto-instrumentation agents (they cannot follow a trace
across segmentio/kafka-go, nor name spans by the chi route pattern).
Decision
Tier 1 (identical in every repo) and Tier 2 (this context's business metric):
- Telemetry adapter.
internal/adapters/outbound/telemetryholdsSetup(OTLP/gRPC traces and metrics, W3C TraceContext + Baggage propagator, Go runtime metrics; exporters dial lazily, so no Collector means dropped telemetry, never a blocked boot or request; returns a shutdown func) andWithTraceContext(stampstrace_id/span_idon a record written with a span-carrying context). Only the composition roots and the adapters import it;internal/domainandinternal/applicationnever import OpenTelemetry. - Logger.
slog.New(telemetry.WithTraceContext(slog.NewJSONHandler(os.Stdout, ...))),LOG_LEVEL, uppercase levels. Request and message paths use the*Contextslog variants. - HTTP middleware order on both chi routers (REST and MCP):
RequestID->otelchi.Middleware->otelchimetric.NewServerRequestDuration->RequestLogger->Recoverer;rejectNULand the Idempotency-Key middleware stay after them.telemetry.Setupruns before the router is built and is flushed last on shutdown. The request line carriesmethod,route(the chi pattern, never the raw path),status,duration_ms,bytes,request_id; 5xx is ERROR, 4xx WARN, the rest INFO;/healthzand/readyzare not logged.RequestLoggerlives ininternal/adapters/inbound/requestlogbecausecmd/mcpmay not import the REST adapter. - Traces over Kafka.
OutboxRepo.Appendstampstraceparent/tracestatefrom the active span into the stored outbox headers inside the transaction that writes the aggregate (internal/adapters/outbound/postgres/outbox_trace.go). The relay publishes them unchanged; each inbound consumer (checkout, order-sourcing, shipping-and-tracking) extracts them and runs every handling attempt in a consumer span named<topic> process. Only extra headers are added: the CloudEvents attributes and body are unchanged (ADR 0003). - Environment contract.
cmd/apiandcmd/mcpreadOTEL_SERVICE_NAME(defaultscustomer-paymentsandcustomer-payments-mcp),OTEL_EXPORTER_OTLP_ENDPOINT(defaulttelemetry.DefaultEndpoint),SERVICE_VERSION,ENVIRONMENTandLOG_LEVEL. - Tier 2 business metric. One counter,
payments.authorizations.decided, unit{authorization}, attributeoutcome(authorized|declined|failed) and, except when authorized,reason: the provider's decline code fordeclined(a small set such asCARD_DECLINED), and forfailedone ofpayment-provider-unavailable,allocation-mismatch,concurrent-update,validation-failed,internal-error.ports.PaymentMetricsis the application port;AuthorizePaymentcalls it once per attempt (a replay returning the session's existing authorization is not an attempt); the OTel implementation is in the telemetry adapter and injected fromcmd/api; a nilDeps.Metricsis a no-op. Choice rationale: authorization at checkout (POST /payment-authorizations, ADR 0001) is the context's primary command and the only one on the shopper's critical path; a decline is a successful command with a DECLINED authorization, so it is an outcome, not an error, and a falling authorized share separates "shoppers' payment methods are refused" from "the provider is down", which request status codes alone cannot. Capture, release and refund are driven by consumed events and are not counted here.
Assumptions (made headless, nearest the reference)
- No Helm chart exists in this repo (ecommerce repos have none yet), so no
otel.*values, chart env orrender_test.shchange were made and no chart was created. When a chart arrives it must render the env contract above for api and mcp. - No
docs/runtime.mdor ADRindex.mdexists; the README gains the "Observability" section instead. - The existing
/metricsparked-facts gauge is a hand-written Prometheus endpoint and is left as is (out of scope); the standard's OTLP path does not depend on it. - The ADR is number 0004, the next free one. Dashboards and alerts live in warehouse-infra and are generated per deployed context; this context is not deployed yet.
Consequences
- A request, its log lines, the events it enqueued and the consumers that handle them share one trace id.
- Request rate, errors and duration come from
http.server.request.durationwith no hand-rolled counter; payment health frompayments.authorizations.decided. - Every new log call on a request or message path must use a
*Contextvariant. - A consumer span is created per handling attempt, so a retried message shows as several spans under the same parent; the parent is the span active at the outbox write, the relay adds no span of its own.
OutboxRepo.Insert(the raw path used by tests) stamps nothing; onlyAppenddoes.- Policy-driven events (clock passes) are enqueued with no active span and carry no
traceparent.