Skip to content

Commit

Permalink
Use ConfigUtil internally (#5048)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg committed Dec 17, 2022
1 parent 10e32fe commit ece93b7
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private static OpenTelemetry maybeAutoConfigureAndSetGlobal() {

// If autoconfigure module is present but global autoconfigure disabled log a warning and return
boolean globalAutoconfigureEnabled =
Boolean.parseBoolean(ConfigUtil.getString(GLOBAL_AUTOCONFIGURE_ENABLED_PROPERTY));
Boolean.parseBoolean(ConfigUtil.getString(GLOBAL_AUTOCONFIGURE_ENABLED_PROPERTY, "false"));
if (!globalAutoconfigureEnabled) {
logger.log(
Level.INFO,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ private ConfigUtil() {}
* 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
* @return the system property if not null, or the environment variable if not null, or {@code
* defaultValue}
*/
@Nullable
public static String getString(String key) {
public static String getString(String key, String defaultValue) {
String normalizedKey = normalizePropertyKey(key);
String systemProperty =
System.getProperties().entrySet().stream()
Expand All @@ -46,7 +46,7 @@ public static String getString(String key) {
.filter(entry -> normalizedKey.equals(normalizeEnvironmentVariableKey(entry.getKey())))
.map(Map.Entry::getValue)
.findFirst()
.orElse(null);
.orElse(defaultValue);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ 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);
assertThat(ConfigUtil.getString("config.key", "default")).isEqualTo("system");
assertThat(ConfigUtil.getString("config-key", "default")).isEqualTo("system");
assertThat(ConfigUtil.getString("other.config.key", "default")).isEqualTo("default");
}

@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);
assertThat(ConfigUtil.getString("config.key", "default")).isEqualTo("system");
assertThat(ConfigUtil.getString("config-key", "default")).isEqualTo("system");
assertThat(ConfigUtil.getString("other.config.key", "default")).isEqualTo("default");
}

@Test
void getString_EnvironmentVariable() {
assertThat(ConfigUtil.getString("config.key")).isEqualTo("environment");
assertThat(ConfigUtil.getString("other.config.key")).isEqualTo(null);
assertThat(ConfigUtil.getString("config.key", "default")).isEqualTo("environment");
assertThat(ConfigUtil.getString("other.config.key", "default")).isEqualTo("default");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static io.opentelemetry.exporter.internal.marshal.WireFormat.MAX_VARINT32_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.OutputStream;

Expand Down Expand Up @@ -71,7 +72,10 @@ public abstract class CodedOutputStream {
static {
int bufferSize = 50 * 1024;
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) {
// Ignore.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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.
Expand All @@ -20,13 +20,7 @@ public final class DebugConfig {
private DebugConfig() {}

static {
// Attempt to mirror the logic in DefaultConfigProperties here...
enabled =
"true".equalsIgnoreCase(System.getProperty(ENABLE_METRICS_DEBUG_PROPERTY))
|| "true"
.equalsIgnoreCase(
System.getenv(
ENABLE_METRICS_DEBUG_PROPERTY.toLowerCase(Locale.ROOT).replace('.', '_')));
enabled = Boolean.parseBoolean(ConfigUtil.getString(ENABLE_METRICS_DEBUG_PROPERTY, "false"));
}

/**
Expand Down

0 comments on commit ece93b7

Please sign in to comment.