Observe Spring Boot app with three pillars of observability: Traces (Tempo), Metrics (Prometheus), Logs (Loki) on Grafana through OpenTelemetry and OpenMetrics.
Observe Spring Boot app with three pillars of observability: Traces (Tempo), Metrics (Prometheus), Logs (Loki) on Grafana through OpenTelemetry and OpenMetrics.
Observe the Spring Boot application with three pillars of observability on Grafana:
This demo project is a Spring Boot version of FastAPI with Observability and is also inspired by Cloud Observability with Grafana and Spring Boot.
Install Loki Docker Driver
docker plugin install grafana/loki-docker-driver:2.9.2 --alias loki --grant-all-permissionsStart all services with docker compose
docker compose up -dSend requests with siege and curl to the Spring Boot app
bash request-script.sh
bash trace.shOr you can send requests with k6:
k6 run --vus 3 --duration 300s k6-script.jsOr send requests from applications' Swagger UI:
Check predefined dashboard Spring Boot Observability on Grafana http://localhost:3000/ and login with default account admin and password admin
Dashboard screenshot:
The dashboard is also available on Grafana Dashboards.
Grafana provides a great solution, which could observe specific actions in service between traces, metrics, and logs through trace ID and exemplar.
Image Source: Grafana
Get Trace ID from an exemplar in metrics, then query in Tempo.
Query with histogram_quantile(.99,sum(rate(http_server_requests_seconds_bucket{application="app-a", uri!="/actuator/prometheus"}[1m])) by(uri, le)) and turn on Exemplars in options.
Get Trace ID and tags (here is compose.service) defined in Tempo data source from span, then query with Loki.
Get Trace ID from log (regex defined in Loki data source), then query in Tempo.
For a more complex scenario, we use three Spring Boot applications with the same code in this demo. There is a cross-service action in /chain endpoint, which provides a good example of how OpenTelemetry Instrumentation work and how Grafana presents trace information.
OpenTelemetry Instrumentation for Java provides a zero-code way to instrument the application by the agent jar as follows:
java -javaagent:path/to/opentelemetry-javaagent.jar -jar myapp.jarThe agent supports a lot of libraries, including Spring Web MVC. According to the document:
It can be used to capture telemetry data at the “edges” of an app or service, such as inbound requests, outbound HTTP calls, database calls, and so on.
So we don't need to modify any line of code in our codebase. The agent will handle everything automatically. In this project, we have three kinds of actions that could be captured by the agent:
HTTP requests: capture HTTP information like request method, status, and so on.
PostgreSQL actions(POST /peanuts and the first request of GET /peanuts/{id}): capture DB information like SQL statement, table name, and so on.
Redis commands(From the second request of GET /peanuts/{id}): capture Redis information like commands, keys, and so on.
The configurations, like the exporter setting, are listed on the document, and are consumed by the agent from one or more of the following sources (ordered from highest to lowest priority):
In this demo project we use environment variables to set the agent configuration:
app-a:
build: ./app/
environment:
- OTEL_EXPORTER_OTLP_ENDPOINT=http://tempo:4317
- OTEL_SERVICE_NAME=app-a
- OTEL_METRICS_EXPORTER=none
- OTEL_LOGS_EXPORTER=none
ports:
- "8080:8080"Or using a configuration file is another common way to set the agent:
# otel.properties
otel.exporter.otlp.endpoint=http://tempo:4317
otel.service.name=app-a
otel.metrics.exporter=none
otel.logs.exporter=none# set otel.javaagent.configuration-file with system properties
java -javaagent:path/to/opentelemetry-javaagent.jar \
-Dotel.javaagent.configuration-file=path/to/otel.properties \
-jar myapp.jarMore configuration details can be found on the official document.
OpenTelemetry Agent will add information to each logging automatically.
OpenTelemetry Agent injects the current span information into each logging event's MDC copy:
- trace_id - the current trace id (same as Span.current().getSpanContext().getTraceId());
- span_id - the current span id (same as Span.current().getSpanContext().getSpanId());
- trace_flags - the current trace flags, formatted according to W3C traceflags format (same as Span.current().getSpanContext().getTraceFlags().asHex()).
Logger MDC auto-instrumentation
In this demo project, we override logging.pattern.level in application.yaml file:
logging:
pattern:
level: "trace_id=%mdc{trace_id} span_id=%mdc{span_id} trace_flags=%mdc{trace_flags} %p"The log would be like this:
2022-10-10 15:18:54.893 trace_id=605c7adf03bdb2f2917206de1eae8f72 span_id=c581b882e2d252c2 trace_flags=01 ERROR 1 --- [nio-8080-exec-6] com.example.app.AppApplication : Hello World!!As previously mentioned, OpenTelemetry Agent can capture telemetry data at the “edges” of an app or service, such as inbound requests, and outbound HTTP calls. We don't need to add anything to our code. To show this, we provide the /chain endpoint in the application as follows:
…When calling app-a chain endpoint(curl localhost:8080/chain), it will send requests to itself root(/) and the other two services' io_task, and cpu_task by order. In the whole process, we don't write any line of code about OpenTelemetry, trace, or span. But the log shows all inbound requests, and outbound HTTP calls were added span information as follows:
…Each endpoint got the same trace_id 743ae05db90d00fd65998ff30cf7094d start from app-a chain. The auto-injected traceparent(could saw in the first line in app-a root log) is how OpenTelemetry Agent passed through all these services.
To get Prometheus metrics from the Spring Boot application, we need two dependencies:
Add these two dependencies to the pom.xml and config to the application.yaml as follows:
org.springframework.boot
spring-boot-starter-actuator
io.micrometer
micrometer-registry-prometheus
management:
endpoints:
web:
exposure:
include: prometheus # only exposure /actuator/prometheus endpoint
metrics:
tags:
application: app # add tag to each prometheus metricThe Prometheus metrics look like this on /actuator/prometheus:
…Exemplar is a new datatype proposed in OpenMetrics. To enable the Exemplar feature, we need to add the micrometer-tracing-bridge-otel dependency, which bridges Micrometer metrics with OpenTelemetry tracing.
io.micrometer
micrometer-tracing-bridge-otel
When the dependency are addressed. We can add a distribution metric to Prometheus metrics in the application.yaml.
management:
metrics:
distribution:
percentiles-histogram:
http:
server:
requests: 'true'As previously mentioned, Exemplar is a new datatype proposed in OpenMetrics, and the default /actuator/prometheus provide metrics with Prometheus format. So we need to add some headers to get the metrics with OpenMetrics format as follows:
curl 'http://localhost:8080/actuator/prometheus' -i -X GET \
-H 'Accept: application/openmetrics-text; version=1.0.0; charset=utf-8'The histogram metrics with trace ID (starts with #) look like this:
…Collects metrics from applications.
Define all Spring Boot applications metrics scrape jobs in etc/prometheus/prometheus.yml.
Prometheus will scrape OpenMetrics format metrics automatically, there is no need to add
No open issues yet, or sync has not completed.