Skip to content

Commit d785629

Browse files
committed
Preserve trace metadata in Unsampled#deepClone
Previously cloning an unsampled trace would lose track of originatingSpanId, which mainly mattered when recovering the original trace after a `withTrace` call.
1 parent c72b86f commit d785629

1 file changed

Lines changed: 13 additions & 8 deletions

File tree

  • tracing/src/main/java/com/palantir/tracing

tracing/src/main/java/com/palantir/tracing/Trace.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.ArrayDeque;
3030
import java.util.Deque;
3131
import java.util.Optional;
32+
import java.util.OptionalInt;
3233

3334
/**
3435
* Represents a trace as an ordered list of non-completed spans. Supports adding and removing of spans. This class is
@@ -236,17 +237,23 @@ private static final class Unsampled extends Trace {
236237
* This allows thread trace state to be cleared when all "started" spans have been "removed".
237238
*/
238239
private int numberOfSpans;
239-
private Optional<String> originatingSpanId = Optional.empty();
240-
private Optional<String> topSpanId = Optional.empty();
241-
242-
private Unsampled(int numberOfSpans, String traceId) {
240+
private Optional<String> originatingSpanId;
241+
private Optional<String> topSpanId;
242+
243+
private Unsampled(
244+
int numberOfSpans,
245+
String traceId,
246+
Optional<String> originatingSpanId,
247+
Optional<String> topSpanId) {
243248
super(traceId);
244249
this.numberOfSpans = numberOfSpans;
250+
this.originatingSpanId = originatingSpanId;
251+
this.topSpanId = topSpanId;
245252
validateNumberOfSpans();
246253
}
247254

248255
private Unsampled(String traceId) {
249-
this(0, traceId);
256+
this(0, traceId, Optional.empty(), Optional.empty());
250257
}
251258

252259
@Override
@@ -269,7 +276,6 @@ void fastStartSpan(String _operation, SpanType _type) {
269276
@Override
270277
protected void push(OpenSpan span) {
271278
if (numberOfSpans == 0) {
272-
// TODO: shouldn't this be span.getOriginatingSpanId?
273279
originatingSpanId = span.getParentSpanId();
274280
topSpanId = Optional.of(span.getSpanId());
275281
}
@@ -317,8 +323,7 @@ Optional<String> getOriginatingSpanId() {
317323

318324
@Override
319325
Trace deepCopy() {
320-
// TODO: shouldn't this preserve originatingSpanId / topSpanId?
321-
return new Unsampled(numberOfSpans, getTraceId());
326+
return new Unsampled(numberOfSpans, getTraceId(), getOriginatingSpanId(), getTopSpanId());
322327
}
323328

324329
/** Internal validation, this should never fail because {@link #pop()} only decrements positive values. */

0 commit comments

Comments
 (0)