Open mukund-ananthu opened 3 days ago
I think you are mixing up the started/ended state of a span with its activation state.
A call to trace.get_current_span()
will always return the current active span (or an invalid span if no span is currently activated). Spans are usually activated with Tracer.start_as_current_span
(or Tracer.start_span
and then some time later with the trace.use_span
context manager) which keeps the span active in the scope the context manager is active.
In your example the tracer.start_as_current_span(name="footrace", end_on_exit=False)
context manager in Bar.foo
keeps the span running but ends its activation state right after print("foo")
. So when trace.get_current_span()
is then later called in test_fo
the isn't ended yet but it also isn't the active span anymore. Instead you'll get an invalid span since there is no other span currently active.
@xrmx My end goal here is to be able to verify the contents of the "footrace" span in test_foo(). How can I achieve this?
Describe your environment
OS: Ubuntu Python version: 3.11.8 SDK version: opentelemetry-sdk=1.25.0
What happened?
Described in steps to reproduce
Steps to Reproduce
conftest.py
sample.py
sample_test.py
Expected Result
Return the current active span
Actual Result
Returns some other span
Additional context
Understood that unfinished spans may not be exported, and hence may be unavailable, but open telemetry could provide a recommended way to test the values of active spans. This is required in scenarios where the spans are closed asynchronously in a different function / file altogether and we want to verify the contents of the active span in the unit tests of the function that creates it.
Would you like to implement a fix?
None