In this article ,we will see how we can monitor spring boot applications using actuator spring boot project and prometheus tool .
Prometheus is a time-series monitoring application written in Go. It can run on a server, in a docker container, or as part of a Kubernetes cluster (or something similar). Prometheus collects, stores, and visualizes time-series data so that you can monitor your systems. You can tell Prometheus exactly where to find metrics by configuring a list of “scrape jobs”. Applications that are being monitored can provide metrics endpoints to Prometheus using any one of the many client libraries available; additionally, separate exporters can gather metrics from applications to make them available in Prometheus. Metrics get stored locally for 15 days, by default, and any Prometheus server can scrape another one for data. Additionally, remote storage is another option for Prometheus data – provided there is a reliable remote storage endpoint.
- The option of “service discovery” allows Prometheus to keep track of all current endpoints effortlessly.
- Outages are quickly detected .
- The PromQL query language is incredibly flexible and Turing-complete.
- There’s also a very low load on the services monitored (metrics get stored in memory as they get generated), allowing fewer resources to get used.
- Additionally, Prometheus users can control traffic volumes, access metrics in the browser, and allow for easy reconfiguration.
Part 1: Spring boot application configuration
We will create a simple spring boot REST application which will expose the metrics to prometheus. Please find below project structure of application –
Below is the pom.xml required for project –
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.db</groupId> <artifactId>spring-boot-prometheus</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
We need spring-boot-starter-actuator dependency ,this would expose the endpoints of our application and makes it available to prometheus server. You can run the application and check the endpoints exposed by actuator by hitting below link in browser –
micrometer-registry-prometheus dependency is required to register metrics with prometheus server. It exposes Actuator metrics to external monitoring systems such as Prometheus, Netflix Atlas, AWS Cloudwatch, and many more.
Similarly, Micrometer automatically exposes /actuator/metrics data into something your monitoring system can understand. All you need to do is include that vendor-specific micrometer dependency in your application.
Micrometer is a separate open-sourced project and is not in the Spring ecosystem, so we have to explicitly add it as a dependency. Since we will be using Prometheus, let’s add it’s specific dependency in our pom.xml:
Part 2: Prometheus Server Configuration
For this example , i will be installing portable prometheus on local system . You can download the prometheus setup from official prometheus website as below –
You need to make below changes in prometheus.yml file of prometheus configuration to scrape the metrices your application .Please add below job to scrape the metrices of your application –
# Details to connect Prometheus with Spring Boot actuator end point to scrap the data # The job name is added as a label `job=` to any time series scraped from this config. - job_name: 'spring-actuator' # Actuator end point to collect the data. metrics_path: '/actuator/prometheus' #How frequently to scape the data from the end point scrape_interval: 5s #target end point. We are using the Docker, so local host will not work. You can change it with #localhost if not using the Docker. static_configs: - targets: ['192.168.43.33:8081']
Run the prometheus server by double clicking prometheus.exe flie .
Now prometheus console will open and it will strat gathering the metrices of your application. You can access the prometheus server in browser using http://localhost:9090
You can gather the metrices of your application by selecting the various parameters provided by prometheus like –
These parameters are useful to monitor your systems. In the next article, we will create a prometheus server using docker and expose the metrices.
Github Downlod Link: