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

add SdkTracerProvider.setScopeConfigurator() and support #7021

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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 @@ -30,9 +30,8 @@ class SdkTracer implements Tracer {

private final TracerSharedState sharedState;
private final InstrumentationScopeInfo instrumentationScopeInfo;

// TODO: add dedicated API for updating scope config.
@SuppressWarnings("FieldCanBeFinal") // For now, allow updating reflectively.
// deliberately not volatile because of performance concerns
// - which means its eventually consistent
private boolean tracerEnabled;

SdkTracer(
Expand Down Expand Up @@ -79,4 +78,14 @@ public SpanBuilder spanBuilder(String spanName) {
InstrumentationScopeInfo getInstrumentationScopeInfo() {
return instrumentationScopeInfo;
}

// Visible for testing
boolean isEnabled() {
return tracerEnabled;
}

// currently not public as experimental
void updateTracerConfig(TracerConfig tracerConfig) {
this.tracerEnabled = tracerConfig.isEnabled();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public final class SdkTracerProvider implements TracerProvider, Closeable {
static final String DEFAULT_TRACER_NAME = "";
private final TracerSharedState sharedState;
private final ComponentRegistry<SdkTracer> tracerSdkComponentRegistry;
private final ScopeConfigurator<TracerConfig> tracerConfigurator;
// deliberately not volatile because of performance concerns
// - which means its eventually consistent
private ScopeConfigurator<TracerConfig> tracerConfigurator;

/**
* Returns a new {@link SdkTracerProviderBuilder} for {@link SdkTracerProvider}.
Expand Down Expand Up @@ -100,6 +102,17 @@ public Sampler getSampler() {
return sharedState.getSampler();
}

// currently not public as experimental
void setScopeConfigurator(ScopeConfigurator<TracerConfig> scopeConfigurator) {
this.tracerConfigurator = scopeConfigurator;
this.tracerSdkComponentRegistry
.getComponents()
.forEach(
sdkTracer ->
sdkTracer.updateTracerConfig(
getTracerConfig(sdkTracer.getInstrumentationScopeInfo())));
}

/**
* Attempts to stop all the activity for {@link Tracer}s created by this provider. Calls {@link
* SpanProcessor#shutdown()} for all registered {@link SpanProcessor}s.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.internal.ScopeConfigurator;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.internal.TracerConfig;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import java.util.function.Supplier;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -184,6 +186,22 @@ void propagatesInstrumentationScopeInfoToTracer() {
assertThat(((SdkTracer) tracer).getInstrumentationScopeInfo()).isEqualTo(expected);
}

@Test
void propagatesEnablementToTracer() {
SdkTracer tracer = (SdkTracer) tracerFactory.get("test");
boolean isEnabled = tracer.isEnabled();
ScopeConfigurator<TracerConfig> flipConfigurator =
new ScopeConfigurator<TracerConfig>() {
@Override
public TracerConfig apply(InstrumentationScopeInfo scopeInfo) {
return isEnabled ? TracerConfig.disabled() : TracerConfig.enabled();
}
};
// all in the same thread, so should see enablement change immediately
tracerFactory.setScopeConfigurator(flipConfigurator);
assertThat(tracer.isEnabled()).isEqualTo(!isEnabled);
}

@Test
void build_SpanLimits() {
SpanLimits initialSpanLimits = SpanLimits.builder().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.sdk.trace.internal.TracerConfig;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -50,6 +51,14 @@ void getInstrumentationScopeInfo() {
assertThat(tracer.getInstrumentationScopeInfo()).isEqualTo(instrumentationScopeInfo);
}

@Test
void updateEnabled() {
tracer.updateTracerConfig(TracerConfig.disabled());
assertThat(tracer.isEnabled()).isFalse();
tracer.updateTracerConfig(TracerConfig.enabled());
assertThat(tracer.isEnabled()).isTrue();
}

@Test
void propagatesInstrumentationScopeInfoToSpan() {
ReadableSpan readableSpan = (ReadableSpan) tracer.spanBuilder("spanName").startSpan();
Expand Down
Loading