Oct 4, 2024
Instrumenting Spring Boot Apps with Prometheus Metrics
In today's microservices-driven world, observability is crucial for maintaining and optimizing application performance. This guide will walk you through instrumenting a Spring Boot application to expose metrics that can be scraped by Prometheus, laying the groundwork for powerful monitoring and alerting capabilities.
Table of Contents
Understanding Prometheus and Time-Series Metrics
Setting Up the Spring Boot Application
Instrumenting Spring Boot with Custom Metrics
Auto-instrumentation with Micrometer
Running the Application and Sending Traffic
Observing Metrics
Understanding Prometheus and Time-Series Metrics
Prometheus is an open-source monitoring toolkit that stores metrics as time-series data, meaning each data point is associated with a timestamp. These metrics typically include:
Counters: Cumulative metrics that only increase (e.g., total number of requests)
Gauges: Metrics that can go up and down (e.g., current CPU usage)
Histograms: Samples observations and counts them in configurable buckets (e.g., request durations)
To leverage Prometheus, applications need to be instrumented to expose metrics in a format that Prometheus can scrape. This instrumentation allows us to collect valuable data about our application's performance and behavior, which can later be used for monitoring, alerting, and optimization.
Setting Up the Spring Boot Application
Let's start by cloning and setting up the sample Spring Boot application:
Build the project:
To run the application, use the following command:
The application will start and be available at http://localhost:8080
.
Instrumenting Spring Boot with Custom Metrics
Inspect the source code. The application is already instrumented with custom metrics using the Micrometer library, which Spring Boot uses for its metrics abstraction. Let's examine the key parts:
It's important to note that these metrics are custom-defined:
Custom HTTP metrics:
http_request_total
: A Counter to track the total number of HTTP requests.http_request_duration_seconds
: A Timer to measure the duration of HTTP requests.http_requests_in_progress
: A Gauge to monitor the number of ongoing HTTP requests.
These metrics are custom because we're explicitly defining them using the Micrometer library. They're not automatically generated or collected by the library itself. Micrometer provides the tools (Counter, Timer, Gauge) to create these metrics, but it's up to us to define them, give them names, descriptions, and labels, and then update them appropriately in our code.
Auto-instrumentation with Micrometer
In addition to our custom metrics, Spring Boot with Micrometer provides a wide range of auto-instrumented metrics out of the box. These metrics are automatically collected and don't require explicit code in our application. Some of these auto-instrumented metrics include:
System metrics:
process_cpu_usage
: A Gauge tracking CPU usage of the JVM process.process_cpu_time_ns_total
: A Counter for total CPU time used by the JVM process.system_cpu_usage
: A Gauge for system-wide CPU usage.jvm_memory_used_bytes
: A Gauge for JVM memory usage.
JVM metrics:
jvm_threads_live_threads
: A Gauge for the number of live threads.jvm_gc_memory_allocated_bytes_total
: A Counter for allocated memory.jvm_gc_pause_seconds
: A Timer for GC pause durations.
These auto-instrumented metrics provide valuable insights into our application's performance and resource utilization without requiring additional code.
Running the Application and Sending Traffic
Now that the application is set up and running, let's send some traffic to it using Postman:
Open Postman and import the provided collection
spring-boot-metrics-postman-collection.json
.Use the various requests in the collection to interact with the API:
Send GET requests to the root endpoint
Create, retrieve, update, and delete items using the /items endpoints
Observing Metrics
After sending traffic to the application, let's observe the metrics: In your browser or using a tool like curl, navigate to http://localhost:8080/actuator/prometheus
. You'll see a list of all the metrics our application is tracking, including both custom and auto-instrumented metrics. Let's break down some key metrics:
The custom
http_request_total counter
shows how many requests of each type we've received.The custom
http_request_duration_seconds
summary provides information about request durations.The custom
http_requests_in_progress
gauge shows how many requests are currently being processed.The auto-instrumented
process_cpu_usage
gives us insight into our application's CPU usage.The auto-instrumented
jvm_memory_used_bytes
provides information about memory usage.The auto-instrumented
jvm_threads_live_threads
shows the number of live threads in our application.
These metrics, both custom and auto-instrumented, are now exposed in a format that Prometheus can understand and scrape. In future steps, you can set up Prometheus to scrape these metrics at regular intervals, storing them for analysis and visualization. This data can then be used with tools like Grafana to create insightful dashboards, or with Prometheus's built-in alerting to notify you of potential issues before they become critical problems.