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

Let LogDataBuilder take a SpanContext directly. #3942

Merged
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 @@ -12,11 +12,9 @@

import io.github.netmikey.logunit.api.LogCapturer;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.logs.data.LogData;
import io.opentelemetry.sdk.logs.data.LogDataBuilder;
Expand Down Expand Up @@ -44,15 +42,12 @@ class OtlpJsonLoggingLogExporterTest {
.setSeverityText("INFO")
.setEpoch(1631533710L, TimeUnit.MILLISECONDS)
.setAttributes(Attributes.of(stringKey("animal"), "cat", longKey("lives"), 9L))
.setContext(
Context.root()
.with(
Span.wrap(
SpanContext.create(
"12345678876543211234567887654322",
"8765432112345876",
TraceFlags.getDefault(),
TraceState.getDefault()))))
.setSpanContext(
SpanContext.create(
"12345678876543211234567887654322",
"8765432112345876",
TraceFlags.getDefault(),
TraceState.getDefault()))
.build();

private static final LogData LOG2 =
Expand All @@ -63,15 +58,12 @@ class OtlpJsonLoggingLogExporterTest {
.setSeverityText("INFO")
.setEpoch(1631533710L, TimeUnit.MILLISECONDS)
.setAttributes(Attributes.of(booleanKey("important"), true))
.setContext(
Context.root()
.with(
Span.wrap(
SpanContext.create(
"12345678876543211234567887654322",
"8765432112345875",
TraceFlags.getDefault(),
TraceState.getDefault()))))
.setSpanContext(
SpanContext.create(
"12345678876543211234567887654322",
"8765432112345875",
TraceFlags.getDefault(),
TraceState.getDefault()))
.build();

@RegisterExtension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.logs.data.LogData;
Expand Down Expand Up @@ -59,15 +57,12 @@ private static LogData sampleLog(long timestamp) {
.setBody("message")
.setSeverity(Severity.ERROR3)
.setEpoch(timestamp, TimeUnit.MILLISECONDS)
.setContext(
Context.root()
.with(
Span.wrap(
SpanContext.create(
"00000000000000010000000000000002",
"0000000000000003",
TraceFlags.getDefault(),
TraceState.getDefault()))))
.setSpanContext(
SpanContext.create(
"00000000000000010000000000000002",
"0000000000000003",
TraceFlags.getDefault(),
TraceState.getDefault()))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.internal.OtelEncodingUtils;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceId;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.exporter.otlp.internal.Marshaler;
import io.opentelemetry.proto.common.v1.AnyValue;
import io.opentelemetry.proto.common.v1.InstrumentationLibrary;
Expand Down Expand Up @@ -63,15 +61,9 @@ void toProtoResourceLogs() {
.setBody(BODY)
.setSeverity(Severity.INFO)
.setSeverityText("INFO")
.setContext(
Context.root()
.with(
Span.wrap(
SpanContext.create(
TRACE_ID,
SPAN_ID,
TraceFlags.getDefault(),
TraceState.getDefault()))))
.setSpanContext(
SpanContext.create(
TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TraceState.getDefault()))
.setAttributes(Attributes.of(AttributeKey.booleanKey("key"), true))
.setEpoch(12345, TimeUnit.NANOSECONDS)
.build()));
Expand Down Expand Up @@ -103,15 +95,9 @@ void toProtoLogRecord() {
.setBody(BODY)
.setSeverity(Severity.INFO)
.setSeverityText("INFO")
.setContext(
Context.root()
.with(
Span.wrap(
SpanContext.create(
TRACE_ID,
SPAN_ID,
TraceFlags.getDefault(),
TraceState.getDefault()))))
.setSpanContext(
SpanContext.create(
TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TraceState.getDefault()))
.setAttributes(Attributes.of(AttributeKey.booleanKey("key"), true))
.setEpoch(12345, TimeUnit.NANOSECONDS)
.build()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceId;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.exporter.otlp.http.logs.OtlpHttpLogExporter;
import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter;
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
Expand Down Expand Up @@ -386,15 +385,12 @@ private static void testLogExporter(LogExporter logExporter) {
.setSeverity(Severity.DEBUG)
.setSeverityText("DEBUG")
.setEpoch(Instant.now())
.setContext(
Context.root()
.with(
Span.wrap(
SpanContext.create(
IdGenerator.random().generateTraceId(),
IdGenerator.random().generateSpanId(),
TraceFlags.getDefault(),
TraceState.getDefault()))))
.setSpanContext(
SpanContext.create(
IdGenerator.random().generateTraceId(),
IdGenerator.random().generateSpanId(),
TraceFlags.getDefault(),
TraceState.getDefault()))
.build();

logExporter.export(Collections.singletonList(logData));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,14 @@ public LogDataBuilder setEpoch(Instant instant) {
return this;
}

/** Set the context. */
/** Sets the context. */
public LogDataBuilder setContext(Context context) {
this.spanContext = Span.fromContext(context).getSpanContext();
return setSpanContext(Span.fromContext(context).getSpanContext());
}

/** Sets the span context. */
public LogDataBuilder setSpanContext(SpanContext spanContext) {
this.spanContext = spanContext == null ? SpanContext.getInvalid() : spanContext;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,65 @@
package io.opentelemetry.sdk.logs.data;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.resources.Resource;
import org.junit.jupiter.api.Test;

class LogDataBuilderTest {

private final Resource resource = Resource.getDefault();
private final InstrumentationLibraryInfo libraryInfo = InstrumentationLibraryInfo.empty();

@Test
void canSetClock() {
Resource resource = Resource.getDefault();
InstrumentationLibraryInfo libraryInfo = InstrumentationLibraryInfo.empty();
Clock clock = mock(Clock.class);
when(clock.now()).thenReturn(12L);
LogDataBuilder builder = LogDataBuilder.create(resource, libraryInfo, clock);

LogData result = builder.build();
assertEquals(12L, result.getEpochNanos());
}

@Test
void canSetSpanContext() {
LogDataBuilder builder = LogDataBuilder.create(resource, libraryInfo);
SpanContext spanContext = mock(SpanContext.class);
LogData result = builder.setSpanContext(spanContext).build();
assertSame(spanContext, result.getSpanContext());
}

@Test
void setSpanContext_nullSafe() {
LogDataBuilder builder = LogDataBuilder.create(resource, libraryInfo);
LogData result = builder.setSpanContext(null).build();
assertSame(SpanContext.getInvalid(), result.getSpanContext());
}

@Test
void canSetSpanContextFromContext() {
String traceId = "33333333333333333333333333333333";
String spanId = "7777777777777777";
SpanContext spanContext =
SpanContext.create(traceId, spanId, TraceFlags.getSampled(), TraceState.getDefault());
Span span = Span.wrap(spanContext);

Context context = mock(Context.class);
when(context.get(any())).thenReturn(span);

LogDataBuilder builder = LogDataBuilder.create(resource, libraryInfo);

LogData result = builder.setContext(context).build();
assertSame(spanContext, result.getSpanContext());
}
}