open-telemetry / open-telemetry/opentelemetry-python

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

Open
#4,489 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
2.6k
Forks
1k
Avg merge
4d 15h
Merged PRs (30d)
19

Description

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 😉

Would you like to implement a fix?

I could as long as the API change is acceptable.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with opentelemetry.trace.TracerProvider and get_tracer_provider(), then run the mypy command in the issue to reproduce the missing-attribute error. Check the corresponding API and SDK implementations and existing tests; done means the public type exposes add_span_processor consistently and the example passes static analysis without breaking provider implementations.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api, observability
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.