Spring Boot application with actuator configuration and integration with external monitoring systems
Spring Boot application with Actuator configuration and integration with external monitoring systems (Prometheus and Grafana).
This README file will focus on the actuator features implementation. For more information about the other project features, please refer to the project template: spring-boot-template.
This section provides an overview of the main features, necessary dependencies, and step-by-step instructions to help you get the application up and running quickly.
The main features of this project are:
The following dependency are required to implement this project features:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
To launch and run the external services (Postgres, Prometheus and Grafana) in Docker containers, the following requirements are needed:
To run the project, the requirements are:
To run the project, follow the steps below.
mvn spring-boot:run
The application will be available at http://localhost:8080.
The external services (Postgres, Prometheus, and Grafana) configured in the docker-compose.yaml file will automatically be launched due to the spring-boot-docker-compose
dependency.
In any case, the external services can also be launched manually by running on of the following command:
# Start all the external services
docker compose up -d
# Start a specific external service
docker compose up -d <service-name>
# Replace <service-name> with the service you want to start (prometheus, grafana or postgres)
The actuators endpoints are available at http://localhost:8080/actuator.
By default, only the health
and info
endpoints are exposed over HTTP. To expose all the endpoints, the following configuration was added to the application.yaml
file.
management:
endpoints:
web:
exposure:
include: "*" # Expose all endpoints
The shutdown endpoint is not exposed because it is not enabled (it is disabled by default).
The shutdown endpoint is used to shutdown the application and so, it is disabled by default.
To enable this endpoint, the following configuration must be added to the application.yaml
file.
management:
endpoint:
shutdown:
enabled: true
The info
endpoint is available at http://localhost:8080/actuator/info and is used to display information about the application.
The information to be displayed must be added to the application.yaml
.
info:
app:
name: @project.name@
description: @project.description@
version: @project.version@
encoding: @project.build.sourceEncoding@
java:
version: @java.version@
We also have to add the following property to the application.yaml
so the info contents are displayed in the actuator info
endpoint.
management:
info:
env:
enabled: true
By default, the health endpoint only returns the status of the application. The status can be UP
, DOWN
, or UNKNOWN
.
{
"status": "UP"
}
If we want to see more details about the application health, we can add the following configuration to the application.yaml
file.
management:
endpoint:
health:
show-details: always
Now, the health endpoint will return more details about the application health. Because we have a PostgreSQL database configured in the application, the health endpoint will return the status of the database connection.
{
"status": "UP",
"components": {
"db": {
"status": "UP",
"details": {
"database": "PostgreSQL",
"validationQuery": "isValid()"
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 494384795648,
"free": 422728798208,
"threshold": 10485760,
"path": "/Users/a.caiadodasilva/code/training/spring-boot-actuator/.",
"exists": true
}
},
"ping": {
"status": "UP"
}
}
}
The metrics endpoint provides information about the application performance. The metrics are divided into three categories: system
, jvm
, and process
.
The metrics endpoint is available at http://localhost:8080/actuator/metrics.
We can create custom metrics to monitor specific parts of the application.
To learn more about metric types, please refer to the Prometheus Metric Types page.
In this project, we created a custom gauge
metric to monitor the number of employees in the database. This metric was implemented in the EmployeeService
class as shown below.
@Service
@Slf4j
public class EmployeeService {
private final EmployeeRepository employeeRepository;
private final MeterRegistry meterRegistry;
public EmployeeService(EmployeeRepository employeeRepository, MeterRegistry meterRegistry) {
this.employeeRepository = employeeRepository;
this.meterRegistry = meterRegistry;
Gauge.builder("employees_count", employeeRepository::count)
.description("The current number of employees in the database")
.register(meterRegistry);
}
...
}
This metric will be available at http://localhost:8080/actuator/metrics/employees_count.
{
"name": "employees_count",
"description": "The current number of employees in the database",
"measurements": [
{
"statistic": "VALUE",
"value": 20
}
],
"availableTags": []
}
To secure the actuator endpoints, we can use Spring Security.
The following dependency was added to enable Spring Security in the Spring Boot application.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
A configuration was added to secure the shutdown
actuator endpoint with HTTP Basic Authentication.
The configuration also includes an inMemory user with credentials to access the shutdown
endpoint.
The configuration can be found in the SecurityConfig.java class.
The Spring Boot application was integrated with two monitoring systems: Prometheus and Grafana.
Prometheus is an open-source monitoring system that collects metrics from the application and stores them in a time-series data, i.e. metrics information is stored with the timestamp at which it was recorded, alongside optional key-value pairs called labels.
The Prometheus monitoring system was integrated with the Spring Boot application to collect metrics from the application.
To integrate Prometheus with the Spring Boot application, we used the micrometer-registry-prometheus
dependency. To learn more about Micrometer, please refer to the Micrometer documentation.
The following dependency was added to the pom.xml
file.
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
After adding the dependency, Spring Boot will automatically configure a PrometheusMeterRegistry
and a CollectorRegistry
to collect and export metrics in a format that can be scraped by Prometheus.
The metrics can then be accessed at http://localhost:8080/actuator/prometheus.
A Prometheus server service was added to the docker-compose.yml
file to start the Prometheus server in a Docker container.
The configuration for the Prometheus server can be found in the prometheus.yml file.
To start the Prometheus server, run the following command:
docker compose up -d prometheus
The Prometheus server will be available at http://localhost:9090.
Grafana is an open-source monitoring and observability platform that allows users to visualize metrics collected by Prometheus.
Although Prometheus provides dashboards to visualize the metrics, Grafana is a more powerful tool for this purpose.
A Grafana server service was added to the docker-compose.yml
file to start the Grafana server in a Docker container.
To start the Grafana server, run the following command:
docker compose up -d grafana
The Grafana server will be available at http://localhost:3000.
The default credentials to access Grafana are:
Change the password after the first login or skip the password change by clicking on the Skip
button.
After logging in, add a new data source to Grafana:
host.docker.internal
instead of localhost
.Instead of creating a new dashboard from scratch, we can import a pre-built dashboard from the Grafana dashboard library.
Visit the Grafana dashboard library and search for a dashboard that fits your needs.
To learn about Importing Grafana dashboards, please refer to the Grafana dashboards documentation.
Example of an imported dashboard applied on this project: