66package io .opentelemetry .instrumentation .api .internal ;
77
88import static io .opentelemetry .api .incubator .config .DeclarativeConfigProperties .empty ;
9- import static java .util .Collections .emptyList ;
109
1110import io .opentelemetry .api .OpenTelemetry ;
1211import io .opentelemetry .api .incubator .ExtendedOpenTelemetry ;
@@ -48,13 +47,9 @@ private static boolean supportsDeclarativeConfig() {
4847 * <p>It's recommended to use {@link #getBoolean(OpenTelemetry, String...)} instead to support
4948 * Declarative Config.
5049 */
51- public static Optional <Boolean > getBoolean (String propertyName ) {
52- Optional <String > string = getString (propertyName );
53- // lambdas must not be used here in early initialization phase on early JDK8 versions
54- if (string .isPresent ()) {
55- return Optional .of (Boolean .parseBoolean (string .get ()));
56- }
57- return Optional .empty ();
50+ public static boolean getBoolean (String propertyName , boolean defaultValue ) {
51+ String strValue = getString (propertyName );
52+ return strValue == null ? defaultValue : Boolean .parseBoolean (strValue );
5853 }
5954
6055 /**
@@ -66,24 +61,24 @@ public static Optional<Boolean> getBoolean(OpenTelemetry openTelemetry, String..
6661 if (node != null ) {
6762 return Optional .ofNullable (node .getBoolean (leaf (propertyName )));
6863 }
69- return getBoolean (toSystemProperty (propertyName ));
64+ String strValue = getString (toSystemProperty (propertyName ));
65+ return strValue == null ? Optional .empty () : Optional .of (Boolean .parseBoolean (strValue ));
7066 }
7167
7268 /**
7369 * Returns the int value of the given property name from system properties and environment
7470 * variables.
7571 */
76- public static Optional < Integer > getInt (String propertyName ) {
77- Optional < String > string = getString (propertyName );
78- // lambdas must not be used here in early initialization phase on early JDK8 versions
79- if ( string . isPresent ()) {
80- try {
81- return Optional . of ( Integer . parseInt ( string . get ()));
82- } catch ( NumberFormatException ignored ) {
83- // ignored
84- }
72+ public static int getInt (String propertyName , int defaultValue ) {
73+ String strValue = getString (propertyName );
74+ if ( strValue == null ) {
75+ return defaultValue ;
76+ }
77+ try {
78+ return Integer . parseInt ( strValue );
79+ } catch ( NumberFormatException ignored ) {
80+ return defaultValue ;
8581 }
86- return Optional .empty ();
8782 }
8883
8984 /**
@@ -93,12 +88,25 @@ public static Optional<Integer> getInt(String propertyName) {
9388 * <p>It's recommended to use {@link #getString(OpenTelemetry, String...)} instead to support
9489 * Declarative Config.
9590 */
96- public static Optional <String > getString (String propertyName ) {
91+ @ Nullable
92+ public static String getString (String propertyName ) {
9793 String value = System .getProperty (propertyName );
9894 if (value != null ) {
99- return Optional . of ( value ) ;
95+ return value ;
10096 }
101- return Optional .ofNullable (System .getenv (toEnvVarName (propertyName )));
97+ return System .getenv (toEnvVarName (propertyName ));
98+ }
99+
100+ /**
101+ * Returns the string value of the given property name from system properties and environment
102+ * variables.
103+ *
104+ * <p>It's recommended to use {@link #getString(OpenTelemetry, String...)} instead to support
105+ * Declarative Config.
106+ */
107+ public static String getString (String propertyName , String defaultValue ) {
108+ String strValue = getString (propertyName );
109+ return strValue == null ? defaultValue : strValue ;
102110 }
103111
104112 /**
@@ -110,21 +118,19 @@ public static Optional<String> getString(OpenTelemetry openTelemetry, String...
110118 if (node != null ) {
111119 return Optional .ofNullable (node .getString (leaf (propertyName )));
112120 }
113- return getString (toSystemProperty (propertyName ));
121+ return Optional . ofNullable ( getString (toSystemProperty (propertyName ) ));
114122 }
115123
116124 /**
117125 * Returns the list of strings value of the given property name from Declarative Config if
118126 * available, otherwise falls back to system properties and environment variables.
119127 */
120- public static List <String > getList (OpenTelemetry openTelemetry , String ... propertyName ) {
121- DeclarativeConfigProperties node = getDeclarativeConfigNode ( openTelemetry , propertyName );
122- if (node ! = null ) {
123- return node . getScalarList ( leaf ( propertyName ), String . class , emptyList ()) ;
128+ public static List <String > getList (String propertyName , List < String > defaultValue ) {
129+ String value = getString ( propertyName );
130+ if (value = = null ) {
131+ return defaultValue ;
124132 }
125- return getString (toSystemProperty (propertyName ))
126- .map (value -> filterBlanksAndNulls (value .split ("," )))
127- .orElse (emptyList ());
133+ return filterBlanksAndNulls (value .split ("," ));
128134 }
129135
130136 /** Returns true if the given OpenTelemetry instance supports Declarative Config. */
0 commit comments