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

Refactor Log SDK to implement OTEP-0150 #3759

Merged
merged 16 commits into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -14,7 +14,8 @@
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.logs.data.LogData;
import io.opentelemetry.sdk.logs.data.LogRecord;
import io.opentelemetry.sdk.logs.data.ReadableLogData;
import io.opentelemetry.sdk.logs.data.ReadableLogRecord;
import io.opentelemetry.sdk.logs.data.Severity;
import io.opentelemetry.sdk.logs.export.LogExporter;
import io.opentelemetry.sdk.resources.Resource;
Expand All @@ -31,30 +32,36 @@ class OtlpJsonLoggingLogExporterTest {
Resource.create(Attributes.builder().put("key", "value").build());

private static final LogData LOG1 =
LogRecord.builder(RESOURCE, InstrumentationLibraryInfo.create("instrumentation", "1"))
.setName("testLog1")
.setBody("body1")
.setFlags(0)
.setSeverity(Severity.INFO)
.setSeverityText("INFO")
.setSpanId("8765432112345876")
.setTraceId("12345678876543211234567887654322")
.setEpochMillis(1631533710L)
.setAttributes(Attributes.of(stringKey("animal"), "cat", longKey("lives"), 9L))
.build();
ReadableLogData.create(
RESOURCE,
InstrumentationLibraryInfo.create("instrumentation", "1"),
ReadableLogRecord.builder()
.setName("testLog1")
.setBody("body1")
.setFlags(0)
.setSeverity(Severity.INFO)
.setSeverityText("INFO")
.setSpanId("8765432112345876")
.setTraceId("12345678876543211234567887654322")
.setEpochMillis(1631533710L)
.setAttributes(Attributes.of(stringKey("animal"), "cat", longKey("lives"), 9L))
.build());

private static final LogData LOG2 =
LogRecord.builder(RESOURCE, InstrumentationLibraryInfo.create("instrumentation2", "2"))
.setName("testLog2")
.setBody("body2")
.setFlags(0)
.setSeverity(Severity.INFO)
.setSeverityText("INFO")
.setSpanId("8765432112345875")
.setTraceId("12345678876543211234567887654322")
.setEpochMillis(1631533710L)
.setAttributes(Attributes.of(booleanKey("important"), true))
.build();
ReadableLogData.create(
RESOURCE,
InstrumentationLibraryInfo.create("instrumentation2", "2"),
ReadableLogRecord.builder()
.setName("testLog2")
.setBody("body2")
.setFlags(0)
.setSeverity(Severity.INFO)
.setSeverityText("INFO")
.setSpanId("8765432112345875")
.setTraceId("12345678876543211234567887654322")
.setEpochMillis(1631533710L)
.setAttributes(Attributes.of(booleanKey("important"), true))
.build());

@RegisterExtension
LogCapturer logs = LogCapturer.create().captureForType(OtlpJsonLoggingLogExporter.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.logs.data.Body;
import io.opentelemetry.sdk.logs.data.LogData;
import io.opentelemetry.sdk.logs.data.LogRecord;
import io.opentelemetry.sdk.logs.data.ReadableLogData;
import io.opentelemetry.sdk.logs.data.ReadableLogRecord;
import io.opentelemetry.sdk.logs.data.Severity;
import io.opentelemetry.sdk.resources.Resource;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -50,13 +51,16 @@ void format() {
}

private static LogData sampleLog(long timestamp) {
return LogRecord.builder(Resource.empty(), InstrumentationLibraryInfo.create("logTest", "1.0"))
.setAttributes(Attributes.of(stringKey("cheese"), "cheddar", longKey("amount"), 1L))
.setBody(Body.stringBody("message"))
.setSeverity(Severity.ERROR3)
.setEpochMillis(timestamp)
.setTraceId(TraceId.fromLongs(1, 2))
.setSpanId(SpanId.fromLong(3))
.build();
return ReadableLogData.create(
Resource.empty(),
InstrumentationLibraryInfo.create("logTest", "1.0"),
ReadableLogRecord.builder()
.setAttributes(Attributes.of(stringKey("cheese"), "cheddar", longKey("amount"), 1L))
.setBody(Body.stringBody("message"))
.setSeverity(Severity.ERROR3)
.setEpochMillis(timestamp)
.setTraceId(TraceId.fromLongs(1, 2))
.setSpanId(SpanId.fromLong(3))
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.logs.data.Body;
import io.opentelemetry.sdk.logs.data.LogData;
import io.opentelemetry.sdk.logs.data.LogRecord;
import io.opentelemetry.sdk.logs.data.ReadableLogData;
import io.opentelemetry.sdk.logs.data.ReadableLogRecord;
import io.opentelemetry.sdk.logs.data.Severity;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.IdGenerator;
Expand Down Expand Up @@ -304,18 +305,19 @@ private static <T extends Message> HttpResponse buildResponse(HttpStatus httpSta
}

private static LogData generateFakeLog() {
return LogRecord.builder(
Resource.getDefault(),
InstrumentationLibraryInfo.create("testLib", "1.0", "http://url"))
.setName("log-name")
.setBody(Body.stringBody("log body"))
.setAttributes(Attributes.builder().put("key", "value").build())
.setSeverity(Severity.INFO)
.setSeverityText(Severity.INFO.name())
.setTraceId(IdGenerator.random().generateTraceId())
.setSpanId(IdGenerator.random().generateSpanId())
.setEpochNanos(TimeUnit.MILLISECONDS.toNanos(Instant.now().toEpochMilli()))
.setFlags(0)
.build();
return ReadableLogData.create(
Resource.getDefault(),
InstrumentationLibraryInfo.create("testLib", "1.0", "http://url"),
ReadableLogRecord.builder()
.setName("log-name")
.setBody(Body.stringBody("log body"))
.setAttributes(Attributes.builder().put("key", "value").build())
.setSeverity(Severity.INFO)
.setSeverityText(Severity.INFO.name())
.setTraceId(IdGenerator.random().generateTraceId())
.setSpanId(IdGenerator.random().generateSpanId())
.setEpochNanos(TimeUnit.MILLISECONDS.toNanos(Instant.now().toEpochMilli()))
.setFlags(0)
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@
import io.opentelemetry.exporter.otlp.internal.Serializer;
import io.opentelemetry.proto.collector.logs.v1.internal.ExportLogsServiceRequest;
import io.opentelemetry.sdk.logs.data.LogData;
import io.opentelemetry.sdk.logs.data.LogRecord;
import io.opentelemetry.sdk.logs.data.ReadableLogRecordBuilder;
import io.opentelemetry.sdk.trace.data.SpanData;
import java.io.IOException;
import java.util.Collection;

/**
* {@link Marshaler} to convert SDK {@link LogRecord} to OTLP ExportLogsServiceRequest.
* {@link Marshaler} to convert SDK {@link ReadableLogRecordBuilder} to OTLP
* ExportLogsServiceRequest.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import io.opentelemetry.proto.logs.v1.LogRecord;
import io.opentelemetry.proto.logs.v1.ResourceLogs;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.logs.data.ReadableLogData;
import io.opentelemetry.sdk.logs.data.ReadableLogRecord;
import io.opentelemetry.sdk.logs.data.Severity;
import io.opentelemetry.sdk.resources.Resource;
import java.io.ByteArrayOutputStream;
Expand All @@ -49,18 +51,19 @@ void toProtoResourceLogs() {
ResourceLogsMarshaler[] resourceLogsMarshalers =
ResourceLogsMarshaler.create(
Collections.singleton(
io.opentelemetry.sdk.logs.data.LogRecord.builder(
Resource.builder().put("one", 1).setSchemaUrl("http://url").build(),
InstrumentationLibraryInfo.create("testLib", "1.0", "http://url"))
.setName(NAME)
.setBody(BODY)
.setSeverity(Severity.INFO)
.setSeverityText("INFO")
.setTraceId(TRACE_ID)
.setSpanId(SPAN_ID)
.setAttributes(Attributes.of(AttributeKey.booleanKey("key"), true))
.setEpochNanos(12345)
.build()));
ReadableLogData.create(
Resource.builder().put("one", 1).setSchemaUrl("http://url").build(),
InstrumentationLibraryInfo.create("testLib", "1.0", "http://url"),
ReadableLogRecord.builder()
.setName(NAME)
.setBody(BODY)
.setSeverity(Severity.INFO)
.setSeverityText("INFO")
.setTraceId(TRACE_ID)
.setSpanId(SPAN_ID)
.setAttributes(Attributes.of(AttributeKey.booleanKey("key"), true))
.setEpochNanos(12345)
.build())));

assertThat(resourceLogsMarshalers).hasSize(1);

Expand All @@ -82,18 +85,19 @@ void toProtoLogRecord() {
parse(
LogRecord.getDefaultInstance(),
LogMarshaler.create(
io.opentelemetry.sdk.logs.data.LogRecord.builder(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"))
.setName(NAME)
.setBody(BODY)
.setSeverity(Severity.INFO)
.setSeverityText("INFO")
.setTraceId(TRACE_ID)
.setSpanId(SPAN_ID)
.setAttributes(Attributes.of(AttributeKey.booleanKey("key"), true))
.setEpochNanos(12345)
.build()));
ReadableLogData.create(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"),
ReadableLogRecord.builder()
.setName(NAME)
.setBody(BODY)
.setSeverity(Severity.INFO)
.setSeverityText("INFO")
.setTraceId(TRACE_ID)
.setSpanId(SPAN_ID)
.setAttributes(Attributes.of(AttributeKey.booleanKey("key"), true))
.setEpochNanos(12345)
.build())));

assertThat(logRecord.getTraceId().toByteArray()).isEqualTo(TRACE_ID_BYTES);
assertThat(logRecord.getSpanId().toByteArray()).isEqualTo(SPAN_ID_BYTES);
Expand All @@ -115,14 +119,15 @@ void toProtoLogRecord_MinimalFields() {
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()));
ReadableLogData.create(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"),
ReadableLogRecord.builder()
.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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.logs.data.LogData;
import io.opentelemetry.sdk.logs.data.LogRecord;
import io.opentelemetry.sdk.logs.data.ReadableLogData;
import io.opentelemetry.sdk.logs.data.ReadableLogRecord;
import io.opentelemetry.sdk.logs.data.Severity;
import io.opentelemetry.sdk.resources.Resource;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -357,19 +358,20 @@ void usingGrpc() {
}

private static LogData generateFakeLog() {
return LogRecord.builder(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"))
.setEpochMillis(System.currentTimeMillis())
.setTraceId(TraceId.getInvalid())
.setSpanId(SpanId.getInvalid())
.setFlags(TraceFlags.getDefault().asByte())
.setSeverity(Severity.ERROR)
.setSeverityText("really severe")
.setName("log1")
.setBody("message")
.setAttributes(Attributes.builder().put("animal", "cat").build())
.build();
return ReadableLogData.create(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"),
ReadableLogRecord.builder()
.setEpochMillis(System.currentTimeMillis())
.setTraceId(TraceId.getInvalid())
.setSpanId(SpanId.getInvalid())
.setFlags(TraceFlags.getDefault().asByte())
.setSeverity(Severity.ERROR)
.setSeverityText("really severe")
.setName("log1")
.setBody("message")
.setAttributes(Attributes.builder().put("animal", "cat").build())
.build());
}

private static final class FakeCollector extends LogsServiceGrpc.LogsServiceImplBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import io.opentelemetry.proto.collector.logs.v1.LogsServiceGrpc;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.logs.data.LogData;
import io.opentelemetry.sdk.logs.data.LogRecord;
import io.opentelemetry.sdk.logs.data.ReadableLogData;
import io.opentelemetry.sdk.logs.data.ReadableLogRecord;
import io.opentelemetry.sdk.logs.data.Severity;
import io.opentelemetry.sdk.resources.Resource;
import java.nio.charset.StandardCharsets;
Expand All @@ -39,19 +40,20 @@ class ExportTest {

private static final List<LogData> LOGS =
Collections.singletonList(
LogRecord.builder(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"))
.setEpochMillis(System.currentTimeMillis())
.setTraceId(TraceId.getInvalid())
.setSpanId(SpanId.getInvalid())
.setFlags(TraceFlags.getDefault().asByte())
.setSeverity(Severity.ERROR)
.setSeverityText("really severe")
.setName("log1")
.setBody("message")
.setAttributes(Attributes.builder().put("animal", "cat").build())
.build());
ReadableLogData.create(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"),
ReadableLogRecord.builder()
.setEpochMillis(System.currentTimeMillis())
.setTraceId(TraceId.getInvalid())
.setSpanId(SpanId.getInvalid())
.setFlags(TraceFlags.getDefault().asByte())
.setSeverity(Severity.ERROR)
.setSeverityText("really severe")
.setName("log1")
.setBody("message")
.setAttributes(Attributes.builder().put("animal", "cat").build())
.build()));

@RegisterExtension
@Order(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import io.opentelemetry.proto.collector.logs.v1.LogsServiceGrpc;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.logs.data.LogData;
import io.opentelemetry.sdk.logs.data.LogRecord;
import io.opentelemetry.sdk.logs.data.ReadableLogData;
import io.opentelemetry.sdk.logs.data.ReadableLogRecord;
import io.opentelemetry.sdk.logs.data.Severity;
import io.opentelemetry.sdk.resources.Resource;
import java.nio.charset.StandardCharsets;
Expand All @@ -39,19 +40,20 @@ class ExportTest {

private static final List<LogData> LOGS =
Collections.singletonList(
LogRecord.builder(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"))
.setEpochMillis(System.currentTimeMillis())
.setTraceId(TraceId.getInvalid())
.setSpanId(SpanId.getInvalid())
.setFlags(TraceFlags.getDefault().asByte())
.setSeverity(Severity.ERROR)
.setSeverityText("really severe")
.setName("log1")
.setBody("message")
.setAttributes(Attributes.builder().put("animal", "cat").build())
.build());
ReadableLogData.create(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"),
ReadableLogRecord.builder()
.setEpochMillis(System.currentTimeMillis())
.setTraceId(TraceId.getInvalid())
.setSpanId(SpanId.getInvalid())
.setFlags(TraceFlags.getDefault().asByte())
.setSeverity(Severity.ERROR)
.setSeverityText("really severe")
.setName("log1")
.setBody("message")
.setAttributes(Attributes.builder().put("animal", "cat").build())
.build()));

@RegisterExtension
@Order(1)
Expand Down
Loading