Skip to content

Commit c71c4a4

Browse files
authored
Cache importlib_metadata.entry_points (#4735)
* Cache importlib_metadata.entry_points * changelog * Update opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py * lru_cache -> cache
1 parent 64de448 commit c71c4a4

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
([#4634](https://github.com/open-telemetry/opentelemetry-python/pull/4634))
2323
- semantic-conventions: Bump to 1.37.0
2424
([#4731](https://github.com/open-telemetry/opentelemetry-python/pull/4731))
25+
- Performance: Cache `importlib_metadata.entry_points`
26+
([#4735](https://github.com/open-telemetry/opentelemetry-python/pull/4735))
2527

2628
## Version 1.36.0/0.57b0 (2025-07-29)
2729

opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,36 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# FIXME: Use importlib.metadata when support for 3.11 is dropped if the rest of
15+
from functools import cache
16+
17+
# FIXME: Use importlib.metadata (not importlib_metadata)
18+
# when support for 3.11 is dropped if the rest of
1619
# the supported versions at that time have the same API.
1720
from importlib_metadata import ( # type: ignore
1821
Distribution,
1922
EntryPoint,
2023
EntryPoints,
2124
PackageNotFoundError,
2225
distributions,
23-
entry_points,
2426
requires,
2527
version,
2628
)
29+
from importlib_metadata import (
30+
entry_points as original_entry_points,
31+
)
32+
33+
34+
@cache
35+
def _original_entry_points_cached():
36+
return original_entry_points()
37+
38+
39+
def entry_points(**params):
40+
"""Replacement for importlib_metadata.entry_points that caches getting all the entry points.
41+
42+
That part can be very slow, and OTel uses this function many times."""
43+
return _original_entry_points_cached().select(**params)
44+
2745

2846
__all__ = [
2947
"entry_points",

0 commit comments

Comments
 (0)