4

I am trying to configure the LoggingMeterRegistry to log metrics for my Spring Boot 2.1.6 application. I want the metrics to be logged every hour.

In my application.yml, I've the following configured

management:
  metrics:
    export:
      logging:
        enabled: true
        step: 60m

But in the logs I see the metrics being logged every minute. I've tried the other variation for the property key as well e.g.

management.metrics.export.logging:
  enabled: true
  step: 60m

I have also tried various formats for the duration string e.g. 1h, PT60M but with no success. The metrics are logged at 1 minute intervals.

I was looking at the code here StepDurationConverterTest and here StepDurationConverter that converts the step duration String to a Duration object and looks like both formats 60m and 1h should work.

Any ideas why I can't seem to change the logging interval?

golfradio
  • 457
  • 3
  • 7
  • 17
  • That step should work. Have you tried putting a break point in `LoggingMeterRegistry` to see if it is reading in your property? – checketts Aug 28 '19 at 06:41

4 Answers4

2

I think the problem here is there's no org.springframework.boot.actuate.autoconfigure.metrics.export.logging package like there is for other MeterRegistrys (eg org.springframework.boot.actuate.autoconfigure.metrics.export.jmx).

Ie theres no auto configuration for the properties in Spring Boot. This is probably because the LoggingMeterRegistry is marked as @Incubating

You need to manually configure the LoggingMeterRegistry as a bean and create your own @ConfigurationProperties LoggingProperties and LoggingPropertiesConfigAdapter to get this to work. Or just hardcode the step period you want.

David Geary
  • 1,756
  • 2
  • 14
  • 23
1

The following @Bean supplies config from Spring Environment allowing you to specify a property logging.step: 1h to get your desired period.

    @Bean
    LoggingMeterRegistry loggingMeterRegistry(Environment env) {
        LoggingRegistryConfig springBasedConfig = prop -> env.getProperty(prop, String.class);
        return new LoggingMeterRegistry(springBasedConfig, Clock.SYSTEM);
    }
drekbour
  • 2,895
  • 18
  • 28
0

To configure step count duration in micrometer: Please follow below step:

@Configuration
public class LoggingMeterRegistryConfig {
    @Bean
    public LoggingMeterRegistry loggingMeterRegistry() {

        LoggingRegistryConfig config = new LoggingRegistryConfig() {

            @Override
            public String get(String s) {

                return null;
            }

            @Override
            public Duration step() {

                return Duration.ofMinutes(2);
            }
        };
        return LoggingMeterRegistry.builder(config).clock(Clock.SYSTEM).threadFactory(new NamedThreadFactory("logging-metrics-publisher")).build();
    }

    }
0

I want to share a solution of mine which I posted at https://stackoverflow.com/a/76884882/418599 .

@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
Antonio Petricca
  • 8,891
  • 5
  • 36
  • 74