Skip to content

Commit

Permalink
Allow adding span processors when building a tracer provider. (#2223)
Browse files Browse the repository at this point in the history
* Allow adding span processors when building a tracer provider.

* Drift

* Mutate provided providers too for now.

* Spot
  • Loading branch information
Anuraag Agrawal authored Dec 18, 2020
1 parent ba90083 commit fb5b497
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 @@ -38,6 +39,7 @@ class OpenTelemetrySdkTest {
@Mock private SdkMeterProvider 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

0 comments on commit fb5b497

Please sign in to comment.