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

Optimize SpanBuilderSdk link memory #753

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
19 changes: 10 additions & 9 deletions sdk/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class SpanBuilderSdk implements Span.Builder {
private Kind spanKind = Kind.INTERNAL;
private final AttributesWithCapacity attributes;
private List<Link> links;
private int totalNumberOfLinksAdded = 0;
private ParentType parentType = ParentType.CURRENT_SPAN;
private long startEpochNanos = 0;

Expand Down Expand Up @@ -127,10 +128,17 @@ public Span.Builder addLink(SpanContext spanContext, Map<String, AttributeValue>
@Override
public Span.Builder addLink(Link link) {
Utils.checkNotNull(link, "link");
totalNumberOfLinksAdded++;
// don't bother doing anything with any links beyond the max.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to drop the earliest in general.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why? Are later links somehow better than earlier ones? What's the business rationale here? [the code is much more efficient if we can just drop the later ones]

if (links.size() == traceConfig.getMaxNumberOfLinks()) {
return this;
}

// This is the Collection.emptyList which is immutable.
if (links.isEmpty()) {
links = new ArrayList<>();
}

links.add(link);
return this;
}
Expand Down Expand Up @@ -214,18 +222,11 @@ public Span startSpan() {
getClock(parentSpan(parentType, parent), clock),
resource,
attributes,
truncatedLinks(),
links.size(),
links,
totalNumberOfLinksAdded,
startEpochNanos);
}

private List<Link> truncatedLinks() {
if (links.size() <= traceConfig.getMaxNumberOfLinks()) {
return links;
}
return links.subList(links.size() - traceConfig.getMaxNumberOfLinks(), links.size());
}

private static Clock getClock(Span parent, Clock clock) {
if (parent instanceof RecordEventsReadableSpan) {
RecordEventsReadableSpan parentRecordEventsSpan = (RecordEventsReadableSpan) parent;
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/main/java/io/opentelemetry/sdk/trace/SpanData.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,14 @@ public static Builder newBuilder() {
.setParentSpanId(SpanId.getInvalid())
.setInstrumentationLibraryInfo(InstrumentationLibraryInfo.EMPTY)
.setLinks(Collections.<io.opentelemetry.trace.Link>emptyList())
.setTotalRecordedLinks(0)
.setAttributes(Collections.<String, AttributeValue>emptyMap())
.setTimedEvents(Collections.<TimedEvent>emptyList())
.setTotalRecordedEvents(0)
.setResource(Resource.getEmpty())
.setTracestate(Tracestate.getDefault())
.setTraceFlags(TraceFlags.getDefault())
.setNumberOfChildren(0)
.setHasRemoteParent(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ public void truncateLink() {
}
RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan();
try {
List<Link> links = span.toSpanData().getLinks();
SpanData spanData = span.toSpanData();
List<Link> links = spanData.getLinks();
assertThat(links.size()).isEqualTo(maxNumberOfLinks);
for (int i = 0; i < maxNumberOfLinks; i++) {
assertThat(links.get(i)).isEqualTo(SpanData.Link.create(sampledSpanContext));
assertThat(spanData.getTotalRecordedLinks()).isEqualTo(2 * maxNumberOfLinks);
}
} finally {
span.end();
Expand Down