SteelReed

Search

Search posts, questions…

6. DevOps & Operations

Distributed tracing with OpenTelemetry: traces, spans, traceparent and sampling

How to debug a request that crosses a gateway, booking, payment and Kafka: traces and spans, the W3C traceparent header, choosing between Spring Boot 4's spring-boot-starter-opentelemetry, the Java agent and the OTel starter, OTLP and the Collector, head vs tail sampling with a Collector config that was actually run, linking logs to traces, and reviewing AI-written tracing config.

18 Nov 20267 min readSenior

–

1. The problem

A customer says booking takes 3 seconds. The request goes gateway → booking-service → payment-service → Kafka → notification-service; five services write five separate log streams, and no line says which request it belongs to.

2. Key idea

Each service only sees its own part. You need an id that follows the request across every boundary, with each step recording its parent: like a parcel's waybill, where every depot stamps in and out times plus the waybill number. The header standard is W3C Trace Context, so Go, Node.js, .NET and proxies all read the same traceparent.

Boundary Context travels via Usually breaks because
HTTP, gRPC the traceparent header hand-built headers, clients created with new and no instrumentation
Another thread in the process a wrapped ThreadLocal @Async, unwrapped custom executors
Kafka, RabbitMQ message headers a hand-written outbox relay, batch consumers

2.1 What to instrument with, how much to keep?

Choice Use when Don't use when
spring-boot-starter-opentelemetry Spring Boot 4 apps, config in application.yml you need spans for libraries without Micrometer Observation support
The OTel Java agent broad library coverage, no code changes native images, another APM agent already present
Head sampling parentbased always on, rate set only at the gateway every error trace must be kept
Tail sampling in the Collector keep every error, slow and payment trace a few hundred requests/s: keeping 100% with short retention is cheaper

One instrumentation per service: the agent plus a starter means duplicate spans and two trace ids per request. Span names are route templates (GET /bookings/{id}); ids go in attributes.

3. In Spring Boot

YAML
# build: org.springframework.boot:spring-boot-starter-opentelemetry
spring:
  application:
    name: booking-service            # becomes service.name
  task:
    execution:
      propagate-context: true        # @Async carries the trace
management:
  tracing:
    sampling:
      probability: 0.1               # 10% by default, applies to root spans only
  opentelemetry:
    tracing:
      export:
        otlp:
          endpoint: http://otel-collector:4318/v1/traces

Logback logs include [traceId-spanId]. Build HTTP clients from the injected RestClient.Builder, never new. Other languages: Go uses go.opentelemetry.io/otel and otelhttp; Node.js has @opentelemetry/auto-instrumentations-node.

Tail sampling and running the Collector
  • Tail sampling decides after a trace's spans have arrived, so every span of a trace must reach the same Collector. Two tiers: agent Collectors (sidecar, DaemonSet) with a loadbalancing exporter keyed on traceID, then a group of Collectors running tail_sampling.
  • SDKs must send 100% of spans to the Collector: on Spring Boot 4 set probability: 1.0, since the default is 10%.
  • A common policy: keep 100% of error traces, 100% above 2 seconds, 100% touching payment-service, 5% of the rest.
  • Watch otelcol_processor_tail_sampling_* metrics and Collector memory; under overload add head sampling at the gateway.
  • A redaction processor strips emails and tokens from attributes before storage.

4. Working with AI

Hand to AI: Collector config, test traffic (one failing, one slow, one successful request), summarizing a long waterfall into "slow where, waiting on whom".

Check yourself in AI-written code:

  • One instrumentation per service; never -javaagent plus a starter.
  • No hand-built traceparent; HTTP clients come from instrumented builders.
  • Span names are route templates without ids.
  • A parentbased sampler everywhere; you know the default is 10% (Spring) or 100% (agent).
  • @Async, custom executors and outbox relays all carry context.

5. Exercises

Exercise 1: Review a traceparent forwarding filter

The gateway runs no agent. The AI wrote a filter that "keeps the trace across every service": read the incoming traceparent, generate one if missing, and send that exact header to booking-service. The gateway receives 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01. What should the outgoing header look like, and where is the filter wrong?

Solution 1
  • Trace id 4bf92f3577b34da6a3ce929d0e0e4736, parent span 00f067aa0ba902b7, flags 01: sampled.
  • The gateway must create its own span with a new span id, e.g. 53995c3f42cd8ad8, and send 00-4bf92f3577b34da6a3ce929d0e0e4736-53995c3f42cd8ad8-01: same trace id, with the gateway's span as parent.
  • Forwarding the header unchanged gives booking-service the wrong parent, so the span tree is missing the gateway level. Fix: use W3CTraceContextPropagator or run the agent; never build the string by hand.
  • An all-zero parent id is invalid: the header is ignored and a new trace starts.

Exercise 2: Read a trace broken at Kafka

In the UI, the POST /bookings trace stops at the send booking-created span; notification-service's work shows up as a separate trace. It started after the AI "cleaned up" message sending into an outbox relay. List the causes and how to check.

Solution 2
  • Messages carry no traceparent: check with kafka-console-consumer --property print.headers=true. The outbox relay reads the DB and sends, so the original request's context is gone. Fix: store traceparent in the outbox table on write, and put it back in the header on send.
  • notification-service has Kafka instrumentation disabled: check its startup logs.
  • Batch consumers: one span processes messages from many traces, so instrumentation uses span links instead of parent–child; follow the link to the original trace.

Summary

  • A trace is the journey, a span is one piece with a parent; traceparent carries context over HTTP and messages.
  • Context is lost across threads, brokers and outboxes unless carried.
  • One instrumentation per service; span names are route templates.
  • Head sampling is cheap but misses errors; tail sampling needs routing by trace id.
  • Spring Boot 4 keeps 10% by default; tail sampling needs SDKs sending 100%.

Further reading: Observability for LLM calls.

Practice interview questions on this topic

All DevOps & Cloud questions →

Loading comments…
Next on the roadmapWeb attacks and the OWASP Top 10: MITM, SQL injection, XSS, CSRF, DDoS