Skip to content

Commit

Permalink
add SdkTracerProvider.setScopeConfigurator() and support
Browse files Browse the repository at this point in the history
  • Loading branch information
jackshirazi committed Jan 15, 2025
1 parent c8da020 commit fd1e9db
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
16 changes: 13 additions & 3 deletions sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java
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,15 @@ 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 @@ -184,6 +184,21 @@ void propagatesInstrumentationScopeInfoToTracer() {
assertThat(((SdkTracer) tracer).getInstrumentationScopeInfo()).isEqualTo(expected);
}

@Test
void propagatesEnablementToTracer() {
final SdkTracer tracer = tracerFactory.get("test");
final 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 @@ -50,6 +50,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

0 comments on commit fd1e9db

Please sign in to comment.