DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

MonitoringLesson 6 of 9
Your progress · 0%
On this page
  1. Traces, spans and context
  2. OpenTelemetry
  3. Read a real trace in Jaeger
  4. Sampling
↖ Course roadmap
Lesson 6 · Tracing

Tracing with OpenTelemetry and Jaeger

Follow one request through many services: spans, trace context and the traceparent header, OpenTelemetry SDKs and the Collector, and reading a real Jaeger waterfall to find the slow and failing parts.

35 min read · intermediate · hands-on

By the end of this lesson you'll be able to

0 of 5 complete

When one click goes through five services, metrics say the request is slow and logs show forty unrelated lines, but neither says where the time went. A trace follows that one request across every service. In this lesson you'll run Jaeger, send real requests through a demo app, and read a real trace to find the slow part and the errors.

Introduction to Tracing : OpenTelemetry & OpentracingThat DevOps Guy · Watch on YouTube ↗

Traces, spans and context

  • A span is one timed operation: "handle GET /dispatch", "SELECT from customers", "call Redis GetDriver". It has a name, a start time, a duration, attributes (key-value details such as http.status_code=200), a status (OK or error), and the ID of its parent span.
  • A trace is all the spans of one request, forming a tree. The first span, with no parent, is the root span.
  • Trace context is the trace ID plus the current span ID. Passing it from service to service is called context propagation, and it's the part that makes distributed tracing work.

Each hop passes traceparent on. A service that drops the header starts a new trace, and the waterfall splits in two.© Diagram: DevOps Academy

Each hop passes traceparent on. A service that drops the header starts a new trace, and the waterfall splits in two.© Diagram: DevOps Academy

The traceparent header

For HTTP, the standard is W3C Trace Context. A traced request carries one header:

traceparent: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
             │  │                                │                └ flags: 01 = sampled
             │  │                                └ parent span ID (16 hex characters)
             │  └ trace ID (32 hex characters)
             └ version

When the cart service receives this, it does three things:

  1. It reads the trace ID and the caller's span ID.
  2. It starts its own span with the same trace ID and the caller's span as parent.
  3. When it calls payment, it sends a new traceparent with the same trace ID and its own span ID.

If any service in the chain doesn't forward the header (an old library, a proxy that strips headers, a message queue without context), the next service starts a brand-new trace. That's the most common reason a trace "stops" halfway.

OpenTelemetry

OpenTelemetry (OTel) is the open standard for producing telemetry: traces, metrics and logs. It's a CNCF project that replaced the older OpenTracing and OpenCensus. Before it, every vendor had its own agent and SDK, and switching from one APM tool to another meant re-instrumenting every service. With OTel you instrument once and choose the backend in config.

It has three parts you need to know:

PartWhat it isExample
SDK and instrumentationLibraries in your app that create spansopentelemetry-instrument python app.py, or the Java agent jar
OTLPThe wire protocol for sending telemetrygRPC on port 4317, HTTP on port 4318
CollectorA separate service that receives, processes and exports telemetryReceive OTLP, batch, drop health checks, send to Jaeger and a vendor

Instrumenting an app

There are two ways, and most teams use both:

  • Zero-code (automatic) instrumentation. An agent or wrapper hooks into common libraries (HTTP servers and clients, database drivers, gRPC) and creates spans with no code changes. For Java that's the -javaagent jar, for Python opentelemetry-instrument, for Node.js a --require hook. It also handles propagation for those libraries.
  • Manual spans around your own important steps, so the trace shows business logic, not just network calls:
from opentelemetry import trace

tracer = trace.get_tracer("checkout")

def charge(order):
    with tracer.start_as_current_span("charge-card") as span:
        span.set_attribute("order.id", order.id)
        ...  # the span ends, with its duration, when the block exits

Where to send spans is configuration, through standard environment variables:

OTEL_SERVICE_NAME=checkout
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
OTEL_TRACES_SAMPLER=parentbased_traceidratio
OTEL_TRACES_SAMPLER_ARG=0.1   # keep 10% of new traces

The Collector

Apps could send straight to Jaeger, and in the lab HotROD does exactly that. In production you put an OpenTelemetry Collector in between. Its config is a pipeline:

# otel-collector.yaml
receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317
      http:
        endpoint: 0.0.0.0:4318
processors:
  batch: {} # send spans in batches, not one by one
exporters:
  otlp/jaeger:
    endpoint: jaeger:4317
    tls:
      insecure: true
