Cloud & Virtualization

Distributed Tracing: Following a Request Across Your Services

By NileVPS Team · Published · 4 min read

اقرأ المقال بالعربي

When an application is a single process, a profiler or a slow query log can usually tell you why a request was slow. Once a request passes through an API gateway, an authentication service, a product service, a cache, a database and a payment provider, that is no longer enough. Each component's own logs look normal, yet the user waited four seconds. Distributed tracing solves this by recording the full path of each request across every service, with timing for each step. This article explains the core concepts, how context travels between services, and how to run a small tracing setup on your own VPS.

Core concepts

Trace

A trace represents one end-to-end operation, such as "GET /checkout". It has a unique trace ID shared by everything that happens on behalf of that request.

Span

A span is a single unit of work inside a trace: an incoming HTTP request, a SQL query, a call to Redis, an outbound API call. Each span records:

  • a name and a span ID
  • the parent span ID, which builds the tree
  • start time and duration
  • attributes such as http.route, db.system or http.response.status_code
  • a status (ok or error) and optional events, such as an exception

Displayed as a waterfall, the spans show immediately which step consumed the time and which steps ran in sequence when they could have run in parallel.

Context propagation

For spans from different services to join the same trace, the trace ID and parent span ID must travel with the request. The W3C Trace Context standard defines an HTTP header for this:

traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01

The fields are version, trace ID, parent span ID and flags (here, sampled). Instrumentation libraries read this header on incoming requests and add it to outgoing ones. Message queues need the same context stored in message headers, otherwise the trace breaks at the queue.

What tracing reveals

Tracing is especially good at exposing problems that are invisible in per-service metrics:

  • N+1 queries: dozens of near-identical database spans under one request.
  • Sequential calls that could be parallel.
  • Slow dependencies: a third-party API that adds 800 ms only for certain regions.
  • Retry storms: repeated spans to the same downstream service after timeouts.
  • Error origin: which service actually threw the error that surfaced as a 500 at the edge.

Setting up OpenTelemetry and Jaeger

OpenTelemetry provides SDKs for most languages and a Collector that receives, processes and exports telemetry. Jaeger is an open-source tracing backend with a web UI, and its recent versions accept OTLP, the OpenTelemetry protocol, directly.

1. Run Jaeger for testing

With Docker installed, the all-in-one image is the quickest way to experiment:

docker run -d --name jaeger \
  -p 127.0.0.1:16686:16686 \
  -p 127.0.0.1:4317:4317 \
  -p 127.0.0.1:4318:4318 \
  jaegertracing/all-in-one:latest

Port 16686 is the UI, 4317 receives OTLP over gRPC and 4318 OTLP over HTTP. Binding to 127.0.0.1 keeps them private; reach the UI through an SSH tunnel with ssh -L 16686:localhost:16686 user@your-server. The all-in-one image stores data in memory, so use a persistent storage backend for anything beyond testing.

2. Instrument an application

Many languages support zero-code auto-instrumentation. For a Python Flask app, for example:

pip install opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a install
export OTEL_SERVICE_NAME=checkout
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
opentelemetry-instrument python app.py

The bootstrap step installs instrumentation for libraries it detects, such as Flask, requests and common database drivers. Node.js, Java, .NET and Go have equivalent options. Send some traffic, open the Jaeger UI and search for the checkout service.

3. Add a Collector in production

Rather than having every app talk to the backend directly, run an OpenTelemetry Collector on each host or as a central gateway. It batches data, retries on failure, removes sensitive attributes and lets you switch backends by changing configuration only.

Sampling strategies

Recording every request is rarely necessary and can be expensive.

StrategyHow it worksTrade-off
Head-basedDecide at the start, e.g. keep 10%Cheap, but may drop the rare slow request
Tail-basedDecide after the trace completesKeeps errors and slow traces, needs more memory in the Collector
Always onKeep everythingSimple; fine for low traffic or short periods

A common pattern is tail sampling that keeps all errored traces, all traces above a latency threshold and a small percentage of the rest.

Good practices

  • Name spans by operation, not by data: GET /orders/{id}, never the actual ID.
  • Never put passwords, tokens or personal data in attributes.
  • Add the trace ID to your logs so you can jump between them, as described in our observability guide.
  • Instrument the edges first: incoming HTTP, database and outbound HTTP cover most latency.
  • Run tracing infrastructure on a separate VPS once volume grows; you can compare sizes on the pricing page.

FAQ

Does tracing slow down my application?

There is some overhead from creating spans and exporting them, but with batching and sampling it is usually small compared with typical network and database latency. Measure it in your own environment before and after enabling instrumentation.

Do I need microservices to benefit from tracing?

No. Even a monolith makes database queries, cache lookups and external API calls. Tracing shows how a single request spends its time across those calls, which is often faster than reading logs.

What is the difference between tracing and logging?

Logs record individual events, often without a clear link between them. A trace links every step of one request into a timed tree. The two complement each other, especially when logs include the trace ID.

Related guides