Skip to content

Commit

Permalink
Add a switch to disable ScheduledMethodMetrics
Browse files Browse the repository at this point in the history
This commit also makes it back off when a custom one has been defined.
  • Loading branch information
izeye authored and jkschneider committed Oct 14, 2018
1 parent 492fa69 commit dda79ab
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,6 @@ public PropertiesMeterFilter propertiesMeterFilter(MetricsProperties properties)
return new PropertiesMeterFilter(properties);
}

// If AOP is not enabled, scheduled interception will not work.
@Bean
@ConditionalOnClass(name = "org.aspectj.lang.ProceedingJoinPoint")
@ConditionalOnProperty(value = "spring.aop.enabled", havingValue = "true", matchIfMissing = true)
public ScheduledMethodMetrics metricsSchedulingAspect(MeterRegistry registry) {
return new ScheduledMethodMetrics(registry);
}

@Bean
@ConditionalOnClass(name = "com.netflix.hystrix.strategy.HystrixPlugins")
@ConditionalOnProperty(value = "management.metrics.binders.hystrix.enabled", matchIfMissing = true)
Expand Down Expand Up @@ -129,4 +121,20 @@ public SpringIntegrationMetrics springIntegrationMetrics(
return new SpringIntegrationMetrics(configurer);
}
}

@Configuration
@ConditionalOnClass(name = "org.aspectj.lang.ProceedingJoinPoint")
@ConditionalOnProperty(value = "spring.aop.enabled", havingValue = "true", matchIfMissing = true)
static class AopRequiredConfiguration {

// If AOP is not enabled, scheduled interception will not work.
@Bean
@ConditionalOnProperty(value = "management.metrics.binders.scheduled.enabled", matchIfMissing = true)
@ConditionalOnMissingBean
public ScheduledMethodMetrics metricsSchedulingAspect(MeterRegistry registry) {
return new ScheduledMethodMetrics(registry);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.List;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -31,10 +33,13 @@
import io.micrometer.core.instrument.config.MeterFilterReply;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.micrometer.spring.PropertiesMeterFilter;

import io.micrometer.spring.scheduling.ScheduledMethodMetrics;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -82,6 +87,40 @@ void configuresMeterRegistries() {
verify(context.getBean(MeterRegistryCustomizer.class)).customize(meterRegistry);
}

@Test
void backsOffWhenSpringAopEnabledIsFalse() {
EnvironmentTestUtils.addEnvironment(context, "spring.aop.enabled=false");

registerAndRefresh(BaseMeterRegistryConfiguration.class);

assertThatThrownBy(() -> context.getBean(ScheduledMethodMetrics.class))
.isInstanceOf(NoSuchBeanDefinitionException.class);
}

@Test
void backsOffWhenScheduledEnabledIsFalse() {
EnvironmentTestUtils.addEnvironment(context, "management.metrics.binders.scheduled.enabled=false");

registerAndRefresh(BaseMeterRegistryConfiguration.class);

assertThatThrownBy(() -> context.getBean(ScheduledMethodMetrics.class))
.isInstanceOf(NoSuchBeanDefinitionException.class);
}

@Test
void backsOffWhenCustomScheduledMethodMetricsIsProvided() {
registerAndRefresh(BaseMeterRegistryConfiguration.class, ScheduledMethodMetricsConfiguration.class);

assertThat(context.getBean(ScheduledMethodMetrics.class)).isInstanceOf(MyScheduledMethodMetrics.class);
}

@Test
void scheduledMethodMetricsIsAvailableByDefault() {
registerAndRefresh(BaseMeterRegistryConfiguration.class);

assertThat(context.getBean(ScheduledMethodMetrics.class)).isInstanceOf(ScheduledMethodMetrics.class);
}

private void registerAndRefresh(Class<?>... configurationClasses) {
if (configurationClasses.length > 0) {
this.context.register(configurationClasses);
Expand Down Expand Up @@ -145,4 +184,22 @@ MeterFilter denyMeterFilter() {

}

@Configuration
static class ScheduledMethodMetricsConfiguration {

@Bean
public ScheduledMethodMetrics scheduledMethodMetrics(MeterRegistry registry) {
return new MyScheduledMethodMetrics(registry);
}

}

static class MyScheduledMethodMetrics extends ScheduledMethodMetrics {

public MyScheduledMethodMetrics(MeterRegistry registry) {
super(registry);
}

}

}

0 comments on commit dda79ab

Please sign in to comment.