service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlp/jaeger]

Why bother? Apps stay simple (they only know "send OTLP to the Collector"). You can add a second backend, remove sensitive attributes, or change sampling in one place. And if the backend is down, the Collector buffers for a while instead of every app dropping data.

Read a real trace in Jaeger

Jaeger is an open-source tracing backend, originally from Uber and now a CNCF project. It stores spans and gives you a UI to search and read traces. The lab ran Jaeger 2.22 with the HotROD demo app from the logs lesson, sending spans over OTLP:

$ docker run -d --name jaeger --network mon -p 16686:16686 -p 4317:4317 -p 4318:4318 jaegertracing/jaeger
$ docker run -d --name hotrod --network mon -p 8080:8080 \
    -e OTEL_EXPORTER_OTLP_ENDPOINT=http://jaeger:4318 jaegertracing/example-hotrod all
$ for c in 123 392 731 567; do curl -s -o /dev/null -w "customer=$c %{http_code} %{time_total}s\n" "localhost:8080/dispatch?customer=$c"; done
customer=123 200 1.031454s
customer=392 200 0.705003s
customer=731 200 0.727585s
customer=567 200 0.734258s
docker run -d --name jaeger --network mon -p 16686:16686 -p 4317:4317 -p 4318:4318 jaegertracing/jaeger
  • dockerbuild and run containers
  • runstart a new container
  • -drun in the background
  • --name jaegername the container
  • --network monwhich network to join
  • -p 16686:16686publish a port, host:container
  • -p 4317:4317publish a port, host:container
  • -p 4318:4318publish a port, host:container
  • jaegertracing/jaegerthe image to start
docker run -d --name hotrod --network mon -p 8080:8080 \
  • --name hotrodname the container
  • -p 8080:8080publish a port, host:container
  • \the image to start
for c in 123 392 731 567; do curl -s -o /dev/null -w "customer=$c %{http_code} %{time_total}s\n" "localhost:8080/dispatch?customer=$c"; done
  • forloop over a list
  • dostart of what the loop runs each time
  • curlmake an HTTP request
  • -ssilent: no progress bar
  • -owrite the body to this file
  • -wprint these details after the request
  • doneend of the loop

Each /dispatch request is one ride booking. Jaeger's Search page lists the traces for a service, with their span count, error count and duration:

Four ride bookings, each about 40 spans across 6 services, each with 2 or 3 errors, which the services retried.© Screenshot: DevOps Academy

Four ride bookings, each about 40 spans across 6 services, each with 2 or 3 errors, which the services retried.© Screenshot: DevOps Academy

Open the slowest one (960 ms) and Jaeger draws the waterfall: one row per span, indented under its parent, with a bar showing when it started and how long it took.

A real trace. The MySQL query is a third of the request, then fourteen Redis calls run one after another, and three of them failed.© Screenshot: DevOps Academy

A real trace. The MySQL query is a third of the request, then fourteen Redis calls run one after another, and three of them failed.© Screenshot: DevOps Academy

Read it top to bottom. The same trace from Jaeger's API, one line per span (start offset, duration, service, operation), cut after the Redis calls:

     0.0ms    959.9ms  frontend   GET /dispatch
    33.5ms    396.3ms  frontend   HTTP GET
    67.9ms    348.7ms  customer   GET /customer
    70.9ms    330.3ms  mysql      SQL SELECT
   464.5ms    266.8ms  frontend   driver.DriverService/FindNearest
   498.9ms    230.2ms  driver     driver.DriverService/FindNearest
   501.9ms     25.1ms  redis-manual FindDriverIDs
   527.2ms     32.6ms  redis-manual GetDriver
   559.9ms      7.8ms  redis-manual GetDriver
   568.1ms      9.7ms  redis-manual GetDriver
   ...
   740.1ms     59.2ms  route      GET /route
   750.3ms     46.4ms  route      GET /route
   750.3ms     47.2ms  route      GET /route

What the trace tells you, in a minute, that no single dashboard could:

  1. The biggest cost is one SQL query. SQL SELECT takes 330 ms of 960 ms. Before touching anything else, look at that query and its index.
  2. Redis is called in a loop. One FindDriverIDs, then GetDriver fourteen times, one after another (each starts when the previous one ends). That's the N+1 pattern: one batched call (Redis MGET, or a pipeline) would replace fourteen round trips.
  3. Some calls fail and get retried. The red icons are spans with error status. Their logs say redis timeout, the same errors you found in Loki in the last lesson.
  4. The route calls run in parallel. Several GET /route spans start at the same time (740 ms, 750 ms, 750 ms). That part was already designed well.

