Sub topic 14
Observations and distributed tracing
This chapter assumes you know how to expose Actuator endpoints and read /actuator/metrics. It also assumes you know how to register a custom Timer with MeterRegistry. You need Spring Boot 3.x, which includes Micrometer Observation and Micrometer Tracing.
From metrics to observations
A metric tells you that something is slow. It does not tell you which request was slow, or which downstream service caused the delay. Distributed tracing fills that gap.
Micrometer's Observation API lets you instrument code once. Registered handlers then turn that single observation into several signals:
- A timer metric, which you can read at
/actuator/metricsand/actuator/prometheus. - A trace span, which a tracing backend such as Zipkin collects.
- Log correlation, which adds the trace ID to log lines.
Spring Boot already creates observations for incoming HTTP requests, RestClient, RestTemplate, and WebClient calls when you build them from the auto-configured builders. You usually only add your own for business operations.
Key terms
- A trace is the full journey of one request across services.
- A span is one timed step inside a trace. Each span has a trace ID shared across the whole trace and its own span ID.
- Context propagation means passing the trace ID to the next service, normally in HTTP headers, so its spans join the same trace.
Adding tracing
Add a tracer bridge and a reporter. This example uses the OpenTelemetry bridge with Zipkin export. Spring Boot manages the versions.
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
</dependency>Then configure sampling and the backend address:
management:
tracing:
sampling:
probability: 1.0
zipkin:
tracing:
endpoint: http://localhost:9411/api/v2/spansThe default sampling probability is 0.1, so only about one request in ten is traced. Setting 1.0 is useful in development. In production, a lower value limits the overhead and storage cost.
Creating your own observation
Inject ObservationRegistry and wrap the work you want to measure.
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import org.springframework.stereotype.Service;
@Service
public class InvoiceService {
private final ObservationRegistry registry;
public InvoiceService(ObservationRegistry registry) {
this.registry = registry;
}
public String createInvoice(String customerId) {
return Observation.createNotStarted("invoice.create", registry)
.lowCardinalityKeyValue("channel", "web")
.highCardinalityKeyValue("customer.id", customerId)
.observe(() -> "invoice-for-" + customerId);
}
}After a call, /actuator/metrics/invoice.create shows a timer, and the tracing backend shows a span named invoice.create.
Low and high cardinality
The two kinds of key-value pairs behave differently:
| Kind | Added to metrics | Added to spans | Use for |
|---|---|---|---|
| Low cardinality | Yes, as tags | Yes | A small, fixed set of values, such as channel or outcome |
| High cardinality | No | Yes | Unbounded values, such as user ID or order ID |
Every distinct tag value creates a new time series. Putting a customer ID in a metric tag can overload your metrics system. Use highCardinalityKeyValue for values like that.
Correlating logs
Spring Boot 3.2 and later adds the trace ID and span ID to the log line when a tracer is present. Search your logs for a trace ID from Zipkin to see every message that belongs to that request.
Exercise
A teammate adds .lowCardinalityKeyValue("order.id", orderId) to an observation. Why is this a problem, and what should they use instead?
Show answer
Low cardinality values become metric tags, and every distinct order ID would create a new time series, which can overload the metrics system. They should use highCardinalityKeyValue("order.id", orderId), which attaches the value to the trace span only.