1212import io .opentelemetry .api .incubator .ExtendedOpenTelemetry ;
1313import io .opentelemetry .api .incubator .config .ConfigProvider ;
1414import io .opentelemetry .api .incubator .config .DeclarativeConfigProperties ;
15+ import io .opentelemetry .api .internal .ConfigUtil ;
1516import java .util .Arrays ;
17+ import java .util .HashMap ;
1618import java .util .List ;
17- import java .util .Locale ;
19+ import java .util .Map ;
1820import java .util .Optional ;
1921import java .util .stream .Collectors ;
2022import 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 ('_' , '-' );
0 commit comments