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