Skip to content

opentelemetry.trace.TracerProvider ABC does not include the add_span_processor method #4489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dave-shawley opened this issue Mar 17, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@dave-shawley
Copy link

dave-shawley commented Mar 17, 2025

Describe your environment

OS: macOS
Python version: Python 3.12.2
SDK version: 1.31.0
API version: 1.31.0

What happened?

The opentelemetry.trace.TracerProvider interface is too narrow. The only method that is defined is the get_tracer abstract method. The examples and documentation recommend acquiring a provider using opentelementry.trace.get_tracer_provider() and then calling method on it to add span processors amongst other operations. For example, the cookbook example for Manually setting span context:

from opentelemetry import trace
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, BatchSpanProcessor

trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))

The SDK implementation class (opentelemetry.sdk.trace.TracerProvider) implements theadd_span_processor method but it is not available on opentelemetry.trace.TracerProvider. This causes errors in static type analysis tools since the type returned from opentelemetry.trace.get_tracer_provider() is opentelemetry.trace.TracerProvider. I think that the ABC should define abstract implementations of get_tracer and add_span_processor.

Would an PR be welcome here? I'm not sure if exposing already implemented methods on the public API is a change of the API surface or not.

Steps to Reproduce

mypy -c '
from opentelemetry import trace
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, BatchSpanProcessor
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
'

Expected Result

No type failures

Actual Result

<string>:4: error: "TracerProvider" has no attribute "add_span_processor"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Additional context

This is the underlying issue behind #2988, and #3713. I opened a new issue since the others seem to have lost momentum. Feel free to close this one as a duplicate of the others as long as the typing discrepancies are fixed 😉

Would you like to implement a fix?

I could as long as the API change is acceptable.

@dave-shawley dave-shawley added the bug Something isn't working label Mar 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant