Support overriding OTel SpanExporters

See gh-35596
pull/35935/head
Moritz Halbritter 1 year ago
parent d51559956f
commit 929283f4dc

@ -133,16 +133,22 @@ public class OpenTelemetryAutoConfiguration {
} }
@Bean @Bean
BatchSpanProcessor otelSpanProcessor(ObjectProvider<SpanExporter> spanExporters, BatchSpanProcessor otelSpanProcessor(SpanExporters spanExporters,
ObjectProvider<SpanExportingPredicate> spanExportingPredicates, ObjectProvider<SpanReporter> spanReporters, ObjectProvider<SpanExportingPredicate> spanExportingPredicates, ObjectProvider<SpanReporter> spanReporters,
ObjectProvider<SpanFilter> spanFilters, ObjectProvider<MeterProvider> meterProvider) { ObjectProvider<SpanFilter> spanFilters, ObjectProvider<MeterProvider> meterProvider) {
BatchSpanProcessorBuilder builder = BatchSpanProcessor.builder(new CompositeSpanExporter( BatchSpanProcessorBuilder builder = BatchSpanProcessor.builder(
spanExporters.orderedStream().toList(), spanExportingPredicates.orderedStream().toList(), new CompositeSpanExporter(spanExporters.getList(), spanExportingPredicates.orderedStream().toList(),
spanReporters.orderedStream().toList(), spanFilters.orderedStream().toList())); spanReporters.orderedStream().toList(), spanFilters.orderedStream().toList()));
meterProvider.ifAvailable(builder::setMeterProvider); meterProvider.ifAvailable(builder::setMeterProvider);
return builder.build(); return builder.build();
} }
@Bean
@ConditionalOnMissingBean
SpanExporters spanExporters(ObjectProvider<SpanExporter> spanExporters) {
return SpanExporters.of(spanExporters.orderedStream().collect(Collectors.toList()));
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
Tracer otelTracer(OpenTelemetry openTelemetry) { Tracer otelTracer(OpenTelemetry openTelemetry) {

@ -0,0 +1,70 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.tracing;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import io.opentelemetry.sdk.trace.export.SpanExporter;
/**
* A collection of {@link SpanExporter span exporters}.
*
* @author Moritz Halbritter
* @since 3.2.0
*/
public interface SpanExporters extends Iterable<SpanExporter> {
/**
* Returns the list of {@link SpanExporter span exporters}.
* @return the list of span exporters
*/
List<SpanExporter> getList();
@Override
default Iterator<SpanExporter> iterator() {
return getList().iterator();
}
@Override
default Spliterator<SpanExporter> spliterator() {
return getList().spliterator();
}
/**
* Constructs a {@link SpanExporters} instance with the given list of
* {@link SpanExporter span exporters}.
* @param spanExporters the list of span exporters
* @return the constructed {@link SpanExporters} instance
*/
static SpanExporters of(List<SpanExporter> spanExporters) {
return () -> spanExporters;
}
/**
* Constructs a {@link SpanExporters} instance with the given {@link SpanExporter span
* exporters}.
* @param spanExporters the span exporters
* @return the constructed {@link SpanExporters} instance
*/
static SpanExporters of(SpanExporter... spanExporters) {
return of(Arrays.asList(spanExporters));
}
}

@ -17,6 +17,7 @@
package org.springframework.boot.actuate.autoconfigure.tracing; package org.springframework.boot.actuate.autoconfigure.tracing;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import io.micrometer.tracing.SpanCustomizer; import io.micrometer.tracing.SpanCustomizer;
@ -35,9 +36,12 @@ import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators; import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapPropagator; import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.extension.trace.propagation.B3Propagator; import io.opentelemetry.extension.trace.propagation.B3Propagator;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.SdkTracerProvider; import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.SpanLimits; import io.opentelemetry.sdk.trace.SpanLimits;
import io.opentelemetry.sdk.trace.SpanProcessor; import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.sdk.trace.samplers.Sampler; import io.opentelemetry.sdk.trace.samplers.Sampler;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
@ -88,6 +92,7 @@ class OpenTelemetryAutoConfigurationTests {
assertThat(context).hasSingleBean(TextMapPropagator.class); assertThat(context).hasSingleBean(TextMapPropagator.class);
assertThat(context).hasSingleBean(OtelSpanCustomizer.class); assertThat(context).hasSingleBean(OtelSpanCustomizer.class);
assertThat(context).hasSingleBean(SpanProcessors.class); assertThat(context).hasSingleBean(SpanProcessors.class);
assertThat(context).hasSingleBean(SpanExporters.class);
}); });
} }
@ -119,6 +124,7 @@ class OpenTelemetryAutoConfigurationTests {
assertThat(context).doesNotHaveBean(TextMapPropagator.class); assertThat(context).doesNotHaveBean(TextMapPropagator.class);
assertThat(context).doesNotHaveBean(OtelSpanCustomizer.class); assertThat(context).doesNotHaveBean(OtelSpanCustomizer.class);
assertThat(context).doesNotHaveBean(SpanProcessors.class); assertThat(context).doesNotHaveBean(SpanProcessors.class);
assertThat(context).doesNotHaveBean(SpanExporters.class);
}); });
} }
@ -151,6 +157,8 @@ class OpenTelemetryAutoConfigurationTests {
assertThat(context).hasSingleBean(SpanCustomizer.class); assertThat(context).hasSingleBean(SpanCustomizer.class);
assertThat(context).hasBean("customSpanProcessors"); assertThat(context).hasBean("customSpanProcessors");
assertThat(context).hasSingleBean(SpanProcessors.class); assertThat(context).hasSingleBean(SpanProcessors.class);
assertThat(context).hasBean("customSpanExporters");
assertThat(context).hasSingleBean(SpanExporters.class);
}); });
} }
@ -164,6 +172,17 @@ class OpenTelemetryAutoConfigurationTests {
}); });
} }
@Test
void shouldAllowMultipleSpanExporters() {
this.contextRunner.withUserConfiguration(MultipleSpanExporterConfiguration.class).run((context) -> {
assertThat(context.getBeansOfType(SpanExporter.class)).hasSize(2);
assertThat(context).hasBean("spanExporter1");
assertThat(context).hasBean("spanExporter2");
SpanExporters spanExporters = context.getBean(SpanExporters.class);
assertThat(spanExporters).hasSize(2);
});
}
@Test @Test
void shouldAllowMultipleTextMapPropagators() { void shouldAllowMultipleTextMapPropagators() {
this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> { this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> {
@ -228,15 +247,6 @@ class OpenTelemetryAutoConfigurationTests {
}); });
} }
private List<TextMapPropagator> getInjectors(TextMapPropagator propagator) {
assertThat(propagator).as("propagator").isNotNull();
if (propagator instanceof CompositeTextMapPropagator compositePropagator) {
return compositePropagator.getInjectors().stream().toList();
}
fail("Expected CompositeTextMapPropagator, found %s".formatted(propagator.getClass()));
throw new AssertionError("Unreachable");
}
@Test @Test
void shouldCustomizeSdkTracerProvider() { void shouldCustomizeSdkTracerProvider() {
this.contextRunner.withUserConfiguration(SdkTracerProviderCustomizationConfiguration.class).run((context) -> { this.contextRunner.withUserConfiguration(SdkTracerProviderCustomizationConfiguration.class).run((context) -> {
@ -255,6 +265,15 @@ class OpenTelemetryAutoConfigurationTests {
}); });
} }
private List<TextMapPropagator> getInjectors(TextMapPropagator propagator) {
assertThat(propagator).as("propagator").isNotNull();
if (propagator instanceof CompositeTextMapPropagator compositePropagator) {
return compositePropagator.getInjectors().stream().toList();
}
fail("Expected CompositeTextMapPropagator, found %s".formatted(propagator.getClass()));
throw new AssertionError("Unreachable");
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
private static class MeterProviderConfiguration { private static class MeterProviderConfiguration {
@ -278,6 +297,21 @@ class OpenTelemetryAutoConfigurationTests {
} }
@Configuration(proxyBeanMethods = false)
private static class MultipleSpanExporterConfiguration {
@Bean
SpanExporter spanExporter1() {
return new DummySpanExporter();
}
@Bean
SpanExporter spanExporter2() {
return new DummySpanExporter();
}
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
private static class CustomConfiguration { private static class CustomConfiguration {
@ -286,6 +320,11 @@ class OpenTelemetryAutoConfigurationTests {
return SpanProcessors.of(mock(SpanProcessor.class)); return SpanProcessors.of(mock(SpanProcessor.class));
} }
@Bean
SpanExporters customSpanExporters() {
return SpanExporters.of(new DummySpanExporter());
}
@Bean @Bean
io.micrometer.tracing.Tracer customMicrometerTracer() { io.micrometer.tracing.Tracer customMicrometerTracer() {
return mock(io.micrometer.tracing.Tracer.class); return mock(io.micrometer.tracing.Tracer.class);
@ -381,4 +420,23 @@ class OpenTelemetryAutoConfigurationTests {
} }
private static class DummySpanExporter implements SpanExporter {
@Override
public CompletableResultCode export(Collection<SpanData> spans) {
return CompletableResultCode.ofSuccess();
}
@Override
public CompletableResultCode flush() {
return CompletableResultCode.ofSuccess();
}
@Override
public CompletableResultCode shutdown() {
return CompletableResultCode.ofSuccess();
}
}
} }

@ -0,0 +1,52 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.tracing;
import java.util.List;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link SpanExporters}.
*
* @author Moritz Halbritter
*/
class SpanExportersTests {
@Test
void ofList() {
SpanExporter spanExporter1 = mock(SpanExporter.class);
SpanExporter spanExporter2 = mock(SpanExporter.class);
SpanExporters spanExporters = SpanExporters.of(List.of(spanExporter1, spanExporter2));
assertThat(spanExporters).containsExactly(spanExporter1, spanExporter2);
assertThat(spanExporters.getList()).containsExactly(spanExporter1, spanExporter2);
}
@Test
void ofArray() {
SpanExporter spanExporter1 = mock(SpanExporter.class);
SpanExporter spanExporter2 = mock(SpanExporter.class);
SpanExporters spanExporters = SpanExporters.of(spanExporter1, spanExporter2);
assertThat(spanExporters).containsExactly(spanExporter1, spanExporter2);
assertThat(spanExporters.getList()).containsExactly(spanExporter1, spanExporter2);
}
}
Loading…
Cancel
Save