open-telemetry / open-telemetry/opentelemetry-python

MyPy Complains About "TracerProvider" has no attribute "add_span_processor"

Open
#3,713 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When running mypy 1.8 on some of our samples, an error is generated saying that "TracerProvider" has no attribute "add_span_processor"

From the looks of it, seems like get_tracer_provider is returning the opentelemetry.trace.TracerProvider
ABC, however opentelemetry.sdk.trace.TracerProvider has more methods defined than the ABC

Repro:

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

# Simple console exporter
exporter = ConsoleSpanExporter()

trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer(__name__)
trace.get_tracer_provider().add_span_processor(SimpleSpanProcessor(exporter))

pip list

Package                                Version
-------------------------------------- -------
Deprecated                             1.2.14
importlib-metadata                     6.11.0
mypy                                   1.8.0
mypy-extensions                        1.0.0
opentelemetry-api                      1.23.0
opentelemetry-instrumentation          0.44b0
opentelemetry-instrumentation-requests 0.44b0
opentelemetry-sdk                      1.23.0
opentelemetry-semantic-conventions     0.44b0
opentelemetry-util-http                0.44b0
pip                                    23.0.1
setuptools                             56.0.0
tomli                                  2.0.1
typing_extensions                      4.10.0
wrapt                                  1.16.0
zipp                                   3.17.0
mypy main.py
main.py:11: error: "TracerProvider" has no attribute "add_span_processor"  [attr-defined]

The code snippet above is from our samples, but it looks similar to the one in the cookbook.

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 by comparing the TracerProvider definitions and get_tracer_provider in opentelemetry-api/src/opentelemetry/trace/init.py with opentelemetry-sdk/src/opentelemetry/sdk/trace/init.py. Run the reported mypy main.py reproduction first. Done means the sample no longer reports that TracerProvider lacks add_span_processor.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.