|
|
|
@ -37,6 +37,7 @@ import org.springframework.boot.test.util.TestPropertyValues;
|
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
|
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
|
|
|
|
|
import org.springframework.core.Ordered;
|
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
import org.springframework.test.context.ContextConfigurationAttributes;
|
|
|
|
|
import org.springframework.test.context.ContextCustomizer;
|
|
|
|
|
import org.springframework.test.context.ContextCustomizerFactory;
|
|
|
|
@ -45,8 +46,9 @@ import org.springframework.test.context.TestContextAnnotationUtils;
|
|
|
|
|
import org.springframework.util.ClassUtils;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@link ContextCustomizerFactory} that globally disables metrics export and tracing
|
|
|
|
|
* unless {@link AutoConfigureObservability} is set on the test class.
|
|
|
|
|
* {@link ContextCustomizerFactory} that globally disables metrics export and tracing in
|
|
|
|
|
* tests. The behaviour can be controlled with {@link AutoConfigureObservability} on the
|
|
|
|
|
* test class or via the {@value #AUTO_CONFIGURE_PROPERTY} property.
|
|
|
|
|
* <p>
|
|
|
|
|
* Registers {@link Tracer#NOOP} if tracing is disabled, micrometer-tracing is on the
|
|
|
|
|
* classpath, and the user hasn't supplied their own {@link Tracer}.
|
|
|
|
@ -56,43 +58,53 @@ import org.springframework.util.ClassUtils;
|
|
|
|
|
*/
|
|
|
|
|
class ObservabilityContextCustomizerFactory implements ContextCustomizerFactory {
|
|
|
|
|
|
|
|
|
|
static final String AUTO_CONFIGURE_PROPERTY = "spring.test.observability.auto-configure";
|
|
|
|
|
|
|
|
|
|
@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());
|
|
|
|
|
return new DisableObservabilityContextCustomizer(annotation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class DisableObservabilityContextCustomizer implements ContextCustomizer {
|
|
|
|
|
|
|
|
|
|
private final boolean disableMetrics;
|
|
|
|
|
|
|
|
|
|
private final boolean disableTracing;
|
|
|
|
|
private final AutoConfigureObservability annotation;
|
|
|
|
|
|
|
|
|
|
DisableObservabilityContextCustomizer(boolean disableMetrics, boolean disableTracing) {
|
|
|
|
|
this.disableMetrics = disableMetrics;
|
|
|
|
|
this.disableTracing = disableTracing;
|
|
|
|
|
DisableObservabilityContextCustomizer(AutoConfigureObservability annotation) {
|
|
|
|
|
this.annotation = annotation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void customizeContext(ConfigurableApplicationContext context,
|
|
|
|
|
MergedContextConfiguration mergedContextConfiguration) {
|
|
|
|
|
if (this.disableMetrics) {
|
|
|
|
|
if (areMetricsDisabled(context.getEnvironment())) {
|
|
|
|
|
TestPropertyValues
|
|
|
|
|
.of("management.defaults.metrics.export.enabled=false",
|
|
|
|
|
"management.simple.metrics.export.enabled=true")
|
|
|
|
|
.applyTo(context);
|
|
|
|
|
}
|
|
|
|
|
if (this.disableTracing) {
|
|
|
|
|
if (isTracingDisabled(context.getEnvironment())) {
|
|
|
|
|
TestPropertyValues.of("management.tracing.enabled=false").applyTo(context);
|
|
|
|
|
registerNoopTracer(context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean areMetricsDisabled(Environment environment) {
|
|
|
|
|
if (this.annotation != null) {
|
|
|
|
|
return !this.annotation.metrics();
|
|
|
|
|
}
|
|
|
|
|
return !environment.getProperty(AUTO_CONFIGURE_PROPERTY, Boolean.class, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isTracingDisabled(Environment environment) {
|
|
|
|
|
if (this.annotation != null) {
|
|
|
|
|
return !this.annotation.tracing();
|
|
|
|
|
}
|
|
|
|
|
return !environment.getProperty(AUTO_CONFIGURE_PROPERTY, Boolean.class, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void registerNoopTracer(ConfigurableApplicationContext context) {
|
|
|
|
|
if (AotDetector.useGeneratedArtifacts()) {
|
|
|
|
|
return;
|
|
|
|
@ -121,12 +133,12 @@ class ObservabilityContextCustomizerFactory implements ContextCustomizerFactory
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
DisableObservabilityContextCustomizer that = (DisableObservabilityContextCustomizer) o;
|
|
|
|
|
return this.disableMetrics == that.disableMetrics && this.disableTracing == that.disableTracing;
|
|
|
|
|
return Objects.equals(this.annotation, that.annotation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int hashCode() {
|
|
|
|
|
return Objects.hash(this.disableMetrics, this.disableTracing);
|
|
|
|
|
return Objects.hash(this.annotation);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|