Skip to content

Commit 0854d4b

Browse files
artembilanspring-builds
authored andcommitted
GH-9335: Fix MicrometerNodeEnhancer for a primary MeterRegistry bean
Fixes: #9335 Spring Boot provides a `CompositeMeterRegistry` bean marked with a `@Primary`. So, this one has to be autowired whenever we would like to deal with application environment. The `MicrometerNodeEnhancer` has a flaw to skip `MeterRegistry` injection if we have more than one `MeterRegistry` bean. * Fix `MicrometerNodeEnhancer` logic to use `ObjectProvider.getIfUnique()` API which is able to deal with the `primary` properly. Otherwise, `null` as it was before (cherry picked from commit e0472d8)
1 parent 102ac08 commit 0854d4b

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

spring-integration-core/src/main/java/org/springframework/integration/graph/MicrometerNodeEnhancer.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2023 the original author or authors.
2+
* Copyright 2019-2024 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.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.integration.graph;
1818

19-
import java.util.Map;
2019
import java.util.concurrent.TimeUnit;
2120

2221
import io.micrometer.common.docs.KeyName;
@@ -26,6 +25,7 @@
2625
import io.micrometer.core.instrument.search.Search;
2726
import io.micrometer.observation.ObservationConvention;
2827

28+
import org.springframework.beans.factory.ObjectProvider;
2929
import org.springframework.context.ApplicationContext;
3030
import org.springframework.integration.support.management.IntegrationManagement;
3131
import org.springframework.integration.support.management.observation.DefaultMessageReceiverObservationConvention;
@@ -58,13 +58,8 @@ public class MicrometerNodeEnhancer {
5858
private final MeterRegistry registry;
5959

6060
MicrometerNodeEnhancer(ApplicationContext applicationContext) {
61-
Map<String, MeterRegistry> registries = applicationContext.getBeansOfType(MeterRegistry.class, false, false);
62-
if (registries.size() == 1) {
63-
this.registry = registries.values().iterator().next();
64-
}
65-
else {
66-
this.registry = null;
67-
}
61+
ObjectProvider<MeterRegistry> meterRegistryProvider = applicationContext.getBeanProvider(MeterRegistry.class);
62+
this.registry = meterRegistryProvider.getIfUnique();
6863
}
6964

7065
/**

spring-integration-core/src/test/java/org/springframework/integration/graph/IntegrationGraphServerTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.context.annotation.Configuration;
4040
import org.springframework.context.annotation.ImportResource;
4141
import org.springframework.context.annotation.ImportRuntimeHints;
42+
import org.springframework.context.annotation.Primary;
4243
import org.springframework.expression.spel.standard.SpelExpressionParser;
4344
import org.springframework.integration.annotation.Filter;
4445
import org.springframework.integration.annotation.InboundChannelAdapter;
@@ -80,6 +81,7 @@
8081

8182
import static org.assertj.core.api.Assertions.assertThat;
8283
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
84+
import static org.mockito.Mockito.mock;
8385

8486
/**
8587
* @author Gary Russell
@@ -296,10 +298,17 @@ void timersViaObservationArePopulated() {
296298
public static class Config {
297299

298300
@Bean
301+
@Primary
299302
public static MeterRegistry meterRegistry() {
300303
return new SimpleMeterRegistry();
301304
}
302305

306+
// To be sure that @Primary one wins for the MicrometerNodeEnhancer
307+
@Bean
308+
public static MeterRegistry mockRegistry() {
309+
return mock();
310+
}
311+
303312
@Bean
304313
public ObservationRegistry observationRegistry(MeterRegistry meterRegistry) {
305314
ObservationRegistry registry = ObservationRegistry.create();

0 commit comments

Comments
 (0)