Skip to content

Commit fcd3413

Browse files
committed
Allow histograms to be disabled for Lettuce metrics
Closes gh-32985
1 parent beb62be commit fcd3413

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/redis/LettuceMetricsAutoConfiguration.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@
2727
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2929
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
30+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3031
import org.springframework.boot.autoconfigure.data.redis.ClientResourcesBuilderCustomizer;
3132
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
3233
import org.springframework.context.annotation.Bean;
@@ -47,8 +48,13 @@
4748
public class LettuceMetricsAutoConfiguration {
4849

4950
@Bean
50-
public ClientResourcesBuilderCustomizer lettuceMetrics(MeterRegistry meterRegistry) {
51-
MicrometerOptions options = MicrometerOptions.builder().histogram(true).build();
51+
@ConditionalOnMissingBean
52+
MicrometerOptions micrometerOptions() {
53+
return MicrometerOptions.builder().histogram(true).build();
54+
}
55+
56+
@Bean
57+
ClientResourcesBuilderCustomizer lettuceMetrics(MeterRegistry meterRegistry, MicrometerOptions options) {
5258
return (client) -> client.commandLatencyRecorder(new MicrometerCommandLatencyRecorder(meterRegistry, options));
5359
}
5460

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/redis/LettuceMetricsAutoConfigurationTests.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,13 +17,16 @@
1717
package org.springframework.boot.actuate.autoconfigure.metrics.redis;
1818

1919
import io.lettuce.core.metrics.MicrometerCommandLatencyRecorder;
20+
import io.lettuce.core.metrics.MicrometerOptions;
2021
import io.lettuce.core.resource.ClientResources;
2122
import org.junit.jupiter.api.Test;
2223

2324
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
2425
import org.springframework.boot.autoconfigure.AutoConfigurations;
2526
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
2627
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
2730
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
2831

2932
import static org.assertj.core.api.Assertions.assertThat;
@@ -49,6 +52,20 @@ void whenThereIsAMeterRegistryThenCommandLatencyRecorderIsAdded() {
4952
});
5053
}
5154

55+
@Test
56+
void whenUserDefinesAMicrometerOptionsBeanThenCommandLatencyRecorderUsesIt() {
57+
this.contextRunner.with(MetricsRun.simple())
58+
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
59+
.withUserConfiguration(CustomMicrometerOptionsConfiguration.class).run((context) -> {
60+
ClientResources clientResources = context.getBean(LettuceConnectionFactory.class)
61+
.getClientResources();
62+
assertThat(clientResources.commandLatencyRecorder())
63+
.isInstanceOf(MicrometerCommandLatencyRecorder.class);
64+
assertThat(clientResources.commandLatencyRecorder()).hasFieldOrPropertyWithValue("options",
65+
context.getBean("customMicrometerOptions"));
66+
});
67+
}
68+
5269
@Test
5370
void whenThereIsNoMeterRegistryThenClientResourcesCustomizationBacksOff() {
5471
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class)).run((context) -> {
@@ -58,4 +75,14 @@ void whenThereIsNoMeterRegistryThenClientResourcesCustomizationBacksOff() {
5875
});
5976
}
6077

78+
@Configuration(proxyBeanMethods = false)
79+
static class CustomMicrometerOptionsConfiguration {
80+
81+
@Bean
82+
MicrometerOptions customMicrometerOptions() {
83+
return MicrometerOptions.create();
84+
}
85+
86+
}
87+
6188
}

0 commit comments

Comments
 (0)