Custom Monitoring Metrics Springboot + Prometheus + Grafana (in a few words)
Rafael Vera-Mara?ón
Data Engineer @ Minsait | Cloud Architechture | AI Integration | Data Governance
Hello, today I want to explain to you in a few words how to add custom metrics to your existing Spring Boot project and expose them through Prometheus and Grafana. Specifically, I will show you how to add the TIMER metric offered by Micrometer to monitor the execution time and frequency of a specific method.
In the next link you can find the main types of metrics in Micrometer:
[ If you want to learn how to start from the scratch and implement Prometheus and Grafana in your project, please visit my previous article.]
Just a reminder: if you already have Prometheus implemented in your project, then you probably already have the following Micrometer dependency.
Def: Micrometer is an instrumentation library for systems built on the Java virtual machine. It serves as a facade for popular APM systems and supports Prometheus by exposing metrics.
<dependency
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>>
Now, let's go through the simple steps.
We need this dependency because of the way Micrometer times methods. It depends on AspectJ, which is included in Spring's AOP feature.
<dependency>
? <groupId>org.springframework.boot</groupId>
? <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
@EnableEncryptableProperties
@SpringBootApplication
@OpenAPIDefinition(
info = @Info(version = "1.0.0", description = "Proyecto de Scoring")
)
public class VehicleRentingApiApplication {
public static void main(String[] args) {
SpringApplication.run(VehicleRentingApiApplication.class, args);}
// THIS IS THE @BEAN AND METHOD
@Bean
public TimedAspect timedAspect(MeterRegistry registry) {
return new TimedAspect(registry);
}
}
In this example I notated @Timed the method "existePersona", assigning the value = "validaciónIdPersona.time" which match the metric name being exposed.
These metrics show the number of times my method was executed, the total time taken, and the maximum value.
@Log4j2
@Service
public class PersonaServiceImpl implements PersonaService {
private DireccionMapper direccionMapper;
private PersonaMapper personaMapper;
....
. rest of the code
....
// Above the method. Notice the @Timed notation + value defined
@Timed(value = "validacionIdPersona.time", description = "Time taken to return solicitudRenting")
public boolean existePersona (int personaId) {
log.info("Procesando la validación del id de una persona");
return personaMapper.existePersona(personaId) != 0;
}
....
. rest of the code
....
The metric won't be showed until the method is timed one time at least! So be sure that you execute de code in order to trigger this method.
In my case, I just make an HTTP call with Postman, afterchecking this method will be involved.
领英推荐
docker compose up
In your browser, call to : https://localhost:9090/graph
Here you can find the custom metrics exposed from your project thanks to Micrometer and its annotations.
just go to your dashboards, there click on the upper edge of one box, and select explore.
you will be able to select the grafana query to this box which returns the pointed metrics. Click on "Metric Browser" and write the value you gave to the metrics on the box "1.Select a metric".
Once selected, hit the button "Use query" first, and the button "Run query" after. Then you will see your selected metric.
Remember that you can add many metric queries you want to this box, just browsing the available metrics.
Best wishes.
I hope it was helpful.
Associate Engineer at Deustche Bank
6 个月I need Kotlin Gradle based