diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/metric/SleuthMetricProperties.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/metric/SleuthMetricProperties.java index 4cbc67902..e40105224 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/metric/SleuthMetricProperties.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/metric/SleuthMetricProperties.java @@ -11,6 +11,16 @@ import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties("spring.sleuth.metric") public class SleuthMetricProperties { + private boolean enabled; + + public boolean isEnabled() { + return this.enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + private Span span = new Span(); public Span getSpan() { diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/metric/TraceMetricsAutoConfiguration.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/metric/TraceMetricsAutoConfiguration.java index e2bf3bbc0..55971834f 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/metric/TraceMetricsAutoConfiguration.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/metric/TraceMetricsAutoConfiguration.java @@ -16,15 +16,23 @@ package org.springframework.cloud.sleuth.metric; +import java.lang.invoke.MethodHandles; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.boot.actuate.metrics.CounterService; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; +import org.springframework.core.type.AnnotatedTypeMetadata; /** * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration} @@ -34,7 +42,7 @@ import org.springframework.context.annotation.Configuration; * @since 1.0.0 */ @Configuration -@ConditionalOnProperty(value="spring.sleuth.metrics.enabled", matchIfMissing=true) +@Conditional(TraceMetricsAutoConfiguration.PickMetricIfMetricsIsMissing.class) @EnableConfigurationProperties public class TraceMetricsAutoConfiguration { @@ -69,4 +77,34 @@ public class TraceMetricsAutoConfiguration { public SpanMetricReporter noOpSpanReporterCounterService() { return new NoOpSpanMetricReporter(); } + + static class PickMetricIfMetricsIsMissing extends SpringBootCondition { + + private static final Log log = LogFactory.getLog(MethodHandles.lookup().lookupClass()); + + static final String DEPRECATED_SPRING_SLEUTH_METRICS_ENABLED = "spring.sleuth.metrics.enabled"; + static final String SPRING_SLEUTH_METRIC_ENABLED = "spring.sleuth.metric.enabled"; + + @Override + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { + Boolean oldValue = context.getEnvironment().getProperty(DEPRECATED_SPRING_SLEUTH_METRICS_ENABLED, Boolean.class); + Boolean newValue = context.getEnvironment().getProperty(SPRING_SLEUTH_METRIC_ENABLED, Boolean.class); + if (oldValue != null) { + log.warn("You're using an old version of the metrics property. Instead of using [" + + DEPRECATED_SPRING_SLEUTH_METRICS_ENABLED + "] please use [" + SPRING_SLEUTH_METRIC_ENABLED + "]"); + return matchCondition(oldValue, DEPRECATED_SPRING_SLEUTH_METRICS_ENABLED); + } + if (newValue != null) { + return matchCondition(newValue, SPRING_SLEUTH_METRIC_ENABLED); + } + return ConditionOutcome.match("No property was passed - assuming that metrics are enabled."); + } + + private ConditionOutcome matchCondition(Boolean value, String property) { + if (Boolean.TRUE.equals(value)) { + return ConditionOutcome.match(); + } + return ConditionOutcome.noMatch("Property [" + property + "] is set to false."); + } + } } diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/metric/TraceMetricsAutoConfigurationTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/metric/TraceMetricsAutoConfigurationTests.java new file mode 100644 index 000000000..990f94fb3 --- /dev/null +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/metric/TraceMetricsAutoConfigurationTests.java @@ -0,0 +1,114 @@ +/* + * Copyright 2013-2016 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 + * + * http://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.cloud.sleuth.metric; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.BDDMockito; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.cloud.sleuth.metric.TraceMetricsAutoConfiguration.PickMetricIfMetricsIsMissing; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.type.AnnotatedTypeMetadata; +import org.springframework.mock.env.MockEnvironment; + +import static org.assertj.core.api.BDDAssertions.then; +import static org.springframework.cloud.sleuth.metric.TraceMetricsAutoConfiguration.PickMetricIfMetricsIsMissing.DEPRECATED_SPRING_SLEUTH_METRICS_ENABLED; +import static org.springframework.cloud.sleuth.metric.TraceMetricsAutoConfiguration.PickMetricIfMetricsIsMissing.SPRING_SLEUTH_METRIC_ENABLED; + +/** + * @author Marcin Grzejszczak + */ +@RunWith(MockitoJUnitRunner.class) +public class TraceMetricsAutoConfigurationTests { + + @Mock ConditionContext conditionContext; + @Mock AnnotatedTypeMetadata annotatedTypeMetadata; + MockEnvironment mockEnvironment = new MockEnvironment(); + + PickMetricIfMetricsIsMissing condition = new PickMetricIfMetricsIsMissing(); + + @Before + public void setup() { + BDDMockito.given(this.conditionContext.getEnvironment()).willReturn(this.mockEnvironment); + } + + @Test + public void should_turn_on_the_feature_when_no_explicit_one_was_provided() { + ConditionOutcome outcome = this.condition.getMatchOutcome(this.conditionContext, this.annotatedTypeMetadata); + + then(outcome.isMatch()).isTrue(); + } + + @Test + public void should_turn_on_the_feature_when_deprecated_property_is_enabled() { + this.mockEnvironment.setProperty(DEPRECATED_SPRING_SLEUTH_METRICS_ENABLED, "true"); + + ConditionOutcome outcome = this.condition.getMatchOutcome(this.conditionContext, this.annotatedTypeMetadata); + + then(outcome.isMatch()).isTrue(); + } + + @Test + public void should_turn_off_the_feature_when_deprecated_property_is_disabled() { + this.mockEnvironment.setProperty(DEPRECATED_SPRING_SLEUTH_METRICS_ENABLED, "false"); + + ConditionOutcome outcome = this.condition.getMatchOutcome(this.conditionContext, this.annotatedTypeMetadata); + + then(outcome.isMatch()).isFalse(); + } + + @Test + public void should_turn_on_the_feature_when_property_is_enabled() { + this.mockEnvironment.setProperty(SPRING_SLEUTH_METRIC_ENABLED, "true"); + + ConditionOutcome outcome = this.condition.getMatchOutcome(this.conditionContext, this.annotatedTypeMetadata); + + then(outcome.isMatch()).isTrue(); + } + + @Test + public void should_turn_off_the_feature_when_property_is_disabled() { + this.mockEnvironment.setProperty(SPRING_SLEUTH_METRIC_ENABLED, "false"); + + ConditionOutcome outcome = this.condition.getMatchOutcome(this.conditionContext, this.annotatedTypeMetadata); + + then(outcome.isMatch()).isFalse(); + } + + @Test + public void should_turn_on_the_feature_when_new_property_is_disabled_and_old_is_enabled() { + this.mockEnvironment.setProperty(DEPRECATED_SPRING_SLEUTH_METRICS_ENABLED, "true"); + this.mockEnvironment.setProperty(SPRING_SLEUTH_METRIC_ENABLED, "false"); + + ConditionOutcome outcome = this.condition.getMatchOutcome(this.conditionContext, this.annotatedTypeMetadata); + + then(outcome.isMatch()).isTrue(); + } + + @Test + public void should_turn_off_the_feature_when_new_property_is_disabled_and_old_is_enabled() { + this.mockEnvironment.setProperty(DEPRECATED_SPRING_SLEUTH_METRICS_ENABLED, "false"); + this.mockEnvironment.setProperty(SPRING_SLEUTH_METRIC_ENABLED, "true"); + + ConditionOutcome outcome = this.condition.getMatchOutcome(this.conditionContext, this.annotatedTypeMetadata); + + then(outcome.isMatch()).isFalse(); + } +} \ No newline at end of file