Skip to content

Commit

Permalink
Refactor Log SDK to implement OTEP-0150 (#3759)
Browse files Browse the repository at this point in the history
* Add log processors to the builder so LogSinkSdkProvider is immutable

* Add api diff

* Rename to SdkLogSinkProvider

* Rework log sdk

* Refactor logging sdk to implement OTEP-0150

* Fix typo

* Add unit tests

* Reorganize to facilitate potential future API

* Rename ReadableLog* to SdkLog*, make SdkLog* package private

* Add LogEmitterProvider interface, tidy up some javadoc

* Respond to PR feedback

* Make SdkLogEmitterBuilder final

* Switch to LogEmitter.logBuilder().emit() pattern
  • Loading branch information
jack-berg authored Nov 1, 2021
1 parent 4755217 commit f171884
Show file tree
Hide file tree
Showing 43 changed files with 1,221 additions and 500 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
- New `AttributesBuilder#remove(String)` and `AttributeBuilder#removeIf(Predicate<AttributeKey<?>>)`
methods improve ergonomics of modifying attributes.

### SDK

#### Logging (alpha)

- This release includes a rework of the Log SDK to
implement [OTEP-0150](https://github.com/open-telemetry/oteps/blob/main/text/logs/0150-logging-library-sdk.md)
and to have more symmetry to the Trace SDK. `LogSink` is now `LogEmitter`. `LogEmitter` instances
are obtained from `SdkLogEmitterProvider`. Other additions include `MultiLogProcessor` (accessed
via `LogProcessor#composite(...)`), `SimpleLogProcessor`, and `InMemoryLogExporter`.

### Auto-configuration (alpha)

- BREAKING CHANGE: Remove deprecated `otel.experimental.exporter.otlp.protocol`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
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.Severity;
import io.opentelemetry.sdk.logs.export.LogExporter;
import io.opentelemetry.sdk.resources.Resource;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -31,28 +31,28 @@ class OtlpJsonLoggingLogExporterTest {
Resource.create(Attributes.builder().put("key", "value").build());

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
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.Severity;
import io.opentelemetry.sdk.resources.Resource;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;

class SystemOutLogExporterTest {
Expand Down Expand Up @@ -50,11 +50,11 @@ void format() {
}

private static LogData sampleLog(long timestamp) {
return LogRecord.builder(Resource.empty(), InstrumentationLibraryInfo.create("logTest", "1.0"))
return LogData.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)
.setEpoch(timestamp, TimeUnit.MILLISECONDS)
.setTraceId(TraceId.fromLongs(1, 2))
.setSpanId(SpanId.fromLong(3))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
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.Severity;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.IdGenerator;
Expand Down Expand Up @@ -319,7 +318,7 @@ private static <T extends Message> HttpResponse buildResponse(HttpStatus httpSta
}

private static LogData generateFakeLog() {
return LogRecord.builder(
return LogData.builder(
Resource.getDefault(),
InstrumentationLibraryInfo.create("testLib", "1.0", "http://url"))
.setName("log-name")
Expand All @@ -329,7 +328,7 @@ private static LogData generateFakeLog() {
.setSeverityText(Severity.INFO.name())
.setTraceId(IdGenerator.random().generateTraceId())
.setSpanId(IdGenerator.random().generateSpanId())
.setEpochNanos(TimeUnit.MILLISECONDS.toNanos(Instant.now().toEpochMilli()))
.setEpoch(Instant.now())
.setFlags(0)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
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.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 LogData} 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,7 @@
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.LogData;
import io.opentelemetry.sdk.logs.data.Severity;
import io.opentelemetry.sdk.resources.Resource;
import java.io.ByteArrayOutputStream;
Expand All @@ -33,6 +34,7 @@
import java.util.Base64;
import java.util.Collections;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;

class LogsRequestMarshalerTest {
Expand All @@ -49,7 +51,7 @@ void toProtoResourceLogs() {
ResourceLogsMarshaler[] resourceLogsMarshalers =
ResourceLogsMarshaler.create(
Collections.singleton(
io.opentelemetry.sdk.logs.data.LogRecord.builder(
LogData.builder(
Resource.builder().put("one", 1).setSchemaUrl("http://url").build(),
InstrumentationLibraryInfo.create("testLib", "1.0", "http://url"))
.setName(NAME)
Expand All @@ -59,7 +61,7 @@ void toProtoResourceLogs() {
.setTraceId(TRACE_ID)
.setSpanId(SPAN_ID)
.setAttributes(Attributes.of(AttributeKey.booleanKey("key"), true))
.setEpochNanos(12345)
.setEpoch(12345, TimeUnit.NANOSECONDS)
.build()));

assertThat(resourceLogsMarshalers).hasSize(1);
Expand All @@ -82,7 +84,7 @@ void toProtoLogRecord() {
parse(
LogRecord.getDefaultInstance(),
LogMarshaler.create(
io.opentelemetry.sdk.logs.data.LogRecord.builder(
LogData.builder(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"))
.setName(NAME)
Expand All @@ -92,7 +94,7 @@ void toProtoLogRecord() {
.setTraceId(TRACE_ID)
.setSpanId(SPAN_ID)
.setAttributes(Attributes.of(AttributeKey.booleanKey("key"), true))
.setEpochNanos(12345)
.setEpoch(12345, TimeUnit.NANOSECONDS)
.build()));

assertThat(logRecord.getTraceId().toByteArray()).isEqualTo(TRACE_ID_BYTES);
Expand All @@ -115,13 +117,13 @@ void toProtoLogRecord_MinimalFields() {
parse(
LogRecord.getDefaultInstance(),
LogMarshaler.create(
io.opentelemetry.sdk.logs.data.LogRecord.builder(
LogData.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)
.setEpoch(12345, TimeUnit.NANOSECONDS)
.build()));

assertThat(logRecord.getTraceId().toByteArray()).isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@
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.Severity;
import io.opentelemetry.sdk.resources.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -357,10 +357,10 @@ void usingGrpc() {
}

private static LogData generateFakeLog() {
return LogRecord.builder(
return LogData.builder(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"))
.setEpochMillis(System.currentTimeMillis())
.setEpoch(Instant.now())
.setTraceId(TraceId.getInvalid())
.setSpanId(SpanId.getInvalid())
.setFlags(TraceFlags.getDefault().asByte())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
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.Severity;
import io.opentelemetry.sdk.resources.Resource;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand All @@ -39,10 +39,10 @@ class ExportTest {

private static final List<LogData> LOGS =
Collections.singletonList(
LogRecord.builder(
LogData.builder(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"))
.setEpochMillis(System.currentTimeMillis())
.setEpoch(Instant.now())
.setTraceId(TraceId.getInvalid())
.setSpanId(SpanId.getInvalid())
.setFlags(TraceFlags.getDefault().asByte())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
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.Severity;
import io.opentelemetry.sdk.resources.Resource;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand All @@ -39,10 +39,10 @@ class ExportTest {

private static final List<LogData> LOGS =
Collections.singletonList(
LogRecord.builder(
LogData.builder(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"))
.setEpochMillis(System.currentTimeMillis())
.setEpoch(Instant.now())
.setTraceId(TraceId.getInvalid())
.setSpanId(SpanId.getInvalid())
.setFlags(TraceFlags.getDefault().asByte())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
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.Severity;
import io.opentelemetry.sdk.resources.Resource;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand All @@ -39,10 +39,10 @@ class ExportTest {

private static final List<LogData> LOGS =
Collections.singletonList(
LogRecord.builder(
LogData.builder(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"))
.setEpochMillis(System.currentTimeMillis())
.setEpoch(Instant.now())
.setTraceId(TraceId.getInvalid())
.setSpanId(SpanId.getInvalid())
.setFlags(TraceFlags.getDefault().asByte())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
import io.opentelemetry.proto.collector.logs.v1.ExportLogsServiceResponse;
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.Severity;
import io.opentelemetry.sdk.resources.Resource;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
Expand All @@ -40,10 +40,10 @@ class OkHttpOnlyExportTest {

private static final List<LogData> LOGS =
Collections.singletonList(
LogRecord.builder(
LogData.builder(
Resource.create(Attributes.builder().put("testKey", "testValue").build()),
InstrumentationLibraryInfo.create("instrumentation", "1"))
.setEpochMillis(System.currentTimeMillis())
.setEpoch(Instant.now())
.setTraceId(TraceId.getInvalid())
.setSpanId(SpanId.getInvalid())
.setFlags(TraceFlags.getDefault().asByte())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import io.opentelemetry.proto.trace.v1.Span.Link;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.logs.data.Body;
import io.opentelemetry.sdk.logs.data.LogRecord;
import io.opentelemetry.sdk.logs.data.LogData;
import io.opentelemetry.sdk.logs.data.Severity;
import io.opentelemetry.sdk.logs.export.LogExporter;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
Expand All @@ -72,7 +72,6 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.jupiter.api.AfterAll;
Expand Down Expand Up @@ -375,8 +374,8 @@ void testOtlpHttpLogExport(String compression) {
}

private static void testLogExporter(LogExporter logExporter) {
LogRecord logRecord =
LogRecord.builder(
LogData logData =
LogData.builder(
RESOURCE,
InstrumentationLibraryInfo.create(
OtlpExporterIntegrationTest.class.getName(), null))
Expand All @@ -387,11 +386,11 @@ private static void testLogExporter(LogExporter logExporter) {
.setSeverityText("DEBUG")
.setTraceId(IdGenerator.random().generateTraceId())
.setSpanId(IdGenerator.random().generateSpanId())
.setEpochNanos(TimeUnit.MILLISECONDS.toNanos(Instant.now().toEpochMilli()))
.setEpoch(Instant.now())
.setFlags(0)
.build();

logExporter.export(Collections.singletonList(logRecord));
logExporter.export(Collections.singletonList(logData));

await()
.atMost(Duration.ofSeconds(30))
Expand Down Expand Up @@ -425,14 +424,13 @@ private static void testLogExporter(LogExporter logExporter) {
.setValue(AnyValue.newBuilder().setStringValue("value").build())
.build()));
assertThat(protoLog.getSeverityNumber().getNumber())
.isEqualTo(logRecord.getSeverity().getSeverityNumber());
.isEqualTo(logData.getSeverity().getSeverityNumber());
assertThat(protoLog.getSeverityText()).isEqualTo("DEBUG");
assertThat(TraceId.fromBytes(protoLog.getTraceId().toByteArray()))
.isEqualTo(logRecord.getTraceId());
assertThat(SpanId.fromBytes(protoLog.getSpanId().toByteArray()))
.isEqualTo(logRecord.getSpanId());
assertThat(protoLog.getTimeUnixNano()).isEqualTo(logRecord.getEpochNanos());
assertThat(protoLog.getFlags()).isEqualTo(logRecord.getFlags());
.isEqualTo(logData.getTraceId());
assertThat(SpanId.fromBytes(protoLog.getSpanId().toByteArray())).isEqualTo(logData.getSpanId());
assertThat(protoLog.getTimeUnixNano()).isEqualTo(logData.getEpochNanos());
assertThat(protoLog.getFlags()).isEqualTo(logData.getFlags());
}

private static class OtlpGrpcServer extends ServerExtension {
Expand Down
Loading

0 comments on commit f171884

Please sign in to comment.