OpenTelemetry
1. The Three Pillars of Observability
OpenTelemetry focuses on the "Big Three" types of data:
Traces: Tracks the path of a single request as it moves through different services (e.g., "User clicks Login" -> "Backend validates JWT" -> "DB fetches User").
Metrics: Numerical data over time (e.g., CPU usage, number of requests per second, or error rates).
Logs: Structured text records of events (e.g.,
User X successfully logged in).
2. Core Components
A. The SDK (Software Development Kit)
This is what you install in your Node.js application. It gathers the data. For Node.js, OTel provides Instrumentation libraries that automatically hook into popular modules like http, express, mongoose, and pg.
B. The Collector
A standalone proxy that receives data from your app, processes it (filters or batches it), and sends it to a backend. It acts as a "buffer" so your app doesn't slow down by trying to send data to a remote server.
C. Exporters
The "plug-ins" that send your data to a storage backend. Since OTel is vendor-neutral, you can swap exporters without changing your app code. You can export to:
Jaeger or Zipkin (for Tracing)
Prometheus (for Metrics)
Grafana Tempo or Honeycomb (for full Observability)
3. Basic Implementation (Node.js)
Instead of manual logging, OTel uses "Automatic Instrumentation." You initialize it before your application code even starts.
// sdk.js
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
const sdk = new NodeSDK({
traceExporter: new OTLPTraceExporter({
url: 'http://localhost:4318/v1/traces',
}),
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();You then run your app node -r ./sdk.js server.js.
4. Context Propagation
This is the "magic" of OTel. If Service A calls Service B, OTel injects a Trace ID into the HTTP headers.
Service A: "I'm starting Trace #123."
Service B: "I see Trace #123 in the headers, I'll add my logs to that same ID."
Result: You get a single visual timeline showing exactly where a delay happened across multiple servers.
5. Why use OpenTelemetry over simple logs?
| Feature | Standard Logging | OpenTelemetry |
| Correlation | Hard to link logs across services. | Perfectly linked via Trace IDs. |
| Performance | Printing logs can block the Event Loop. | Asynchronous and non-blocking. |
| Vendor Lock-in | Locked into a specific logger/platform. | Vendor-neutral; swap backends easily. |
| Automation | Must write manual logs everywhere. | Automatically instruments HTTP, DBs, and Redis. |
6. Summary of Workflow
Instrument: Add the OTel SDK to your Node.js app.
Collect: Send data to the OTel Collector.
Export: Send from the Collector to a visualization tool like Grafana or Jaeger.