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.
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.
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