From fd1e9db2b6a9aa2dd525ef28ca871cd73f7d0759 Mon Sep 17 00:00:00 2001 From: Jack Shirazi Date: Wed, 15 Jan 2025 15:53:15 +0000 Subject: [PATCH 1/5] add SdkTracerProvider.setScopeConfigurator() and support --- .../io/opentelemetry/sdk/trace/SdkTracer.java | 16 +++++++++++++--- .../sdk/trace/SdkTracerProvider.java | 15 ++++++++++++++- .../sdk/trace/SdkTracerProviderTest.java | 15 +++++++++++++++ .../opentelemetry/sdk/trace/SdkTracerTest.java | 8 ++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java index 0238d897907..d2527061954 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java @@ -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( @@ -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(); + } + } 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 14ccc37c41f..5d70bee0e36 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 @@ -30,7 +30,9 @@ public final class SdkTracerProvider implements TracerProvider, Closeable { static final String DEFAULT_TRACER_NAME = ""; private final TracerSharedState sharedState; private final ComponentRegistry tracerSdkComponentRegistry; - private final ScopeConfigurator tracerConfigurator; + // deliberately not volatile because of performance concerns + // - which means its eventually consistent + private ScopeConfigurator tracerConfigurator; /** * Returns a new {@link SdkTracerProviderBuilder} for {@link SdkTracerProvider}. @@ -100,6 +102,17 @@ public Sampler getSampler() { return sharedState.getSampler(); } + // currently not public as experimental + void setScopeConfigurator(ScopeConfigurator 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. diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java index 44f75373cb1..923ad31f4cf 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java @@ -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 flipConfigurator = new ScopeConfigurator() { + @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(); diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerTest.java index c17c121a36e..b98f7caf1f9 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerTest.java @@ -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(); From de0778aaf8ff3eeefc8848f0db46584252ac2b92 Mon Sep 17 00:00:00 2001 From: Jack Shirazi Date: Wed, 15 Jan 2025 17:54:03 +0000 Subject: [PATCH 2/5] fix typos --- .../io/opentelemetry/sdk/trace/SdkTracerProviderTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java index 923ad31f4cf..506c66f973d 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java @@ -14,10 +14,12 @@ import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.sdk.trace.internal.TracerConfig; import io.opentelemetry.internal.testing.slf4j.SuppressLogger; 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.samplers.Sampler; import java.util.function.Supplier; @@ -186,14 +188,14 @@ void propagatesInstrumentationScopeInfoToTracer() { @Test void propagatesEnablementToTracer() { - final SdkTracer tracer = tracerFactory.get("test"); + final SdkTracer tracer = (SdkTracer) tracerFactory.get("test"); final boolean isEnabled = tracer.isEnabled(); ScopeConfigurator flipConfigurator = new ScopeConfigurator() { @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); From 1658f53cf443b621c7d723d8c889e405f13939a8 Mon Sep 17 00:00:00 2001 From: Jack Shirazi Date: Wed, 15 Jan 2025 18:13:07 +0000 Subject: [PATCH 3/5] fix import --- .../src/test/java/io/opentelemetry/sdk/trace/SdkTracerTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerTest.java index b98f7caf1f9..0ca18de5f15 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerTest.java @@ -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; From 9a3cfc88cfad9dd7b9957bbfc394e7114baabdd9 Mon Sep 17 00:00:00 2001 From: Jack Shirazi Date: Wed, 15 Jan 2025 18:34:46 +0000 Subject: [PATCH 4/5] stupid error --- .../io/opentelemetry/sdk/trace/SdkTracerProviderTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java index 506c66f973d..73e495df5fe 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java @@ -188,8 +188,8 @@ void propagatesInstrumentationScopeInfoToTracer() { @Test void propagatesEnablementToTracer() { - final SdkTracer tracer = (SdkTracer) tracerFactory.get("test"); - final boolean isEnabled = tracer.isEnabled(); + SdkTracer tracer = (SdkTracer) tracerFactory.get("test"); + boolean isEnabled = tracer.isEnabled(); ScopeConfigurator flipConfigurator = new ScopeConfigurator() { @Override public TracerConfig apply(InstrumentationScopeInfo scopeInfo) { From 79b639fefb6e499d73b5649ae52c283c63d8d575 Mon Sep 17 00:00:00 2001 From: Jack Shirazi Date: Wed, 15 Jan 2025 19:19:48 +0000 Subject: [PATCH 5/5] spotless --- .../io/opentelemetry/sdk/trace/SdkTracer.java | 1 - .../sdk/trace/SdkTracerProviderTest.java | 17 +++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java index d2527061954..23ba973f1ca 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java @@ -88,5 +88,4 @@ boolean isEnabled() { void updateTracerConfig(TracerConfig tracerConfig) { this.tracerEnabled = tracerConfig.isEnabled(); } - } diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java index 73e495df5fe..53be991c1eb 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderTest.java @@ -14,13 +14,13 @@ import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.Tracer; -import io.opentelemetry.sdk.trace.internal.TracerConfig; import io.opentelemetry.internal.testing.slf4j.SuppressLogger; 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; @@ -190,13 +190,14 @@ void propagatesInstrumentationScopeInfoToTracer() { void propagatesEnablementToTracer() { SdkTracer tracer = (SdkTracer) tracerFactory.get("test"); boolean isEnabled = tracer.isEnabled(); - ScopeConfigurator flipConfigurator = new ScopeConfigurator() { - @Override - public TracerConfig apply(InstrumentationScopeInfo scopeInfo) { - return isEnabled ? TracerConfig.disabled() : TracerConfig.enabled(); - } - }; - //all in the same thread, so should see enablement change immediately + ScopeConfigurator flipConfigurator = + new ScopeConfigurator() { + @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); }