Spring Boot 3.5.9 application with OpenTelemetry instrumentation using the Java Agent approach.
This example uses OpenTelemetry Java Agent (zero-code instrumentation):
- Attach:
-javaagent:/path/to/opentelemetry-javaagent.jar - No code changes or OTel dependencies required
- Configuration via environment variables only
- Automatic instrumentation of 150+ libraries
- ✅ HTTP requests and responses (Spring MVC)
- ✅ MongoDB queries and operations
- ✅ JVM metrics (memory, threads, GC)
- ✅ Distributed trace propagation (W3C)
- ✅ Logback logging with trace correlation
- ✅ Custom methods with
@WithSpanannotations
Advantages:
- Zero code changes required
- No dependency management needed
- Works with legacy applications
- Comprehensive auto-instrumentation
Limitations:
- Configuration via environment variables only (no application.properties support)
- Not compatible with GraalVM native-image
- May conflict with other JVM agents
Add custom spans using @WithSpan annotations for deeper application visibility.
-
Add dependency to
build.gradle:implementation 'io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:2.10.0' -
Enable annotations (already configured in
compose.yaml):OTEL_INSTRUMENTATION_ANNOTATIONS_ENABLED: true
-
Annotate methods:
import io.opentelemetry.instrumentation.annotations.WithSpan; @WithSpan("UserService.save") public User save(User user) { return userRepository.save(user); }
- Docker Desktop or Docker Engine with Compose
- Base14 Scout credentials (setup guide)
- Java 25+ (only for local development without Docker)
# Clone and navigate
git clone https://github.com/base-14/examples.git
cd examples/java/spring-boot-java25-mongodb-java-agent
# Set Base14 Scout credentials as environment variables
export SCOUT_ENDPOINT=https://your-tenant.base14.io/v1/traces
export SCOUT_CLIENT_ID=your_client_id
export SCOUT_CLIENT_SECRET=your_client_secret
export SCOUT_TOKEN_URL=https://your-tenant.base14.io/oauth/token
# Start application (MongoDB + Spring Boot + OTel Collector)
docker-compose up --build -d
# Verify it's running
curl http://localhost:8080/actuator/health
curl http://localhost:8080/users/testMessageThe app runs on port 8080, MongoDB on 27017, OTel Collector on 4317/4318.
Check the application logs for the agent startup message:
docker logs spring-app | grep otel.javaagentYou should see: [otel.javaagent] OpenTelemetry Javaagent 2.10.0
The OpenTelemetry Collector requires Base14 Scout credentials to export
telemetry data. Set these before running docker-compose up:
| Variable | Required | Description |
|---|---|---|
SCOUT_ENDPOINT |
Yes | Base14 Scout OTLP endpoint |
SCOUT_CLIENT_ID |
Yes | OAuth2 client ID from Base14 Scout |
SCOUT_CLIENT_SECRET |
Yes | OAuth2 client secret from Base14 Scout |
SCOUT_TOKEN_URL |
Yes | OAuth2 token endpoint |
Example:
export SCOUT_ENDPOINT=https://your-tenant.base14.io/v1/traces
export SCOUT_CLIENT_ID=your_client_id
export SCOUT_CLIENT_SECRET=your_client_secret
export SCOUT_TOKEN_URL=https://your-tenant.base14.io/oauth/tokenSee the Base14 Collector Setup Guide for obtaining credentials.
Java Agent uses environment variables only for configuration:
| Variable | Default | Description |
|---|---|---|
OTEL_SERVICE_NAME |
java-spring-boot-otel-mongodb |
Service identifier |
OTEL_EXPORTER_OTLP_PROTOCOL |
http/protobuf |
OTLP protocol |
OTEL_EXPORTER_OTLP_ENDPOINT |
http://otel-collector:4318 |
Collector URL |
OTEL_TRACES_EXPORTER |
otlp |
Trace exporter |
OTEL_METRICS_EXPORTER |
otlp |
Metrics exporter |
OTEL_LOGS_EXPORTER |
otlp |
Logs exporter |
OTEL_INSTRUMENTATION_ANNOTATIONS_ENABLED |
true |
@WithSpan support |
Automatically included in telemetry:
service.name=java-spring-boot-otel-mongodb
service.namespace=base14
service.version=0.0.1-SNAPSHOT
deployment.environment=dev| Method | Endpoint | Description |
|---|---|---|
GET |
/users/ |
List all users |
POST |
/users/saveUser |
Create user |
PUT |
/users/{id} |
Update user |
DELETE |
/users/{id} |
Delete user |
GET |
/users/testMessage |
Test endpoint |
# Create user
curl -X POST http://localhost:8080/users/saveUser \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","address":"123 Main St"}'
# List users
curl http://localhost:8080/users/| Endpoint | Purpose |
|---|---|
/actuator/health |
Health status |
/actuator/metrics |
Metrics list |
/actuator/prometheus |
Prometheus format |
Requires MongoDB and OTel Collector running locally on standard ports.
Download the Java Agent first:
wget https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v2.22.0/opentelemetry-javaagent.jarThen run with the agent:
# Set required environment variables
export OTEL_SERVICE_NAME=java-spring-boot-otel-mongodb
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_TRACES_EXPORTER=otlp
export OTEL_METRICS_EXPORTER=otlp
export OTEL_LOGS_EXPORTER=otlp
export SPRING_DATA_MONGODB_URI=mongodb://localhost:27017/company
# Build and run
./gradlew build
java -javaagent:./opentelemetry-javaagent.jar -jar build/libs/demo-0.0.1-SNAPSHOT.jarUpdate application.properties to use mongodb://localhost:27017/company.
docker-compose up --build # Build and start
docker-compose down # Stop all
docker-compose down -v # Stop and remove volumes
docker logs spring-app -f # View logs
docker-compose restart spring-service # Restart app onlyRun only MongoDB in Docker, app locally for quick iteration:
docker-compose up mongodb -d # Start MongoDB only
# Run app locally with Java Agent (see above)- HTTP requests (method, URL, status)
- MongoDB queries and operations
- Exceptions and stack traces
- HTTP: Request count, duration, errors
- JVM: Memory, GC, threads
- MongoDB: Connection pool, operation duration
- Process: CPU, file descriptors
All logs include trace_id and span_id for correlation.
Check startup logs for the agent message:
docker logs spring-app | grep otel.javaagentExpected: [otel.javaagent] OpenTelemetry Javaagent 2.22.0
If missing, verify the agent path in Dockerfile and ENTRYPOINT.
docker ps | grep otel-collector # Check collector is running
docker logs otel-collector # View collector logsEnsure OTEL_EXPORTER_OTLP_ENDPOINT points to the correct collector address.
docker logs spring-app | grep -i otel # Check app logsVerify:
- Java Agent loaded successfully
- Environment variables are set correctly
- Collector is receiving data (check collector debug logs)
docker-compose ps # Check MongoDB health
docker logs mongodb # View MongoDB logsWait for MongoDB healthcheck to complete before starting the app.
Add to compose.yaml environment:
environment:
OTEL_LOG_LEVEL: DEBUG| Component | Version |
|---|---|
| Spring Boot | 3.5.9 |
| OpenTelemetry Java Agent | 2.23.0 |
| MongoDB | 7.0 |
| OTel Collector | 0.144.0 |
| Gradle | 9.2.1 |
| Java | 25 |
- Java Agent Approach Guide - Base14 documentation
- OpenTelemetry Java Agent - Official Java Agent docs
- Spring Boot Actuator - Actuator reference
- Base14 Scout - Observability platform