Skip to content

Commit

Permalink
GH-3643: Defer MeterRegistry bean dependency
Browse files Browse the repository at this point in the history
Fixes #3643

The `ImportBeanDefinitionRegistrar` register its bean definition in the early phase.
It causes a problem with Spring Boot when an explicit `@EnableIntegrationManagement`
is declared: the `MeterRegistry` is provided later on via auto-configuration
and `MicrometerMetricsCaptor` has no knowledge about `MeterRegistry` at the
`ImportBeanDefinitionRegistrar` phase.

* Reworks `MicrometerMetricsCaptorRegistrar` to the `MicrometerMetricsCaptorConfiguration`
and use special conditional `MicrometerMetricsCaptorImportSelector` for that
`@EnableIntegrationManagement`.
This way a `MicrometerMetricsCaptor` `@Bean` consults `ObjectProvider<MeterRegistry>`
on its creating time and returns `null` if no `MeterRegistry` beans at all.
  • Loading branch information
artembilan authored and garyrussell committed Oct 18, 2021
1 parent a03af08 commit 72b9e70
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2020 the original author or authors.
* Copyright 2014-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,7 @@
import java.lang.annotation.Target;

import org.springframework.context.annotation.Import;
import org.springframework.integration.support.management.micrometer.MicrometerMetricsCaptorRegistrar;
import org.springframework.integration.support.management.micrometer.MicrometerMetricsCaptorImportSelector;

/**
* Enables default configuring of management in Spring Integration components in an existing application.
Expand All @@ -39,7 +39,7 @@
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({ MicrometerMetricsCaptorRegistrar.class, IntegrationManagementConfiguration.class })
@Import({ MicrometerMetricsCaptorImportSelector.class, IntegrationManagementConfiguration.class })
public @interface EnableIntegrationManagement {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import org.springframework.integration.router.RecipientListRouterManagement;
import org.springframework.integration.support.context.NamedComponent;
import org.springframework.integration.support.management.MappingMessageRouterManagement;
import org.springframework.integration.support.management.micrometer.MicrometerMetricsCaptorRegistrar;
import org.springframework.integration.support.management.micrometer.MicrometerMetricsCaptorConfiguration;
import org.springframework.integration.support.utils.IntegrationUtils;
import org.springframework.lang.Nullable;
import org.springframework.messaging.MessageChannel;
Expand Down Expand Up @@ -159,7 +159,7 @@ protected <T> Map<String, T> getBeansOfType(Class<T> type) {
}

private synchronized Graph buildGraph() {
if (micrometerEnhancer == null && MicrometerMetricsCaptorRegistrar.METER_REGISTRY_PRESENT) {
if (micrometerEnhancer == null && MicrometerMetricsCaptorConfiguration.METER_REGISTRY_PRESENT) {
micrometerEnhancer = new MicrometerNodeEnhancer(this.applicationContext);
}
String implementationVersion = IntegrationGraphServer.class.getPackage().getImplementationVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

package org.springframework.integration.support.management.micrometer;

import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.ClassUtils;

import io.micrometer.core.instrument.MeterRegistry;
Expand All @@ -35,7 +33,8 @@
*
* @since 5.2.9
*/
public class MicrometerMetricsCaptorRegistrar implements ImportBeanDefinitionRegistrar {
@Configuration(proxyBeanMethods = false)
public class MicrometerMetricsCaptorConfiguration {

/**
* A {@code boolean} flag to indicate if the
Expand All @@ -45,18 +44,13 @@ public class MicrometerMetricsCaptorRegistrar implements ImportBeanDefinitionReg
public static final boolean METER_REGISTRY_PRESENT =
ClassUtils.isPresent("io.micrometer.core.instrument.MeterRegistry", null);

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
ListableBeanFactory beanFactory = (ListableBeanFactory) registry;
if (METER_REGISTRY_PRESENT
&& !registry.containsBeanDefinition(MicrometerMetricsCaptor.MICROMETER_CAPTOR_NAME)
&& beanFactory.getBeanNamesForType(MeterRegistry.class, false, false).length > 0) {

registry.registerBeanDefinition(MicrometerMetricsCaptor.MICROMETER_CAPTOR_NAME,
BeanDefinitionBuilder.genericBeanDefinition(MicrometerMetricsCaptor.class,
() -> new MicrometerMetricsCaptor(beanFactory.getBeanProvider(MeterRegistry.class)))
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
.getBeanDefinition());
@Bean(name = MicrometerMetricsCaptor.MICROMETER_CAPTOR_NAME)
public MicrometerMetricsCaptor micrometerMetricsCaptor(ObjectProvider<MeterRegistry> meterRegistries) {
if (meterRegistries.stream().findAny().isPresent()) {
return new MicrometerMetricsCaptor(meterRegistries);
}
else {
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.integration.support.management.micrometer;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

/**
* An {@link ImportSelector} to conditionally add a {@link MicrometerMetricsCaptorConfiguration}
* bean when {@code io.micrometer.core.instrument.MeterRegistry} is present in classpath.
*
* @author Artem Bilan
*
* @since 5.5.5
*/
public class MicrometerMetricsCaptorImportSelector implements ImportSelector {

@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
if (MicrometerMetricsCaptorConfiguration.METER_REGISTRY_PRESENT) {
return new String[]{ MicrometerMetricsCaptorConfiguration.class.getName() };
}
else {
return new String[0];
}
}

}

0 comments on commit 72b9e70

Please sign in to comment.