Metrics, Logs, and Distributed Tracing
Master the three pillars of telemetry, how trace context propagates across microservices, and how to debug distributed latency.
In a monolithic application, debugging a failure is relatively straightforward. A user encounters an error, an exception bubbles up, and the application runtime writes a single stack trace into a local log file on disk. A developer opens the log file, looks at the line number, and reproduces the problem.
In a modern distributed architecture, a single user click might travel through an API gateway, an authentication service, an order orchestrator, an inventory microservice, a payment gateway, and a notification worker. When that user experiences a four-second delay or an intermittent internal server error, no single machine holds the complete story.
Each microservice writes its own disconnected log messages to its own local disk. If one service encounters an intermittent timeout, engineers face needle-in-a-haystack triage:
- Did the checkout request fail because the payment partner timed out, or because the database connection pool was exhausted ten minutes earlier?
- Which of the seven microservices spent three out of the four seconds?
- How can an on-call engineer detect that error rates are climbing before hundreds of customers contact support?
Solving this problem requires distributed observability, built upon three complementary telemetry signals: metrics, logs, and distributed traces.
01Core concepts and intuition
Observability is not simply collecting data; it is the ability to infer the internal state of a complex system based on its external outputs. The three core telemetry primitives each serve a distinct diagnostic role:
Metrics: Aggregatable numerical data
Metrics are numeric measurements recorded over regular time intervals. Common types include:
- Counters: Monotonically increasing values that only go up or reset to zero on restart (such as
http_requests_total). - Gauges: Numerical snapshots that fluctuate up and down (such as
active_database_connectionsormemory_usage_bytes). - Histograms: Sample observations grouped into configurable buckets to measure distribution (such as request duration to calculate 50th, 95th, and 99th percentile latencies).
Metrics excel at answering: "Is there a problem right now, where is it, and how bad is it?" Because metrics are purely numerical values paired with key-value labels, they compress efficiently and can be queried across millions of events with sub-second response times. They are the ideal foundation for automated alerting dashboards.
Logs: Timestamped discrete event records
A log is an immutable record of a specific event that took place at a single moment in time. While metrics tell you that error rates jumped from 0.01% to 5%, logs reveal the exact underlying reason: a null pointer exception, an invalid JSON syntax error, or an authentication certificate expiration.
Modern systems use structured logging, where log messages are serialized as standardized JSON objects rather than raw strings. Every log entry includes structured metadata such as timestamp, severity level, service name, environment, and request parameters.
Distributed traces: Request journeys across boundaries
A distributed trace represents the end-to-end execution path of a single transaction as it travels across process and network boundaries.
A trace is composed of one or more spans:
- A span represents a single contiguous unit of work (for example, handling an incoming HTTP request, executing a database SQL query, or publishing an event to a Kafka broker).
- Each span records a name, start time, duration, status, and custom attributes.
- Spans form a directed acyclic tree. The root span originates at the edge gateway, and subsequent downstream operations register as child spans.
Distributed tracing answers: "Where along this multi-hop journey was time spent, and which component caused the bottleneck?"
Loading the rest of the lesson…