I’ve been exploring the OpenTelemetry documentation and see that there are two primary methods for enabling OpenTelemetry in Java applications: using the Java Agent Bytecode approach or integrating with Spring Boot Native.
Apart from the benefits mentioned for Spring Boot integration, are there additional advantages? Specifically, I was expecting to see built-in spans and metrics for various Spring Boot modules such as Kafka, Redis, and Spring Data.
Is there a way to capture internal metrics from the Spring Boot framework itself using OpenTelemetry?
If you’re looking to obtain internal metrics from the Spring Boot framework and leverage OpenTelemetry for enhanced observability, you’re in the right place. OpenTelemetry provides several benefits when integrated with Spring Boot, including automatic instrumentation and detailed visibility into your application. Here’s a detailed guide on how to achieve this:
1. Add Dependencies
To start using OpenTelemetry with Spring Boot, you’ll need to include the necessary dependencies. For Maven, add the following to your pom.xml
:
<dependencies>
<!-- OpenTelemetry Spring Boot Starter -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-spring-boot-starter</artifactId>
<version>1.16.0</version>
</dependency>
<!-- Spring Boot Actuator for metrics -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
For Gradle, add the following to your build.gradle
:
dependencies {
// OpenTelemetry Spring Boot Starter
implementation 'io.opentelemetry:opentelemetry-spring-boot-starter:1.16.0'
// Spring Boot Actuator
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}
2. Configure Actuator Metrics
Ensure that Spring Boot Actuator is configured to expose metrics. Update your application.properties
or application.yml
as follows:
application.properties
# Expose all actuator endpoints
management.endpoints.web.exposure.include=*
# Enable metrics endpoint
management.endpoint.metrics.enabled=true
application.yml
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
metrics:
enabled: true
3. Configure OpenTelemetry
Configure OpenTelemetry to export metrics to your chosen backend. This can be done using environment variables or configuration files. For example, to export to Jaeger:
# Example environment variables for exporting to Jaeger
OTEL_EXPORTER_JAEGER_ENDPOINT=http://localhost:14250
OTEL_TRACES_EXPORTER=jaeger
Refer to the OpenTelemetry documentation for detailed configuration options.
4. Benefits and Types of Metrics
Automatic Instrumentation: OpenTelemetry automatically instruments a wide range of Spring Boot components and popular libraries. Benefits include:
- Spring Web (MVC and WebFlux): Captures spans for HTTP requests, including request processing times and response statuses.
- Spring Data: Provides instrumentation for repository methods, capturing query execution times and database interactions.
Integration with Popular Libraries:
- Kafka: Use
opentelemetry-instrumentation-kafka-clients
to get spans for Kafka producer and consumer operations. Metrics related to message production and consumption are also captured. - Redis: Using
opentelemetry-instrumentation-redis
, spans are created for Redis commands, including execution times and errors. Metrics related to Redis operations are available. - JDBC: The
opentelemetry-instrumentation-jdbc
library captures spans for JDBC operations, including query execution times and connection pool metrics.
Enhanced Visibility:
- Distributed Tracing: Track requests as they move through various services and components.
- Context Propagation: Ensures trace context is carried across service boundaries, aiding in the correlation of logs, metrics, and traces.
Custom Metrics and Spans:
You can also define custom spans and metrics if needed. For instance, to create a custom span:
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
public class CustomService {
private static final Tracer tracer = OpenTelemetry.getGlobalTracer("my-tracer");
public void performOperation() {
Span span = tracer.spanBuilder("customOperation").startSpan();
try {
// Your custom logic here
} finally {
span.end();
}
}
}
Rich Metrics Exposure:
Spring Boot Actuator, combined with OpenTelemetry, provides detailed metrics such as:
- JVM Metrics: Memory usage, garbage collection pauses, thread counts.
- HTTP Requests: Request rates, response times, error rates.
- Custom Application Metrics: Metrics defined using Micrometer, bridged to OpenTelemetry.
5. Access Metrics
You can access internal Spring Boot metrics via Actuator endpoints:
- HTTP Request Metrics:
http://localhost:8080/actuator/metrics/http.server.requests
- JVM Metrics:
http://localhost:8080/actuator/metrics/jvm.memory.used
,http://localhost:8080/actuator/metrics/jvm.gc.pause
- Custom Metrics: Available via the Actuator metrics endpoint.
Here’s how you can query HTTP request metrics:
curl http://localhost:8080/actuator/metrics/http.server.requests
Summary
- Automatic Instrumentation: OpenTelemetry provides automatic spans and metrics for Spring Boot modules and popular libraries with minimal configuration.
- End-to-End Observability: Gain comprehensive visibility into request flows, context propagation, and system performance.
- Custom Metrics and Spans: Flexibility to define additional metrics and spans tailored to your needs.
- Rich Metrics Exposure: Detailed metrics from Spring Boot Actuator, including JVM and custom metrics.
By integrating OpenTelemetry with Spring Boot, you can achieve robust observability and monitoring capabilities, capturing internal framework metrics and providing insights into application performance and behavior.
References
- OpenTelemetry Spring Boot Starter Documentation
- Spring Boot Actuator Documentation
- OpenTelemetry Java Instrumentation
- Micrometer Integration with OpenTelemetry