Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions instrumentation-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
testImplementation("io.opentelemetry.javaagent:opentelemetry-testing-common")
testImplementation("io.opentelemetry:opentelemetry-sdk-testing")
testImplementation("io.opentelemetry:opentelemetry-exporter-common")
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-incubator")
testImplementation("org.junit-pioneer:junit-pioneer")

jmhImplementation(project(":instrumentation-api-incubator"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ public final class InstrumenterBuilder<REQUEST, RESPONSE> {

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

private static final SpanSuppressionStrategy spanSuppressionStrategy =
SpanSuppressionStrategy.fromConfig(
ConfigPropertiesUtil.getString(
"otel.instrumentation.experimental.span-suppression-strategy"));

final OpenTelemetry openTelemetry;
final String instrumentationName;
SpanNameExtractor<? super REQUEST> spanNameExtractor;
Expand Down Expand Up @@ -373,8 +368,18 @@ private String getSchemaUrl() {
}

SpanSuppressor buildSpanSuppressor() {
// otel.instrumentation.experimental.* doesn't fit the usual pattern of configuration properties
// for instrumentations, so we need to handle both declarative and non-declarative configs here
String value =
ConfigPropertiesUtil.isDeclarativeConfig(openTelemetry)
? ConfigPropertiesUtil.getString(
openTelemetry, "common", "experimental", "span_suppression_strategy")
.orElse(null)
: ConfigPropertiesUtil.getString(
"otel.instrumentation.experimental.span-suppression-strategy")
.orElse(null);
return new SpanSuppressors.ByContextKey(
spanSuppressionStrategy.create(getSpanKeysFromAttributesExtractors()));
SpanSuppressionStrategy.fromConfig(value).create(getSpanKeysFromAttributesExtractors()));
}

private Set<SpanKey> getSpanKeysFromAttributesExtractors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@

package io.opentelemetry.instrumentation.api.internal;

import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty;
import static java.util.Collections.emptyList;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.incubator.ExtendedOpenTelemetry;
import io.opentelemetry.api.incubator.config.ConfigProvider;
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;

Expand All @@ -17,43 +25,111 @@
*/
public final class ConfigPropertiesUtil {

public static boolean getBoolean(String propertyName, boolean defaultValue) {
String strValue = getString(propertyName);
return strValue == null ? defaultValue : Boolean.parseBoolean(strValue);
private static final boolean supportsDeclarativeConfig = supportsDeclarativeConfig();

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.
Comment on lines +36 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only happens in OpenTelemetry API instrumentation tests

these are tests of OpenTelemetry API in the users class loader, so not following how that affects this class which is in the agent?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without the check, we get tests where the incubator exists, but is too old to have ExtendedOpenTelemetry:

gradle :instrumentation:opentelemetry-api:opentelemetry-api-1.42:javaagent:incubatorTest

	at io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil.isDeclarativeConfig(ConfigPropertiesUtil.java:116)
	at io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder.buildSpanSuppressor(InstrumenterBuilder.java:374)
	at io.opentelemetry.instrumentation.api.instrumenter.Instrumenter.<init>(Instrumenter.java:105)
	at io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder.buildInstrumenter(InstrumenterBuilder.java:298)
	at io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder.buildInstrumenter(InstrumenterBuilder.java:287)
	at io.opentelemetry.instrumentation.testing.TestInstrumenters.<init>(TestInstrumenters.java:41)
	at io.opentelemetry.instrumentation.testing.InstrumentationTestRunner.<init>(InstrumentationTestRunner.java:68)
	at io.opentelemetry.instrumentation.testing.AgentTestRunner.<init>(AgentTestRunner.java:47)
	at io.opentelemetry.instrumentation.testing.AgentTestRunner.<clinit>(AgentTestRunner.java:40)
	at io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension.<init>(AgentInstrumentationExtension.java:35)
	at io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension.create(AgentInstrumentationExtension.java:39)
	at io.opentelemetry.javaagent.instrumentation.opentelemetryapi.v1_42.incubator.logs.LoggerTest.<clinit>(LoggerTest.java:41)
	at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized0(Native Method)
	at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized(Unsafe.java:1160)
	at java.base/java.lang.reflect.Field.acquireOverrideFieldAccessor(Field.java:1200)
	at java.base/java.lang.reflect.Field.getOverrideFieldAccessor(Field.java:1169)
	at java.base/java.lang.reflect.Field.get(Field.java:444)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:395)
	at java.base/java.util.stream.DistinctOps$1$2.end(DistinctOps.java:168)
	at java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:261)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:510)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: java.lang.ClassNotFoundException: io.opentelemetry.api.incubator.ExtendedOpenTelemetry
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
	... 29 more

return false;
}
}

