Skip to content

Commit ed0a492

Browse files
garyrussellartembilan
authored andcommitted
GH-2148: Use ObjectProvider to Locate MeterRegistry
**cherry-pick to 2.9.x, 2.8.x**
1 parent 7d19b94 commit ed0a492

File tree

2 files changed

+23
-51
lines changed

2 files changed

+23
-51
lines changed

spring-kafka/src/main/java/org/springframework/kafka/support/micrometer/MicrometerHolder.java

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,11 @@
1616

1717
package org.springframework.kafka.support.micrometer;
1818

19-
import java.util.Collections;
2019
import java.util.Map;
21-
import java.util.Map.Entry;
2220
import java.util.concurrent.ConcurrentHashMap;
2321

24-
import org.springframework.beans.factory.config.BeanDefinition;
25-
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
22+
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
2623
import org.springframework.context.ApplicationContext;
27-
import org.springframework.context.ConfigurableApplicationContext;
2824
import org.springframework.lang.Nullable;
2925

3026
import io.micrometer.core.instrument.MeterRegistry;
@@ -69,50 +65,22 @@ public MicrometerHolder(@Nullable ApplicationContext context, String name,
6965
if (context == null) {
7066
throw new IllegalStateException("No micrometer registry present");
7167
}
72-
Map<String, MeterRegistry> registries = context.getBeansOfType(MeterRegistry.class, false, false);
73-
this.timerName = timerName;
74-
this.timerDesc = timerDesc;
75-
this.name = name;
76-
this.tags = tags;
77-
registries = filterRegistries(registries, context);
78-
if (registries.size() == 1) {
79-
this.registry = registries.values().iterator().next();
80-
buildTimer(NONE_EXCEPTION_METERS_KEY);
81-
}
82-
else {
83-
throw new IllegalStateException("No micrometer registry present (or more than one and "
84-
+ "none marked @Primary)");
85-
}
86-
}
87-
88-
private Map<String, MeterRegistry> filterRegistries(Map<String, MeterRegistry> registries,
89-
ApplicationContext context) {
90-
91-
if (registries.size() == 1) {
92-
return registries;
68+
try {
69+
this.registry = context.getBeanProvider(MeterRegistry.class).getIfUnique();
9370
}
94-
MeterRegistry primary = null;
95-
if (context instanceof ConfigurableApplicationContext) {
96-
BeanDefinitionRegistry bdr = (BeanDefinitionRegistry) ((ConfigurableApplicationContext) context)
97-
.getBeanFactory();
98-
for (Entry<String, MeterRegistry> entry : registries.entrySet()) {
99-
BeanDefinition beanDefinition = bdr.getBeanDefinition(entry.getKey());
100-
if (beanDefinition.isPrimary()) {
101-
if (primary != null) {
102-
primary = null;
103-
break;
104-
}
105-
else {
106-
primary = entry.getValue();
107-
}
108-
}
109-
}
71+
catch (NoUniqueBeanDefinitionException ex) {
72+
throw new IllegalStateException(ex);
11073
}
111-
if (primary != null) {
112-
return Collections.singletonMap("primary", primary);
74+
if (this.registry != null) {
75+
this.timerName = timerName;
76+
this.timerDesc = timerDesc;
77+
this.name = name;
78+
this.tags = tags;
79+
buildTimer(NONE_EXCEPTION_METERS_KEY);
11380
}
11481
else {
115-
return registries;
82+
throw new IllegalStateException("No micrometer registry present (or more than one and "
83+
+ "there is not exactly one marked with @Primary)");
11684
}
11785
}
11886

spring-kafka/src/test/java/org/springframework/kafka/support/micrometer/MicrometerHolderTests.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2121
import static org.mockito.ArgumentMatchers.any;
22-
import static org.mockito.ArgumentMatchers.anyBoolean;
2322
import static org.mockito.BDDMockito.given;
2423
import static org.mockito.Mockito.mock;
2524
import static org.mockito.Mockito.times;
@@ -31,6 +30,7 @@
3130

3231
import org.junit.jupiter.api.Test;
3332

33+
import org.springframework.beans.factory.ObjectProvider;
3434
import org.springframework.context.ApplicationContext;
3535
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3636
import org.springframework.context.annotation.Bean;
@@ -51,9 +51,10 @@ public class MicrometerHolderTests {
5151
void testMicrometerHolderRecordSuccessWorksGracefullyAfterDestroy() {
5252
MeterRegistry meterRegistry = new SimpleMeterRegistry();
5353
ApplicationContext ctx = mock(ApplicationContext.class);
54+
ObjectProvider<MeterRegistry> beanProvider = mock(ObjectProvider.class);
55+
given(ctx.getBeanProvider(MeterRegistry.class)).willReturn(beanProvider);
5456
Timer.Sample sample = mock(Timer.Sample.class);
55-
given(ctx.getBeansOfType(any(), anyBoolean(), anyBoolean()))
56-
.willReturn(Collections.singletonMap("registry", meterRegistry));
57+
given(beanProvider.getIfUnique()).willReturn(meterRegistry);
5758

5859
MicrometerHolder micrometerHolder = new MicrometerHolder(ctx, "holderName",
5960
"timerName", "timerDesc", Collections.emptyMap());
@@ -68,21 +69,24 @@ void testMicrometerHolderRecordSuccessWorksGracefullyAfterDestroy() {
6869

6970
micrometerHolder.success(sample);
7071

71-
verify(ctx, times(1)).getBeansOfType(any(), anyBoolean(), anyBoolean());
72+
verify(ctx, times(1)).getBeanProvider(any(Class.class));
7273
verify(sample, times(1)).stop(any(Timer.class));
7374
verifyNoMoreInteractions(ctx, sample);
7475
}
7576

7677
@Test
7778
void multiReg() {
7879
assertThatIllegalStateException().isThrownBy(() -> new MicrometerHolder(
79-
new AnnotationConfigApplicationContext(Config1.class), "", "", "", Collections.emptyMap()));
80+
new AnnotationConfigApplicationContext(Config1.class), "", "", "", Collections.emptyMap()))
81+
.withMessage("No micrometer registry present (or more than one and "
82+
+ "there is not exactly one marked with @Primary)");
8083
}
8184

8285
@Test
8386
void twoPrimaries() {
8487
assertThatIllegalStateException().isThrownBy(() -> new MicrometerHolder(
85-
new AnnotationConfigApplicationContext(Config2.class), "", "", "", Collections.emptyMap()));
88+
new AnnotationConfigApplicationContext(Config2.class), "", "", "", Collections.emptyMap()))
89+
.withMessageContaining("more than one 'primary' bean");
8690
}
8791

8892
@Test

0 commit comments

Comments
 (0)