spring-bootprometheusmicrometerprometheus-pushgateway

Micrometer with Prometheus Pushgateway not sending the recent metrics


I have a Spring boot application based on CommandLineRunner, when it starts it does some calculations, sends the metrics to Prometheus and shuts down.

I am using Prometheus Push Gateway with Micrometer, mainly based on this tutorial: https://luramarchanjo.tech/2020/01/05/spring-boot-2.2-and-prometheus-pushgateway-with-micrometer.html

This works fine if I leave the application running however with my particular Spring boot application, it looses the metrics sent just before the shutdown.

I have had similar issue with CloudWatch however it was clear with the Registry implementation CloudWatchMeterRegistry, that it starts a thread in the background and it uses the property cloudwatch.step to schedule the dispatch of the collected metrics. I am struggling to see how PrometheusMeterRegistry is working and not sending the metrics before the application shutsdown.

I have tried to add meterRegistry.close(); just before the shutdown, however it still has the same issue!


Solution

  • After some investigation resolved this by calling the shutdown() method on PrometheusPushGatewayManager.

    @SpringBootApplication
    public class SpringBootConsoleApplication implements CommandLineRunner {
    
      @Autowired
      PrometheusPushGatewayManager prometheusPushGatewayManager;
    
      @PreDestroy
      public void onExit() {
        System.out.println("Exiting..");
        prometheusPushGatewayManager.shutdown();
      }
      
      ...
    

    And add following in the application.properties:

    management.metrics.export.prometheus.pushgateway.shutdown-operation=PUSH