OpenTelemetry Python – How to instanciate a new span as a child span for a given trace_id

Question:

My goal is to perform tracing of the whole process of my application through several component. I am using GCP and Pub/Sub message queue to communicate information between components (developped in Python).

I am currently trying to keep the same root trace between component A and component B by creating a new span as a child span of my root trace.

Here is a small diagram:

Component A            ---> Pub/Sub message       ---> component B
(create the root trace)      (contain information)      (new span for root trace)

I have a given trace_id and span_id of my parent that I can transmit through Pub/Sub but I can’t figure out how to declare a new span as a child of this last. All I managed to do is to link a new trace to the parent one but it is not the behavior I am looking for.

Has someone already tried to do something like that ?

Regards,

Asked By: Clement Bouthors

||

Answers:

It’s called trace context propagation and there are multiple formats such w3c trace context, jaeger, b3 etc… https://github.com/open-telemetry/opentelemetry-specification/blob/b46bcab5fb709381f1fd52096a19541370c7d1b3/specification/context/api-propagators.md#propagators-distribution. You will have to use one of the propagator’s inject/extract methods for this. Here is the simple example using W3CTraceContext propagator.

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (BatchSpanProcessor,
                                            ConsoleSpanExporter)
from opentelemetry.trace.propagation.tracecontext import 
    TraceContextTextMapPropagator

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))

tracer = trace.get_tracer(__name__)

prop = TraceContextTextMapPropagator()
carrier = {}

# Injecting the context into carrier and send it over

with tracer.start_as_current_span("first-span") as span:
    prop.inject(carrier=carrier)
    print("Carrier after injecting span context", carrier)


# Extracting the remote context from carrier and starting a new span under same trace.

ctx = prop.extract(carrier=carrier)
with tracer.start_as_current_span("next-span", context=ctx):
    pass

Answered By: Srikanth Chekuri

For others coming here, this is what I ended up doing based on @dalalstreetboi3739’s suggestions:
(I realized that the "trace_id" I got is already a w3c-conformant trace header https://www.w3.org/TR/trace-context/#trace-context-http-headers-format which allowed me to use the TraceContextTextMapPropagator directly ):

data = get_json()
trace_id_str = data['traceid']
carrier = {'traceparent': trace_id_str}

ctx = TraceContextTextMapPropagator().extract(carrier=carrier)
with tracer.start_as_current_span("/api/doit", ctx) as span:
   # do awesome stuff here
   # current span is now a child of the parent "trace_id"
   requests.get("http://what")
Answered By: Trondh

Were you able to succeed in tracing all the spans in cloud trace successfully? i have been able to create the child span but my parent span terminates before I my child span executes due to which it doesn’t trace things properly. Any suggestions

Answered By: user19646128