OPS-15Recommended
Trace requests across services
When a request crosses services, record one trace that follows it, and pass the trace context with the W3C traceparent header.
How to do it
Add OpenTelemetry to each service with its automatic instrumentation, so incoming and outgoing calls create spans. Give each service a fixed service.name. Keep every error trace and a share of the rest by sampling in a collector after the trace ends. A service deciding at the start cannot know a request will fail. Use the trace ID as the request ID in logs.
Examples
HTTP example
traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
JavaScript example
import { NodeSDK } from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
new NodeSDK({
serviceName: 'checkout',
traceExporter: new OTLPTraceExporter({ url: 'https://otel.example.com/v1/traces' }),
instrumentations: [getNodeAutoInstrumentations()],
}).start();
Python example
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
provider = TracerProvider(resource=Resource.create({"service.name": "checkout"}))
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter(endpoint="https://otel.example.com/v1/traces")))
trace.set_tracer_provider(provider)
RequestsInstrumentor().instrument()
YAML example
# OpenTelemetry Collector: keep every failed trace and 10% of the rest
processors:
tail_sampling:
decision_wait: 10s
policies:
- name: keep-errors
type: status_code
status_code: { status_codes: [ERROR] }
- name: sample-rest
type: probabilistic
probabilistic: { sampling_percentage: 10 }
How to check
Make one request that crosses two services on staging. The tracing tool shows one trace with spans from both, and the logs carry the same trace ID. A request that fails is always in the tracing tool.
Sources
Related rules
- WEB-57 Report how long the server took in Website
- DAT-11 Load related rows in one query in Data and databases
- OPS-77 Name telemetry with the OpenTelemetry conventions in Operations and observability
- OPS-81 Give telemetry a budget and sample the bulk in Operations and observability
- STK-62 Use OpenTelemetry to instrument your code in Software choices
- STK-99 Use Grafana Tempo to store traces in Software choices
- SCL-2 Profile the slow path before you change it in Scaling
- EVT-2 Wrap every event in one envelope in Integration and events
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/operations/metrics-and-traces/distributed-tracing">Agent Wranglers operations and observability playbook</a>. In code you write, add a comment next to it with the same link.