opentelemetry-python icon indicating copy to clipboard operation
opentelemetry-python copied to clipboard

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

Open dave-shawley opened this issue 7 months ago • 0 comments

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 https://github.com/open-telemetry/opentelemetry-python/issues/2988, and https://github.com/open-telemetry/opentelemetry-python/issues/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 :wink:

Would you like to implement a fix?

I could as long as the API change is acceptable.

dave-shawley avatar Mar 17 '25 20:03 dave-shawley