Skip to content

Commit d8f5c04

Browse files
Fix/declarative config no exceptions (#8079)
Co-authored-by: Jack Berg <34418638+jack-berg@users.noreply.github.com>
1 parent e40dca3 commit d8f5c04

File tree

3 files changed

+60
-36
lines changed

3 files changed

+60
-36
lines changed

api/incubator/src/main/java/io/opentelemetry/api/incubator/config/DeclarativeConfigProperties.java

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ static Map<String, Object> toMap(DeclarativeConfigProperties declarativeConfigPr
4646
/**
4747
* Returns a {@link String} configuration property.
4848
*
49-
* @return null if the property has not been configured
50-
* @throws DeclarativeConfigException if the property is not a valid scalar string
49+
* @return null if the property has not been configured or if the property is not a valid scalar
50+
* string
5151
*/
5252
@Nullable
5353
String getString(String name);
@@ -56,8 +56,7 @@ static Map<String, Object> toMap(DeclarativeConfigProperties declarativeConfigPr
5656
* Returns a {@link String} configuration property.
5757
*
5858
* @return a {@link String} configuration property or {@code defaultValue} if a property with
59-
* {@code name} has not been configured
60-
* @throws DeclarativeConfigException if the property is not a valid scalar string
59+
* {@code name} has not been configured or is not a valid scalar string
6160
*/
6261
default String getString(String name, String defaultValue) {
6362
return defaultIfNull(getString(name), defaultValue);
@@ -67,8 +66,8 @@ default String getString(String name, String defaultValue) {
6766
* Returns a {@link Boolean} configuration property. Implementations should use the same rules as
6867
* {@link Boolean#parseBoolean(String)} for handling the values.
6968
*
70-
* @return null if the property has not been configured
71-
* @throws DeclarativeConfigException if the property is not a valid scalar boolean
69+
* @return null if the property has not been configured or if the property is not a valid scalar
70+
* boolean
7271
*/
7372
@Nullable
7473
Boolean getBoolean(String name);
@@ -77,8 +76,7 @@ default String getString(String name, String defaultValue) {
7776
* Returns a {@link Boolean} configuration property.
7877
*
7978
* @return a {@link Boolean} configuration property or {@code defaultValue} if a property with
80-
* {@code name} has not been configured
81-
* @throws DeclarativeConfigException if the property is not a valid scalar boolean
79+
* {@code name} has not been configured or is not a valid scalar boolean
8280
*/
8381
default boolean getBoolean(String name, boolean defaultValue) {
8482
return defaultIfNull(getBoolean(name), defaultValue);
@@ -90,8 +88,8 @@ default boolean getBoolean(String name, boolean defaultValue) {
9088
* <p>If the underlying config property is {@link Long}, it is converted to {@link Integer} with
9189
* {@link Long#intValue()} which may result in loss of precision.
9290
*
93-
* @return null if the property has not been configured
94-
* @throws DeclarativeConfigException if the property is not a valid scalar integer
91+
* @return null if the property has not been configured or if the property is not a valid scalar
92+
* integer
9593
*/
9694
@Nullable
9795
Integer getInt(String name);
@@ -103,8 +101,7 @@ default boolean getBoolean(String name, boolean defaultValue) {
103101
* {@link Long#intValue()} which may result in loss of precision.
104102
*
105103
* @return a {@link Integer} configuration property or {@code defaultValue} if a property with
106-
* {@code name} has not been configured
107-
* @throws DeclarativeConfigException if the property is not a valid scalar integer
104+
* {@code name} has not been configured or is not a valid scalar integer
108105
*/
109106
default int getInt(String name, int defaultValue) {
110107
return defaultIfNull(getInt(name), defaultValue);
@@ -113,8 +110,8 @@ default int getInt(String name, int defaultValue) {
113110
/**
114111
* Returns a {@link Long} configuration property.
115112
*
116-
* @return null if the property has not been configured
117-
* @throws DeclarativeConfigException if the property is not a valid scalar long
113+
* @return null if the property has not been configured or if the property is not a valid scalar
114+
* long
118115
*/
119116
@Nullable
120117
Long getLong(String name);
@@ -123,8 +120,7 @@ default int getInt(String name, int defaultValue) {
123120
* Returns a {@link Long} configuration property.
124121
*
125122
* @return a {@link Long} configuration property or {@code defaultValue} if a property with {@code
126-
* name} has not been configured
127-
* @throws DeclarativeConfigException if the property is not a valid scalar long
123+
* name} has not been configured or is not a valid scalar long
128124
*/
129125
default long getLong(String name, long defaultValue) {
130126
return defaultIfNull(getLong(name), defaultValue);
@@ -133,8 +129,8 @@ default long getLong(String name, long defaultValue) {
133129
/**
134130
* Returns a {@link Double} configuration property.
135131
*
136-
* @return null if the property has not been configured
137-
* @throws DeclarativeConfigException if the property is not a valid scalar double
132+
* @return null if the property has not been configured or if the property is not a valid scalar
133+
* double
138134
*/
139135
@Nullable
140136
Double getDouble(String name);
@@ -143,8 +139,7 @@ default long getLong(String name, long defaultValue) {
143139
* Returns a {@link Double} configuration property.
144140
*
145141
* @return a {@link Double} configuration property or {@code defaultValue} if a property with
146-
* {@code name} has not been configured
147-
* @throws DeclarativeConfigException if the property is not a valid scalar double
142+
* {@code name} has not been configured or is not a valid scalar double
148143
*/
149144
default double getDouble(String name, double defaultValue) {
150145
return defaultIfNull(getDouble(name), defaultValue);
@@ -158,8 +153,8 @@ default double getDouble(String name, double defaultValue) {
158153
* @param scalarType the scalar type, one of {@link String}, {@link Boolean}, {@link Long} or
159154
* {@link Double}
160155
* @return a {@link List} configuration property, or null if the property has not been configured
161-
* @throws DeclarativeConfigException if the property is not a valid sequence of scalars, or if
162-
* {@code scalarType} is not supported
156+
* or if the property is not a valid sequence of scalars
157+
* @throws DeclarativeConfigException if {@code scalarType} is not supported
163158
*/
164159
@Nullable
165160
<T> List<T> getScalarList(String name, Class<T> scalarType);
@@ -172,8 +167,7 @@ default double getDouble(String name, double defaultValue) {
172167
* @param scalarType the scalar type, one of {@link String}, {@link Boolean}, {@link Long} or
173168
* {@link Double}
174169
* @return a {@link List} configuration property or {@code defaultValue} if a property with {@code
175-
* name} has not been configured
176-
* @throws DeclarativeConfigException if the property is not a valid sequence of scalars
170+
* name} has not been configured or is not a valid sequence of scalars
177171
*/
178172
default <T> List<T> getScalarList(String name, Class<T> scalarType, List<T> defaultValue) {
179173
return defaultIfNull(getScalarList(name, scalarType), defaultValue);
@@ -183,8 +177,7 @@ default <T> List<T> getScalarList(String name, Class<T> scalarType, List<T> defa
183177
* Returns a {@link DeclarativeConfigProperties} configuration property.
184178
*
185179
* @return a map-valued configuration property, or {@code null} if {@code name} has not been
186-
* configured
187-
* @throws DeclarativeConfigException if the property is not a mapping
180+
* configured or is not a mapping
188181
*/
189182
@Nullable
190183
DeclarativeConfigProperties getStructured(String name);
@@ -193,8 +186,7 @@ default <T> List<T> getScalarList(String name, Class<T> scalarType, List<T> defa
193186
* Returns a list of {@link DeclarativeConfigProperties} configuration property.
194187
*
195188
* @return a map-valued configuration property, or {@code defaultValue} if {@code name} has not
196-
* been configured
197-
* @throws DeclarativeConfigException if the property is not a mapping
189+
* been configured or is not a mapping
198190
*/
199191
default DeclarativeConfigProperties getStructured(
200192
String name, DeclarativeConfigProperties defaultValue) {
@@ -210,8 +202,7 @@ default DeclarativeConfigProperties getStructured(
210202
* but empty vs. not set, use {@link #getStructured(String)}.
211203
*
212204
* @return a map-valued configuration property, or an empty {@link DeclarativeConfigProperties}
213-
* instance if {@code name} has not been configured
214-
* @throws DeclarativeConfigException if the property is not a mapping
205+
* instance if {@code name} has not been configured or is not a mapping
215206
*/
216207
default DeclarativeConfigProperties get(String name) {
217208
return getStructured(name, empty());
@@ -221,8 +212,7 @@ default DeclarativeConfigProperties get(String name) {
221212
* Returns a list of {@link DeclarativeConfigProperties} configuration property.
222213
*
223214
* @return a list of map-valued configuration property, or {@code null} if {@code name} has not
224-
* been configured
225-
* @throws DeclarativeConfigException if the property is not a sequence of mappings
215+
* been configured or is not a sequence of mappings
226216
*/
227217
@Nullable
228218
List<DeclarativeConfigProperties> getStructuredList(String name);
@@ -231,8 +221,7 @@ default DeclarativeConfigProperties get(String name) {
231221
* Returns a list of {@link DeclarativeConfigProperties} configuration property.
232222
*
233223
* @return a list of map-valued configuration property, or {@code defaultValue} if {@code name}
234-
* has not been configured
235-
* @throws DeclarativeConfigException if the property is not a sequence of mappings
224+
* has not been configured or is not a sequence of mappings
236225
*/
237226
default List<DeclarativeConfigProperties> getStructuredList(
238227
String name, List<DeclarativeConfigProperties> defaultValue) {

sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/fileconfig/YamlDeclarativeConfigProperties.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,21 @@ private static boolean isMap(Object object) {
156156
@Nullable
157157
@Override
158158
public String getString(String name) {
159+
Objects.requireNonNull(name, "Null configuration property name");
159160
return stringOrNull(simpleEntries.get(name), name);
160161
}
161162

162163
@Nullable
163164
@Override
164165
public Boolean getBoolean(String name) {
166+
Objects.requireNonNull(name, "Null configuration property name");
165167
return booleanOrNull(simpleEntries.get(name), name);
166168
}
167169

168170
@Nullable
169171
@Override
170172
public Integer getInt(String name) {
173+
Objects.requireNonNull(name, "Null configuration property name");
171174
Object value = simpleEntries.get(name);
172175
if (value instanceof Integer) {
173176
return (Integer) value;
@@ -184,18 +187,21 @@ public Integer getInt(String name) {
184187
@Nullable
185188
@Override
186189
public Long getLong(String name) {
190+
Objects.requireNonNull(name, "Null configuration property name");
187191
return longOrNull(simpleEntries.get(name), name);
188192
}
189193

190194
@Nullable
191195
@Override
192196
public Double getDouble(String name) {
197+
Objects.requireNonNull(name, "Null configuration property name");
193198
return doubleOrNull(simpleEntries.get(name), name);
194199
}
195200

196201
@Nullable
197202
@Override
198203
public <T> List<T> getScalarList(String name, Class<T> scalarType) {
204+
Objects.requireNonNull(name, "Null configuration property name");
199205
if (!SUPPORTED_SCALAR_TYPES.contains(scalarType)) {
200206
throw new DeclarativeConfigException(
201207
"Unsupported scalar type "
@@ -290,12 +296,14 @@ private static Double doubleOrNull(@Nullable Object value, String name) {
290296
@Nullable
291297
@Override
292298
public DeclarativeConfigProperties getStructured(String name) {
299+
Objects.requireNonNull(name, "Null configuration property name");
293300
return mapEntries.get(name);
294301
}
295302

296303
@Nullable
297304
@Override
298305
public List<DeclarativeConfigProperties> getStructuredList(String name) {
306+
Objects.requireNonNull(name, "Null configuration property name");
299307
List<YamlDeclarativeConfigProperties> value = listEntries.get(name);
300308
if (value != null) {
301309
return Collections.unmodifiableList(value);
@@ -314,7 +322,8 @@ public Set<String> getPropertyKeys() {
314322

315323
@Override
316324
public String toString() {
317-
StringJoiner joiner = new StringJoiner(", ", "YamlDeclarativeConfigProperties{", "}");
325+
StringJoiner joiner =
326+
new StringJoiner(", ", YamlDeclarativeConfigProperties.class.getSimpleName() + "{", "}");
318327
simpleEntries.forEach((key, value) -> joiner.add(key + "=" + value));
319328
listEntries.forEach((key, value) -> joiner.add(key + "=" + value));
320329
mapEntries.forEach((key, value) -> joiner.add(key + "=" + value));

sdk-extensions/incubator/src/test/java/io/opentelemetry/sdk/extension/incubator/fileconfig/YamlDeclarativeConfigPropertiesTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ void additionalProperties() {
130130
.isEqualTo(Arrays.asList(1.1d, 2.2d));
131131
assertThat(otherProps.getScalarList("bool_list_key", Boolean.class))
132132
.isEqualTo(Arrays.asList(true, false));
133-
// If reading a scalar list which is mixed, entries which are not aligned with the requested
133+
// If reading a scalar list which is mixed, entries which are not aligned with
134+
// the requested
134135
// type are filtered out
135136
assertThat(otherProps.getScalarList("mixed_list_key", String.class))
136137
.isEqualTo(Collections.singletonList("val1"));
@@ -230,8 +231,13 @@ void wrongType() {
230231
assertThat(otherProps.getDouble("str_key")).isNull();
231232
assertThat(otherProps.getBoolean("str_key")).isNull();
232233
assertThat(otherProps.getScalarList("str_key", String.class)).isNull();
234+
assertThat(otherProps.getScalarList("str_list_key", Long.class)).isNull();
235+
assertThat(otherProps.getScalarList("str_list_key", Boolean.class)).isNull();
236+
assertThat(otherProps.getScalarList("str_list_key", Double.class)).isNull();
233237
assertThat(otherProps.getStructured("str_key")).isNull();
234238
assertThat(otherProps.getStructuredList("str_key")).isNull();
239+
assertThat(otherProps.getStructured("str_list_key")).isNull();
240+
assertThat(otherProps.getStructuredList("map_key")).isNull();
235241

236242
assertWarning("Ignoring value for key [int_key] because it is Integer instead of String: 1");
237243
assertWarning(
@@ -240,6 +246,26 @@ void wrongType() {
240246
"Ignoring value for key [str_key] because it is String instead of Double: str_value");
241247
assertWarning(
242248
"Ignoring value for key [str_key] because it is String instead of Boolean: str_value");
249+
assertWarning(
250+
"Ignoring value for key [str_list_key] because it is String instead of Long: val1");
251+
}
252+
253+
@Test
254+
void wrongTypeWithDefault() {
255+
DeclarativeConfigProperties otherProps = structuredConfigProps.getStructured("other");
256+
assertThat(otherProps).isNotNull();
257+
258+
assertThat(otherProps.getString("int_key", "default")).isEqualTo("default");
259+
assertThat(otherProps.getInt("str_key", 100)).isEqualTo(100);
260+
assertThat(otherProps.getLong("str_key", 100L)).isEqualTo(100L);
261+
assertThat(otherProps.getDouble("str_key", 1.1)).isEqualTo(1.1);
262+
assertThat(otherProps.getBoolean("str_key", true)).isTrue();
263+
assertThat(
264+
otherProps.getScalarList("str_key", String.class, Collections.singletonList("default")))
265+
.isEqualTo(Collections.singletonList("default"));
266+
assertThat(otherProps.getStructured("str_key", empty())).isEqualTo(empty());
267+
assertThat(otherProps.getStructuredList("str_key", Collections.emptyList()))
268+
.isEqualTo(Collections.emptyList());
243269
}
244270

245271
private void assertWarning(String message) {

0 commit comments

Comments
 (0)