Skip to content
Merged
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 @@ -51,7 +51,6 @@
public final class InstrumenterBuilder<REQUEST, RESPONSE> {

private static final Logger logger = Logger.getLogger(InstrumenterBuilder.class.getName());
private static final boolean supportsDeclarativeConfig = supportsDeclarativeConfig();

final OpenTelemetry openTelemetry;
final String instrumentationName;
Expand All @@ -75,20 +74,6 @@ public final class InstrumenterBuilder<REQUEST, RESPONSE> {
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) ->
Expand Down Expand Up @@ -394,21 +379,23 @@ 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");
}
String experimentalOverride =
ConfigPropertiesUtil.getString(
"otel.instrumentation.experimental.span-suppression-strategy");
String result =
commonConfig.getString(
"span_suppression_strategy/development",
experimentalOverride == null ? "" : experimentalOverride);
return result.isEmpty() ? null : result;
}

private Set<SpanKey> getSpanKeysFromAttributesExtractors() {
Expand Down Expand Up @@ -488,15 +475,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<RQ, RS> {
Instrumenter<RQ, RS> create(InstrumenterBuilder<RQ, RS> builder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<InstrumentationScopeInfo, Map<String, MetricData>> metricsByScope = new HashMap<>();
protected Set<InstrumentationScopeInfo> instrumentationScopes = new HashSet<>();

Expand All @@ -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();
Expand Down Expand Up @@ -285,7 +289,7 @@ public final <E extends Exception> void runWithSpan(String spanName, ThrowingRun
*/
public final <T, E extends Throwable> T runWithSpan(
String spanName, ThrowingSupplier<T, E> callback) throws E {
return testInstrumenters.runWithSpan(spanName, callback);
return getTestInstrumenters().runWithSpan(spanName, callback);
}

/**
Expand All @@ -308,7 +312,7 @@ public final <E extends Throwable> void runWithHttpClientSpan(
*/
public final <T, E extends Throwable> T runWithHttpClientSpan(
String spanName, ThrowingSupplier<T, E> callback) throws E {
return testInstrumenters.runWithHttpClientSpan(spanName, callback);
return getTestInstrumenters().runWithHttpClientSpan(spanName, callback);
}

/**
Expand All @@ -330,13 +334,20 @@ public final <E extends Throwable> void runWithHttpServerSpan(ThrowingRunnable<E
*/
public final <T, E extends Throwable> T runWithHttpServerSpan(ThrowingSupplier<T, E> 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, E extends Throwable> T runWithNonRecordingSpan(ThrowingSupplier<T, E> 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) {
Expand Down
Loading