Merge branch '2.7.x'

Closes gh-32990
pull/32994/head
Andy Wilkinson 2 years ago
commit 8e08931411

@ -26,6 +26,7 @@ import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfigu
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.ClientResourcesBuilderCustomizer; import org.springframework.boot.autoconfigure.data.redis.ClientResourcesBuilderCustomizer;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -44,8 +45,13 @@ import org.springframework.context.annotation.Bean;
public class LettuceMetricsAutoConfiguration { public class LettuceMetricsAutoConfiguration {
@Bean @Bean
public ClientResourcesBuilderCustomizer lettuceMetrics(MeterRegistry meterRegistry) { @ConditionalOnMissingBean
MicrometerOptions options = MicrometerOptions.builder().histogram(true).build(); MicrometerOptions micrometerOptions() {
return MicrometerOptions.builder().histogram(true).build();
}
@Bean
ClientResourcesBuilderCustomizer lettuceMetrics(MeterRegistry meterRegistry, MicrometerOptions options) {
return (client) -> client.commandLatencyRecorder(new MicrometerCommandLatencyRecorder(meterRegistry, options)); return (client) -> client.commandLatencyRecorder(new MicrometerCommandLatencyRecorder(meterRegistry, options));
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.boot.actuate.autoconfigure.metrics.redis; package org.springframework.boot.actuate.autoconfigure.metrics.redis;
import io.lettuce.core.metrics.MicrometerCommandLatencyRecorder; import io.lettuce.core.metrics.MicrometerCommandLatencyRecorder;
import io.lettuce.core.metrics.MicrometerOptions;
import io.lettuce.core.resource.ClientResources; import io.lettuce.core.resource.ClientResources;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -24,6 +25,8 @@ import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -49,6 +52,20 @@ class LettuceMetricsAutoConfigurationTests {
}); });
} }
@Test
void whenUserDefinesAMicrometerOptionsBeanThenCommandLatencyRecorderUsesIt() {
this.contextRunner.with(MetricsRun.simple())
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
.withUserConfiguration(CustomMicrometerOptionsConfiguration.class).run((context) -> {
ClientResources clientResources = context.getBean(LettuceConnectionFactory.class)
.getClientResources();
assertThat(clientResources.commandLatencyRecorder())
.isInstanceOf(MicrometerCommandLatencyRecorder.class);
assertThat(clientResources.commandLatencyRecorder()).hasFieldOrPropertyWithValue("options",
context.getBean("customMicrometerOptions"));
});
}
@Test @Test
void whenThereIsNoMeterRegistryThenClientResourcesCustomizationBacksOff() { void whenThereIsNoMeterRegistryThenClientResourcesCustomizationBacksOff() {
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class)).run((context) -> { this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class)).run((context) -> {
@ -58,4 +75,14 @@ class LettuceMetricsAutoConfigurationTests {
}); });
} }
@Configuration(proxyBeanMethods = false)
static class CustomMicrometerOptionsConfiguration {
@Bean
MicrometerOptions customMicrometerOptions() {
return MicrometerOptions.create();
}
}
} }

Loading…
Cancel
Save