Spring Boot Actuator provides dependency management and auto-configuration for Micrometer Tracing, a facade for popular tracer libraries.
Tip
|
To learn more about Micrometer Tracing capabilities, see its reference documentation. |
Spring Boot ships auto-configuration for the following tracers:
-
OpenTelemetry with Zipkin, Wavefront, or OTLP
-
OpenZipkin Brave with Zipkin or Wavefront
We need an example application that we can use to get started with tracing. For our purposes, the simple “Hello World!” web application that’s covered in the “getting-started.adoc” section will suffice. We’re going to use the OpenTelemetry tracer with Zipkin as trace backend.
To recap, our main application code looks like this:
code:MyApplication
Note
|
There’s an added logger statement in the home() method, which will be important later.
|
Now we have to add the following dependencies:
-
org.springframework.boot:spring-boot-starter-actuator
-
io.micrometer:micrometer-tracing-bridge-otel
- bridges the Micrometer Observation API to OpenTelemetry. -
io.opentelemetry:opentelemetry-exporter-zipkin
- reports traces to Zipkin.
Add the following application properties:
management:
tracing:
sampling:
probability: 1.0
By default, Spring Boot samples only 10% of requests to prevent overwhelming the trace backend. This property switches it to 100% so that every request is sent to the trace backend.
To collect and visualize the traces, we need a running trace backend. We use Zipkin as our trace backend here. The Zipkin Quickstart guide provides instructions how to start Zipkin locally.
After Zipkin is running, you can start your application.
If you open a web browser to http://localhost:8080
, you should see the following output:
Hello World!
Behind the scenes, an observation has been created for the HTTP request, which in turn gets bridged to OpenTelemetry, which reports a new trace to Zipkin.
Now open the Zipkin UI at http://localhost:9411
and press the "Run Query" button to list all collected traces.
You should see one trace.
Press the "Show" button to see the details of that trace.
Correlation IDs provide a helpful way to link lines in your log files to spans/traces. If you are using Micrometer Tracing, Spring Boot will include correlation IDs in your logs by default.
The default correlation ID is built from traceId
and spanId
MDC values.
For example, if Micrometer Tracing has added an MDC traceId
of 803B448A0489F84084905D3093480352
and an MDC spanId
of 3425F23BB2432450
the log output will include the correlation ID [803B448A0489F84084905D3093480352-3425F23BB2432450]
.
If you prefer to use a different format for your correlation ID, you can use the configprop:logging.pattern.correlation[] property to define one. For example, the following will provide a correlation ID for Logback in format previously used by Spring Cloud Sleuth:
logging:
pattern:
correlation: "[${spring.application.name:},%X{traceId:-},%X{spanId:-}] "
include-application-name: false
Note
|
In the example above, configprop:logging.include-application-name[] is set to false to avoid the application name being duplicated in the log messages (configprop:logging.pattern.correlation[] already contains it).
It’s also worth mentioning that configprop:logging.pattern.correlation[] contains a trailing space so that it is separated from the logger name that comes right after it by default.
|
To automatically propagate traces over the network, use the auto-configured RestTemplateBuilder
or WebClient.Builder
to construct the client.
Warning
|
If you create the WebClient or the RestTemplate without using the auto-configured builders, automatic trace propagation won’t work!
|
As Micrometer Tracer supports multiple tracer implementations, there are multiple dependency combinations possible with Spring Boot.
All tracer implementations need the org.springframework.boot:spring-boot-starter-actuator
dependency.
Tracing with OpenTelemetry and reporting to Zipkin requires the following dependencies:
-
io.micrometer:micrometer-tracing-bridge-otel
- bridges the Micrometer Observation API to OpenTelemetry. -
io.opentelemetry:opentelemetry-exporter-zipkin
- reports traces to Zipkin.
Use the management.zipkin.tracing.*
configuration properties to configure reporting to Zipkin.
Tracing with OpenTelemetry and reporting to Wavefront requires the following dependencies:
-
io.micrometer:micrometer-tracing-bridge-otel
- bridges the Micrometer Observation API to OpenTelemetry. -
io.micrometer:micrometer-tracing-reporter-wavefront
- reports traces to Wavefront.
Use the management.wavefront.*
configuration properties to configure reporting to Wavefront.
Tracing with OpenTelemetry and reporting using OTLP requires the following dependencies:
-
io.micrometer:micrometer-tracing-bridge-otel
- bridges the Micrometer Observation API to OpenTelemetry. -
io.opentelemetry:opentelemetry-exporter-otlp
- reports traces to a collector that can accept OTLP.
Use the management.otlp.tracing.*
configuration properties to configure reporting using OTLP.
Tracing with OpenZipkin Brave and reporting to Zipkin requires the following dependencies:
-
io.micrometer:micrometer-tracing-bridge-brave
- bridges the Micrometer Observation API to Brave. -
io.zipkin.reporter2:zipkin-reporter-brave
- reports traces to Zipkin.
Note
|
If your project doesn’t use Spring MVC or Spring WebFlux, the io.zipkin.reporter2:zipkin-sender-urlconnection dependency is needed, too.
|
Use the management.zipkin.tracing.*
configuration properties to configure reporting to Zipkin.
Tracing with OpenZipkin Brave and reporting to Wavefront requires the following dependencies:
-
io.micrometer:micrometer-tracing-bridge-brave
- bridges the Micrometer Observation API to Brave. -
io.micrometer:micrometer-tracing-reporter-wavefront
- reports traces to Wavefront.
Use the management.wavefront.*
configuration properties to configure reporting to Wavefront.
A TracingAwareMeterObservationHandler
is automatically registered on the ObservationRegistry
, which creates spans for every completed observation.
You can create your own spans by starting an observation.
For this, inject ObservationRegistry
into your component:
code:CustomObservation
This will create an observation named "some-operation" with the tag "some-tag=some-value".
Tip
|
If you want to create a span without creating a metric, you need to use the lower-level Tracer API from Micrometer.
|
You can create baggage with the Tracer
API:
code:CreatingBaggage
This example creates baggage named baggage1
with the value value1
.
The baggage is automatically propagated over the network if you’re using W3C propagation.
If you’re using B3 propagation, baggage is not automatically propagated.
To manually propagate baggage over the network, use the configprop:management.tracing.baggage.remote-fields[] configuration property (this works for W3C, too).
For the example above, setting this property to baggage1
results in an HTTP header baggage1: value1
.
If you want to propagate the baggage to the MDC, use the configprop:management.tracing.baggage.correlation.fields[] configuration property.
For the example above, setting this property to baggage1
results in an MDC entry named baggage1
.
Tracing components which are reporting data are not auto-configured when using @SpringBootTest
.
See the testing section for more details.