This repo contains a demo project providing a minimal example of getting started with Spring Boot Observability. The entire stack is represented in a Docker Compose file, and the app creates sample test data to demonstrate metric and log generation.
The application can be run using ./mvnw spring-boot:run,
thanks to Spring Boot's Docker Compose support
this will boot everything needed the stack via the Docker Compose file.
The seed-data.sh shell script can then be used to add some sample data to Grafana dashboard, which can be viewed at http://localhost:3000/dashboards.
Data can also manually be added by calling the endpoints detailed in the seed data script directly.
Spring Boot Observability simplifies monitoring and debugging by collecting and centralizing metrics, logs, and traces. Powered by Micrometer, it provides a unified way to gather application metrics and integrates seamlessly with various tools like Prometheus, Loki, and Grafana.
Micrometer is the backbone of metrics observability in Spring Boot, providing a vendor-neutral facade—similar to SLF4J— for defining and exporting metrics across multiple monitoring platforms. With its integration into Spring Boot Actuator, it automatically collects application metrics, such as JVM statistics, HTTP requests, database connections, and more. Key features of Micrometer include:
- Vendor-neutral: Works seamlessly with various monitoring tools (e.g., Prometheus, Datadog, or New Relic).
- Domain-Specific Metrics: Allows you to define custom metrics for your application domain.
- Built-in Instrumentation: Includes support for out-of-the-box metrics for common components like HTTP, R2DBC, Hibernate, and more.
-
Prometheus:
- Prometheus is used to scrape and store time-series metrics from your Spring Boot application.
- Micrometer provides a Prometheus registry, which when combined with Spring Actuator exposes metrics at the
/actuator/prometheusendpoint. - Prometheus collects data from this endpoint at regular intervals, stores the metrics, and supports querying them using PromQL.
-
Loki:
- Loki is designed for log aggregation and works like Prometheus but for logs.
- Logs generated by your Spring Boot application can be streamed to Loki, often using logback or other logging frameworks configured for JSON output.
- The logs can then be queried and displayed using Grafana, making it easier to connect metrics with logs for troubleshooting.
-
Grafana:
- Grafana serves as the visualization layer in the observability stack.
- Metrics from Prometheus and logs from Loki can be visualized in dashboards.
- Developers can create custom dashboards to monitor application performance, view trends, and correlate metrics with logs for streamlined debugging.
graph TD
A[Spring Boot Application] -->|Exposes Metrics| B[/Micrometer\]
A -->|Logs to Logback| I[/Loki4jAppender\]
I -->|Streams to| C[Loki]
B -->|Scraped by| D[Prometheus]
D -->|Stores Metrics| G[(Prometheus Database)]
C -->|Stores Logs| H[(Loki Database)]
G -->|Data Source| E[Grafana]
H -->|Data Source| E[Grafana]
E -->|Visualizes| F[Dashboards]
- Application Metrics: Spring Boot exposes built-in and custom metrics through Micrometer.
- Prometheus Scraping: Prometheus scrapes the
/actuator/prometheusendpoint and stores the metrics as time-series data. - Log Streaming with Loki: Logback (or other logging frameworks) streams application logs to Loki for aggregation.
- Visualization in Grafana: Grafana uses Prometheus as the metrics source and Loki as the log source. Dashboards are configured to provide real-time insights into metrics and logs.
In an effort to keep things simple, this example only covers 2 of the 3 pillars of observability. Notably Tracing is missing.
I recommend watching the talk below and checking out the supporting repo to learn more about tracing.
