Don't copy events toSpanData if ended. (#2979)

This commit is contained in:
Anuraag Agrawal 2021-03-05 01:13:39 +09:00 committed by GitHub
parent 5e2791703f
commit 64ed0415d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -483,6 +483,12 @@ final class RecordEventsReadableSpan implements ReadWriteSpan {
return Collections.emptyList();
}
// if the span has ended, then the events are unmodifiable
// so we can return them directly and save copying all the data.
if (hasEnded) {
return Collections.unmodifiableList(events);
}
return Collections.unmodifiableList(new ArrayList<>(events));
}

View File

@ -195,6 +195,17 @@ class RecordEventsReadableSpanTest {
.isInstanceOf(UnsupportedOperationException.class);
}
@Test
void toSpanData_immutableEvents_ended() {
RecordEventsReadableSpan span = createTestSpan(SpanKind.INTERNAL);
span.end();
SpanData spanData = span.toSpanData();
assertThatThrownBy(
() -> spanData.getEvents().add(EventData.create(1000, "test", Attributes.empty())))
.isInstanceOf(UnsupportedOperationException.class);
}
@Test
void toSpanData_RootSpan() {
RecordEventsReadableSpan span = createTestRootSpan();