I am working on implementing opentelemetry tracing for a multi-microservice application deployed in Google Cloud's App Engine. App Engine provides tracing by default, and includes a context in the header (` x-cloud-trace-context `) with each request. I'm building the API layer using FastAPI, and want to extract the context from the header to create spans for the API layer with the same context using the opentelemetry Python SDK. However, when using the ` propagator.extract ` method, I'm getting a blank context.
from opentelemetry import trace
from opentelemetry import propagate
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.trace import (
SpanContext,
get_current_span,
set_span_in_context,
)
provider = TracerProvider(resource=Resource.create({"service.name": "api-backend"}))
trace.set_tracer_provider(provider)
tracer = trace.get_tracer(__name__)
PROPAGATOR = propagate.get_global_textmap()
def get_span_from_request_header(request_headers):
headers = {"x-cloud-trace-context": request_headers.get("x-cloud-trace-context")}
context = PROPAGATOR.extract(headers)
print(context)
span = get_current_span(context=context)
set_span_in_context(span, context)
print(f" Printing span: {span.get_span_context()}")
return span
In above code both the print statement gives, blank response.
When I printed the header it is giving output in below format, I tried to create a SpanContext object from the trace_id
and span_id
but it failed giving error that in SpanContext
it expects trace
_
id
as an integer value
"X-Cloud-Trace-Context: TRACE_ID/SPAN_ID;o=TRACE_TRUE"
Trace_ID - "Hexadecimal string"
SPAN_ID - "Hexadecimal string"
How can I extract the context from the header provided by Google Cloud and create spans for the API layer using opentelemetry Python SDK?
I was expecting that it should create a context from header from some method in SDK but I am unable to find that