Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

spring-boot-observability

> 后端框架
Open source

Observe Spring Boot app with three pillars of observability: Traces (Tempo), Metrics (Prometheus), Logs (Loki) on Grafana through OpenTelemetry and OpenMetrics.

364 stars0 likes0 views
WebsiteGitHub

About

Observe Spring Boot app with three pillars of observability: Traces (Tempo), Metrics (Prometheus), Logs (Loki) on Grafana through OpenTelemetry and OpenMetrics.

Spring Boot with Observability

Observe the Spring Boot application with three pillars of observability on Grafana:

  1. Traces with Tempo and OpenTelemetry Instrumentation for Java
  2. Metrics with Prometheus, Spring Boot Actuator, and Micrometer
  3. Logs with Loki and Logback

This demo project is a Spring Boot version of FastAPI with Observability and is also inspired by Cloud Observability with Grafana and Spring Boot.

Table of contents

  • Spring Boot with Observability
    • Table of contents
    • Quick Start
    • Explore with Grafana
      • Metrics to Traces
      • Traces to Logs
      • Logs to Traces
    • Detail
      • Spring Boot Application
        • OpenTelemetry Instrumentation
        • Logs
        • Traces
        • Metrics
          • Metrics with Exemplar
      • Prometheus - Metrics
        • Prometheus Config
        • Grafana Data Source
      • Tempo - Traces
        • Grafana Data Source
      • Loki - Logs
        • Loki Docker Driver
        • Grafana Data Source
      • Grafana
    • Reference

Quick Start

  1. Install Loki Docker Driver

    bash
    docker plugin install grafana/loki-docker-driver:2.9.2 --alias loki --grant-all-permissions
  2. Start all services with docker compose

    bash
    docker compose up -d
  3. Send requests with siege and curl to the Spring Boot app

    bash
    bash request-script.sh
    bash trace.sh

    Or you can send requests with k6:

    bash
    k6 run --vus 3 --duration 300s k6-script.js

    Or send requests from applications' Swagger UI:

    • app-a: http://localhost:8080/swagger-ui/index.html
    • app-b: http://localhost:8081/swagger-ui/index.html
    • app-c: http://localhost:8082/swagger-ui/index.html
  4. 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.

Explore with Grafana

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

Metrics to Traces

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.

Traces to Logs

Get Trace ID and tags (here is compose.service) defined in Tempo data source from span, then query with Loki.

Logs to Traces

Get Trace ID from log (regex defined in Loki data source), then query in Tempo.

Detail

Spring Boot Application

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

OpenTelemetry Instrumentation for Java provides a zero-code way to instrument the application by the agent jar as follows:

bash
java -javaagent:path/to/opentelemetry-javaagent.jar -jar myapp.jar

The 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:

  1. HTTP requests: capture HTTP information like request method, status, and so on.

  2. PostgreSQL actions(POST /peanuts and the first request of GET /peanuts/{id}): capture DB information like SQL statement, table name, and so on.

  3. 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):

  • system properties
  • environment variables
  • the configuration file
  • the ConfigPropertySource SPI

In this demo project we use environment variables to set the agent configuration:

yaml
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:

properties
# otel.properties
otel.exporter.otlp.endpoint=http://tempo:4317
otel.service.name=app-a
otel.metrics.exporter=none
otel.logs.exporter=none
bash
# 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.jar

More configuration details can be found on the official document.

Logs

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:

yaml
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:

log
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!!

Traces

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.

Metrics

To get Prometheus metrics from the Spring Boot application, we need two dependencies:

  1. Spring Boot Actuator: Actuator provides a lot of monitoring features by HTTP or JMX endpoints for the Spring Boot applications.
  2. Micrometer: Micrometer provides a general API to collect metrics and transform the format for different monitoring systems, including Prometheus.

Add these two dependencies to the pom.xml and config to the application.yaml as follows:

xml

    org.springframework.boot
    spring-boot-starter-actuator

    io.micrometer
    micrometer-registry-prometheus
yaml
management:
  endpoints:
    web:
      exposure:
        include: prometheus # only exposure /actuator/prometheus endpoint
  metrics:
    tags:
      application: app # add tag to each prometheus metric

The Prometheus metrics look like this on /actuator/prometheus:

…
Metrics with Exemplar

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.

xml

    io.micrometer
    micrometer-tracing-bridge-otel

When the dependency are addressed. We can add a distribution metric to Prometheus metrics in the application.yaml.

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:

bash
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:

…

Prometheus - Metrics

Collects metrics from applications.

Prometheus Config

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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Javagrafanalokiobservabilityopenmetrics

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category后端框架
PricingOpen source

> Related tools

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架