open-telemetry / open-telemetry/opentelemetry-python

Explicit context passed to trace.start_as_current_span does not become current

Open
#3,350 3 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

api bug sdk tracing
Dominant language
Python
Stars
2.6k
Forks
1k
Avg merge
4d 15h
Merged PRs (30d)
19

Description

Describe your environment

Affects the current version of OTel 1.18.0 but affects all versions. I believe this is an issue in both the API and SDK Tracer implementations.

Steps to reproduce

Run the following script

from opentelemetry.sdk.trace import TracerProvider as SdkTracerProvider
from opentelemetry import trace, baggage, context

key = context.create_key("mykey")

def test(tracer):
    ctx = context.set_value(key, "myvalue")
    with tracer.start_as_current_span("myspan", context=ctx) as span:
        # Missing the key set above
        print(context.get_current())

print("API tracer")
test(trace.get_tracer("foo"))
print()

print("SDK tracer")
test(SdkTracerProvider().get_tracer("foo"))

What is the expected behavior?

The printed context should have the explicitly set context key that was passed into tracer.start_as_current_span(). That is my expectation but curious if other people expect something else.

What is the actual behavior?

The printed context is missing the explicitly set context key

API tracer
{}

SDK tracer
{'current-span-06d983ad-613d-4f3e-bb80-0c8443a0722f': _Span(name="myspan", context=SpanContext(trace_id=0x319f42abd006fd7de5f7ee763abbaf23, span_id=0xc5ef9f69b807b7c3, trace_flags=0x01, trace_state=[], is_remote=False))}

Additional context
trace.use_span() (which both tracer impls use) set the span in the current implicit context, ignoring the span's parent context.

This exacerbates usability issues outline in https://github.com/open-telemetry/opentelemetry-python/issues/2432

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with trace.use_span in opentelemetry-api/src/opentelemetry/trace/init.py around the referenced line, then compare the API and SDK Tracer start_as_current_span implementations. Run the reproduction script to observe both behaviors. Done means an explicitly passed context remains available inside the span for both tracer implementations.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
observability-sre
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.