Use ConfigUtil internally (#5048)

This commit is contained in:
jack-berg 2022-12-17 16:10:46 -06:00 committed by GitHub
parent 10e32fe80c
commit ece93b77a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 22 deletions

View File

@ -225,7 +225,7 @@ public final class GlobalOpenTelemetry {
// If autoconfigure module is present but global autoconfigure disabled log a warning and return // If autoconfigure module is present but global autoconfigure disabled log a warning and return
boolean globalAutoconfigureEnabled = boolean globalAutoconfigureEnabled =
Boolean.parseBoolean(ConfigUtil.getString(GLOBAL_AUTOCONFIGURE_ENABLED_PROPERTY)); Boolean.parseBoolean(ConfigUtil.getString(GLOBAL_AUTOCONFIGURE_ENABLED_PROPERTY, "false"));
if (!globalAutoconfigureEnabled) { if (!globalAutoconfigureEnabled) {
logger.log( logger.log(
Level.INFO, Level.INFO,

View File

@ -28,10 +28,10 @@ public final class ConfigUtil {
* properties take priority over environment variables. * properties take priority over environment variables.
* *
* @param key the property key * @param key the property key
* @return the system property if not null, or the environment variable if not null, or null * @return the system property if not null, or the environment variable if not null, or {@code
* defaultValue}
*/ */
@Nullable public static String getString(String key, String defaultValue) {
public static String getString(String key) {
String normalizedKey = normalizePropertyKey(key); String normalizedKey = normalizePropertyKey(key);
String systemProperty = String systemProperty =
System.getProperties().entrySet().stream() System.getProperties().entrySet().stream()
@ -46,7 +46,7 @@ public final class ConfigUtil {
.filter(entry -> normalizedKey.equals(normalizeEnvironmentVariableKey(entry.getKey()))) .filter(entry -> normalizedKey.equals(normalizeEnvironmentVariableKey(entry.getKey())))
.map(Map.Entry::getValue) .map(Map.Entry::getValue)
.findFirst() .findFirst()
.orElse(null); .orElse(defaultValue);
} }
/** /**

View File

@ -16,23 +16,23 @@ class ConfigUtilTest {
@Test @Test
@SetSystemProperty(key = "config.key", value = "system") @SetSystemProperty(key = "config.key", value = "system")
void getString_SystemPropertyPriority() { void getString_SystemPropertyPriority() {
assertThat(ConfigUtil.getString("config.key")).isEqualTo("system"); assertThat(ConfigUtil.getString("config.key", "default")).isEqualTo("system");
assertThat(ConfigUtil.getString("config-key")).isEqualTo("system"); assertThat(ConfigUtil.getString("config-key", "default")).isEqualTo("system");
assertThat(ConfigUtil.getString("other.config.key")).isEqualTo(null); assertThat(ConfigUtil.getString("other.config.key", "default")).isEqualTo("default");
} }
@Test @Test
@SetSystemProperty(key = "CONFIG-KEY", value = "system") @SetSystemProperty(key = "CONFIG-KEY", value = "system")
void getString_SystemPropertyNormalized() { void getString_SystemPropertyNormalized() {
assertThat(ConfigUtil.getString("config.key")).isEqualTo("system"); assertThat(ConfigUtil.getString("config.key", "default")).isEqualTo("system");
assertThat(ConfigUtil.getString("config-key")).isEqualTo("system"); assertThat(ConfigUtil.getString("config-key", "default")).isEqualTo("system");
assertThat(ConfigUtil.getString("other.config.key")).isEqualTo(null); assertThat(ConfigUtil.getString("other.config.key", "default")).isEqualTo("default");
} }
@Test @Test
void getString_EnvironmentVariable() { void getString_EnvironmentVariable() {
assertThat(ConfigUtil.getString("config.key")).isEqualTo("environment"); assertThat(ConfigUtil.getString("config.key", "default")).isEqualTo("environment");
assertThat(ConfigUtil.getString("other.config.key")).isEqualTo(null); assertThat(ConfigUtil.getString("other.config.key", "default")).isEqualTo("default");
} }
@Test @Test

View File

@ -41,6 +41,7 @@ import static io.opentelemetry.exporter.internal.marshal.WireFormat.FIXED64_SIZE
import static io.opentelemetry.exporter.internal.marshal.WireFormat.MAX_VARINT32_SIZE; import static io.opentelemetry.exporter.internal.marshal.WireFormat.MAX_VARINT32_SIZE;
import static io.opentelemetry.exporter.internal.marshal.WireFormat.MAX_VARINT_SIZE; import static io.opentelemetry.exporter.internal.marshal.WireFormat.MAX_VARINT_SIZE;
import io.opentelemetry.api.internal.ConfigUtil;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -71,7 +72,10 @@ public abstract class CodedOutputStream {
static { static {
int bufferSize = 50 * 1024; int bufferSize = 50 * 1024;
try { try {
bufferSize = Integer.parseInt(System.getProperty("otel.experimental.otlp.buffer-size")); String bufferSizeConfig = ConfigUtil.getString("otel.experimental.otlp.buffer-size", "");
if (!bufferSizeConfig.isEmpty()) {
bufferSize = Integer.parseInt(bufferSizeConfig);
}
} catch (Throwable t) { } catch (Throwable t) {
// Ignore. // Ignore.
} }

View File

@ -5,7 +5,7 @@
package io.opentelemetry.sdk.metrics.internal.debug; package io.opentelemetry.sdk.metrics.internal.debug;
import java.util.Locale; import io.opentelemetry.api.internal.ConfigUtil;
/** /**
* Determines if the SDK is in debugging mode (captures stack traces) or not. * Determines if the SDK is in debugging mode (captures stack traces) or not.
@ -20,13 +20,7 @@ public final class DebugConfig {
private DebugConfig() {} private DebugConfig() {}
static { static {
// Attempt to mirror the logic in DefaultConfigProperties here... enabled = Boolean.parseBoolean(ConfigUtil.getString(ENABLE_METRICS_DEBUG_PROPERTY, "false"));
enabled =
"true".equalsIgnoreCase(System.getProperty(ENABLE_METRICS_DEBUG_PROPERTY))
|| "true"
.equalsIgnoreCase(
System.getenv(
ENABLE_METRICS_DEBUG_PROPERTY.toLowerCase(Locale.ROOT).replace('.', '_')));
} }
/** /**