sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[Feature Request] Make sure OTel baggage propagates properly throughout activities

Open cretz opened this issue 2 years ago • 3 comments

Describe the solution you'd like

Write a test confirming that baggage is properly serialized/rehydrated to/within activities and if it is not, fix that.

cretz avatar Aug 04 '23 22:08 cretz

I did a quick change to _TracingActivityInboundInterceptor to add attach/detach calls, and then it works:

        ctx = self.root._context_from_headers(input.headers)
        with self.root.tracer.start_as_current_span(
            f"RunActivity:{info.activity_type}",
            context=ctx,
            attributes={
                "temporalWorkflowID": info.workflow_id,
                "temporalRunID": info.workflow_run_id,
                "temporalActivityID": info.activity_id,
            },
        ):
            token = opentelemetry.context.attach(ctx)
            try:
                return await super().execute_activity(input)
            finally:
                opentelemetry.context.detach(token)

recht avatar Aug 04 '23 22:08 recht

Thanks! I guess it's intentional that https://opentelemetry-python.readthedocs.io/en/latest/api/trace.html#opentelemetry.trace.Tracer.start_as_current_span does not do that.

cretz avatar Aug 04 '23 22:08 cretz

At first glance, this appears caused by a known OTel Python bug https://github.com/open-telemetry/opentelemetry-python/issues/3350 (as linked from https://github.com/open-telemetry/opentelemetry-python/issues/2432). We will workaround by re-attaching the context (so long as we don't lose the span we created). I will write tests to confirm.

cretz avatar Aug 04 '23 22:08 cretz