[JAVA] Easily get any metrics with Spring Boot + Micrometer + Prometheus Exporter

Here, I will write about how to get arbitrary metrics with the combination of Spring Boot + Micrometer + Prometheus Exporter.

Settings To enable Micrometer in an environment with Spring Boot installed, configure the required libraries such as pom and gradle.

example Here is an example of pom.

pom.xml


<!--Mandatory-->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-core</artifactId>
    <version>${micrometer.version}</version>
</dependency>
<!-- spring boot 1.Required if using 5x-->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-spring-legacy</artifactId>
    <version>${micrometer.version}</version>
</dependency>
<!--Below, required to enable Prometheus Exporter-->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>${micrometer.version}</version>
</dependency>
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient</artifactId>
    <version>${prometheus.version}</version>
</dependency>
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_common</artifactId>
    <version>${prometheus.version}</version>
</dependency>

micrometer-core This library is essential when using a micrometer.

micrometer-spring-legacy micormeter has been treated as an official metric tool since Spring Boot 2,x. For that reason, if you use a version older than 2.x (1.5.x), you need a library for the backport. If you are running in a 1.5.x environment, specify this.

micrometer-registry-prometheus / simpleclient /simpleclient_common For how to propagate and display the acquired metrics, select any one in the library micrometer-registry-**.

Since we want to use Prometheus Exporter here, specify the registry related to Prometheus.

In addition, when performing prometheus-related processing in Java, it is necessary to specify the libraries (simpleclient, simpleclient_common) provided by prometheus.

Code Here's how to embed it in your application code.

Initialize Counter

Increment using the initialized Counter

SadaController.java


@RestController
public class SadaController {
    @Autowired
    protected MeterRegistry meterRegistry;
 
    private Counter metricsCounter;

    @PostConstruct
    public void init() { 
        metricsCounter = meterRegistry.counter("api.sada.count"); // initialize
    }
 
    @RequestMapping(path = "/sada", method = { RequestMethod.GET })
    @ApiOperation(value = "sada")
    @ApiResponses(value = { @ApiResponse(code = 200, response = String.class, message = "echo") })
    public String sada(@RequestParam(name = "echo", required = false, defaultValue = "masashi") @Valid String echo) {
        metricsCounter.increment(); // increment
        return echo;
    }

Prometheus Exporter If you have added micrometer-registry-prometheus to your library, the / prometheus endpoint is automatically enabled when Spring Boot starts.

By default, metrics about the state of the JVM and Thread can be obtained.

In addition, custom metrics such as those mentioned above will also be available via Prometheus Exporter.

# TYPE api_sada_count_total counter
api_sada_count_total 15.0

The above is an example where the number of times the above Controller was called was obtained as a metric.

Metrics obtained in this way can be imported into Prometheus and Datadog.

Instead of summarizing

That's the simplest way to use Micrometer, but there are many other features, so if you want to know more, go to the official documentation.

https://micrometer.io/docs

Recommended Posts

Easily get any metrics with Spring Boot + Micrometer + Prometheus Exporter
Get started with Spring boot
Get validation results with Spring Boot
Download with Spring Boot
[Spring Boot] Get user information with Rest API (beginner)
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
I tried to get started with Swagger using Spring Boot
Easily develop web applications with STS and Spring Boot. In 10 minutes.
Create an app with Spring Boot 2
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Message cooperation started with Spring Boot
Spring Boot gradle build with Docker
Processing at application startup with Spring Boot
Hello World with Eclipse + Spring Boot + Maven
Send regular notifications with LineNotify + Spring Boot
Perform transaction confirmation test with Spring Boot
Try using Spring Boot with VS Code
Start web application development with Spring Boot
Launch Nginx + Spring Boot application with docker-compose
I tried Lazy Initialization with Spring Boot 2.2.0
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Asynchronous processing with Spring Boot using @Async
Implement paging function with Spring Boot + Thymeleaf
(IntelliJ + gradle) Hello World with Spring Boot
Use cache with EhCashe 2.x with Spring Boot
Form class validation test with Spring Boot
Run WEB application with Spring Boot + Thymeleaf
Achieve BASIC authentication with Spring Boot + Spring Security