Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adding span processors when building a tracer provider. #2223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<SpanProcessor> spanProcessors) {
this.sharedState =
new TracerSharedState(clock, idsGenerator, resource, traceConfig, spanProcessors);
this.tracerSdkComponentRegistry =
new ComponentRegistry<>(
instrumentationLibraryInfo -> new SdkTracer(sharedState, instrumentationLibraryInfo));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpanProcessor> spanProcessors = new ArrayList<>();

private Clock clock = SystemClock.getInstance();
private IdGenerator idsGenerator = IdGenerator.random();
Expand Down Expand Up @@ -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() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ final class TracerSharedState {
private final List<SpanProcessor> registeredSpanProcessors = new ArrayList<>();

TracerSharedState(
Clock clock, IdGenerator idGenerator, Resource resource, TraceConfig traceConfig) {
Clock clock,
IdGenerator idGenerator,
Resource resource,
TraceConfig traceConfig,
List<SpanProcessor> spanProcessors) {
this.clock = clock;
this.idGenerator = idGenerator;
this.resource = resource;
this.activeTraceConfig = traceConfig;
spanProcessors.forEach(this::addSpanProcessor);
}

Clock getClock() {
Expand Down