javaspring-bootmetricsmicrometerspring-micrometer

MicroMeter LoggingMeterRegistry configuration in SpringBoot 3 application


I am writing a SpringBoot 3 application which needs to report metrics inside log files too.

I registered an instance of io.micrometer.core.instrument.logging.LoggingMeterRegistry by:

@Bean
LoggingMeterRegistry loggingMeterRegistry() {
    return new LoggingMeterRegistry();
}

It works as expected, but:

  1. I don't know how to provide configuration settings (like the step) by the application.yaml file.
  2. I don't know how to register a custom instance of the class com.codahale.metrics.MetricFilter to filter the output.

Could somebody tell me how to achieve these goals?

Thank you so much in advance!

Edit: my issue seems similar to How to configure LoggingMeterRegistry step duration in Spring Boot 2.x? .


Solution

  • Here my very clean workaround for the first question "I don't know how to provide configuration settings (like the step) by the application.yaml file" (found after a lot of debugging of LoggingMeterRegistry).

    Configuration class

    @Bean
    LoggingMeterRegistry loggingMeterRegistry(Environment environments) {
        LoggingRegistryConfig config =
            key -> {
                String property = null;
    
                switch (key) {
                    case "logging.enabled":
                        property = "management.metrics.export.logging.enabled";
                        break;
    
                    case "logging.step":
                        property = "management.metrics.export.logging.step";
                        break;
    
                }
    
                return
                      (null != property)
                    ? environments.getProperty(property)
                    : null;
            };
    
        return new LoggingMeterRegistry(config, Clock.SYSTEM);
    }
    

    application.yaml

    management:
      metrics:
        export:
          logging:
            step: 10s
    

    Now, I am going to find a solution for the second issue...