Using OpenTelemetry in Spring Boot 4 to monitor your application
Follow a Spring Boot request across a service method, a worker thread, and an HTTP call using OpenTelemetry and Jaeger.
When an API gets slow, the request duration tells you how long the client waited, but it is very hard to determine why your application is slow. This is where OpenTelemetry and traces can help to figure out where in the application time was wasted. And when part of the work happens on another thread, you need to carry the trace context across that handoff to see how the work fits together.
In my earlier post on OpenTelemetry, I set up the Java SDK by hand. This time I’ll use Spring Boot 4.2 and follow a quote request through a service method, an asynchronous task, and an outgoing HTTP call. We’ll send traces and metrics through the same OpenTelemetry Collector and look at the request in Jaeger.
The code for this post can be found: gjong/demo-spring-otel
For this article the following flow is expected in both the application and the reporting of tracing and metrics.
Setting up the project
The example uses Java 21, Spring MVC, and Actuator. Add Spring Boot’s OpenTelemetry starter to export telemetry over OTLP. We also use the RestClient starter for the outgoing HTTP calls; simulating any interaction your application may have with the outside world. The last relevant dependency is the AspectJ starter; which is needed to allow Micrometer’s observability annotations to work as designed.
1<dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-opentelemetry</artifactId>
4</dependency>
5<dependency>
6 <groupId>org.springframework.boot</groupId>
7 <artifactId>spring-boot-starter-restclient</artifactId>
8</dependency>
9<dependency>
10 <groupId>org.springframework.boot</groupId>
11 <artifactId>spring-boot-starter-aspectj</artifactId>
12</dependency>Sending traces and metrics to the Collector
The application sends both metrics and traces over HTTP to the same Collector.
Traces go to /v1/traces and metrics go to /v1/metrics.
The Collector forwards traces to Jaeger and prints metrics with its debug exporter.
This lets us verify metric delivery while using Jaeger to inspect the trace.
1opentelemetry.collector: http://localhost:4318
2
3management:
4 observations:
5 conventions: open-telemetry
6 annotations.enabled: true
7 opentelemetry:
8 tracing:
9 export:
10 otlp:
11 endpoint: ${opentelemetry.collector}/v1/traces
12 tracing:
13 sampling:
14 probability: 1.0
15 otlp:
16 metrics:
17 export:
18 url: ${opentelemetry.collector}/v1/metrics
19 step: 10sThe example samples every locally initiated trace and exports metrics every ten seconds so we can inspect both with a few requests.
The signal-specific endpoint takes the full URL.
If you enter only http://localhost:4318 there, the trace exporter sends requests to the wrong path.
See more on the Spring’s OpenTelemetry guide.
Adding spans to the quote service
In the application an endpoint is exposed under quotes/{sku} that calls several internal methods.
This will simulate a real live scenario.
1@GetMapping("/quotes/{sku}")
2 Quote quote(@PathVariable String sku)Spring MVC itself deals with the span for GET /quotes/{sku}.
The time spent in the quote service is included in that span.
To show a service method on its own row in Jaeger, add @Observed to the method.
1@Observed
2public void prepareQuote() {
3 log.info("Preparing quote");
4}The controller calls this method on an injected Spring bean, so the call goes through the proxy that applies the annotation. Micrometer records a child span and a timer; both get exported by Spring to the OpenTelemetry collector. The private helper in the quote service runs within its caller’s span. See more on Spring Boot observability reference.
Following a call across threads
To make the example more real live like the application calls itself using an HTTP call;
simulating your application calling an external service and propagating the trace.
To achieve this we must inject the injected RestClient.Builder of Spring Boot;
to which Spring itself has already wired the needed configuration for trace propagation to work.
The application then calls itself on the /current-date end-point to prove that tracing works across multiple applications.
This gives us both sides of the HTTP call with one Spring Boot process.
1@Async
2@Observed
3public void executeRemoteCall() {
4 restClient.get()
5 .uri("current-date")
6 .retrieve()
7 .toBodilessEntity();
8}@EnableAsync lets Spring run the method on a worker thread.
The request’s trace context lives on the calling thread, so enable context propagation on Spring Boot’s task executor to carry it with the task.
1spring:
2 task:
3 execution:
4 propagate-context: trueOnce the context reaches the worker thread, the instrumented client can create an outgoing HTTP span and send the context in the request headers. Spring MVC uses those headers to connect the receiving server span to the same trace. See also the Spring Boot context propagation official documentation.
Finding the trace in Jaeger
Start the Collector and Jaeger with docker compose up -d from the sample project directory,
then run mvn spring-boot:run.
With the application listening on port 8080, call GET /quotes/BOOK-42.
Open Jaeger, select the spring-boot-otel service, and open the trace for the quote request.
Follow the service spans to the outgoing HTTP call and the receiving GET /current-date span.
To check metrics, run docker compose logs -f collector and look for http.server.request.duration after the next export.
Below is an example of how a trace will look for this demo application in Jaeger:
