Implement distributed tracing with OpenTelemetry: instrumentation and context propagation, trace/span semantics, exporters and sampling, service maps, baggage, and troubleshooting; include minimal config snippets for a typical web + worker stack.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Implementing Distributed Tracing with OpenTelemetry
OpenTelemetry (OTel) is a CNCF project for collecting telemetry data (traces, metrics, logs) in a vendor-agnostic way. Distributed tracing tracks requests across microservices, showing latency bottlenecks, errors, and dependencies. This guide covers implementation for a typical Node.js web (Express) + worker (BullMQ) stack, with general concepts applicable to any language (e.g., Python, Java, Go).
Key benefits: End-to-end visibility, root-cause analysis, service maps.
Prerequisites:
Node.js 18+.
Backend: Jaeger (all-in-one for dev) or OpenTelemetry Collector → backend (e.g., Tempo, Jaeger).
Run Jaeger: docker run -d -p 16686:16686 -p 4317:4317 -p 4318:4318 jaegertracing/all-in-one:latest.
1. Core Concepts: Trace/Span Semantics
Trace: A directed acyclic graph (DAG) of spans representing a single request's journey.
Span:
Unit of work (e.g., HTTP request, DB query).
Properties: trace_id (64-bit), span_id (64-bit), parent_span_id, name (e.g., "GET /users"), kind (SERVER, CLIENT, PRODUCER, CONSUMER, INTERNAL), status (OK/ERROR), start/end timestamps.
Attributes: Key-value metadata (e.g., http.method=GET, db.statement=SELECT * FROM users). Use semantic conventions: opentelemetry.io/docs/specs/semconv.
Events: Timestamped annotations (e.g., console.log with severity).
Links: Reference other spans/traces (e.g., for forks).
Resource: Describes the entity producing telemetry (e.g., service.name="web", service.version="1.0").
Spans form parent-child hierarchies. Root span starts a trace.
2. Instrumentation
Auto-instrumentation (easiest): SDKs instrument popular libs (HTTP, DBs, queues) automatically.
Manual instrumentation: Wrap custom code with tracer.startActiveSpan().
For Node.js:
bash
npm init -y
npm i @opentelemetry/api @opentelemetry/sdk-trace-node @opentelemetry/auto-instrumentations-node @opentelemetry/exporter-trace-otlp-http @opentelemetry/sdk-trace-base @opentelemetry/propagator-b3
npm i express bullmq ioredis # App deps
Auto-generated from span attributes (service.name, span.kind, peer/service attrs).
Visualize in Jaeger UI (localhost:16686 → Service Dependencies tab), Grafana Tempo, or SigNoz.
Shows edges (latency, error rates) between services.
Ensure consistent service.name across services (e.g., env var OTEL_SERVICE_NAME=web).
7. Minimal Config Snippets: Web + Worker Stack
Env vars (all services):
code
OTEL_SERVICE_NAME=web # or 'worker'
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_TRACES_SAMPLER=parentbased_always_on # Or 'traceidratio' with OTEL_TRACES_SAMPLER_ARG=0.01
OTEL_PROPAGATORS=tracecontext,baggage # Default