Skip to content

Commit 6770c04

Browse files
authored
Fix IllegalArgumentException: Unrecognized AnyValue type on empty LogRecord.body (#2529)
1 parent 3cd0f7f commit 6770c04

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

disk-buffering/src/main/java/io/opentelemetry/contrib/disk/buffering/internal/serialization/mapping/logs/LogRecordDataMapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public final class LogRecordDataMapper {
2929

3030
private static final LogRecordDataMapper INSTANCE = new LogRecordDataMapper();
3131

32+
private static final AnyValue EMPTY_BODY = new AnyValue.Builder().build();
33+
3234
public static LogRecordDataMapper getInstance() {
3335
return INSTANCE;
3436
}
@@ -135,6 +137,8 @@ private static Value<?> anyValueToBody(AnyValue source) {
135137
source.array_value.values.stream()
136138
.map(LogRecordDataMapper::anyValueToBody)
137139
.collect(toList()));
140+
} else if (source.equals(EMPTY_BODY)) {
141+
return Value.of("");
138142
}
139143
throw new IllegalArgumentException("Unrecognized AnyValue type");
140144
}

disk-buffering/src/test/java/io/opentelemetry/contrib/disk/buffering/internal/serialization/mapping/logs/LogRecordDataMapperTest.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
import io.opentelemetry.api.logs.Severity;
1212
import io.opentelemetry.contrib.disk.buffering.internal.serialization.mapping.logs.models.LogRecordDataImpl;
1313
import io.opentelemetry.contrib.disk.buffering.testutils.TestData;
14+
import io.opentelemetry.proto.common.v1.AnyValue;
1415
import io.opentelemetry.proto.logs.v1.LogRecord;
1516
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
1617
import io.opentelemetry.sdk.logs.data.LogRecordData;
1718
import io.opentelemetry.sdk.resources.Resource;
19+
import java.io.IOException;
1820
import org.junit.jupiter.api.Test;
1921

2022
class LogRecordDataMapperTest {
@@ -34,14 +36,50 @@ class LogRecordDataMapperTest {
3436
.setEventName("my.event.name")
3537
.build();
3638

39+
private static final LogRecordData LOG_RECORD_WITH_EMPTY_STRING_BODY =
40+
LogRecordDataImpl.builder()
41+
.setResource(TestData.RESOURCE_FULL)
42+
.setSpanContext(TestData.SPAN_CONTEXT)
43+
.setInstrumentationScopeInfo(TestData.INSTRUMENTATION_SCOPE_INFO_FULL)
44+
.setAttributes(TestData.ATTRIBUTES)
45+
.setBodyValue(Value.of(""))
46+
.setSeverity(Severity.DEBUG)
47+
.setSeverityText("Log severity text")
48+
.setTimestampEpochNanos(100L)
49+
.setObservedTimestampEpochNanos(200L)
50+
.setTotalAttributeCount(3)
51+
.setEventName("my.event.name")
52+
.build();
53+
3754
@Test
38-
void verifyMapping() {
39-
LogRecord proto = mapToProto(LOG_RECORD);
55+
void verifyMapping() throws IOException {
56+
LogRecord proto = encodeAndDecode(mapToProto(LOG_RECORD));
4057

4158
assertThat(mapToSdk(proto, LOG_RECORD.getResource(), LOG_RECORD.getInstrumentationScopeInfo()))
4259
.isEqualTo(LOG_RECORD);
4360
}
4461

62+
@Test
63+
void verifyEmptyBodyMapping() throws IOException {
64+
LogRecord proto = encodeAndDecode(mapToProto(LOG_RECORD_WITH_EMPTY_STRING_BODY));
65+
66+
LogRecord emptyBodyProto =
67+
proto
68+
.newBuilder()
69+
// It can't be replicated in tests, but in production, after decoding the protobuf,
70+
// we ended up with an empty AnyValue when the original body was an empty string.
71+
// So we are faking it.
72+
.body(new AnyValue.Builder().build())
73+
.build();
74+
75+
assertThat(
76+
mapToSdk(
77+
emptyBodyProto,
78+
LOG_RECORD_WITH_EMPTY_STRING_BODY.getResource(),
79+
LOG_RECORD_WITH_EMPTY_STRING_BODY.getInstrumentationScopeInfo()))
80+
.isEqualTo(LOG_RECORD_WITH_EMPTY_STRING_BODY);
81+
}
82+
4583
private static LogRecord mapToProto(LogRecordData data) {
4684
return LogRecordDataMapper.getInstance().mapToProto(data);
4785
}
@@ -50,4 +88,8 @@ private static LogRecordData mapToSdk(
5088
LogRecord data, Resource resource, InstrumentationScopeInfo scopeInfo) {
5189
return LogRecordDataMapper.getInstance().mapToSdk(data, resource, scopeInfo);
5290
}
91+
92+
private static LogRecord encodeAndDecode(LogRecord proto) throws IOException {
93+
return LogRecord.ADAPTER.decode(proto.encode());
94+
}
5395
}

0 commit comments

Comments
 (0)