Logging and distributed tracing are essential for understanding complex
systems. This tutorial covers structured logging, the ELK stack, Loki, and
distributed tracing with Jaeger. You'll learn to implement correlation IDs
and trace requests across multiple services.
Why this matters:
In microservices and distributed systems, understanding how a request flows
through multiple services is critical for debugging and performance analysis.
1. Logging Fundamentals
Logs are the primary source of information for debugging and monitoring.
Log levels: DEBUG, INFO, WARN, ERROR, FATAL.
Log format: Structured (JSON) vs unstructured (plain text).
Best practices: Include timestamp, service name, and correlation ID.
Add OpenTelemetry tracing to a simple microservice. Export traces to Jaeger.
Sample answer
import { NodeTracerProvider } from '@opentelemetry/node';
import { JaegerExporter } from '@opentelemetry/exporter-jaeger';
import { SimpleSpanProcessor } from '@opentelemetry/tracing';
import { trace } from '@opentelemetry/api';
const provider = new NodeTracerProvider();
const exporter = new JaegerExporter({
endpoint: 'http://localhost:14268/api/traces',
});
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.register();
const tracer = trace.getTracer('my-service');
// In your request handler:
const span = tracer.startSpan('handle-request');
span.setAttribute('method', 'GET');
// ... process request
span.end();
Homework
Homework 1
Implement structured logging and distributed tracing in your capstone
project. Add correlation IDs, structured log entries, and trace instrumentation for the
critical paths.
Sample outline
Logging: Use pino (Node.js) or structlog (Python) with JSON
format.
Correlation ID: Add middleware to generate/pass correlation
IDs.
Tracing: Add OpenTelemetry instrumentation for HTTP endpoints.
Visualisation: Send logs to Loki and traces to Jaeger.
Mini‑Project
Observability Stack
Build a complete observability stack for your capstone project:
Structured logging with correlation IDs
Loki (or ELK) for log aggregation
Jaeger for distributed tracing
Grafana for unified visualisation
Combine logs, metrics, and traces
Sample outline
Logs: Structured JSON logs from your application.
Loki: Log aggregation with Grafana integration.
Jaeger: Trace collection and visualisation.
Grafana: Unified dashboard with logs, traces, and metrics.
Correlation: Link logs to traces using correlation IDs.
Tutorial Summary
You learned to implement structured logging and distributed tracing for
complex systems. You used correlation IDs to link logs and traces, set up
the ELK stack and Loki for log aggregation, and integrated Jaeger for
distributed tracing. These skills are essential for debugging and
monitoring modern, distributed applications.
Key takeaway: Observability (logs + metrics + traces) is
the foundation of reliable, maintainable systems. Invest in good
instrumentation from the start.