What is OpenTelemetry and how is it used in Node.js?

Answer

OpenTelemetry (OTel) is an open-source observability framework providing APIs, SDKs, and tools for generating, collecting, and exporting telemetry data: traces (request flows across services), metrics (counters, histograms, gauges), and logs. It is vendor-neutral — export to Jaeger, Zipkin, Datadog, New Relic, or any OTel-compatible backend. Node.js setup: (1) Install: @opentelemetry/sdk-node, @opentelemetry/auto-instrumentations-node; (2) Initialize before loading app code: const sdk = new NodeSDK({ traceExporter: new OTLPTraceExporter(), instrumentations: [getNodeAutoInstrumentations()] }); sdk.start();; (3) Auto-instrumentation automatically instruments Express routes, HTTP calls, database queries (pg, mongodb, redis) without code changes. Traces show the full lifecycle of a request across services, including which function was slow. Metrics track request rates, error rates, and latency percentiles (p50, p95, p99). Baggage propagates key-value data (user ID, feature flags) alongside trace context. OTel is essential for debugging distributed systems and understanding production performance.