Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public final class LogRecordDataMapper {

private static final LogRecordDataMapper INSTANCE = new LogRecordDataMapper();

private static final AnyValue EMPTY_BODY = new AnyValue.Builder().build();

public static LogRecordDataMapper getInstance() {
return INSTANCE;
}
Expand Down Expand Up @@ -135,6 +137,8 @@ private static Value<?> anyValueToBody(AnyValue source) {
source.array_value.values.stream()
.map(LogRecordDataMapper::anyValueToBody)
.collect(toList()));
} else if (source.equals(EMPTY_BODY)) {
return Value.of("");
}
throw new IllegalArgumentException("Unrecognized AnyValue type");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import io.opentelemetry.api.logs.Severity;
import io.opentelemetry.contrib.disk.buffering.internal.serialization.mapping.logs.models.LogRecordDataImpl;
import io.opentelemetry.contrib.disk.buffering.testutils.TestData;
import io.opentelemetry.proto.common.v1.AnyValue;
import io.opentelemetry.proto.logs.v1.LogRecord;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.resources.Resource;
import java.io.IOException;
import org.junit.jupiter.api.Test;

class LogRecordDataMapperTest {
Expand All @@ -34,14 +36,50 @@ class LogRecordDataMapperTest {
.setEventName("my.event.name")
.build();

private static final LogRecordData LOG_RECORD_WITH_EMPTY_STRING_BODY =
LogRecordDataImpl.builder()
.setResource(TestData.RESOURCE_FULL)
.setSpanContext(TestData.SPAN_CONTEXT)
.setInstrumentationScopeInfo(TestData.INSTRUMENTATION_SCOPE_INFO_FULL)
.setAttributes(TestData.ATTRIBUTES)
.setBodyValue(Value.of(""))
.setSeverity(Severity.DEBUG)
.setSeverityText("Log severity text")
.setTimestampEpochNanos(100L)
.setObservedTimestampEpochNanos(200L)
.setTotalAttributeCount(3)
.setEventName("my.event.name")
.build();

@Test
void verifyMapping() {
LogRecord proto = mapToProto(LOG_RECORD);
void verifyMapping() throws IOException {
LogRecord proto = encodeAndDecode(mapToProto(LOG_RECORD));

assertThat(mapToSdk(proto, LOG_RECORD.getResource(), LOG_RECORD.getInstrumentationScopeInfo()))
.isEqualTo(LOG_RECORD);
}

@Test
void verifyEmptyBodyMapping() throws IOException {
LogRecord proto = encodeAndDecode(mapToProto(LOG_RECORD_WITH_EMPTY_STRING_BODY));

LogRecord emptyBodyProto =
proto
.newBuilder()
// It can't be replicated in tests, but in production, after decoding the protobuf,
// we ended up with an empty AnyValue when the original body was an empty string.
// So we are faking it.
.body(new AnyValue.Builder().build())
.build();

assertThat(
mapToSdk(
emptyBodyProto,
LOG_RECORD_WITH_EMPTY_STRING_BODY.getResource(),
LOG_RECORD_WITH_EMPTY_STRING_BODY.getInstrumentationScopeInfo()))
.isEqualTo(LOG_RECORD_WITH_EMPTY_STRING_BODY);
}

private static LogRecord mapToProto(LogRecordData data) {
return LogRecordDataMapper.getInstance().mapToProto(data);
}
Expand All @@ -50,4 +88,8 @@ private static LogRecordData mapToSdk(
LogRecord data, Resource resource, InstrumentationScopeInfo scopeInfo) {
return LogRecordDataMapper.getInstance().mapToSdk(data, resource, scopeInfo);
}

private static LogRecord encodeAndDecode(LogRecord proto) throws IOException {
return LogRecord.ADAPTER.decode(proto.encode());
}
}
Loading