MicroMeter LoggingMeterRegistry configuration in SpringBoot 3 application
Asked Answered
M

2

0

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

Maulstick answered 11/8, 2023 at 14:15 Comment(0)
M
0

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

Maulstick answered 11/8, 2023 at 15:26 Comment(0)
F
0

For your second question, I have used this pattern to customize the meter registry in a SpringBoot application. In the example, I only care about cache.[gets|puts|evictions] and not all the JVM values etc.

See the Micrometer MeterFilter docs for more background.

import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.config.MeterFilter;

@Bean
public MeterRegistryCustomizer<MeterRegistry> buildMeterRegistry() {
    return new MeterRegistryCustomizer<MeterRegistry>() {

        @Override
        public void customize(MeterRegistry registry) {
            registry.config()
                    .meterFilter(MeterFilter.denyUnless(meter -> meter.getName().startsWith("cache")))
                    .meterFilter(MeterFilter.denyNameStartsWith("cache.removals"));
        }
    };
}    

I had also discovered a slightly different way to load properties from the environment (question 1), if it helps.

@Bean
public LoggingMeterRegistry loggingMeterRegistry(Environment environment) {
    LoggingRegistryConfig config = new LoggingRegistryConfig() {
        final Environment env = environment;

        @Override
        public String get(String key) {
            return env.getProperty(key);
        }

        @Override
        public String prefix() {
            // Whatever prefix you want to use in the application.yml
            return "sample.metric.logging";
        }
    };

    LOG.info("Creating LoggingMeterRegistry, logging every {} seconds", config.step().get(ChronoUnit.SECONDS));
    return new LoggingMeterRegistry(config, Clock.SYSTEM);
}

application.yml

I looked at the code to identify what the property names are and picked a couple for demonstration.

sample:
  metric:
    logging:
      logInactive: true
      step: 5000ms
Freddafreddi answered 9/4, 2024 at 23:32 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.