Custom Monitoring Metrics Springboot + Prometheus + Grafana (in a few words)
SPRINGBOOT + SPRINGSECURITY v.2.7.10 + JAVA 17 +MyBATIS v.2.3.0 (ORACLE) in Windows

Custom Monitoring Metrics Springboot + Prometheus + Grafana (in a few words)

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.

  • add the necessary dependencies:

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>        

  • Register the TimedAspect bean in our Spring context allowing Micrometer to add a timer to custom methods. The bean should be in your. @SpringBootApplication or @Configuration class

@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);
   }


}        

  • Afterwards, to find the method that you want to time, add the @Timed annotation to it. Use the value attribute to give the metric a name.

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.


  • Start your application and send an HTTP GET request to https://localhost:8080/actuator/prometheus. You will see all the metrics being exposed.

No hay texto alternativo para esta imagen


  • Run up your Docker containers.

docker compose up        


No hay texto alternativo para esta imagen


  • Find your custom metrics in Prometheus.

In your browser, call to : https://localhost:9090/graph

No hay texto alternativo para esta imagen

Here you can find the custom metrics exposed from your project thanks to Micrometer and its annotations.


  • Once we are sure our custom metrics are properly exposed and readable, let's check it into Grafana.

just go to your dashboards, there click on the upper edge of one box, and select explore.

No hay texto alternativo para esta imagen

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

No hay texto alternativo para esta imagen

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.

No hay texto alternativo para esta imagen

Best wishes.

I hope it was helpful.


#SpringBoot #JavaDevelopment #Micrometer #Prometheus #Grafana #MetricsMonitoring #SoftwareDevelopment #Programming #TechTips #DeveloperCommunity #CodePerformance #ApplicationMonitoring #DevOps #SoftwareEngineering #DataVisualization

sagili ramanareddy

Associate Engineer at Deustche Bank

6 个月

I need Kotlin Gradle based

回复

要查看或添加评论,请登录

Rafael Vera-Mara?ón的更多文章

社区洞察

其他会员也浏览了