diff --git a/sdk/all/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java b/sdk/all/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java index 1193b98e166..f4fc5f93753 100644 --- a/sdk/all/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java +++ b/sdk/all/src/test/java/io/opentelemetry/sdk/OpenTelemetrySdkTest.java @@ -26,6 +26,7 @@ import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.IdGenerator; import io.opentelemetry.sdk.trace.SdkTracerProvider; +import io.opentelemetry.sdk.trace.SpanProcessor; import io.opentelemetry.sdk.trace.config.TraceConfig; import io.opentelemetry.sdk.trace.samplers.Sampler; import org.junit.jupiter.api.Test; @@ -41,6 +42,7 @@ class OpenTelemetrySdkTest { @Mock private MeterSdkProvider meterProvider; @Mock private ContextPropagators propagators; @Mock private Clock clock; + @Mock private SpanProcessor spanProcessor; @Test void testGetGlobal() { diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProvider.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProvider.java index bb6ff2cad81..837def790ec 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProvider.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProvider.java @@ -13,6 +13,7 @@ import io.opentelemetry.sdk.internal.ComponentRegistry; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.config.TraceConfig; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Nullable; @@ -40,8 +41,13 @@ public static SdkTracerProviderBuilder builder() { } SdkTracerProvider( - Clock clock, IdGenerator idsGenerator, Resource resource, TraceConfig traceConfig) { - this.sharedState = new TracerSharedState(clock, idsGenerator, resource, traceConfig); + Clock clock, + IdGenerator idsGenerator, + Resource resource, + TraceConfig traceConfig, + List spanProcessors) { + this.sharedState = + new TracerSharedState(clock, idsGenerator, resource, traceConfig, spanProcessors); this.tracerSdkComponentRegistry = new ComponentRegistry<>( instrumentationLibraryInfo -> new SdkTracer(sharedState, instrumentationLibraryInfo)); diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilder.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilder.java index d28943c0b72..f732b76124e 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilder.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProviderBuilder.java @@ -9,10 +9,13 @@ import io.opentelemetry.sdk.internal.SystemClock; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.trace.config.TraceConfig; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; /** Builder of {@link SdkTracerProvider}. */ public final class SdkTracerProviderBuilder { + private final List spanProcessors = new ArrayList<>(); private Clock clock = SystemClock.getInstance(); private IdGenerator idsGenerator = IdGenerator.random(); @@ -67,13 +70,23 @@ public SdkTracerProviderBuilder setTraceConfig(TraceConfig traceConfig) { return this; } + /** + * Add a SpanProcessor to the span pipeline that will be built. + * + * @return this + */ + public SdkTracerProviderBuilder addSpanProcessor(SpanProcessor spanProcessor) { + spanProcessors.add(spanProcessor); + return this; + } + /** * Create a new TraceSdkProvider instance. * * @return An initialized TraceSdkProvider. */ public SdkTracerProvider build() { - return new SdkTracerProvider(clock, idsGenerator, resource, traceConfig); + return new SdkTracerProvider(clock, idsGenerator, resource, traceConfig, spanProcessors); } SdkTracerProviderBuilder() {} diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/TracerSharedState.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/TracerSharedState.java index 0fa38aa5878..d3c9cce4ce1 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/TracerSharedState.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/TracerSharedState.java @@ -30,11 +30,16 @@ final class TracerSharedState { private final List registeredSpanProcessors = new ArrayList<>(); TracerSharedState( - Clock clock, IdGenerator idGenerator, Resource resource, TraceConfig traceConfig) { + Clock clock, + IdGenerator idGenerator, + Resource resource, + TraceConfig traceConfig, + List spanProcessors) { this.clock = clock; this.idGenerator = idGenerator; this.resource = resource; this.activeTraceConfig = traceConfig; + spanProcessors.forEach(this::addSpanProcessor); } Clock getClock() {