Skip to content

feat(integrations): Defer OpenAI integration setup until openai is imported - #8341

Draft
ericapisani wants to merge 2 commits into
masterfrom
tel-133-defer-openai-integration-setup-until-the
Draft

ericapisani wants to merge 2 commits into
masterfrom
tel-133-defer-openai-integration-setup-until-the

Conversation

@ericapisani

@ericapisani ericapisani commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Behind the deferred_auto_enabling_integrations experiment, init() no longer imports openai just because the OpenAI integration is auto-enabling. A sys.meta_path finder wraps the openai loader and sets the integration up once the application imports openai itself. Integrations not in the trigger mapping keep the eager setup.

First PR in a stack of two; the remaining AI integrations follow in #8342.

The finder stays on sys.meta_path once installed (it returns None immediately when nothing is pending), because removing it while another thread is iterating meta_path can make that import skip a finder. Deferred setup_once() runs without _installer_lock, since setup that imports another trigger would otherwise deadlock.

Benchmark

Python 3.14, Django 6.1.3 + celery 5.7.0 + openai 3.57.0, app code never imports openai, median of 15 fresh processes:

init() time peak RSS modules after init()
flag off 348 ms 94.0 MB 1436
flag on 180 ms 69.9 MB 857

Verified on CPython 3.15.2 that lazy import openai fires the hook on first use.

Fixes TEL-133
Refs #6263

…ported

Behind the deferred_auto_enabling_integrations experiment, init() no
longer imports openai just because the OpenAI integration is
auto-enabling. A sys.meta_path finder wraps the openai loader and sets
the integration up once the application imports openai itself.
Integrations not in the trigger mapping keep the eager setup.

Refs TEL-133
@linear-code

linear-code Bot commented Nov 20, 2026

Copy link
Copy Markdown

TEL-133

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Results 📊

✅ 139568 passed | ⏭️ 7905 skipped | Total: 147473 | Pass Rate: 94.64% | Execution Time: 867m 22s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +73
Passed Tests 📈 +73
Failed Tests —
Skipped Tests —

All tests are passing successfully.

✅ Patch coverage is 85.87% (79 of 92 changed executable lines covered; target 80%).
Project statement coverage is 90.23% (unchanged from base (7ad850f) to head (d46daab)).

Changed files with executable lines (1)
File Patch coverage Changed executable lines
sentry_sdk/integrations/init.py 85.87% 79/92 covered; missed: 267, 268, 347, 352, 379, 388, 389, 390, 413, 432, 437, 444, 445; partial branches: 271, 343, 351, 355, 378, 408, 430, 431, 441
Coverage diff
@@            Coverage Diff             @@
##        master     #8341       +/-##
==========================================
  Coverage    90.23%    90.23%        —%
==========================================
  Files          203       203         —
  Tracked lines     27349     27438       +89
  Branches     10252     10286       +34
==========================================
+ Hits         24675     24756       +81
- Misses        2674      2682        +8
- Partials      1557      1567       +10

Generated by Coverage Action

Leave the deferred setup finder on sys.meta_path once installed. Removing
it while another thread iterates sys.meta_path can make that thread skip
the next finder, and the finder returns early when nothing is pending.

Treat any exception from find_spec at init as the library not being
installed, and drop a pending integration whose loader has no
exec_module instead of leaving it pending forever. Deferred setup now
reads options from the client it registers the integration on.

Refs TEL-133

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.

1 participant