This guide covers Spring Boot applications running on ECS, EC2, Kubernetes, or any container/VM environment. For Java on AWS Lambda, see the Java Lambda guide.
Introduction
You can instrument Spring Boot applications with OpenTelemetry using one of two approaches:- OpenTelemetry Java Agent — uses bytecode instrumentation and provides the broadest out-of-the-box coverage, but has limitations around configuration and integration with other agents.
-
OpenTelemetry Spring Boot Starter — A lightweight, flexible alternative to the Java Agent. Choose this if any of the following apply:
- You are using Spring Boot Native Image, which the Java agent does not support
- The Java agent’s startup overhead exceeds your requirements
- You are already using another Java monitoring agent, which may conflict with the Otel Java agent
1. Add the OpenTelemetry Spring Boot Starter
Add the following dependencies to your project:2. Set Application Properties
Add these properties to yourapplication.properties file. Replace YOUR_TRACE0_ENV_API_KEY with the API key from your Trace0 environment.
3. Add Standalone Instrumentation Libraries
As discussed above, the Spring Boot Starter does not instrument most libraries out of the box. To instrument additional libraries and frameworks, you need to add the corresponding OpenTelemetry instrumentation libraries to your application. This ensures you receive spans from those libraries and gain full visibility into what is happening inside your application. The example below shows how to add instrumentation for the AWS SDK.DynamoDB client for instrumentation.
4. Add a Global Exception Handler
By default, unhandled exceptions in Spring Boot are caught by Spring’s error handling mechanism after the OpenTelemetry trace context has closed. This means the error is never attached to the trace, making it invisible in the Trace0 trace. Adding a@RestControllerAdvice global exception handler ensures the exception is caught and logged within the active request trace, so the error and stack trace are correctly correlated to the right trace.