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

[On Hold] Improve behavior of un-ended ReadableSpan #693

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.opentelemetry.trace.Tracer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
Expand Down Expand Up @@ -159,24 +160,29 @@ static RecordEventsReadableSpan startSpan(
@Override
public SpanData toSpanData() {
SpanContext spanContext = getSpanContext();
return SpanData.newBuilder()
.setName(getName())
.setInstrumentationLibraryInfo(instrumentationLibraryInfo)
.setTraceId(spanContext.getTraceId())
.setSpanId(spanContext.getSpanId())
.setTraceFlags(spanContext.getTraceFlags())
.setTracestate(spanContext.getTracestate())
.setAttributes(getAttributes())
.setStartEpochNanos(startEpochNanos)
.setEndEpochNanos(getEndEpochNanos())
.setKind(kind)
.setLinks(getLinks())
.setParentSpanId(parentSpanId)
.setHasRemoteParent(hasRemoteParent)
.setResource(resource)
.setStatus(getStatus())
.setTimedEvents(adaptTimedEvents())
.build();
// Set immutable info outside the lock.
SpanData.Builder builder =
SpanData.newBuilder()
.setInstrumentationLibraryInfo(instrumentationLibraryInfo)
.setTraceId(spanContext.getTraceId())
.setSpanId(spanContext.getSpanId())
.setTraceFlags(spanContext.getTraceFlags())
.setTracestate(spanContext.getTracestate())
.setStartEpochNanos(startEpochNanos)
.setKind(kind)
.setLinks(getLinks())
.setParentSpanId(parentSpanId)
.setHasRemoteParent(hasRemoteParent)
.setResource(resource);
synchronized (lock) {
builder
.setName(getName())
.setAttributes(hasBeenEnded ? attributes : new HashMap<>(attributes))
Oberon00 marked this conversation as resolved.
Show resolved Hide resolved
.setEndEpochNanos(getEndEpochNanos())
.setStatus(getStatus())
.setTimedEvents(adaptTimedEvents());
}
return builder.build();
}

private List<SpanData.TimedEvent> adaptTimedEvents() {
Expand Down Expand Up @@ -261,22 +267,20 @@ private List<TimedEvent> getTimedEvents() {
*/
@VisibleForTesting
List<Link> getLinks() {
synchronized (lock) {
if (links == null) {
return Collections.emptyList();
}
List<Link> result = new ArrayList<>(links.size());
for (Link link : links) {
Link newLink = link;
if (!(link instanceof SpanData.Link)) {
// Make a copy because the given Link may not be immutable and we may reference a lot of
// memory.
newLink = SpanData.Link.create(link.getContext(), link.getAttributes());
}
result.add(newLink);
if (links == null) {
return Collections.emptyList();
}
List<Link> result = new ArrayList<>(links.size());
for (Link link : links) {
Link newLink = link;
if (!(link instanceof SpanData.Link)) {
// Make a copy because the given Link may not be immutable and we may reference a lot of
// memory.
newLink = SpanData.Link.create(link.getContext(), link.getAttributes());
}
return Collections.unmodifiableList(result);
result.add(newLink);
}
return Collections.unmodifiableList(result);
}

/**
Expand Down