Skip to content
Open
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
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,16 @@ 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")
: ConfigPropertiesUtil.getString(
"otel.instrumentation.experimental.span-suppression-strategy");
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,6 +5,12 @@

package io.opentelemetry.instrumentation.api.internal;

import static io.opentelemetry.api.incubator.config.DeclarativeConfigProperties.empty;

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;
Expand All @@ -17,11 +23,51 @@
*/
public final class ConfigPropertiesUtil {

private static final boolean isIncubator = isIncubator();

private static boolean isIncubator() {
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;
}
}

/**
* Returns the boolean value of the given property name from system properties and environment
* variables.
*
* <p>It's recommended to use {@link #getBoolean(OpenTelemetry, boolean, String...)} instead to
* support Declarative Config.
*/
public static boolean getBoolean(String propertyName, boolean defaultValue) {
String strValue = getString(propertyName);
return strValue == null ? defaultValue : Boolean.parseBoolean(strValue);
}

/**
* 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 boolean getBoolean(
OpenTelemetry openTelemetry, boolean defaultValue, String... propertyName) {
DeclarativeConfigProperties node = getDeclarativeConfigNode(openTelemetry, propertyName);
if (node != null) {
return node.getBoolean(leaf(propertyName), defaultValue);
}
return getBoolean(toSystemProperty(propertyName), defaultValue);
}

/**
* Returns the int value of the given property name from system properties and environment
* variables.
*/
public static int getInt(String propertyName, int defaultValue) {
String strValue = getString(propertyName);
if (strValue == null) {
Expand All @@ -34,6 +80,13 @@ public static int getInt(String propertyName, int defaultValue) {
}
}

/**
* 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.
*/
@Nullable
public static String getString(String propertyName) {
String value = System.getProperty(propertyName);
Expand All @@ -43,19 +96,54 @@ public static String getString(String propertyName) {
return 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.
*/
@Nullable
public static String getString(OpenTelemetry openTelemetry, String... propertyName) {
DeclarativeConfigProperties node = getDeclarativeConfigNode(openTelemetry, propertyName);
if (node != null) {
return node.getString(leaf(propertyName));
}
return getString(toSystemProperty(propertyName));
}

/**
* 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 String getStringOrFallback(
OpenTelemetry openTelemetry, String defaultValue, String... propertyName) {
String value = getString(openTelemetry, propertyName);
if (value == null) {
return defaultValue;
}
return value;
}

public static List<String> getList(String propertyName, List<String> defaultValue) {
String value = getString(propertyName);
/**
* 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, List<String> defaultValue, String... propertyName) {
DeclarativeConfigProperties node = getDeclarativeConfigNode(openTelemetry, propertyName);
if (node != null) {
return node.getScalarList(leaf(propertyName), String.class, defaultValue);
}
String value = getString(toSystemProperty(propertyName));
if (value == null) {
return defaultValue;
}
return filterBlanksAndNulls(value.split(","));
}

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

private static List<String> filterBlanksAndNulls(String[] values) {
return Arrays.stream(values)
.map(String::trim)
Expand All @@ -67,5 +155,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 @@ -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
Loading