Ignore null / invalid spancontext and handle null attributes for links. (#2985)
* Ignore null / invalid spancontext and handle null attributes for links. * Cleanup * Fix
This commit is contained in:
parent
edf08d7bc1
commit
f113c03483
|
|
@ -137,6 +137,9 @@ public interface SpanBuilder {
|
|||
* operations, where a single batch handler processes multiple requests from different traces or
|
||||
* the same trace.
|
||||
*
|
||||
* <p>Implementations may ignore calls with an {@linkplain SpanContext#isValid() invalid span
|
||||
* context}.
|
||||
*
|
||||
* @param spanContext the context of the linked {@code Span}.
|
||||
* @return this.
|
||||
*/
|
||||
|
|
@ -149,6 +152,9 @@ public interface SpanBuilder {
|
|||
* operations, where a single batch handler processes multiple requests from different traces or
|
||||
* the same trace.
|
||||
*
|
||||
* <p>Implementations may ignore calls with an {@linkplain SpanContext#isValid() invalid span
|
||||
* context}.
|
||||
*
|
||||
* @param spanContext the context of the linked {@code Span}.
|
||||
* @param attributes the attributes of the {@code Link}.
|
||||
* @return this.
|
||||
|
|
|
|||
|
|
@ -83,12 +83,21 @@ final class SdkSpanBuilder implements SpanBuilder {
|
|||
|
||||
@Override
|
||||
public SpanBuilder addLink(SpanContext spanContext) {
|
||||
if (spanContext == null || !spanContext.isValid()) {
|
||||
return this;
|
||||
}
|
||||
addLink(LinkData.create(spanContext));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpanBuilder addLink(SpanContext spanContext, Attributes attributes) {
|
||||
if (spanContext == null || !spanContext.isValid()) {
|
||||
return this;
|
||||
}
|
||||
if (attributes == null) {
|
||||
attributes = Attributes.empty();
|
||||
}
|
||||
int totalAttributeCount = attributes.size();
|
||||
addLink(
|
||||
LinkData.create(
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;
|
|||
import static io.opentelemetry.api.common.AttributeKey.stringKey;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import io.opentelemetry.api.common.AttributeKey;
|
||||
|
|
@ -90,6 +91,21 @@ class SdkSpanBuilderTest {
|
|||
|
||||
@Test
|
||||
void addLink() {
|
||||
// Verify methods do not crash.
|
||||
SpanBuilder spanBuilder = sdkTracer.spanBuilder(SPAN_NAME);
|
||||
spanBuilder.addLink(sampledSpanContext);
|
||||
spanBuilder.addLink(sampledSpanContext, Attributes.empty());
|
||||
|
||||
RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan();
|
||||
try {
|
||||
assertThat(span.toSpanData().getLinks()).hasSize(2);
|
||||
} finally {
|
||||
span.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void addLink_invalid() {
|
||||
// Verify methods do not crash.
|
||||
SpanBuilder spanBuilder = sdkTracer.spanBuilder(SPAN_NAME);
|
||||
spanBuilder.addLink(Span.getInvalid().getSpanContext());
|
||||
|
|
@ -97,7 +113,7 @@ class SdkSpanBuilderTest {
|
|||
|
||||
RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan();
|
||||
try {
|
||||
assertThat(span.toSpanData().getLinks()).hasSize(2);
|
||||
assertThat(span.toSpanData().getLinks()).isEmpty();
|
||||
} finally {
|
||||
span.end();
|
||||
}
|
||||
|
|
@ -174,22 +190,19 @@ class SdkSpanBuilderTest {
|
|||
|
||||
@Test
|
||||
void addLinkSpanContext_null() {
|
||||
assertThatThrownBy(() -> sdkTracer.spanBuilder(SPAN_NAME).addLink(null))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
assertThatCode(() -> sdkTracer.spanBuilder(SPAN_NAME).addLink(null)).doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void addLinkSpanContextAttributes_nullContext() {
|
||||
assertThatThrownBy(() -> sdkTracer.spanBuilder(SPAN_NAME).addLink(null, Attributes.empty()))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
assertThatCode(() -> sdkTracer.spanBuilder(SPAN_NAME).addLink(null, Attributes.empty()))
|
||||
.doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
void addLinkSpanContextAttributes_nullAttributes() {
|
||||
assertThatThrownBy(
|
||||
() ->
|
||||
sdkTracer.spanBuilder(SPAN_NAME).addLink(Span.getInvalid().getSpanContext(), null))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
assertThatCode(() -> sdkTracer.spanBuilder(SPAN_NAME).addLink(sampledSpanContext, null))
|
||||
.doesNotThrowAnyException();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue