Add @AutoConfigureObservability annotation

This annotation is read by ObservabilityContextCustomizerFactory, which
then sets test properties depending on the annotation attributes.

@AutoConfigureMetrics is deprecated, to support backwards compatability
it's now meta-annotated with @AutoConfigureObservability

See gh-31308
This commit is contained in:
Moritz Halbritter
2022-06-10 11:36:55 +02:00
parent 36f01eb40b
commit b250d8a1e4
14 changed files with 453 additions and 149 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 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.
@@ -23,17 +23,22 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.test.autoconfigure.actuate.observability.AutoConfigureObservability;
/**
* Annotation that can be applied to a test class to enable auto-configuration for metrics
* exporters.
*
* @author Chris Bono
* @since 2.4.0
* @deprecated use {@link AutoConfigureObservability} instead
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Deprecated
@AutoConfigureObservability(tracing = false)
public @interface AutoConfigureMetrics {
}

View File

@@ -1,66 +0,0 @@
/*
* Copyright 2012-2022 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.boot.test.autoconfigure.actuate.metrics;
import java.util.List;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextAnnotationUtils;
/**
* {@link ContextCustomizerFactory} that globally disables metrics export unless
* {@link AutoConfigureMetrics} is set on the test class.
*
* @author Chris Bono
*/
class MetricsExportContextCustomizerFactory implements ContextCustomizerFactory {
@Override
public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {
boolean disableMetricsExport = TestContextAnnotationUtils.findMergedAnnotation(testClass,
AutoConfigureMetrics.class) == null;
return disableMetricsExport ? new DisableMetricExportContextCustomizer() : null;
}
static class DisableMetricExportContextCustomizer implements ContextCustomizer {
@Override
public void customizeContext(ConfigurableApplicationContext context,
MergedContextConfiguration mergedContextConfiguration) {
TestPropertyValues.of("management.defaults.metrics.export.enabled=false",
"management.simple.metrics.export.enabled=true").applyTo(context);
}
@Override
public boolean equals(Object obj) {
return (obj != null) && (getClass() == obj.getClass());
}
@Override
public int hashCode() {
return getClass().hashCode();
}
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright 2012-2022 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.boot.test.autoconfigure.actuate.observability;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation that can be applied to a test class to enable auto-configuration for
* observability.
*
* @author Moritz Halbritter
* @since 3.0.0
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface AutoConfigureObservability {
/**
* Whether metrics should be enabled in the test.
* @return whether metrics should be enabled in the test
*/
boolean metrics() default true;
/**
* Whether tracing should be enabled in the test.
* @return whether metrics should be enabled in the test
*/
boolean tracing() default true;
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2012-2022 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.boot.test.autoconfigure.actuate.observability;
import java.util.List;
import java.util.Objects;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContextAnnotationUtils;
/**
* {@link ContextCustomizerFactory} that globally disables metrics export unless
* {@link AutoConfigureObservability} is set on the test class.
*
* @author Chris Bono
* @author Moritz Halbritter
*/
class ObservabilityContextCustomizerFactory implements ContextCustomizerFactory {
@Override
public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {
AutoConfigureObservability annotation = TestContextAnnotationUtils.findMergedAnnotation(testClass,
AutoConfigureObservability.class);
if (annotation == null) {
return new DisableObservabilityContextCustomizer(true, true);
}
return new DisableObservabilityContextCustomizer(!annotation.metrics(), !annotation.tracing());
}
static class DisableObservabilityContextCustomizer implements ContextCustomizer {
private final boolean disableMetrics;
private final boolean disableTracing;
DisableObservabilityContextCustomizer(boolean disableMetrics, boolean disableTracing) {
this.disableMetrics = disableMetrics;
this.disableTracing = disableTracing;
}
@Override
public void customizeContext(ConfigurableApplicationContext context,
MergedContextConfiguration mergedContextConfiguration) {
if (this.disableMetrics) {
TestPropertyValues.of("management.defaults.metrics.export.enabled=false",
"management.simple.metrics.export.enabled=true").applyTo(context);
}
if (this.disableTracing) {
TestPropertyValues.of("management.tracing.enabled=false").applyTo(context);
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DisableObservabilityContextCustomizer that = (DisableObservabilityContextCustomizer) o;
return this.disableMetrics == that.disableMetrics && this.disableTracing == that.disableTracing;
}
@Override
public int hashCode() {
return Objects.hash(this.disableMetrics, this.disableTracing);
}
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2022 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.
*/
/**
* Auto-configuration for handling observability in tests.
*/
package org.springframework.boot.test.autoconfigure.actuate.observability;

View File

@@ -5,7 +5,7 @@ org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExe
# Spring Test ContextCustomizerFactories
org.springframework.test.context.ContextCustomizerFactory=\
org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory,\
org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory,\
org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory,\
org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizerFactory,\
org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizerFactory,\
org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory