Revert "Merge pull request #32399 from jonatan-ivanov"

This reverts commit ad40ef3398, reversing
changes made to 6eb32647f5.
pull/32399/head
Andy Wilkinson 2 years ago
parent ad40ef3398
commit 7e9e64a2ea

@ -20,21 +20,19 @@ import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler; import io.micrometer.core.instrument.observation.DefaultMeterObservationHandler;
import io.micrometer.core.instrument.observation.MeterObservationHandler; import io.micrometer.core.instrument.observation.MeterObservationHandler;
import io.micrometer.observation.GlobalObservationConvention; import io.micrometer.observation.GlobalObservationConvention;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationHandler; import io.micrometer.observation.ObservationHandler;
import io.micrometer.observation.ObservationPredicate; import io.micrometer.observation.ObservationPredicate;
import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.ObservationRegistry;
import io.micrometer.tracing.Tracer; import io.micrometer.tracing.handler.TracingObservationHandler;
import io.micrometer.tracing.handler.TracingAwareMeterObservationHandler;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.tracing.MicrometerTracingAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -44,10 +42,9 @@ import org.springframework.context.annotation.Configuration;
* *
* @author Moritz Halbritter * @author Moritz Halbritter
* @author Brian Clozel * @author Brian Clozel
* @author Jonatan Ivanov
* @since 3.0.0 * @since 3.0.0
*/ */
@AutoConfiguration(after = { CompositeMeterRegistryAutoConfiguration.class, MicrometerTracingAutoConfiguration.class }) @AutoConfiguration(after = CompositeMeterRegistryAutoConfiguration.class)
@ConditionalOnClass(ObservationRegistry.class) @ConditionalOnClass(ObservationRegistry.class)
@EnableConfigurationProperties(ObservationProperties.class) @EnableConfigurationProperties(ObservationProperties.class)
public class ObservationAutoConfiguration { public class ObservationAutoConfiguration {
@ -70,16 +67,21 @@ public class ObservationAutoConfiguration {
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(type = "io.micrometer.tracing.Tracer") @ConditionalOnBean(MeterRegistry.class)
static class OnlyMetricsConfiguration { static class MetricsConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(MeterObservationHandler.class) @ConditionalOnMissingBean(MeterObservationHandler.class)
@ConditionalOnBean(MeterRegistry.class)
DefaultMeterObservationHandler defaultMeterObservationHandler(MeterRegistry meterRegistry) { DefaultMeterObservationHandler defaultMeterObservationHandler(MeterRegistry meterRegistry) {
return new DefaultMeterObservationHandler(meterRegistry); return new DefaultMeterObservationHandler(meterRegistry);
} }
}
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingClass("io.micrometer.tracing.handler.TracingObservationHandler")
static class OnlyMetricsConfiguration {
@Bean @Bean
OnlyMetricsObservationHandlerGrouping onlyMetricsObservationHandlerGrouping() { OnlyMetricsObservationHandlerGrouping onlyMetricsObservationHandlerGrouping() {
return new OnlyMetricsObservationHandlerGrouping(); return new OnlyMetricsObservationHandlerGrouping();
@ -88,16 +90,8 @@ public class ObservationAutoConfiguration {
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@ConditionalOnBean(Tracer.class) @ConditionalOnClass(TracingObservationHandler.class)
static class MetricsWithTracingConfiguration { static class TracingConfiguration {
@Bean
@ConditionalOnMissingBean(MeterObservationHandler.class)
@ConditionalOnBean(MeterRegistry.class)
TracingAwareMeterObservationHandler<Observation.Context> tracingAwareMeterObservationHandler(
MeterRegistry meterRegistry, Tracer tracer) {
return new TracingAwareMeterObservationHandler<>(new DefaultMeterObservationHandler(meterRegistry), tracer);
}
@Bean @Bean
TracingObservationHandlerGrouping tracingObservationHandlerGrouping() { TracingObservationHandlerGrouping tracingObservationHandlerGrouping() {

@ -34,7 +34,6 @@ import io.micrometer.observation.ObservationHandler.FirstMatchingCompositeObserv
import io.micrometer.observation.ObservationPredicate; import io.micrometer.observation.ObservationPredicate;
import io.micrometer.observation.ObservationRegistry; import io.micrometer.observation.ObservationRegistry;
import io.micrometer.tracing.Tracer; import io.micrometer.tracing.Tracer;
import io.micrometer.tracing.handler.TracingAwareMeterObservationHandler;
import io.micrometer.tracing.handler.TracingObservationHandler; import io.micrometer.tracing.handler.TracingObservationHandler;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Answers; import org.mockito.Answers;
@ -56,7 +55,6 @@ import static org.mockito.Mockito.mock;
* Tests for {@link ObservationAutoConfiguration}. * Tests for {@link ObservationAutoConfiguration}.
* *
* @author Moritz Halbritter * @author Moritz Halbritter
* @author Jonatan Ivanov
*/ */
class ObservationAutoConfigurationTests { class ObservationAutoConfigurationTests {
@ -67,16 +65,6 @@ class ObservationAutoConfigurationTests {
private final ApplicationContextRunner tracingContextRunner = new ApplicationContextRunner() private final ApplicationContextRunner tracingContextRunner = new ApplicationContextRunner()
.with(MetricsRun.simple()).withConfiguration(AutoConfigurations.of(ObservationAutoConfiguration.class)); .with(MetricsRun.simple()).withConfiguration(AutoConfigurations.of(ObservationAutoConfiguration.class));
@Test
void beansShouldNotBeSuppliedWhenMicrometerObservationIsNotOnClassPath() {
this.tracingContextRunner.withClassLoader(new FilteredClassLoader("io.micrometer.observation"))
.run((context) -> {
assertThat(context).hasSingleBean(MeterRegistry.class);
assertThat(context).doesNotHaveBean(ObservationRegistry.class);
assertThat(context).doesNotHaveBean(MeterObservationHandler.class);
});
}
@Test @Test
void autoConfiguresDefaultMeterObservationHandler() { void autoConfiguresDefaultMeterObservationHandler() {
this.contextRunner.run((context) -> { this.contextRunner.run((context) -> {
@ -87,17 +75,9 @@ class ObservationAutoConfigurationTests {
// Observation leads to a timer // Observation leads to a timer
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class); MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
assertThat(meterRegistry.get("test-observation").timer().count()).isEqualTo(1); assertThat(meterRegistry.get("test-observation").timer().count()).isEqualTo(1);
assertThat(context).hasSingleBean(DefaultMeterObservationHandler.class);
assertThat(context.getBeansOfType(ObservationHandler.class)).hasSize(1);
}); });
} }
@Test
void allowsDefaultMeterObservationHandlerToBeDisabled() {
this.contextRunner.withClassLoader(new FilteredClassLoader(MeterRegistry.class))
.run((context) -> assertThat(context).doesNotHaveBean(ObservationHandler.class));
}
@Test @Test
void autoConfiguresObservationPredicates() { void autoConfiguresObservationPredicates() {
this.contextRunner.withUserConfiguration(ObservationPredicates.class).run((context) -> { this.contextRunner.withUserConfiguration(ObservationPredicates.class).run((context) -> {
@ -139,8 +119,6 @@ class ObservationAutoConfigurationTests {
assertThat(handlers.get(1)).isInstanceOf(CustomMeterObservationHandler.class); assertThat(handlers.get(1)).isInstanceOf(CustomMeterObservationHandler.class);
assertThat(((CustomMeterObservationHandler) handlers.get(1)).getName()) assertThat(((CustomMeterObservationHandler) handlers.get(1)).getName())
.isEqualTo("customMeterObservationHandler1"); .isEqualTo("customMeterObservationHandler1");
assertThat(context).doesNotHaveBean(DefaultMeterObservationHandler.class);
assertThat(context).doesNotHaveBean(TracingAwareMeterObservationHandler.class);
}); });
} }
@ -154,26 +132,9 @@ class ObservationAutoConfigurationTests {
Observation.start("test-observation", () -> customContext, observationRegistry).stop(); Observation.start("test-observation", () -> customContext, observationRegistry).stop();
assertThat(handlers).hasSize(1); assertThat(handlers).hasSize(1);
assertThat(handlers.get(0)).isInstanceOf(ObservationHandlerWithCustomContext.class); assertThat(handlers.get(0)).isInstanceOf(ObservationHandlerWithCustomContext.class);
assertThat(context).hasSingleBean(DefaultMeterObservationHandler.class);
assertThat(context).doesNotHaveBean(TracingAwareMeterObservationHandler.class);
}); });
} }
@Test
void autoConfiguresTracingAwareMeterObservationHandler() {
this.tracingContextRunner.withUserConfiguration(CustomTracingObservationHandlers.class).run((context) -> {
ObservationRegistry observationRegistry = context.getBean(ObservationRegistry.class);
List<ObservationHandler<?>> handlers = context.getBean(CalledHandlers.class).getCalledHandlers();
// Intentionally not stopped since that will trigger additional logic in
// TracingAwareMeterObservationHandler that we don't test here
Observation.start("test-observation", observationRegistry);
assertThat(handlers).hasSize(1);
assertThat(handlers.get(0)).isInstanceOf(CustomTracingObservationHandler.class);
assertThat(context).hasSingleBean(TracingAwareMeterObservationHandler.class);
assertThat(context.getBeansOfType(ObservationHandler.class)).hasSize(2);
});
}
@Test @Test
void autoConfiguresObservationHandlerWhenTracingIsActive() { void autoConfiguresObservationHandlerWhenTracingIsActive() {
this.tracingContextRunner.withUserConfiguration(ObservationHandlersTracing.class).run((context) -> { this.tracingContextRunner.withUserConfiguration(ObservationHandlersTracing.class).run((context) -> {
@ -195,8 +156,6 @@ class ObservationAutoConfigurationTests {
assertThat(handlers.get(2)).isInstanceOf(CustomMeterObservationHandler.class); assertThat(handlers.get(2)).isInstanceOf(CustomMeterObservationHandler.class);
assertThat(((CustomMeterObservationHandler) handlers.get(2)).getName()) assertThat(((CustomMeterObservationHandler) handlers.get(2)).getName())
.isEqualTo("customMeterObservationHandler1"); .isEqualTo("customMeterObservationHandler1");
assertThat(context).doesNotHaveBean(TracingAwareMeterObservationHandler.class);
assertThat(context).doesNotHaveBean(DefaultMeterObservationHandler.class);
}); });
} }
@ -277,22 +236,6 @@ class ObservationAutoConfigurationTests {
} }
@Configuration(proxyBeanMethods = false)
@Import(CalledHandlersConfiguration.class)
static class CustomTracingObservationHandlers {
@Bean
CustomTracingObservationHandler customTracingHandler1(CalledHandlers calledHandlers) {
return new CustomTracingObservationHandler("customTracingHandler1", calledHandlers);
}
@Bean
Tracer tracer() {
return mock(Tracer.class); // simulating tracer configuration
}
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@Import(CalledHandlersConfiguration.class) @Import(CalledHandlersConfiguration.class)
static class ObservationHandlersTracing { static class ObservationHandlersTracing {
@ -339,11 +282,6 @@ class ObservationAutoConfigurationTests {
return new CustomMeterObservationHandler("customMeterObservationHandler1", calledHandlers); return new CustomMeterObservationHandler("customMeterObservationHandler1", calledHandlers);
} }
@Bean
Tracer tracer() {
return mock(Tracer.class); // simulating tracer configuration
}
} }
private static class CustomTracingObservationHandler implements TracingObservationHandler<Context> { private static class CustomTracingObservationHandler implements TracingObservationHandler<Context> {

Loading…
Cancel
Save