|
|
|
@ -34,6 +34,7 @@ import io.micrometer.observation.ObservationHandler.FirstMatchingCompositeObserv
|
|
|
|
|
import io.micrometer.observation.ObservationPredicate;
|
|
|
|
|
import io.micrometer.observation.ObservationRegistry;
|
|
|
|
|
import io.micrometer.tracing.Tracer;
|
|
|
|
|
import io.micrometer.tracing.handler.TracingAwareMeterObservationHandler;
|
|
|
|
|
import io.micrometer.tracing.handler.TracingObservationHandler;
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
import org.mockito.Answers;
|
|
|
|
@ -55,6 +56,7 @@ import static org.mockito.Mockito.mock;
|
|
|
|
|
* Tests for {@link ObservationAutoConfiguration}.
|
|
|
|
|
*
|
|
|
|
|
* @author Moritz Halbritter
|
|
|
|
|
* @author Jonatan Ivanov
|
|
|
|
|
*/
|
|
|
|
|
class ObservationAutoConfigurationTests {
|
|
|
|
|
|
|
|
|
@ -65,6 +67,16 @@ class ObservationAutoConfigurationTests {
|
|
|
|
|
private final ApplicationContextRunner tracingContextRunner = new ApplicationContextRunner()
|
|
|
|
|
.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
|
|
|
|
|
void autoConfiguresDefaultMeterObservationHandler() {
|
|
|
|
|
this.contextRunner.run((context) -> {
|
|
|
|
@ -75,9 +87,17 @@ class ObservationAutoConfigurationTests {
|
|
|
|
|
// Observation leads to a timer
|
|
|
|
|
MeterRegistry meterRegistry = context.getBean(MeterRegistry.class);
|
|
|
|
|
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
|
|
|
|
|
void autoConfiguresObservationPredicates() {
|
|
|
|
|
this.contextRunner.withUserConfiguration(ObservationPredicates.class).run((context) -> {
|
|
|
|
@ -119,6 +139,8 @@ class ObservationAutoConfigurationTests {
|
|
|
|
|
assertThat(handlers.get(1)).isInstanceOf(CustomMeterObservationHandler.class);
|
|
|
|
|
assertThat(((CustomMeterObservationHandler) handlers.get(1)).getName())
|
|
|
|
|
.isEqualTo("customMeterObservationHandler1");
|
|
|
|
|
assertThat(context).doesNotHaveBean(DefaultMeterObservationHandler.class);
|
|
|
|
|
assertThat(context).doesNotHaveBean(TracingAwareMeterObservationHandler.class);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -132,9 +154,26 @@ class ObservationAutoConfigurationTests {
|
|
|
|
|
Observation.start("test-observation", () -> customContext, observationRegistry).stop();
|
|
|
|
|
assertThat(handlers).hasSize(1);
|
|
|
|
|
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
|
|
|
|
|
void autoConfiguresObservationHandlerWhenTracingIsActive() {
|
|
|
|
|
this.tracingContextRunner.withUserConfiguration(ObservationHandlersTracing.class).run((context) -> {
|
|
|
|
@ -156,6 +195,8 @@ class ObservationAutoConfigurationTests {
|
|
|
|
|
assertThat(handlers.get(2)).isInstanceOf(CustomMeterObservationHandler.class);
|
|
|
|
|
assertThat(((CustomMeterObservationHandler) handlers.get(2)).getName())
|
|
|
|
|
.isEqualTo("customMeterObservationHandler1");
|
|
|
|
|
assertThat(context).doesNotHaveBean(TracingAwareMeterObservationHandler.class);
|
|
|
|
|
assertThat(context).doesNotHaveBean(DefaultMeterObservationHandler.class);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -236,6 +277,22 @@ 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)
|
|
|
|
|
@Import(CalledHandlersConfiguration.class)
|
|
|
|
|
static class ObservationHandlersTracing {
|
|
|
|
@ -282,6 +339,11 @@ class ObservationAutoConfigurationTests {
|
|
|
|
|
return new CustomMeterObservationHandler("customMeterObservationHandler1", calledHandlers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
Tracer tracer() {
|
|
|
|
|
return mock(Tracer.class); // simulating tracer configuration
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class CustomTracingObservationHandler implements TracingObservationHandler<Context> {
|
|
|
|
|