Skip to content

Commit 89c7973

Browse files
authored
add logging when incompatible types are found (#7693)
1 parent 1e763b2 commit 89c7973

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

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

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Objects;
2121
import java.util.Set;
2222
import java.util.StringJoiner;
23+
import java.util.logging.Level;
24+
import java.util.logging.Logger;
2325
import javax.annotation.Nullable;
2426

2527
/**
@@ -36,6 +38,9 @@
3638
*/
3739
public final class YamlDeclarativeConfigProperties implements DeclarativeConfigProperties {
3840

41+
private static final Logger logger =
42+
Logger.getLogger(YamlDeclarativeConfigProperties.class.getName());
43+
3944
private static final Set<Class<?>> SUPPORTED_SCALAR_TYPES =
4045
Collections.unmodifiableSet(
4146
new LinkedHashSet<>(
@@ -151,13 +156,13 @@ private static boolean isMap(Object object) {
151156
@Nullable
152157
@Override
153158
public String getString(String name) {
154-
return stringOrNull(simpleEntries.get(name));
159+
return stringOrNull(simpleEntries.get(name), name);
155160
}
156161

157162
@Nullable
158163
@Override
159164
public Boolean getBoolean(String name) {
160-
return booleanOrNull(simpleEntries.get(name));
165+
return booleanOrNull(simpleEntries.get(name), name);
161166
}
162167

163168
@Nullable
@@ -176,13 +181,13 @@ public Integer getInt(String name) {
176181
@Nullable
177182
@Override
178183
public Long getLong(String name) {
179-
return longOrNull(simpleEntries.get(name));
184+
return longOrNull(simpleEntries.get(name), name);
180185
}
181186

182187
@Nullable
183188
@Override
184189
public Double getDouble(String name) {
185-
return doubleOrNull(simpleEntries.get(name));
190+
return doubleOrNull(simpleEntries.get(name), name);
186191
}
187192

188193
@Nullable
@@ -210,13 +215,13 @@ public <T> List<T> getScalarList(String name, Class<T> scalarType) {
210215
.map(
211216
entry -> {
212217
if (scalarType == String.class) {
213-
return stringOrNull(entry);
218+
return stringOrNull(entry, name);
214219
} else if (scalarType == Boolean.class) {
215-
return booleanOrNull(entry);
220+
return booleanOrNull(entry, name);
216221
} else if (scalarType == Long.class) {
217-
return longOrNull(entry);
222+
return longOrNull(entry, name);
218223
} else if (scalarType == Double.class) {
219-
return doubleOrNull(entry);
224+
return doubleOrNull(entry, name);
220225
}
221226
return null;
222227
})
@@ -231,40 +236,52 @@ public <T> List<T> getScalarList(String name, Class<T> scalarType) {
231236
}
232237

233238
@Nullable
234-
private static String stringOrNull(@Nullable Object value) {
239+
private static String stringOrNull(@Nullable Object value, String name) {
235240
if (value instanceof String) {
236241
return (String) value;
237242
}
243+
if (value != null) {
244+
logTypeWarning(name, value, String.class);
245+
}
238246
return null;
239247
}
240248

241249
@Nullable
242-
private static Boolean booleanOrNull(@Nullable Object value) {
250+
private static Boolean booleanOrNull(@Nullable Object value, String name) {
243251
if (value instanceof Boolean) {
244252
return (Boolean) value;
245253
}
254+
if (value != null) {
255+
logTypeWarning(name, value, Boolean.class);
256+
}
246257
return null;
247258
}
248259

249260
@Nullable
250-
private static Long longOrNull(@Nullable Object value) {
261+
private static Long longOrNull(@Nullable Object value, String name) {
251262
if (value instanceof Integer) {
252263
return ((Integer) value).longValue();
253264
}
254265
if (value instanceof Long) {
255266
return (Long) value;
256267
}
268+
if (value != null) {
269+
logTypeWarning(name, value, Long.class);
270+
}
257271
return null;
258272
}
259273

260274
@Nullable
261-
private static Double doubleOrNull(@Nullable Object value) {
275+
private static Double doubleOrNull(@Nullable Object value, String name) {
262276
if (value instanceof Float) {
263277
return ((Float) value).doubleValue();
264278
}
265279
if (value instanceof Double) {
266280
return (Double) value;
267281
}
282+
if (value != null) {
283+
logTypeWarning(name, value, Double.class);
284+
}
268285
return null;
269286
}
270287

@@ -307,4 +324,11 @@ public String toString() {
307324
public ComponentLoader getComponentLoader() {
308325
return componentLoader;
309326
}
327+
328+
private static void logTypeWarning(String key, Object value, Class<?> expected) {
329+
logger.log(
330+
Level.WARNING,
331+
"Ignoring value for key [{0}] because it is {1} instead of {2}: {3}",
332+
new Object[] {key, value.getClass().getSimpleName(), expected.getSimpleName(), value});
333+
}
310334
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.assertj.core.api.Assertions.assertThat;
1010

1111
import com.google.common.collect.ImmutableSet;
12+
import io.github.netmikey.logunit.api.LogCapturer;
1213
import io.opentelemetry.api.incubator.config.DeclarativeConfigProperties;
1314
import io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model.OpenTelemetryConfigurationModel;
1415
import java.io.ByteArrayInputStream;
@@ -18,9 +19,13 @@
1819
import java.util.List;
1920
import org.junit.jupiter.api.BeforeEach;
2021
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.RegisterExtension;
2123

2224
class YamlDeclarativeConfigPropertiesTest {
2325

26+
@RegisterExtension
27+
LogCapturer logs = LogCapturer.create().captureForType(YamlDeclarativeConfigProperties.class);
28+
2429
private static final String extendedSchema =
2530
"file_format: \"1.0-rc.1\"\n"
2631
+ "disabled: false\n"
@@ -225,6 +230,22 @@ void wrongType() {
225230
assertThat(otherProps.getScalarList("str_key", String.class)).isNull();
226231
assertThat(otherProps.getStructured("str_key")).isNull();
227232
assertThat(otherProps.getStructuredList("str_key")).isNull();
233+
234+
assertWarning("Ignoring value for key [int_key] because it is Integer instead of String: 1");
235+
assertWarning(
236+
"Ignoring value for key [str_key] because it is String instead of Long: str_value");
237+
assertWarning(
238+
"Ignoring value for key [str_key] because it is String instead of Double: str_value");
239+
assertWarning(
240+
"Ignoring value for key [str_key] because it is String instead of Boolean: str_value");
241+
}
242+
243+
private void assertWarning(String message) {
244+
logs.assertContains(
245+
e ->
246+
String.format(e.getMessage().replaceAll("\\{\\d}", "%s"), e.getArgumentArray())
247+
.contains(message),
248+
message);
228249
}
229250

230251
@Test

0 commit comments

Comments
 (0)