Lab 14: Observability

Time: 60 minutes | Level: Architect | Docker: docker run -it --rm node:20-alpine sh

"You can't manage what you can't measure." This lab covers the three pillars of observability: traces (OpenTelemetry), metrics, and structured logging (pino), plus Node.js's built-in diagnostics_channel.


Step 1: The Three Pillars of Observability

TRACES          → WHAT happened, in what order, how long each step took
                  (distributed request tracing across services)

METRICS         → HOW MUCH: counts, rates, histograms over time
                  (request rate, error rate, latency p99, heap usage)

LOGS            → WHAT exactly happened, with context
                  (structured JSON: level, msg, traceId, userId, duration)

OpenTelemetry (OTel) is the CNCF standard for all three pillars. One SDK, any backend (Jaeger, Zipkin, Tempo, Datadog, etc.).


Step 2: Install OpenTelemetry

npm install @opentelemetry/sdk-trace-base @opentelemetry/api
# For auto-instrumentation:
# npm install @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node

Step 3: TracerProvider + Custom Spans

📸 Verified Output:


Step 4: Metrics — Counter, Histogram, Gauge


Step 5: Pino Structured Logging

💡 Use pino-pretty for development: node app.js | pino-pretty. In production, output raw JSON and use a log aggregator (Loki, CloudWatch, ELK).


Step 6: AsyncLocalStorage + Trace Context Propagation


Step 7: node:diagnostics_channel

💡 diagnostics_channel is zero-overhead when no one is subscribed. It's how Node.js's built-in HTTP, PostgreSQL drivers, and Express can publish hook points without impacting performance.


Step 8: Capstone — Full Observability Stack


Summary

Pillar
Tool
Key Concept

Traces

@opentelemetry/sdk-trace-base

Spans, traceId, context propagation

Metrics

@opentelemetry/sdk-metrics

Counter, Histogram, Gauge

Logs

pino

Structured JSON, child loggers

Context

AsyncLocalStorage

Propagate traceId through async calls

Hooks

diagnostics_channel

Zero-overhead instrumentation points

Export

OTLP exporter

Send to Jaeger, Tempo, Datadog, etc.

Auto-instrument

@opentelemetry/auto-instrumentations-node

HTTP, pg, redis — zero config

Last updated