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

Replace usage of pkg_resources (setuptools) with importlib(._)resources?

Open jenshnielsen opened this issue 2 years ago • 1 comments

Is your feature request related to a problem?

Importing opentelemetry is rather slow due to the import of pkg_resources in opentelemetry.context here

It is a known shortcoming of pkg_resources that at import time it will pass all installed packages which is potentially slow. In my use case it adds ~0.7 s to the import time.

For this reason setuptools actively discourages the use of pkg_resources See here

Describe the solution you'd like

Replace the use of pkg_resources with importlib.metadata / importlib.resources which is part of the python standard library in newer versions of python and use their backports in importlib_metadata / importlib_resources as needed for older python versions.

In this particular use case pkg_resources is used to find entry points and that should be replaceable by doing

from importlib_metadata import entry_points
...
(entry_point,) = entry_points(group="opentelemetry_context", name=configured_context)
_RUNTIME_CONTEXT = entry_point.load()

Describe alternatives you've considered

  • Keep the current state. Using code that is slow and discouraged by its owners.

Additional context

This will also mean that opentelemetry can potentially drop the need for a runtime dependency on setuptools and when older python versions (<3.9) are dropped only use the std library for entry point selection.

jenshnielsen avatar Sep 08 '22 13:09 jenshnielsen

Clarification:

This is not the only place pkg_resources is used but just the first. The others should be replaceable in the same way.

❯ rg pkg_resources
opentelemetry-semantic-conventions\tests\test_semconv.py
18:from pkg_resources import DistributionNotFound, require

opentelemetry-sdk\src\opentelemetry\sdk\_configuration\__init__.py
26:from pkg_resources import iter_entry_points

opentelemetry-sdk\src\opentelemetry\sdk\resources\__init__.py
66:import pkg_resources
138:_OPENTELEMETRY_SDK_VERSION = pkg_resources.get_distribution(

opentelemetry-sdk\src\opentelemetry\sdk\error_handler\__init__.py
65:from pkg_resources import iter_entry_points

opentelemetry-proto\tests\test_proto.py
18:from pkg_resources import DistributionNotFound, require

opentelemetry-api\tests\__init__.py
14:import pkg_resources
22:pkg_resources.declare_namespace(__name__)

opentelemetry-api\src\opentelemetry\util\_providers.py
19:from pkg_resources import iter_entry_points

opentelemetry-api\src\opentelemetry\trace\__init__.py
219:                ``pkg_resources.get_distribution(instrumenting_library_name).version``.

opentelemetry-api\src\opentelemetry\propagate\__init__.py
75:from pkg_resources import iter_entry_points

opentelemetry-api\src\opentelemetry\context\__init__.py
22:from pkg_resources import iter_entry_points

opentelemetry-api\src\opentelemetry\metrics\_internal\__init__.py
122:                ``pkg_resources.get_distribution(instrumenting_library_name).version``.

opentelemetry-api\tests\propagators\test_propagators.py
56:    @patch("pkg_resources.iter_entry_points")

docs-requirements.txt
8:# doesn't work for pkg_resources.

exporter\opentelemetry-exporter-opencensus\src\opentelemetry\exporter\opencensus\util.py
19:import pkg_resources
29:OPENTELEMETRY_VERSION = pkg_resources.get_distribution(

jenshnielsen avatar Sep 08 '22 13:09 jenshnielsen