Operations 6 min read

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.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Mastering Observability in Spring Boot 4 with OpenTelemetry: A Step‑by‑Step Guide

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/metrics

The Micrometer metrics generated by the Spring Boot application will be sent to any OTLP‑compatible backend (e.g., Grafana, Jaeger, Tempo).

Grafana metrics view
Grafana metrics view

Configuring Traces Export

Define the tracing endpoint similarly:

management.opentelemetry.tracing.export.otlp.endpoint=http://localhost:4318/v1/traces

Traces collected by the OpenTelemetry starter will be exported to the configured OTLP collector.

Grafana traces view
Grafana traces view

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/logs

Logs are now exported via OTLP to the configured backend.

Grafana logs view
Grafana logs view

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.

observabilityMetricsOpenTelemetryloggingSpring BootTracingOTLP
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.