Skip to content

Commit 2df0753

Browse files
committed
normalize system props before lookup to avoid ambiguity of "experimental-foo" and "experimental.foo" that both translate to "foo/development"
1 parent cf3b05d commit 2df0753

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/ConfigPropertiesUtil.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
import io.opentelemetry.api.incubator.ExtendedOpenTelemetry;
1313
import io.opentelemetry.api.incubator.config.ConfigProvider;
1414
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
15+
import io.opentelemetry.api.internal.ConfigUtil;
1516
import java.util.Arrays;
17+
import java.util.HashMap;
1618
import java.util.List;
17-
import java.util.Locale;
19+
import java.util.Map;
1820
import java.util.Optional;
1921
import java.util.stream.Collectors;
2022
import javax.annotation.Nullable;
@@ -27,6 +29,26 @@ public final class ConfigPropertiesUtil {
2729

2830
private static final boolean supportsDeclarativeConfig = supportsDeclarativeConfig();
2931

32+
private static final Map<String, String> config = load();
33+
34+
public static Map<String, String> load() {
35+
Map<String, String> config = new HashMap<>();
36+
System.getenv()
37+
.forEach(
38+
(name, value) -> config.put(ConfigUtil.normalizeEnvironmentVariableKey(name), value));
39+
ConfigUtil.safeSystemProperties()
40+
.forEach(
41+
(key, value) ->
42+
config.put(ConfigUtil.normalizePropertyKey(key.toString()), value.toString()));
43+
return config;
44+
}
45+
46+
/** Resets the cached config for testing purposes. */
47+
public static void resetForTest() {
48+
config.clear();
49+
config.putAll(load());
50+
}
51+
3052
private static boolean supportsDeclarativeConfig() {
3153
try {
3254
Class.forName("io.opentelemetry.api.incubator.ExtendedOpenTelemetry");
@@ -91,11 +113,7 @@ public static int getInt(String propertyName, int defaultValue) {
91113
*/
92114
@Nullable
93115
public static String getString(String propertyName) {
94-
String value = System.getProperty(propertyName);
95-
if (value != null) {
96-
return value;
97-
}
98-
return System.getenv(toEnvVarName(propertyName));
116+
return config.get(ConfigUtil.normalizePropertyKey(propertyName));
99117
}
100118

101119
/**
@@ -136,10 +154,6 @@ private static List<String> filterBlanksAndNulls(String[] values) {
136154
.collect(Collectors.toList());
137155
}
138156

139-
private static String toEnvVarName(String propertyName) {
140-
return propertyName.toUpperCase(Locale.ROOT).replace('-', '_').replace('.', '_');
141-
}
142-
143157
private static String leaf(String[] propertyName) {
144158
return propertyName[propertyName.length - 1];
145159
}
@@ -174,7 +188,7 @@ public static String toSystemProperty(String[] propertyName) {
174188
for (int i = 0; i < propertyName.length; i++) {
175189
if (propertyName[i].endsWith("/development")) {
176190
propertyName[i] =
177-
"experimental-" + propertyName[i].substring(0, propertyName[i].length() - 12);
191+
"experimental." + propertyName[i].substring(0, propertyName[i].length() - 12);
178192
}
179193
}
180194
return "otel.instrumentation." + String.join(".", propertyName).replace('_', '-');

instrumentation-api/src/test/java/io/opentelemetry/instrumentation/api/internal/ConfigPropertiesUtilTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import java.util.List;
2121
import java.util.stream.Stream;
22+
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.Test;
2324
import org.junit.jupiter.params.ParameterizedTest;
2425
import org.junit.jupiter.params.provider.Arguments;
@@ -28,6 +29,11 @@
2829

2930
class ConfigPropertiesUtilTest {
3031

32+
@BeforeEach
33+
void setUp() {
34+
ConfigPropertiesUtil.resetForTest();
35+
}
36+
3137
@SetEnvironmentVariable(key = "TEST_PROPERTY_STRING", value = "env")
3238
@SetSystemProperty(key = "test.property.string", value = "sys")
3339
@Test
@@ -176,7 +182,7 @@ void toSystemProperty() {
176182
assertThat(ConfigPropertiesUtil.toSystemProperty(new String[] {"a_b", "c", "d"}))
177183
.isEqualTo("otel.instrumentation.a-b.c.d");
178184
assertThat(ConfigPropertiesUtil.toSystemProperty(new String[] {"a_b/development", "c", "d"}))
179-
.isEqualTo("otel.instrumentation.experimental-a-b.c.d");
185+
.isEqualTo("otel.instrumentation.experimental.a-b.c.d");
180186
}
181187

182188
@SetEnvironmentVariable(key = "OTEL_INSTRUMENTATION_TEST_PROPERTY_LIST", value = "a,b,c")

0 commit comments

Comments
 (0)