spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Auto-configure testing support for tracing

Open marcingrzejszczak opened this issue 1 year ago • 4 comments

Currently there's no out of the box way to set up a test with tracing turned on that would collect spans in a queue for further analysis. What is required now from the user is to either use a TestSpanHandler from Brave or ArrayListSpanReporter from OTel.

With the changes in this PR we're introducing an abstract concept of aggregating spans for Micrometer Tracing API.

In Spring Boot we would need the @TracingTest test slice that would turn on the @AutoConfigureObservability with tracing on and would register the TestSpanHandler as a bean.

related issue

marcingrzejszczak avatar Feb 08 '24 09:02 marcingrzejszczak

A tracing slice would limit the context to only the beans that are related to tracing. It doesn't sound like that's what we want here. Don't we just want a way to opt in to the registration of the TestSpanHandler bean, leaving all of the context's other beans pretty much as they are?

wilkinsona avatar Feb 08 '24 10:02 wilkinsona

Yeah, you're right. That would be the way to go.

marcingrzejszczak avatar Feb 08 '24 13:02 marcingrzejszczak

Just a couple of points:

Otel considerations: Currently a BatchSpanProcessor at OpenTelemetryAutoConfiguration being used. It reports the span asynchronously. I think that it should not be desirable in tests as it could cause race conditions. A way of solve the issue would be to override the SdkTracerProvider with a SimpleSpanProcessor instead:

@Bean
	SdkTracerProvider testSdkTracerProvider(ObjectProvider<SpanExporter> spanExporters,
			ObjectProvider<SpanExportingPredicate> spanExportingPredicates, ObjectProvider<SpanReporter> spanReporters,
			ObjectProvider<SpanFilter> spanFilters,
			final ObjectProvider<SdkTracerProviderBuilderCustomizer> customizers) {

		final SdkTracerProviderBuilder builder = SdkTracerProvider.builder();

		builder.addSpanProcessor(SimpleSpanProcessor.create(new CompositeSpanExporter(
				spanExporters.orderedStream().toList(), spanExportingPredicates.orderedStream().toList(),
				spanReporters.orderedStream().toList(), spanFilters.orderedStream().toList())));

		customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));

		return builder.build();
	}

Brave considerations It would be nice to auto-configure a Sampler.ALWAYS_SAMPLE for testing purposes for the user not to worry about it.

Also we would need some mechanism to clear the spans reported between tests (JUnit extension??)

JordiMartinezVicent avatar Feb 08 '24 15:02 JordiMartinezVicent

As for the test extension I guess we should summon @sbrannen

marcingrzejszczak avatar Feb 09 '24 09:02 marcingrzejszczak