forked from open-telemetry/opentelemetry-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GlobalOpenTelemetry trigger of autoconfiguration is opt-in (open-tele…
…metry#5010) * Do not initialize AutoConfiguredOpenTelemetrySdk in OpenTelemetry.get * GlobalOpenTelemetry triggers autoconfigure based on env var / system property
- Loading branch information
Showing
11 changed files
with
202 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
api/all/src/main/java/io/opentelemetry/api/internal/ConfigUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.api.internal; | ||
|
||
import java.util.Locale; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Configuration utilities. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
public final class ConfigUtil { | ||
|
||
private ConfigUtil() {} | ||
|
||
/** | ||
* 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. | ||
* | ||
* @param key the property key | ||
* @return the system property if not null, or the environment variable if not null, or null | ||
*/ | ||
@Nullable | ||
public static String getString(String key) { | ||
String normalizedKey = normalizePropertyKey(key); | ||
String systemProperty = | ||
System.getProperties().entrySet().stream() | ||
.filter(entry -> normalizedKey.equals(normalizePropertyKey(entry.getKey().toString()))) | ||
.map(entry -> entry.getValue().toString()) | ||
.findFirst() | ||
.orElse(null); | ||
if (systemProperty != null) { | ||
return systemProperty; | ||
} | ||
return System.getenv().entrySet().stream() | ||
.filter(entry -> normalizedKey.equals(normalizeEnvironmentVariableKey(entry.getKey()))) | ||
.map(Map.Entry::getValue) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
/** | ||
* Normalize an environment variable key by converting to lower case and replacing "_" with ".". | ||
*/ | ||
public static String normalizeEnvironmentVariableKey(String key) { | ||
return key.toLowerCase(Locale.ROOT).replace("_", "."); | ||
} | ||
|
||
/** Normalize a property key by converting to lower case and replacing "-" with ".". */ | ||
public static String normalizePropertyKey(String key) { | ||
return key.toLowerCase(Locale.ROOT).replace("-", "."); | ||
} | ||
|
||
/** Returns defaultValue if value is null, otherwise value. This is an internal method. */ | ||
public static <T> T defaultIfNull(@Nullable T value, T defaultValue) { | ||
return value == null ? defaultValue : value; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
api/all/src/test/java/io/opentelemetry/api/internal/ConfigUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.api.internal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junitpioneer.jupiter.SetSystemProperty; | ||
|
||
/** Relies on environment configuration in {@code ./api/all/build.gradle.kts}. */ | ||
class ConfigUtilTest { | ||
|
||
@Test | ||
@SetSystemProperty(key = "config.key", value = "system") | ||
void getString_SystemPropertyPriority() { | ||
assertThat(ConfigUtil.getString("config.key")).isEqualTo("system"); | ||
assertThat(ConfigUtil.getString("config-key")).isEqualTo("system"); | ||
assertThat(ConfigUtil.getString("other.config.key")).isEqualTo(null); | ||
} | ||
|
||
@Test | ||
@SetSystemProperty(key = "CONFIG-KEY", value = "system") | ||
void getString_SystemPropertyNormalized() { | ||
assertThat(ConfigUtil.getString("config.key")).isEqualTo("system"); | ||
assertThat(ConfigUtil.getString("config-key")).isEqualTo("system"); | ||
assertThat(ConfigUtil.getString("other.config.key")).isEqualTo(null); | ||
} | ||
|
||
@Test | ||
void getString_EnvironmentVariable() { | ||
assertThat(ConfigUtil.getString("config.key")).isEqualTo("environment"); | ||
assertThat(ConfigUtil.getString("other.config.key")).isEqualTo(null); | ||
} | ||
|
||
@Test | ||
void normalizeEnvironmentVariable() { | ||
assertThat(ConfigUtil.normalizeEnvironmentVariableKey("CONFIG_KEY")).isEqualTo("config.key"); | ||
assertThat(ConfigUtil.normalizeEnvironmentVariableKey("config_key")).isEqualTo("config.key"); | ||
assertThat(ConfigUtil.normalizeEnvironmentVariableKey("config-key")).isEqualTo("config-key"); | ||
assertThat(ConfigUtil.normalizeEnvironmentVariableKey("configkey")).isEqualTo("configkey"); | ||
} | ||
|
||
@Test | ||
void normalizePropertyKey() { | ||
assertThat(ConfigUtil.normalizePropertyKey("CONFIG_KEY")).isEqualTo("config_key"); | ||
assertThat(ConfigUtil.normalizePropertyKey("CONFIG.KEY")).isEqualTo("config.key"); | ||
assertThat(ConfigUtil.normalizePropertyKey("config-key")).isEqualTo("config.key"); | ||
assertThat(ConfigUtil.normalizePropertyKey("configkey")).isEqualTo("configkey"); | ||
} | ||
|
||
@Test | ||
void defaultIfnull() { | ||
assertThat(ConfigUtil.defaultIfNull("val1", "val2")).isEqualTo("val1"); | ||
assertThat(ConfigUtil.defaultIfNull(null, "val2")).isEqualTo("val2"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
...ns/autoconfigure-spi/src/main/java/io/opentelemetry/sdk/autoconfigure/spi/ConfigUtil.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.