prometheusmetricslatencymicrometer

@Timed metric not showing on prometheus enpoint


This is a method in my Controller that I want to find the latency of. this is not an api endpoint just a random method.

@Timed("my_method")
  public JobParameters myMethod(String fileNames) {
    List<String> fileNamesList = Arrays.stream(fileNames.split(",")).toList();
    var map = fileNamesList.stream().collect(Collectors.toMap(str -> str.split("_")[0],
            str -> str));
    JobParametersBuilder jobParametersBuilder = new JobParametersBuilder()
            .addLong("startAt", System.currentTimeMillis());

    for (String key : Arrays.asList("ABC", "DEF", "HIJ")) {
      String fileName = map.get(key);
      if (fileName != null) {
        jobParametersBuilder.addString(key.toLowerCase() + "FileName", fileName);
      }
    }
    return jobParametersBuilder.toJobParameters();

  }

My application.yml looks like this (for prometheus )

  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
      group:
        liveness:
          include: diskSpace,ping
          show-details: always
metrics:
  export:
    prometheus:
      enabled: true

prometheus.yml

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'java-app'
    metrics_path: "/actuator/prometheus"
    static_configs:
      - targets: ['localhost:4545']

my pom.xml has these

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
      <version>3.1.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.micrometer/micrometer-registry-prometheus -->
    <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-registry-prometheus</artifactId>
      <version>1.11.4</version>
    </dependency>

My application is running on port 4545 and prometheus is running on 9090. I am able to access http://localhost:4545/actuator/prometheus and http://localhost:4545/actuator/metrics but I am not able to see metrics for @Timed("my_method").


Solution

  • add this to your controller

      @Bean
      public TimedAspect timedAspect(MeterRegistry registry) {
        return new TimedAspect(registry);
      }
    

    and this annotation @EnableAspectJAutoProxy