Mastering Observability in Spring Boot 4 with OpenTelemetry: A Step‑by‑Step Guide
Spring Boot 4 introduces an official OpenTelemetry starter that simplifies the collection, processing, and export of metrics, traces, and logs, and this guide walks you through adding dependencies, configuring OTLP endpoints for Grafana, Jaeger, and other backends, and setting up Logback for log export.
Observability in Cloud‑Native Applications
Modern cloud‑native systems require unified, standardised collection of metrics, traces and logs. Without observability it is impossible to understand system behaviour across distributed components.
OpenTelemetry and Spring Boot 4
OpenTelemetry (OTel) provides a vendor‑neutral API, SDK and the OTLP protocol for exporting data. Spring Boot 4.0 introduces an official OpenTelemetry Starter that integrates Micrometer with OTel, enabling automatic collection of metrics, traces and logs.
Adding the Starter Dependency
Include the starter in your build:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-opentelemetry</artifactId>
</dependency> implementation 'org.springframework.boot:spring-boot-starter-opentelemetry'Configuring Metrics Export
Set the OTLP endpoint for metrics in application.properties or application.yml:
management.otlp.metrics.export.url=http://localhost:4318/v1/metricsThe Micrometer metrics generated by the Spring Boot application will be sent to any OTLP‑compatible backend (e.g., Grafana, Jaeger, Tempo).
Configuring Traces Export
Define the tracing endpoint similarly:
management.opentelemetry.tracing.export.otlp.endpoint=http://localhost:4318/v1/tracesTraces collected by the OpenTelemetry starter will be exported to the configured OTLP collector.
Configuring Logs Export
Log export requires the OpenTelemetry Logback appender.
Add the appender dependency:
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-logback-appender-1.0</artifactId>
<version>2.21.0-alpha</version>
</dependency>Configure Logback (e.g., logback-spring.xml) to use the appender:
<appender name="OTEL" class="io.opentelemetry.instrumentation.logback.appender.v1_0.OpenTelemetryAppender"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="OTEL"/>
</root>Register the OpenTelemetry instance in a Spring component so the appender can access it:
@Component
class InstallOpenTelemetryAppender implements InitializingBean {
private final OpenTelemetry openTelemetry;
InstallOpenTelemetryAppender(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
}
@Override
public void afterPropertiesSet() {
OpenTelemetryAppender.install(this.openTelemetry);
}
}Set the logging endpoint:
management.opentelemetry.logging.export.otlp.endpoint=http://localhost:4318/v1/logsLogs are now exported via OTLP to the configured backend.
OpenTelemetry Data Flow Overview
+-----------------+ +-----------------+ +-----------------+
| Spring Boot App | --> | OpenTelemetry | --> | OTel Backend |
| (Metrics/Traces/Logs) | Collector/SDK | | Grafana, Jaeger, Tempo, … |
+-----------------+ +-----------------+ +-----------------+The Spring Boot application generates observability data via Micrometer and the OpenTelemetry starter. The OpenTelemetry SDK/Collector converts the data to OTLP and forwards it to any compatible backend for storage and visualisation.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
