|
37 | 37 | import org.springframework.context.ConfigurableApplicationContext;
|
38 | 38 | import org.springframework.context.annotation.ConfigurationClassPostProcessor;
|
39 | 39 | import org.springframework.core.Ordered;
|
| 40 | +import org.springframework.core.env.Environment; |
40 | 41 | import org.springframework.test.context.ContextConfigurationAttributes;
|
41 | 42 | import org.springframework.test.context.ContextCustomizer;
|
42 | 43 | import org.springframework.test.context.ContextCustomizerFactory;
|
|
45 | 46 | import org.springframework.util.ClassUtils;
|
46 | 47 |
|
47 | 48 | /**
|
48 |
| - * {@link ContextCustomizerFactory} that globally disables metrics export and tracing |
49 |
| - * unless {@link AutoConfigureObservability} is set on the test class. |
| 49 | + * {@link ContextCustomizerFactory} that globally disables metrics export and tracing in |
| 50 | + * tests. The behaviour can be controlled with {@link AutoConfigureObservability} on the |
| 51 | + * test class or via the {@value #AUTO_CONFIGURE_PROPERTY} property. |
50 | 52 | * <p>
|
51 | 53 | * Registers {@link Tracer#NOOP} if tracing is disabled, micrometer-tracing is on the
|
52 | 54 | * classpath, and the user hasn't supplied their own {@link Tracer}.
|
|
56 | 58 | */
|
57 | 59 | class ObservabilityContextCustomizerFactory implements ContextCustomizerFactory {
|
58 | 60 |
|
| 61 | + static final String AUTO_CONFIGURE_PROPERTY = "spring.test.observability.auto-configure"; |
| 62 | + |
59 | 63 | @Override
|
60 | 64 | public ContextCustomizer createContextCustomizer(Class<?> testClass,
|
61 | 65 | List<ContextConfigurationAttributes> configAttributes) {
|
62 | 66 | AutoConfigureObservability annotation = TestContextAnnotationUtils.findMergedAnnotation(testClass,
|
63 | 67 | AutoConfigureObservability.class);
|
64 |
| - if (annotation == null) { |
65 |
| - return new DisableObservabilityContextCustomizer(true, true); |
66 |
| - } |
67 |
| - return new DisableObservabilityContextCustomizer(!annotation.metrics(), !annotation.tracing()); |
| 68 | + return new DisableObservabilityContextCustomizer(annotation); |
68 | 69 | }
|
69 | 70 |
|
70 | 71 | private static class DisableObservabilityContextCustomizer implements ContextCustomizer {
|
71 | 72 |
|
72 |
| - private final boolean disableMetrics; |
73 |
| - |
74 |
| - private final boolean disableTracing; |
| 73 | + private final AutoConfigureObservability annotation; |
75 | 74 |
|
76 |
| - DisableObservabilityContextCustomizer(boolean disableMetrics, boolean disableTracing) { |
77 |
| - this.disableMetrics = disableMetrics; |
78 |
| - this.disableTracing = disableTracing; |
| 75 | + DisableObservabilityContextCustomizer(AutoConfigureObservability annotation) { |
| 76 | + this.annotation = annotation; |
79 | 77 | }
|
80 | 78 |
|
81 | 79 | @Override
|
82 | 80 | public void customizeContext(ConfigurableApplicationContext context,
|
83 | 81 | MergedContextConfiguration mergedContextConfiguration) {
|
84 |
| - if (this.disableMetrics) { |
| 82 | + if (areMetricsDisabled(context.getEnvironment())) { |
85 | 83 | TestPropertyValues
|
86 | 84 | .of("management.defaults.metrics.export.enabled=false",
|
87 | 85 | "management.simple.metrics.export.enabled=true")
|
88 | 86 | .applyTo(context);
|
89 | 87 | }
|
90 |
| - if (this.disableTracing) { |
| 88 | + if (isTracingDisabled(context.getEnvironment())) { |
91 | 89 | TestPropertyValues.of("management.tracing.enabled=false").applyTo(context);
|
92 | 90 | registerNoopTracer(context);
|
93 | 91 | }
|
94 | 92 | }
|
95 | 93 |
|
| 94 | + private boolean areMetricsDisabled(Environment environment) { |
| 95 | + if (this.annotation != null) { |
| 96 | + return !this.annotation.metrics(); |
| 97 | + } |
| 98 | + return !environment.getProperty(AUTO_CONFIGURE_PROPERTY, Boolean.class, false); |
| 99 | + } |
| 100 | + |
| 101 | + private boolean isTracingDisabled(Environment environment) { |
| 102 | + if (this.annotation != null) { |
| 103 | + return !this.annotation.tracing(); |
| 104 | + } |
| 105 | + return !environment.getProperty(AUTO_CONFIGURE_PROPERTY, Boolean.class, false); |
| 106 | + } |
| 107 | + |
96 | 108 | private void registerNoopTracer(ConfigurableApplicationContext context) {
|
97 | 109 | if (AotDetector.useGeneratedArtifacts()) {
|
98 | 110 | return;
|
@@ -121,12 +133,12 @@ public boolean equals(Object o) {
|
121 | 133 | return false;
|
122 | 134 | }
|
123 | 135 | DisableObservabilityContextCustomizer that = (DisableObservabilityContextCustomizer) o;
|
124 |
| - return this.disableMetrics == that.disableMetrics && this.disableTracing == that.disableTracing; |
| 136 | + return Objects.equals(this.annotation, that.annotation); |
125 | 137 | }
|
126 | 138 |
|
127 | 139 | @Override
|
128 | 140 | public int hashCode() {
|
129 |
| - return Objects.hash(this.disableMetrics, this.disableTracing); |
| 141 | + return Objects.hash(this.annotation); |
130 | 142 | }
|
131 | 143 |
|
132 | 144 | }
|
|
0 commit comments