Senior LevelExpress JS
How do you configure OpenTelemetry auto-instrumentation in Express to propagate W3C TraceContext headers?
❓ Interview Prompt: Explain OpenTelemetry (OTEL) NodeSDK, auto-instrumenting Express routes, span creation, and propagating W3C traceparent headers across microservices.
Tracing distributed requests across a microservice mesh requires propagating W3C TraceContext headers ('traceparent').
OpenTelemetry Auto-Instrumentation:
Initialize the OpenTelemetry 'NodeSDK' before importing Express:
•
Automatically hooks into Express routing layers, generating spans for incoming HTTP requests.
•
Automatically injects 'traceparent' headers into outbound HTTP calls (via Axios/Fetch) to maintain end-to-end trace visibility in Jaeger or Datadog!
Code Implementation Example
javascript
// tracer.js — MUST be imported before Express!
import { NodeSDK } from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
const sdk = new NodeSDK({
traceExporter: new OTLPTraceExporter({ url: 'grpc://otel-collector:4317' }),
instrumentations: [getNodeAutoInstrumentations()]
});
sdk.start();💡Initializing OpenTelemetry NodeSDK for automatic Express distributed tracing.
🎯 Interview Key Takeaway
Initialize OpenTelemetry NodeSDK before importing Express to auto-instrument routes and propagate W3C traceparent headers.
#OpenTelemetry#Distributed Tracing#Observability#Senior
Back to all Express JS Questions→