diff --git a/common/src/main/java/io/opentelemetry/common/impl/ConfigUtil.java b/common/src/main/java/io/opentelemetry/common/impl/ConfigUtil.java index a129fd331bd..cd8ee760793 100644 --- a/common/src/main/java/io/opentelemetry/common/impl/ConfigUtil.java +++ b/common/src/main/java/io/opentelemetry/common/impl/ConfigUtil.java @@ -39,9 +39,13 @@ public static Properties safeSystemProperties() { * Return the system property or environment variable for the {@code key}. * *

Normalize the {@code key} using {@link #normalizePropertyKey(String)}. Match to system - * property keys also normalized with {@link #normalizePropertyKey(String)}. Match to environment - * variable keys normalized with {@link #normalizeEnvironmentVariableKey(String)}. System - * properties take priority over environment variables. + * property keys also normalized with {@link #normalizePropertyKey(String)}. Further normalize the + * {@code key} using {@link #normalizeEnvironmentVariableKey(String)} and match to environment + * variable keys normalized the same way. System properties take priority over environment + * variables. + * + *

For example, {@code otel.experimental.sdk.jvm_stacktrace} matches the environment variable + * {@code OTEL_EXPERIMENTAL_SDK_JVM_STACKTRACE}. * * @param key the property key * @param defaultValue the fallback value if neither system property nor environment variable is @@ -61,8 +65,10 @@ public static String getString(String key, String defaultValue) { if (systemProperty != null) { return systemProperty; } + String environmentVariableKey = normalizeEnvironmentVariableKey(normalizedKey); return System.getenv().entrySet().stream() - .filter(entry -> normalizedKey.equals(normalizeEnvironmentVariableKey(entry.getKey()))) + .filter( + entry -> environmentVariableKey.equals(normalizeEnvironmentVariableKey(entry.getKey()))) .map(Map.Entry::getValue) .findFirst() .orElse(defaultValue); diff --git a/common/src/test/java/io/opentelemetry/common/impl/ConfigUtilTest.java b/common/src/test/java/io/opentelemetry/common/impl/ConfigUtilTest.java index 996376383af..de13d2bf69a 100644 --- a/common/src/test/java/io/opentelemetry/common/impl/ConfigUtilTest.java +++ b/common/src/test/java/io/opentelemetry/common/impl/ConfigUtilTest.java @@ -40,6 +40,7 @@ void getString_SystemPropertyNormalized() { @Test void getString_EnvironmentVariable() { assertThat(ConfigUtil.getString("config.key", "default")).isEqualTo("environment"); + assertThat(ConfigUtil.getString("config_key", "default")).isEqualTo("environment"); assertThat(ConfigUtil.getString("other.config.key", "default")).isEqualTo("default"); }