From a54261df6aba27e2c608ee735b0474ef2f2f190d Mon Sep 17 00:00:00 2001 From: Jack Shirazi Date: Thu, 21 Nov 2024 15:34:35 +0000 Subject: [PATCH 1/4] add experimental updateTracerConfigurations --- .../io/opentelemetry/sdk/OpenTelemetrySdk.java | 16 ++++++++++++++++ .../io/opentelemetry/sdk/trace/SdkTracer.java | 8 +++++--- .../sdk/trace/SdkTracerProvider.java | 10 ++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/sdk/all/src/main/java/io/opentelemetry/sdk/OpenTelemetrySdk.java b/sdk/all/src/main/java/io/opentelemetry/sdk/OpenTelemetrySdk.java index 81c1c3dd8c1..4462d18940e 100644 --- a/sdk/all/src/main/java/io/opentelemetry/sdk/OpenTelemetrySdk.java +++ b/sdk/all/src/main/java/io/opentelemetry/sdk/OpenTelemetrySdk.java @@ -19,6 +19,8 @@ import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.trace.SdkTracerProvider; import java.io.Closeable; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; @@ -168,6 +170,20 @@ public TracerBuilder tracerBuilder(String instrumentationScopeName) { public SdkTracerProvider unobfuscate() { return delegate; } + + // currently not public as experimental + void updateTracerConfigurations() { + // and delegate method is experimental so also not public + // It would be: delegate.updateTracerConfigurations(); + try { + Method method = SdkTracerProvider.class.getDeclaredMethod("updateTracerConfigurations"); + method.setAccessible(true); + method.invoke(delegate); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + throw new IllegalStateException( + "Error calling SdkTracerProvider.updateTracerConfigurations(): ", e); + } + } } /** 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 7b74c035e82..39ddfc3ae59 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 @@ -19,9 +19,6 @@ final class SdkTracer implements ExtendedTracer { private final TracerSharedState sharedState; private final InstrumentationScopeInfo instrumentationScopeInfo; - - // TODO: add dedicated API for updating scope config. - @SuppressWarnings("FieldCanBeFinal") // For now, allow updating reflectively. private boolean tracerEnabled; SdkTracer( @@ -54,6 +51,11 @@ InstrumentationScopeInfo getInstrumentationScopeInfo() { return instrumentationScopeInfo; } + // currently not public as experimental + void updateTracerConfig(TracerConfig tracerConfig) { + this.tracerEnabled = tracerConfig.isEnabled(); + } + @Override public boolean isEnabled() { return tracerEnabled; 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 036e812c5ab..a7cd24d2264 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 @@ -100,6 +100,16 @@ public Sampler getSampler() { return sharedState.getSampler(); } + // currently not public as experimental + void updateTracerConfigurations() { + 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. From 6f68542bd47aa516436040cca8bd73aff06291ee Mon Sep 17 00:00:00 2001 From: jackshirazi Date: Thu, 21 Nov 2024 16:52:19 +0000 Subject: [PATCH 2/4] add non-volatile comment --- .../src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java | 2 ++ 1 file changed, 2 insertions(+) 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 39ddfc3ae59..19ead2a2128 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 @@ -19,6 +19,8 @@ final class SdkTracer implements ExtendedTracer { private final TracerSharedState sharedState; private final InstrumentationScopeInfo instrumentationScopeInfo; + // deliberately not volatile because of performance concerns + // - which means its eventually consistent private boolean tracerEnabled; SdkTracer( From 824b997e8f41abe5244de978040e663bc9eabd63 Mon Sep 17 00:00:00 2001 From: Jack Shirazi Date: Fri, 22 Nov 2024 13:55:28 +0000 Subject: [PATCH 3/4] add sdktracer enablement test --- .../java/io/opentelemetry/sdk/trace/SdkTracerTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) 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 dde5f7ef919dc4e1050d57fe8be2e13d57da987f Mon Sep 17 00:00:00 2001 From: Jack Shirazi Date: Fri, 22 Nov 2024 14:17:10 +0000 Subject: [PATCH 4/4] add 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;