55
66package io .opentelemetry .instrumentation .api .config ;
77
8+ import static java .util .Collections .emptyList ;
9+ import static java .util .Collections .emptyMap ;
810import static java .util .Objects .requireNonNull ;
911
1012import com .google .auto .value .AutoValue ;
1113import java .time .Duration ;
12- import java .util .Collections ;
1314import java .util .List ;
1415import java .util .Map ;
1516import java .util .Properties ;
16- import java .util .function .Function ;
1717import org .checkerframework .checker .nullness .qual .Nullable ;
1818import org .slf4j .Logger ;
1919import org .slf4j .LoggerFactory ;
@@ -99,66 +99,75 @@ public String getString(String name, String defaultValue) {
9999 */
100100 @ Nullable
101101 public Boolean getBoolean (String name ) {
102- return getTypedProperty (name , Boolean ::parseBoolean , null );
102+ return getTypedProperty (name , ConfigValueParsers ::parseBoolean );
103103 }
104104
105105 /**
106106 * Returns a boolean-valued configuration property or {@code defaultValue} if a property with name
107107 * {@code name} has not been configured.
108108 */
109109 public boolean getBoolean (String name , boolean defaultValue ) {
110- return getTypedProperty (name , Boolean ::parseBoolean , defaultValue );
110+ return safeGetTypedProperty (name , ConfigValueParsers ::parseBoolean , defaultValue );
111111 }
112112
113113 /**
114114 * Returns a integer-valued configuration property or {@code null} if a property with name {@code
115115 * name} has not been configured.
116+ *
117+ * @throws ConfigParsingException if the property is not a valid integer.
116118 */
117119 @ Nullable
118120 public Integer getInt (String name ) {
119- return getTypedProperty (name , Integer ::parseInt , null );
121+ return getTypedProperty (name , ConfigValueParsers ::parseInt );
120122 }
121123
122124 /**
123125 * Returns a integer-valued configuration property or {@code defaultValue} if a property with name
124- * {@code name} has not been configured.
126+ * {@code name} has not been configured or when parsing has failed. This is the safe variant of
127+ * {@link #getInt(String)}.
125128 */
126129 public int getInt (String name , int defaultValue ) {
127- return getTypedProperty (name , Integer ::parseInt , defaultValue );
130+ return safeGetTypedProperty (name , ConfigValueParsers ::parseInt , defaultValue );
128131 }
129132
130133 /**
131134 * Returns a long-valued configuration property or {@code null} if a property with name {@code
132135 * name} has not been configured.
136+ *
137+ * @throws ConfigParsingException if the property is not a valid long.
133138 */
134139 @ Nullable
135140 public Long getLong (String name ) {
136- return getTypedProperty (name , Long ::parseLong , null );
141+ return getTypedProperty (name , ConfigValueParsers ::parseLong );
137142 }
138143
139144 /**
140145 * Returns a long-valued configuration property or {@code defaultValue} if a property with name
141- * {@code name} has not been configured.
146+ * {@code name} has not been configured or when parsing has failed. This is the safe variant of
147+ * {@link #getLong(String)}.
142148 */
143149 public long getLong (String name , long defaultValue ) {
144- return getTypedProperty (name , Long ::parseLong , defaultValue );
150+ return safeGetTypedProperty (name , ConfigValueParsers ::parseLong , defaultValue );
145151 }
146152
147153 /**
148154 * Returns a double-valued configuration property or {@code null} if a property with name {@code
149155 * name} has not been configured.
156+ *
157+ * @throws ConfigParsingException if the property is not a valid long.
150158 */
151159 @ Nullable
152160 public Double getDouble (String name ) {
153- return getTypedProperty (name , Double ::parseDouble , null );
161+ return getTypedProperty (name , ConfigValueParsers ::parseDouble );
154162 }
155163
156164 /**
157165 * Returns a double-valued configuration property or {@code defaultValue} if a property with name
158- * {@code name} has not been configured.
166+ * {@code name} has not been configured or when parsing has failed. This is the safe variant of
167+ * {@link #getDouble(String)}.
159168 */
160169 public double getDouble (String name , double defaultValue ) {
161- return getTypedProperty (name , Double ::parseDouble , defaultValue );
170+ return safeGetTypedProperty (name , ConfigValueParsers ::parseDouble , defaultValue );
162171 }
163172
164173 /**
@@ -176,15 +185,18 @@ public double getDouble(String name, double defaultValue) {
176185 * </ul>
177186 *
178187 * <p>If no unit is specified, milliseconds is the assumed duration unit.
188+ *
189+ * @throws ConfigParsingException if the property is not a valid long.
179190 */
180191 @ Nullable
181192 public Duration getDuration (String name ) {
182- return getTypedProperty (name , ConfigValueParsers ::parseDuration , null );
193+ return getTypedProperty (name , ConfigValueParsers ::parseDuration );
183194 }
184195
185196 /**
186197 * Returns a duration-valued configuration property or {@code defaultValue} if a property with
187- * name {@code name} has not been configured.
198+ * name {@code name} has not been configured or when parsing has failed. This is the safe variant
199+ * of {@link #getDuration(String)}.
188200 *
189201 * <p>Durations can be of the form "{number}{unit}", where unit is one of:
190202 *
@@ -199,7 +211,7 @@ public Duration getDuration(String name) {
199211 * <p>If no unit is specified, milliseconds is the assumed duration unit.
200212 */
201213 public Duration getDuration (String name , Duration defaultValue ) {
202- return getTypedProperty (name , ConfigValueParsers ::parseDuration , defaultValue );
214+ return safeGetTypedProperty (name , ConfigValueParsers ::parseDuration , defaultValue );
203215 }
204216
205217 /**
@@ -208,7 +220,8 @@ public Duration getDuration(String name, Duration defaultValue) {
208220 * {@code one,two,three}.
209221 */
210222 public List <String > getList (String name ) {
211- return getList (name , Collections .emptyList ());
223+ List <String > list = getTypedProperty (name , ConfigValueParsers ::parseList );
224+ return list == null ? emptyList () : list ;
212225 }
213226
214227 /**
@@ -217,42 +230,51 @@ public List<String> getList(String name) {
217230 * e.g. {@code one,two,three}.
218231 */
219232 public List <String > getList (String name , List <String > defaultValue ) {
220- return getTypedProperty (name , ConfigValueParsers ::parseList , defaultValue );
233+ return safeGetTypedProperty (name , ConfigValueParsers ::parseList , defaultValue );
221234 }
222235
223236 /**
224237 * Returns a map-valued configuration property or an empty map if a property with name {@code
225238 * name} has not been configured. The format of the original value must be comma-separated for
226239 * each key, with an '=' separating the key and value, e.g. {@code
227240 * key=value,anotherKey=anotherValue}.
241+ *
242+ * @throws ConfigParsingException if the property is not a valid long.
228243 */
229244 public Map <String , String > getMap (String name ) {
230- return getMap (name , Collections .emptyMap ());
245+ Map <String , String > map = getTypedProperty (name , ConfigValueParsers ::parseMap );
246+ return map == null ? emptyMap () : map ;
231247 }
232248
233249 /**
234250 * Returns a map-valued configuration property or {@code defaultValue} if a property with name
235- * {@code name} has not been configured. The format of the original value must be comma-separated
236- * for each key, with an '=' separating the key and value, e.g. {@code
237- * key=value,anotherKey=anotherValue}.
251+ * {@code name} has not been configured or when parsing has failed. This is the safe variant of
252+ * {@link #getMap(String)}. The format of the original value must be comma-separated for each key,
253+ * with an '=' separating the key and value, e.g. {@code key=value,anotherKey=anotherValue}.
238254 */
239255 public Map <String , String > getMap (String name , Map <String , String > defaultValue ) {
240- return getTypedProperty (name , ConfigValueParsers ::parseMap , defaultValue );
256+ return safeGetTypedProperty (name , ConfigValueParsers ::parseMap , defaultValue );
241257 }
242258
243- private <T > T getTypedProperty (String name , Function <String , T > parser , T defaultValue ) {
244- String value = getRawProperty (name , null );
245- if (value == null || value .trim ().isEmpty ()) {
246- return defaultValue ;
247- }
259+ private <T > T safeGetTypedProperty (String name , ConfigValueParser <T > parser , T defaultValue ) {
248260 try {
249- return parser .apply (value );
261+ T value = getTypedProperty (name , parser );
262+ return value == null ? defaultValue : value ;
250263 } catch (RuntimeException t ) {
251- logger .debug ("Cannot parse {}" , value , t );
264+ logger .debug ("Error occurred during parsing: {}" , t . getMessage () , t );
252265 return defaultValue ;
253266 }
254267 }
255268
269+ @ Nullable
270+ private <T > T getTypedProperty (String name , ConfigValueParser <T > parser ) {
271+ String value = getRawProperty (name , null );
272+ if (value == null || value .trim ().isEmpty ()) {
273+ return null ;
274+ }
275+ return parser .parse (name , value );
276+ }
277+
256278 private String getRawProperty (String name , String defaultValue ) {
257279 return getAllProperties ().getOrDefault (NamingConvention .DOT .normalize (name ), defaultValue );
258280 }
0 commit comments