Skip to content

Commit 6c889a7

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 b542ea1 commit 6c889a7

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
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;
1615
import java.util.Arrays;
16+
import java.util.ConcurrentModificationException;
1717
import java.util.HashMap;
1818
import java.util.List;
19+
import java.util.Locale;
1920
import java.util.Map;
2021
import java.util.Optional;
22+
import java.util.Properties;
2123
import java.util.stream.Collectors;
2224
import javax.annotation.Nullable;
2325

@@ -35,11 +37,11 @@ public static Map<String, String> load() {
3537
Map<String, String> config = new HashMap<>();
3638
System.getenv()
3739
.forEach(
38-
(name, value) -> config.put(ConfigUtil.normalizeEnvironmentVariableKey(name), value));
39-
ConfigUtil.safeSystemProperties()
40+
(name, value) -> config.put(normalizeEnvironmentVariableKey(name), value));
41+
safeSystemProperties()
4042
.forEach(
4143
(key, value) ->
42-
config.put(ConfigUtil.normalizePropertyKey(key.toString()), value.toString()));
44+
config.put(normalizePropertyKey(key.toString()), value.toString()));
4345
return config;
4446
}
4547

@@ -113,7 +115,7 @@ public static int getInt(String propertyName, int defaultValue) {
113115
*/
114116
@Nullable
115117
public static String getString(String propertyName) {
116-
return config.get(ConfigUtil.normalizePropertyKey(propertyName));
118+
return config.get(normalizePropertyKey(propertyName));
117119
}
118120

119121
/**
@@ -194,5 +196,28 @@ public static String toSystemProperty(String[] propertyName) {
194196
return "otel.instrumentation." + String.join(".", propertyName).replace('_', '-');
195197
}
196198

199+
/**
200+
* Normalize an environment variable key by converting to lower case and replacing "_" with ".".
201+
*/
202+
public static String normalizeEnvironmentVariableKey(String key) {
203+
return key.toLowerCase(Locale.ROOT).replace("_", ".");
204+
}
205+
206+
/** Normalize a property key by converting to lower case and replacing "-" with ".". */
207+
public static String normalizePropertyKey(String key) {
208+
return key.toLowerCase(Locale.ROOT).replace("-", ".");
209+
}
210+
211+
/**
212+
* Returns a copy of system properties which is safe to iterate over.
213+
*
214+
* <p>In java 8 and android environments, iterating through system properties may trigger {@link
215+
* ConcurrentModificationException}. This method ensures callers can iterate safely without risk
216+
* of exception. See https://github.com/open-telemetry/opentelemetry-java/issues/6732 for details.
217+
*/
218+
public static Properties safeSystemProperties() {
219+
return (Properties) System.getProperties().clone();
220+
}
221+
197222
private ConfigPropertiesUtil() {}
198223
}

0 commit comments

Comments
 (0)