Skip to content

perf: Avoid scanning installed packages in package_version - #8191

Open
Jackevansevo wants to merge 3 commits into
getsentry:masterfrom
Jackevansevo:perf/package-version-lookup
Open

Jackevansevo wants to merge 3 commits into
getsentry:masterfrom
Jackevansevo:perf/package-version-lookup

Conversation

@Jackevansevo

@Jackevansevo Jackevansevo commented Nov 5, 2026 •

Copy link
Copy Markdown

Description

package_version parsed every installed package's metadata to read one version. It now looks up only that package, as suggested in #2316, which covers all 7 places that call it.

Same scan as #6865 (fixed there for MCP only), and the same lookup as the pydantic_ai integration (direct importlib.metadata call).

#2338 chose the cached list because importlib.metadata needs Python 3.8+. That path stays as the fallback, so this only avoids the first scan's cost. ModulesIntegration still builds the full list on the first error event.

With ~250 packages, package_version("flask") goes from 106 ms to 5 ms (-95%).

Draft: the test suite hasn't been run on this yet.

Issues

🤖 Generated with Claude Code

@Jackevansevo
Jackevansevo force-pushed the perf/package-version-lookup branch from 30fc342 to 3f7480b Compare November 5, 2026 00:06
@Jackevansevo Jackevansevo changed the title perf: Avoid scanning installed packages in package_version perf(flask): Avoid scanning installed packages for the version check Nov 5, 2026
@Jackevansevo
Jackevansevo force-pushed the perf/package-version-lookup branch 2 times, most recently from 4fa731a to 3f7480b Compare November 5, 2026 00:12
@sentrivana

Copy link
Copy Markdown
Collaborator

Hey @Jackevansevo, thanks for the PR. The change itself is good, but it's very targeted (just to Flask). Is there a way to generalize it so that any integration that needs a version check first checks via the less expensive route?

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jackevansevo
Jackevansevo force-pushed the perf/package-version-lookup branch from 3f7480b to ea2c4c6 Compare November 5, 2026 14:39
@Jackevansevo Jackevansevo changed the title perf(flask): Avoid scanning installed packages for the version check perf: Avoid scanning installed packages in package_version Nov 5, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jackevansevo

Copy link
Copy Markdown
Author

Hey @Jackevansevo, thanks for the PR. The change itself is good, but it's very targeted (just to Flask). Is there a way to generalize it so that any integration that needs a version check first checks via the less expensive route?

Thanks for the feedback.

I've moved the fast path check into package_version. Which should effect the following integrations:

  • Flask
  • Anthropic
  • Ariadne
  • Graphene
  • Ray
  • Strawberry
  • MCP

If importlib.metadata fails, there's nothing found or we're on older versions of Python it will just fall back to what is was doing previously (calling _get_installed_modules()).


It's worth flagging here that if we skip calling _get_installed_modules() on startup this result is no longer being cached. So that (potentially) expensive check potentially gets run on first error instead of startup.

But only integrations using package_version() would have cached this anyway.

@Jackevansevo
Jackevansevo marked this pull request as ready for review November 5, 2026 17:27
@Jackevansevo
Jackevansevo requested a review from a team as a code owner November 5, 2026 17:27

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit f47e6a4. Configure here.

Comment thread tests/test_utils.py Resolved
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants