open-telemetry / open-telemetry/opentelemetry-python
tracer_provider.shutdown() does not provide a configurable timeout
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.6k
- Forks
- 1k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 19
Description
Is your feature request related to a problem?
I have tests that run to validate the instantiation and configuration of my apps global tracer provider.
See the minimal example below:
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
provider = TracerProvider()
exporter = OTLPSpanExporter(**kwargs)
processor = BatchSpanProcessor(exporter)
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
I notice that when I run tests with uv run pytest that verify this code, they never exit and I start to get errors about the endpoint being unavailable. I added a shutdown method to run after the tests
@pytest.fixture(scope="session", autouse=True)
def cleanup_otel():
"""Ensure OpenTelemetry tracer provider is properly shut down after tests."""
yield
# Shutdown the tracer provider to stop background threads
tracer_provider = trace.get_tracer_provider()
if hasattr(tracer_provider, 'shutdown'):
tracer_provider.shutdown()
This takes 60 seconds to exit even though the tests pass in about 1 second.
Describe the solution you'd like
I'd like to be able to set a configurable timeout for the shutdown as follows:
tracer_provider.shutdown(timeout=1) # forces the tracer provider to shutdown after 1 second
This would make unit testing my Otel configurations much faster.
Describe alternatives you've considered
No response
Additional Context
This seems to be somewhat related to this other issue https://github.com/open-telemetry/opentelemetry-python/issues/3309.
It also seems that the Log and Metrics exporter support a configurable timeout: https://opentelemetry-python.readthedocs.io/en/latest/_modules/opentelemetry/exporter/otlp/proto/grpc/metric_exporter.html#OTLPMetricExporter.shutdown
Would you like to implement a fix?
I am willing to help with some direction
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 with TracerProvider.shutdown(), BatchSpanProcessor, and the OTLPSpanExporter behavior shown in the example. Compare the trace shutdown path with the configurable timeout support mentioned for the metrics exporter, then run the relevant test suite. Done means shutdown accepts and honors a configurable timeout without leaving background work running indefinitely.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- observability-sre, testing-qa
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100