Today we'll learn how to make your app more observable using Spring Boot Actuator. Specifically, we'll go over:
- Adding and configuring Spring Boot Actuator.
- Using built-in metrics and endpoints for monitoring.
- Logging and integrating with external systems like Grafana, Prometheus, and the ELK stack (Elasticsearch, Logstash, Kibana).
- Setting up custom metrics for your application.
So get ready — time to look under the hood of your app!
Adding Spring Boot Actuator
Quick refresher: Spring Boot Actuator is a module that adds observability and exposes metrics, available via API or the console. It's like a Jedi tool for a developer who wants to keep an eye on their app.
Official docs: Spring Boot Actuator
We'll start by adding the Actuator dependency to the project. Make sure you have Spring Boot set up (in case you forgot).
Add the dependency
Add the following dependency to your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
If you're using Gradle, add this:
implementation 'org.springframework.boot:spring-boot-starter-actuator'
After that, run mvn install or gradle build to pull the dependencies into your project.
Main Actuator features
Actuator gives you a set of ready-made endpoints for monitoring and managing the app. Here are a few:
| Endpoint | Purpose |
|---|---|
/actuator/health |
Shows application status (UP/DOWN) |
/actuator/info |
General info about the app (configurable) |
/actuator/metrics| Application performance metrics |
|
/actuator/env |
Environment variables and their values |
/actuator/loggers| Manage logging levels |
|
/actuator/beans |
Info about all Spring Beans registered in the app |
Configuring and enabling endpoints
Step 1: Allow endpoints
By default, not all Actuator endpoints are exposed. Let's open them up in the config:
# application.yml
management:
endpoints:
web:
exposure:
include: "*" # Expose all endpoints
Or, if you're using application.properties:
management.endpoints.web.exposure.include=*
Open your browser and go to http://localhost:8080/actuator. You'll see the list of available endpoints.
Step 2: Enable metrics
Spring Boot Actuator integrates with Micrometer, a toolkit for collecting performance metrics. To get started, let's look at some built-in metrics:
- Start the application.
- Go to
/actuator/metrics. - You'll see a list of all available metrics. For example:
{
"names": [
"jvm.memory.used",
"jvm.memory.max",
"process.uptime",
"system.cpu.usage",
"http.server.requests"
]
}
Try opening a specific metric, e.g. /actuator/metrics/system.cpu.usage. You'll get CPU usage data.
Integrating with Prometheus
Prometheus is a monitoring system that pairs well with Actuator. Let's set it up.
Step 1: Add the Prometheus dependency
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
Step 2: Enable the /actuator/prometheus endpoint
management:
endpoints:
web:
exposure:
include: prometheus
Now Prometheus can scrape metrics from your app by querying /actuator/prometheus.
Logging with the ELK stack
Logs are simple: keep them readable and centralized. The combo of Elasticsearch, Logstash, and Kibana works great for this.
Step 1: Set up the logback-spring.xml file
Create logback-spring.xml in src/main/resources.
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>logs/spring-boot-app.log</file>
<encoder>
<pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
Step 2: Integrate with Logstash
Logstash will collect logs from spring-boot-app.log. Configure it to send data to Elasticsearch for later analysis in Kibana.
Setting up custom metrics
Say you want to track how many times a specific method in your app gets called. You can create custom metrics using Micrometer.
Example: Custom metric
Add the following configuration:
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Service;
@Service
public class CustomMetricsService {
private final Counter customCounter;
public CustomMetricsService(MeterRegistry registry) {
this.customCounter = registry.counter("custom.metric.counter", "type", "custom_metric");
}
public void performAction() {
// Method logic
customCounter.increment(); // Increment the counter
}
}
Now, each time performAction() is called, your custom counter will increment. You can see this metric at /actuator/metrics/custom.metric.counter.
Securing metrics
Not all Actuator endpoints should be exposed to the world. For example, /actuator/metrics and /actuator/beans contain sensitive info that could be abused. Use Spring Security to restrict access:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/actuator/health").permitAll() // Available to everyone
.antMatchers("/actuator/**").authenticated() // Requires authentication
.and()
.httpBasic();
}
Now your app isn't just functional — it's observable. You can analyze metrics, log events, and be confident the app is running smoothly.
GO TO FULL VERSION