Search code examples
spring-bootspring-boot-actuatorspring-micrometer

Configure distribution statistic for http.server.requests in spring-boot


Is there a way to configure distribution statistic expiry and buffer length for http.server.requests metrics?

I need to increase the expiry and couldn't find the proper way of doing this with spring boot actuator. Is it possible to configure these settings?


Solution

  • You should look at DistributionStatisticConfig. The creation of DEFAULT instance shows how to set expiry and bufferLength.

    All you have to do in Spring Boot is register a bean MeterRegistryCustomizer in your @SpringBootApplication or @Configuration class.

    import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
    
    @Bean
    public MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
        return registry -> registry.config()
            .commonTags("myTag", myTagValue)
            .meterFilter(new MeterFilter() {
    
                @Override
                public DistributionStatisticConfig configure(Meter.Id id,
                                                             DistributionStatisticConfig config) {
                    if (id.getName().startsWith("http.server.requests")) {
                        return config.merge(DistributionStatisticConfig.builder()
                            .percentilesHistogram(true)
                            .percentiles(0.5, 0.9, 0.99)
                            .percentilePrecision(1)
                            .minimumExpectedValue(1L)
                            .maximumExpectedValue(Long.MAX_VALUE)
                            .expiry(Duration.ofMinutes(1))
                            .bufferLength(2)
                            .build());
                    }
                    return config;
                }
            });
    }
    

    You can also ask on Micrometer's Slack channel.