From a log line to its trace

HotROD writes the trace ID into every log line:

2026-11-04T04:37:32.258Z	INFO	customer/server.go:64	HTTP request received	{"service": "customer", "trace_id": "9881c56bd9002eeee8637029115c5b57", "span_id": "5bb184a1881997f0", "method": "GET", "url": "/customer?customer=123"}

Paste 9881c56bd9002eeee8637029115c5b57 into Jaeger's Lookup by Trace ID box and you get the waterfall above. Go the other way in Loki with {container="hotrod"} |= "9881c56bd9002eeee8637029115c5b57" to get every log line of that one request, from every service. In Grafana you can make this a click: a Loki data source setting called derived fields turns each trace_id in a log line into a link to the trace. That link between logs and traces is what lesson 1 called observability.

Sampling

A busy service handles thousands of requests a second, each with dozens of spans. Keeping all of them costs a lot of network, storage and money, and you'd never read most of them. So traces are sampled:

  • Head sampling decides at the start of the request, in the first service, for example "keep 10%". The decision travels in the traceparent flags, so every service agrees. It's simple and cheap, but it throws away 90% of the slow and failed requests too.
  • Tail sampling decides after the whole trace has finished, in the Collector (the tail_sampling processor). It can keep every trace with an error or a duration above 1 second, and only a few per cent of the fast, healthy ones. It's better, and it needs a Collector that sees all spans of a trace and the memory to hold them for a few seconds.

Start with head sampling at a rate you can afford. Move to tail sampling when you keep missing the interesting traces.

Jaeger, Tempo or a vendor

Jaeger is the classic open-source backend. Grafana Tempo stores traces cheaply in object storage and sits next to Loki and Prometheus in Grafana. Datadog, New Relic, Dynatrace and Honeycomb all accept OTLP too. Instrument with OpenTelemetry and the choice stays a config change.

In production

Run the Collector as an agent on each node (or a sidecar) plus a central gateway for tail sampling. Strip personal data from span attributes in the Collector. Put trace IDs in every log line and return them in an X-Request-Id or traceparent response header, so support can turn a customer's complaint into a trace in seconds.

⌘
Mini mission

Explain one slow request

Run Jaeger and HotROD, book four rides, and open the slowest trace. Write down: its total duration, the number of services and spans, the single slowest span and its share of the total, any spans marked as errors and their message, and one change that would make the request faster.

Input: http://localhost:16686 and curl 'localhost:8080/dispatch?customer=123'Output: ~/trace-notes.txt
Reveal one safe solution
$ for c in 123 392 731 567; do curl -s -o /dev/null -w "%{time_total}s\n" "localhost:8080/dispatch?customer=$c"; done
$ curl -s localhost:16686/api/traces/<trace-id> > ~/trace.json   # the trace ID from Jaeger or a log line
for c in 123 392 731 567; do curl -s -o /dev/null -w "%{time_total}s\n" "localhost:8080/dispatch?customer=$c"; done
  • forloop over a list
  • dostart of what the loop runs each time
  • curlmake an HTTP request
  • -ssilent: no progress bar
  • -owrite the body to this file
  • -wprint these details after the request
  • doneend of the loop
curl -s localhost:16686/api/traces/<trace-id> > ~/trace.json
  • >write output to a file, replacing it

On the lab the slowest trace took 960 ms across 6 services and 40 spans. The MySQL SQL SELECT took 330 ms (about 34%). Three GetDriver spans failed with redis timeout. The best fix: fetch the drivers from Redis in one batched call instead of fourteen calls in a row. Your numbers will differ a little, the pattern won't.

Try it yourself

0 of 6 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 5

What is a span?

Go deeper

Official reading for this lesson

  • DocsOpenTelemetry concepts (traces and spans)OpenTelemetry ↗
  • DocsContext propagationOpenTelemetry ↗
  • DocsW3C Trace Context (traceparent)W3C ↗
  • DocsOpenTelemetry CollectorOpenTelemetry ↗
  • DocsZero-code instrumentationOpenTelemetry ↗
  • DocsJaeger getting startedJaeger ↗
  • ToolHotROD demo applicationJaeger on GitHub ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

← PreviousCentralised logs with LokiNext lesson →Monitoring Kubernetes