Skip to content

tracer_provider.shutdown() does not provide a configurable timeout #4623

@Salazar-99

Description

@Salazar-99

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 #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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions