Skip to content

package_version() parses every installed package's metadata, slowing sentry_sdk.init() #8190

Description

@Jackevansevo

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.73.0

Steps to Reproduce

Steps to Reproduce

In an environment with a few hundred packages, with Flask installed:

import time
import flask
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration

t = time.perf_counter()
sentry_sdk.init(
    dsn="https://abc@o1.ingest.sentry.io/1",
    integrations=[FlaskIntegration()],
    auto_enabling_integrations=False,
)
print(f"{(time.perf_counter() - t) * 1000:.0f} ms")

Background

Flask deprecated __version__ and #2316 suggested importlib.metadata.version. #2338 fixed the deprecation using _get_installed_modules() instead. #6769 / #6865 addressed the same scan for MCP when mcp isn't installed.

Suggested fix

Have package_version call importlib.metadata.version(package), keeping the existing path for Python < 3.8 and returning None on PackageNotFoundError. ModulesIntegration would still build the full list on the first error event. Happy to open a PR if this approach works for you.

Expected Result

Checking Flask's minimum version costs about as much as looking up one package.

Actual Result

FlaskIntegration.setup_once calls package_version("flask"), which parses the metadata of every installed package just to read one version. importlib.metadata.version("flask"), as suggested in #2316, reads only Flask's.

With ~250 packages: package_version("flask") takes 106 ms, and importlib.metadata.version("flask") takes 5 ms (-95%).

Activity

  1. linear-code commented on Nov 4, 2026

    @linear-code
  2. Shellghostt commented on Nov 8, 2026

    @Shellghostt

    Hi, I'd like to pick this up. Plan: make package_version() in sentry_sdk/utils.py look up the single distribution via importlib.metadata.version() (top-level guarded import, PackageNotFoundError -> None), keeping the existing _get_installed_modules() scan only as the fallback when importlib.metadata is unavailable (Python < 3.8). Tests: extend tests/test_utils.py with an installed-package case and a regression test asserting the full installed-modules scan is not invoked. Will validate with tox (py3.12-common), ruff, and mypy as per the repo guidelines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions