How to create a recording Span given a parent Span's traceId and spanId?

150 Views Asked by At

The data flow I am trying to instrument has a variety of in-band mechanisms for propagating context (SQS, Kinesis messages, entries written in a DB, etc.) In the process of manually instrumenting some components, I need to extract the context and create recording Spans which will reflect the work being done by those components. I have tried a couple of versions of this:

Span parentSpan = Span.wrap(SpanContext.createFromRemoteParent(traceId,
                                                               parentSpanId,
                                                               TraceFlags.getDefault(), 
                                                               TraceState.getDefault()));

Context propagatedContext = Context.current().with(parentSpan);

Span span = tracer.spanBuilder("please-export-me")
                  .setParent(propagatedContext)
                  .startSpan();

However, the resulting span is of type PropagatedSpan and hence isRecording is false.

What is the correct way to solve this use case?

1

There are 1 best solutions below

0
On BEST ANSWER

Figured this out! The problem in the above code is in the TraceFlags. TraceFlags.getDefault() returns 0x00, which marks it as not sampled. We need 0x01 - which is returned by TraceFlags.getSampled(). The code below works, with span now being the SdkSpan implementation, which is exported:

Span parentSpan = Span.wrap(SpanContext.createFromRemoteParent(traceId,
                                                               parentSpanId,
                                                               TraceFlags.getSampled(), // NOT getDefault()! 
                                                               TraceState.getDefault()));

Context propagatedContext = Context.current().with(parentSpan);

Span span = tracer.spanBuilder("please-export-me")
                  .setParent(propagatedContext)
                  .startSpan();