Skip to content

Commit

Permalink
Use standard ArrayList size rather than max number of links for initi…
Browse files Browse the repository at this point in the history
…al span links allocation (#6252)
  • Loading branch information
johnbley authored May 7, 2024
1 parent 2e74669 commit a745d60
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public Span addLink(SpanContext spanContext, Attributes attributes) {
return this;
}
if (links == null) {
links = new ArrayList<>(spanLimits.getMaxNumberOfLinks());
links = new ArrayList<>();
}
if (links.size() < spanLimits.getMaxNumberOfLinks()) {
links.add(link);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static java.util.stream.Collectors.joining;
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 static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
Expand Down Expand Up @@ -911,6 +912,46 @@ void addLink() {
}
}

@Test
void addLink_InvalidArgs() {
SdkSpan span = createTestSpan(SpanKind.INTERNAL);
assertThatCode(() -> span.addLink(null)).doesNotThrowAnyException();
assertThatCode(() -> span.addLink(SpanContext.getInvalid())).doesNotThrowAnyException();
assertThatCode(() -> span.addLink(null, null)).doesNotThrowAnyException();
assertThatCode(() -> span.addLink(SpanContext.getInvalid(), Attributes.empty()))
.doesNotThrowAnyException();
}

@Test
void addLink_FaultIn() {
SdkSpan span =
SdkSpan.startSpan(
spanContext,
SPAN_NAME,
instrumentationScopeInfo,
SpanKind.INTERNAL,
Span.getInvalid(),
Context.root(),
SpanLimits.getDefault(),
spanProcessor,
testClock,
resource,
null,
null, // exercises the fault-in path
0,
0);
SdkSpan linkedSpan = createTestSpan(SpanKind.INTERNAL);
span.addLink(linkedSpan.getSpanContext());

SpanData spanData = span.toSpanData();
assertThat(spanData.getTotalRecordedLinks()).isEqualTo(1);
assertThat(spanData.getLinks())
.satisfiesExactly(
link -> {
assertThat(link.getSpanContext()).isEqualTo(linkedSpan.getSpanContext());
});
}

@Test
void droppingAttributes() {
int maxNumberOfAttributes = 8;
Expand Down

0 comments on commit a745d60

Please sign in to comment.