Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LogData nullable spanId, traceId #3745

Merged
merged 2 commits into from
Oct 14, 2021
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 @@ -15,6 +15,7 @@
import io.opentelemetry.proto.logs.v1.internal.SeverityNumber;
import io.opentelemetry.sdk.logs.data.Severity;
import java.io.IOException;
import javax.annotation.Nullable;

final class LogMarshaler extends MarshalerWithSize {
private final long timeUnixNano;
Expand All @@ -25,8 +26,8 @@ final class LogMarshaler extends MarshalerWithSize {
private final KeyValueMarshaler[] attributeMarshalers;
private final int droppedAttributesCount;
private final int flags;
private final String traceId;
private final String spanId;
@Nullable private final String traceId;
@Nullable private final String spanId;

static LogMarshaler create(io.opentelemetry.sdk.logs.data.LogData logData) {
KeyValueMarshaler[] attributeMarshalers =
Expand Down Expand Up @@ -59,8 +60,8 @@ private LogMarshaler(
KeyValueMarshaler[] attributeMarshalers,
int droppedAttributesCount,
int flags,
String traceId,
String spanId) {
@Nullable String traceId,
@Nullable String spanId) {
super(
calculateSize(
timeUnixNano,
Expand Down Expand Up @@ -114,8 +115,8 @@ private static int calculateSize(
KeyValueMarshaler[] attributeMarshalers,
int droppedAttributesCount,
int flags,
String traceId,
String spanId) {
@Nullable String traceId,
@Nullable String spanId) {
int size = 0;
size += MarshalerUtil.sizeFixed64(LogRecord.TIME_UNIX_NANO, timeUnixNano);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ void toProtoLogRecord() {
assertThat(logRecord.getTraceId().toByteArray()).isEqualTo(TRACE_ID_BYTES);
assertThat(logRecord.getSpanId().toByteArray()).isEqualTo(SPAN_ID_BYTES);
assertThat(logRecord.getName()).isEqualTo(NAME);
assertThat(logRecord.getSeverityText()).isEqualTo("INFO");
assertThat(logRecord.getBody()).isEqualTo(AnyValue.newBuilder().setStringValue(BODY).build());
assertThat(logRecord.getAttributesList())
.containsExactly(
KeyValue.newBuilder()
.setKey("key")
.setValue(AnyValue.newBuilder().setBoolValue(true).build())
.build());
assertThat(logRecord.getTimeUnixNano()).isEqualTo(12345);
}

@Test
void toProtoLogRecord_MinimalFields() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails without the updates to the marshaler.

LogRecord logRecord =
parse(
LogRecord.getDefaultInstance(),
LogMarshaler.create(
io.opentelemetry.sdk.logs.data.LogRecord.builder(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"))
.setBody(BODY)
.setSeverity(Severity.INFO)
.setAttributes(Attributes.of(AttributeKey.booleanKey("key"), true))
.setEpochNanos(12345)
.build()));

assertThat(logRecord.getTraceId().toByteArray()).isEmpty();
assertThat(logRecord.getSpanId().toByteArray()).isEmpty();
assertThat(logRecord.getName()).isBlank();
assertThat(logRecord.getSeverityText()).isBlank();
assertThat(logRecord.getBody()).isEqualTo(AnyValue.newBuilder().setStringValue(BODY).build());
assertThat(logRecord.getAttributesList())
.containsExactly(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public final class LogSinkSdkProvider {
*
* @return a new {@link LogSinkSdkProviderBuilder} for this class.
*/
static LogSinkSdkProviderBuilder builder() {
public static LogSinkSdkProviderBuilder builder() {
return new LogSinkSdkProviderBuilder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ public interface LogData {
*
* @return the trace id.
*/
@Nullable
String getTraceId();

/**
* Returns the span id for this log.
*
* @return the span id.
*/
@Nullable
String getSpanId();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ static LogRecord create(
Resource resource,
InstrumentationLibraryInfo instrumentationLibraryInfo,
long epochNanos,
String traceId,
String spanId,
@Nullable String traceId,
@Nullable String spanId,
int flags,
Severity severity,
@Nullable String severityText,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public final class LogRecordBuilder {
private final InstrumentationLibraryInfo instrumentationLibraryInfo;

private long epochNanos;
private String traceId = "";
private String spanId = "";
@Nullable private String traceId;
@Nullable private String spanId;
private int flags;
private Severity severity = Severity.UNDEFINED_SEVERITY_NUMBER;
@Nullable private String severityText;
Expand Down