open-telemetry / open-telemetry/opentelemetry-python
trace.get_current_span doesn't provide active span under test
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.6k
- Forks
- 1k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 19
Description
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
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry import trace
import google.auth.credentials
import pytest
@pytest.fixture
def creds():
"""
Provide test creds to unit tests so that they can run without them.
"""
yield google.auth.credentials.AnonymousCredentials()
@pytest.fixture(scope="session", autouse=True)
def set_trace_provider():
provider = TracerProvider()
trace.set_tracer_provider(provider)
@pytest.fixture(scope="function")
def span_exporter():
exporter = InMemorySpanExporter()
processor = SimpleSpanProcessor(exporter)
provider = trace.get_tracer_provider()
provider.add_span_processor(processor)
yield exporter
sample.py
from opentelemetry import trace
class Bar():
def foo(self):
tracer = trace.get_tracer("com.bar")
with tracer.start_as_current_span(name="footrace", end_on_exit=False) as _:
print("foo")
sample_test.py
import pytest
from sample import Bar
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry import trace
def test_fo(span_exporter):
b = Bar()
b.foo()
#spans = span_exporter.get_finished_spans()
#assert len(spans) == 1 -> This fails because the span hasn't ended because end_on_exit=False
span = trace.get_current_span()
assert span is not None -> Succeeds, but the contents of the span doesn't match the active span
# For example, the span.name is not footrace, but something else
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the reproduction from conftest.py, sample.py, and sample_test.py, then inspect trace.get_current_span() and the span context around the with block. Compare the returned span with the expected active span and determine the documented behavior for spans left open with end_on_exit=False. Done means the behavior is clarified or corrected and covered by a focused test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- observability, testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100