2HAASStart a project
All posts

Guides

Microservice observability: logs, metrics and traces

When a request crosses ten services, one log file cannot explain a slow checkout. Logs, metrics and traces each answer a different question. Here is how they fit.

The 2HAAS team2 min read

In a single application, a slow request is a matter of reading one log. In a system of microservices, the same request passes through a gateway, three services, a queue and two databases. Each one says it was fine. The user still waited eight seconds.

Observability is how you answer "what happened?" without guessing. It rests on three signals.

Three signals, three questions

Signal Question it answers Example
Metrics Is something wrong? Checkout latency doubled at 14:05
Traces Where is it wrong? 2.4 seconds were spent in the inventory service
Logs Why is it wrong? Database connection timed out, pool exhausted

Each is weak alone. Together they take you from an alert to a cause.

Metrics: the early warning

Metrics are numbers measured over time. They are cheap to store and ideal for dashboards and alerts. For every service, start with three:

  • Rate: requests per second,
  • Errors: the share of requests that fail,
  • Duration: how long requests take, as percentiles and not as an average.

An average hides the slow requests your unhappiest users experience. The 95th and 99th percentiles show them.

Traces: the journey of one request

A trace follows a single request across services. Each service adds a span with its start time and duration, and passes a trace ID to the next service in a request header. The result is a timeline: which service called which, and where the time went.

Traces are what make a distributed system readable.

Logs: the detail

Logs record individual events. They become far more useful when they are structured and when every line carries the trace ID. Then "show me the logs for this slow request" is one filter.

Use one standard to collect them

OpenTelemetry is the vendor-neutral standard for producing all three signals. Its libraries instrument common frameworks, HTTP clients and database drivers automatically, and the data can be sent to the backend of your choice. Instrumenting once and keeping the freedom to change tools later is worth a lot.

A worked example

  1. An alert fires: the 95th percentile of checkout latency is above two seconds.
  2. You open a slow trace. Most of the time sits in a single span, the call to the inventory service.
  3. The metrics for that service show CPU at its limit on one instance.
  4. The logs for the same trace ID show repeated database connection timeouts.

The cause is an exhausted connection pool. Finding it took four steps and no guesswork.

Where to start

  1. Structured logs with a request ID in every service.
  2. Rate, error and duration metrics, with one dashboard per service.
  3. Tracing on the most important path, such as checkout or login.
  4. Alerts on what users feel, such as latency and failed requests, not on every CPU spike.

Observability tells you about failures. Handling them well is the other half: see error handling in microservices.

Keep reading

  1. Guides2 min read

    Reduce Docker image size with multi-stage builds

    Large Docker images slow down every build, push and deploy. Multi-stage builds, slim base images and a .dockerignore file can cut an image to a fraction of its size.

  2. Tech Tips2 min read

    Structured logging: make your logs searchable

    Plain text logs are easy to write and painful to search. Structured JSON logs with levels and consistent fields turn finding one failed order into a single query.

  3. Guides3 min read

    Error handling in microservices: retries and circuit breakers

    In a distributed system, something is always failing a little. Timeouts, retries with backoff, circuit breakers and consistent errors keep one fault from spreading.

Tell us what’s in the way.

A few lines is enough. You get a candid yes, a candid no, or a sharper question.

  • Working software in week four
  • A reply in one business day
  • Handover is the deliverable

Prefer email? [email protected]

The problem, who it is for, and what is in the way right now.

One business day to a real reply. No sales sequence, no call required.