public static int getInt(String propertyName, int defaultValue) {
String strValue = getString(propertyName);
if (strValue == null) {
return defaultValue;
/**
* Returns the boolean value of the given property name from system properties and environment
* variables.
*
* <p>It's recommended to use {@link #getBoolean(OpenTelemetry, String...)} instead to support
* Declarative Config.
*/
public static Optional<Boolean> getBoolean(String propertyName) {
Optional<String> string = getString(propertyName);
// lambdas must not be used here in early initialization phase on early JDK8 versions
if (string.isPresent()) {
return Optional.of(Boolean.parseBoolean(string.get()));
}
try {
return Integer.parseInt(strValue);
} catch (NumberFormatException ignored) {
return defaultValue;
return Optional.empty();
}

/**
* Returns the boolean value of the given property name from Declarative Config if available,
* otherwise falls back to system properties and environment variables.
*/
public static Optional<Boolean> getBoolean(OpenTelemetry openTelemetry, String... propertyName) {
DeclarativeConfigProperties node = getDeclarativeConfigNode(openTelemetry, propertyName);
if (node != null) {
return Optional.ofNullable(node.getBoolean(leaf(propertyName)));
}
return getBoolean(toSystemProperty(propertyName));
}

@Nullable
public static String getString(String propertyName) {
/**
* Returns the int value of the given property name from system properties and environment
* variables.
*/
public static Optional<Integer> getInt(String propertyName) {
Optional<String> string = getString(propertyName);
// lambdas must not be used here in early initialization phase on early JDK8 versions
if (string.isPresent()) {
try {
return Optional.of(Integer.parseInt(string.get()));
} catch (NumberFormatException ignored) {
// ignored
}
}
return Optional.empty();
}

/**
* Returns the string value of the given property name from system properties and environment
* variables.
*
* <p>It's recommended to use {@link #getString(OpenTelemetry, String...)} instead to support
* Declarative Config.
*/
public static Optional<String> getString(String propertyName) {
String value = System.getProperty(propertyName);
if (value != null) {
return value;
return Optional.of(value);
}
return System.getenv(toEnvVarName(propertyName));
return Optional.ofNullable(System.getenv(toEnvVarName(propertyName)));
}

public static String getString(String propertyName, String defaultValue) {
String strValue = getString(propertyName);
return strValue == null ? defaultValue : strValue;
/**
* Returns the string value of the given property name from Declarative Config if available,
* otherwise falls back to system properties and environment variables.
*/
public static Optional<String> getString(OpenTelemetry openTelemetry, String... propertyName) {
DeclarativeConfigProperties node = getDeclarativeConfigNode(openTelemetry, propertyName);
if (node != null) {
return Optional.ofNullable(node.getString(leaf(propertyName)));
}
return getString(toSystemProperty(propertyName));
}

public static List<String> getList(String propertyName, List<String> defaultValue) {
String value = getString(propertyName);
if (value == null) {
return defaultValue;
/**
* Returns the list of strings value of the given property name from Declarative Config if
* available, otherwise falls back to system properties and environment variables.
*/
public static List<String> getList(OpenTelemetry openTelemetry, String... propertyName) {
DeclarativeConfigProperties node = getDeclarativeConfigNode(openTelemetry, propertyName);
if (node != null) {
return node.getScalarList(leaf(propertyName), String.class, emptyList());
}
return filterBlanksAndNulls(value.split(","));
return getString(toSystemProperty(propertyName))
.map(value -> filterBlanksAndNulls(value.split(",")))
.orElse(emptyList());
}

/** Returns true if the given OpenTelemetry instance supports Declarative Config. */
public static boolean isDeclarativeConfig(OpenTelemetry openTelemetry) {
return supportsDeclarativeConfig && openTelemetry instanceof ExtendedOpenTelemetry;
}

private static List<String> filterBlanksAndNulls(String[] values) {
Expand All @@ -67,5 +143,33 @@ private static String toEnvVarName(String propertyName) {
return propertyName.toUpperCase(Locale.ROOT).replace('-', '_').replace('.', '_');
}

private static String leaf(String[] propertyName) {
return propertyName[propertyName.length - 1];
}

@Nullable
private static DeclarativeConfigProperties getDeclarativeConfigNode(
OpenTelemetry openTelemetry, String... propertyName) {
if (isDeclarativeConfig(openTelemetry)) {
ExtendedOpenTelemetry extendedOpenTelemetry = (ExtendedOpenTelemetry) openTelemetry;
ConfigProvider configProvider = extendedOpenTelemetry.getConfigProvider();
DeclarativeConfigProperties instrumentationConfig = configProvider.getInstrumentationConfig();
if (instrumentationConfig == null) {
return empty();
}
DeclarativeConfigProperties node = instrumentationConfig.getStructured("java", empty());
// last part is the leaf property
for (int i = 0; i < propertyName.length - 1; i++) {
node = node.getStructured(propertyName[i], empty());
}
return node;
}
return null;
}

public static String toSystemProperty(String[] propertyName) {
return "otel.instrumentation." + String.join(".", propertyName).replace('_', '-');
}

private ConfigPropertiesUtil() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ public final class ContextPropagationDebug {
private static final boolean FAIL_ON_CONTEXT_LEAK;

static {
boolean agentDebugEnabled = ConfigPropertiesUtil.getBoolean("otel.javaagent.debug", false);
boolean agentDebugEnabled =
ConfigPropertiesUtil.getBoolean("otel.javaagent.debug").orElse(false);

THREAD_PROPAGATION_DEBUGGER =
ConfigPropertiesUtil.getBoolean(
"otel.javaagent.experimental.thread-propagation-debugger.enabled", agentDebugEnabled);
"otel.javaagent.experimental.thread-propagation-debugger.enabled")
.orElse(agentDebugEnabled);
FAIL_ON_CONTEXT_LEAK =
ConfigPropertiesUtil.getBoolean("otel.javaagent.testing.fail-on-context-leak", false);
ConfigPropertiesUtil.getBoolean("otel.javaagent.testing.fail-on-context-leak")
.orElse(false);
}

// context to which debug locations were added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class SemconvStability {
boolean oldCode = true;
boolean stableCode = false;

String value = ConfigPropertiesUtil.getString("otel.semconv-stability.opt-in");
String value = ConfigPropertiesUtil.getString("otel.semconv-stability.opt-in").orElse(null);
if (value != null) {
Set<String> values = new HashSet<>(asList(value.split(",")));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class SupportabilityMetrics {

private static final SupportabilityMetrics INSTANCE =
new SupportabilityMetrics(
ConfigPropertiesUtil.getBoolean("otel.javaagent.debug", false), logger::fine)
ConfigPropertiesUtil.getBoolean("otel.javaagent.debug").orElse(false), logger::fine)
.start();

public static SupportabilityMetrics instance() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ private static Stream<Arguments> configArgs() {
Arguments.of("Span-Kind", SpanSuppressionStrategy.SPAN_KIND),
Arguments.of("semconv", SpanSuppressionStrategy.SEMCONV),
Arguments.of("SemConv", SpanSuppressionStrategy.SEMCONV),
Arguments.of("asdfasdfasdf", SpanSuppressionStrategy.SEMCONV),
Arguments.of(null, SpanSuppressionStrategy.SEMCONV));
Arguments.of("asdfasdfasdf", SpanSuppressionStrategy.SEMCONV));
}

@ParameterizedTest
Expand Down
Loading