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 @@ -39,9 +39,13 @@ public static Properties safeSystemProperties() {
* Return the system property or environment variable for the {@code key}.
*
* <p>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
Comment thread
psx95 marked this conversation as resolved.
Resolved
* variable keys normalized the same way. System properties take priority over environment
* variables.
*
* <p>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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
Loading