From 7fb2e3089a248630ac0988152fb99f2622433402 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Wed, 24 Dec 2025 08:10:59 -0800 Subject: [PATCH 1/3] Simplify InstrumenterBuilder config --- .../api/instrumenter/InstrumenterBuilder.java | 29 ++++++------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java index 60cfb078f36a..f2a06ca63347 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java @@ -394,21 +394,19 @@ SpanSuppressor buildSpanSuppressor() { @Nullable private String getSpanSuppressionStrategy() { // we cannot use DeclarativeConfigUtil here because it's not available in instrumentation-api - if (maybeDeclarativeConfig(openTelemetry)) { + DeclarativeConfigProperties commonConfig = empty(); + if (openTelemetry instanceof ExtendedOpenTelemetry) { DeclarativeConfigProperties instrumentationConfig = ((ExtendedOpenTelemetry) openTelemetry).getConfigProvider().getInstrumentationConfig(); - if (instrumentationConfig == null) { - return null; + if (instrumentationConfig != null) { + commonConfig = + instrumentationConfig.getStructured("java", empty()).getStructured("common", empty()); } - - return instrumentationConfig - .getStructured("java", empty()) - .getStructured("common", empty()) - .getString("span_suppression_strategy/development"); - } else { - return ConfigPropertiesUtil.getString( - "otel.instrumentation.experimental.span-suppression-strategy"); } + return commonConfig.getString( + "span_suppression_strategy/development", + ConfigPropertiesUtil.getString( + "otel.instrumentation.experimental.span-suppression-strategy")); } private Set getSpanKeysFromAttributesExtractors() { @@ -488,15 +486,6 @@ public void setSpanStatusExtractorCustomizer( } } - /** - * Returns true if the current classpath supports declarative config and the instance may support - * declarative config (the agent implements ExtendedOpenTelemetry even when declarative config - * isn't used). - */ - private static boolean maybeDeclarativeConfig(OpenTelemetry openTelemetry) { - return supportsDeclarativeConfig && openTelemetry instanceof ExtendedOpenTelemetry; - } - private interface InstrumenterConstructor { Instrumenter create(InstrumenterBuilder builder); From c414c21421278f6ed3d56001cedcca81a578b0d2 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Wed, 24 Dec 2025 09:00:34 -0800 Subject: [PATCH 2/3] try --- .../api/instrumenter/InstrumenterBuilder.java | 24 +++++-------------- .../testing/InstrumentationTestRunner.java | 23 +++++++++++++----- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java index f2a06ca63347..d761013a367f 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java @@ -51,7 +51,6 @@ public final class InstrumenterBuilder { private static final Logger logger = Logger.getLogger(InstrumenterBuilder.class.getName()); - private static final boolean supportsDeclarativeConfig = supportsDeclarativeConfig(); final OpenTelemetry openTelemetry; final String instrumentationName; @@ -75,20 +74,6 @@ public final class InstrumenterBuilder { boolean propagateOperationListenersToOnEnd = false; boolean enabled = true; - private static boolean supportsDeclarativeConfig() { - try { - Class.forName("io.opentelemetry.api.incubator.ExtendedOpenTelemetry"); - return true; - } catch (ClassNotFoundException e) { - // The incubator module is not available. - // This only happens in OpenTelemetry API instrumentation tests, where an older version of - // OpenTelemetry API is used that does not have ExtendedOpenTelemetry. - // Having the incubator module without ExtendedOpenTelemetry class should still return false - // for those tests to avoid a ClassNotFoundException. - return false; - } - } - static { Experimental.internalAddOperationListenerAttributesExtractor( (builder, operationListenerAttributesExtractor) -> @@ -403,10 +388,13 @@ private String getSpanSuppressionStrategy() { instrumentationConfig.getStructured("java", empty()).getStructured("common", empty()); } } - return commonConfig.getString( - "span_suppression_strategy/development", + String experimentalOverride = ConfigPropertiesUtil.getString( - "otel.instrumentation.experimental.span-suppression-strategy")); + "otel.instrumentation.experimental.span-suppression-strategy"); + String result = + commonConfig.getString( + "span_suppression_strategy/development", experimentalOverride == null ? "" : experimentalOverride); + return result.isEmpty() ? null : result; } private Set getSpanKeysFromAttributesExtractors() { diff --git a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/InstrumentationTestRunner.java b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/InstrumentationTestRunner.java index 245bc30dcb81..7cfdc0183679 100644 --- a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/InstrumentationTestRunner.java +++ b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/InstrumentationTestRunner.java @@ -52,7 +52,11 @@ */ public abstract class InstrumentationTestRunner { - private final TestInstrumenters testInstrumenters; + private final OpenTelemetry openTelemetry; + // Lazy initialized so that test runners can load without triggering Instrumenter construction + // in the OpenTelemetry API bridging tests where some of the newer OpenTelemetry APIs used by + // Instrumenter are absent. + @Nullable private TestInstrumenters testInstrumenters; protected Map> metricsByScope = new HashMap<>(); protected Set instrumentationScopes = new HashSet<>(); @@ -65,7 +69,7 @@ public abstract class InstrumentationTestRunner { tracesByScope = new HashMap<>(); protected InstrumentationTestRunner(OpenTelemetry openTelemetry) { - testInstrumenters = new TestInstrumenters(openTelemetry); + this.openTelemetry = openTelemetry; } public abstract void beforeTestClass(); @@ -285,7 +289,7 @@ public final void runWithSpan(String spanName, ThrowingRun */ public final T runWithSpan( String spanName, ThrowingSupplier callback) throws E { - return testInstrumenters.runWithSpan(spanName, callback); + return getTestInstrumenters().runWithSpan(spanName, callback); } /** @@ -308,7 +312,7 @@ public final void runWithHttpClientSpan( */ public final T runWithHttpClientSpan( String spanName, ThrowingSupplier callback) throws E { - return testInstrumenters.runWithHttpClientSpan(spanName, callback); + return getTestInstrumenters().runWithHttpClientSpan(spanName, callback); } /** @@ -330,13 +334,20 @@ public final void runWithHttpServerSpan(ThrowingRunnable T runWithHttpServerSpan(ThrowingSupplier callback) throws E { - return testInstrumenters.runWithHttpServerSpan(callback); + return getTestInstrumenters().runWithHttpServerSpan(callback); } /** Runs the provided {@code callback} inside the scope of a non-recording span. */ public final T runWithNonRecordingSpan(ThrowingSupplier callback) throws E { - return testInstrumenters.runWithNonRecordingSpan(callback); + return getTestInstrumenters().runWithNonRecordingSpan(callback); + } + + private TestInstrumenters getTestInstrumenters() { + if (testInstrumenters == null) { + testInstrumenters = new TestInstrumenters(openTelemetry); + } + return testInstrumenters; } private static void awaitUntilAsserted(Runnable runnable) { From 132e9d27071a75ae2f064688dbb9811690da5b36 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Fri, 26 Dec 2025 09:00:53 -0800 Subject: [PATCH 3/3] spotless --- .../instrumentation/api/instrumenter/InstrumenterBuilder.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java index d761013a367f..946db867e7a1 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/InstrumenterBuilder.java @@ -393,7 +393,8 @@ private String getSpanSuppressionStrategy() { "otel.instrumentation.experimental.span-suppression-strategy"); String result = commonConfig.getString( - "span_suppression_strategy/development", experimentalOverride == null ? "" : experimentalOverride); + "span_suppression_strategy/development", + experimentalOverride == null ? "" : experimentalOverride); return result.isEmpty() ? null : result; }