diff --git a/scripts/populate_tox/tox.jinja b/scripts/populate_tox/tox.jinja index 12e5d44abf..d169bfc4d7 100644 --- a/scripts/populate_tox/tox.jinja +++ b/scripts/populate_tox/tox.jinja @@ -125,6 +125,8 @@ deps = integration_deactivation: openai integration_deactivation: anthropic integration_deactivation: langchain + {py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation: openai-agents + {py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation: pydantic-ai-slim # === Integrations === diff --git a/sentry_sdk/integrations/__init__.py b/sentry_sdk/integrations/__init__.py index 277feb2518..84d1ca49b2 100644 --- a/sentry_sdk/integrations/__init__.py +++ b/sentry_sdk/integrations/__init__.py @@ -8,7 +8,7 @@ from typing import TYPE_CHECKING import sentry_sdk -from sentry_sdk.utils import capture_internal_exceptions, logger +from sentry_sdk.utils import capture_internal_exceptions, logger, package_version if TYPE_CHECKING: from collections.abc import Container, Sequence @@ -144,7 +144,12 @@ def iter_default_integrations( # integrations are only set up once the application imports the trigger module. # This can't live on the integration class, since loading the class imports the library. _DEFERRED_INTEGRATION_TRIGGERS = { + "sentry_sdk.integrations.anthropic.AnthropicIntegration": "anthropic", + "sentry_sdk.integrations.google_genai.GoogleGenAIIntegration": "google.genai", + "sentry_sdk.integrations.huggingface_hub.HuggingfaceHubIntegration": "huggingface_hub.inference._client", "sentry_sdk.integrations.openai.OpenAIIntegration": "openai", + "sentry_sdk.integrations.openai_agents.OpenAIAgentsIntegration": "agents", + "sentry_sdk.integrations.pydantic_ai.PydanticAIIntegration": "pydantic_ai", } iter_default_integrations = _generate_default_integrations_iterator( @@ -266,7 +271,10 @@ def setup_integrations( spec = importlib.util.find_spec(trigger) except Exception: spec = None - if spec is None: + # `agents` is a generic module name, so make sure it's openai-agents' + if spec is None or ( + trigger == "agents" and package_version("openai-agents") is None + ): excluded.add(import_string) elif hasattr(spec.loader, "exec_module"): excluded.add(import_string) @@ -297,11 +305,12 @@ def setup_integrations( } deactivated_identifiers = set() + # A deferred integration can't be set up yet, so it deactivates its targets + # whenever its library is installed, even if it later turns out too old to enable. for integration, targets_to_deactivate in _INTEGRATION_DEACTIVATES.items(): if ( - integration in integrations - and integration not in disabled_integration_identifiers - ): + integration in integrations or integration in deferred + ) and integration not in disabled_integration_identifiers: for target in targets_to_deactivate: if target not in user_provided_integrations: deactivated_identifiers.add(target) @@ -407,6 +416,16 @@ def exec_module(self, module: "ModuleType") -> None: module.__loader__ = self._loader if module.__spec__ is not None: module.__spec__.loader = self._loader + + # Setup may access the trigger as an attribute of its parent package + # before the import system binds it there + with capture_internal_exceptions(): + parent, _, child = self._trigger.rpartition(".") + if parent: + setattr( + sys.modules[parent], child, sys.modules.get(self._trigger, module) + ) + _set_up_deferred_integration(self._trigger) def __getattr__(self, name: str) -> "Any": diff --git a/tests/integrations/anthropic/test_anthropic.py b/tests/integrations/anthropic/test_anthropic.py index 7fa10b4e9b..f2e67cdbb7 100644 --- a/tests/integrations/anthropic/test_anthropic.py +++ b/tests/integrations/anthropic/test_anthropic.py @@ -78,6 +78,7 @@ async def __call__(self, *args, **kwargs): from sentry_sdk.integrations.stdlib import StdlibIntegration from sentry_sdk.traces import SpanStatus from sentry_sdk.utils import package_version +from tests.integrations.utils import run_deferred_setup_app ANTHROPIC_VERSION = package_version("anthropic") @@ -10015,3 +10016,20 @@ def test_stream_messages_reasoning_tokens( assert span_data[SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 50 assert span_data[SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 150 assert span_data[SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS_REASONING] == 15 + + +def test_deferred_setup_on_import(tmp_path): + result = run_deferred_setup_app( + tmp_path, + identifier="anthropic", + trigger="anthropic", + app_imports="from anthropic.resources import Messages", + patched="Messages.create", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": True, + "patched": True, + "reported_in_event": True, + } diff --git a/tests/integrations/google_genai/test_google_genai.py b/tests/integrations/google_genai/test_google_genai.py index 444333e45f..b3c4bba889 100644 --- a/tests/integrations/google_genai/test_google_genai.py +++ b/tests/integrations/google_genai/test_google_genai.py @@ -12,6 +12,7 @@ from sentry_sdk.consts import OP, SPANDATA from sentry_sdk.integrations.google_genai import GoogleGenAIIntegration from sentry_sdk.integrations.google_genai.utils import extract_contents_messages +from tests.integrations.utils import run_deferred_setup_app @pytest.fixture @@ -4762,3 +4763,20 @@ async def test_async_embed_content_data_collection( assert span_data[SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-004" + + +def test_deferred_setup_on_import(tmp_path): + result = run_deferred_setup_app( + tmp_path, + identifier="google_genai", + trigger="google.genai", + app_imports="from google.genai.models import Models", + patched="Models.generate_content", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": True, + "patched": True, + "reported_in_event": True, + } diff --git a/tests/integrations/huggingface_hub/test_huggingface_hub.py b/tests/integrations/huggingface_hub/test_huggingface_hub.py index 5242e40fdb..46b9310ac2 100644 --- a/tests/integrations/huggingface_hub/test_huggingface_hub.py +++ b/tests/integrations/huggingface_hub/test_huggingface_hub.py @@ -10,6 +10,7 @@ from sentry_sdk.integrations.huggingface_hub import HuggingfaceHubIntegration from sentry_sdk.utils import package_version, safe_serialize from tests.conftest import ApproxDict +from tests.integrations.utils import run_deferred_setup_app try: from huggingface_hub.utils._errors import HfHubHTTPError @@ -2712,3 +2713,20 @@ def test_chat_completion_streaming_data_collection_tools( assert span_data[SPANDATA.GEN_AI_USAGE_INPUT_TOKENS] == 183 assert span_data[SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 14 assert span_data[SPANDATA.GEN_AI_USAGE_TOTAL_TOKENS] == 197 + + +def test_deferred_setup_on_import(tmp_path): + result = run_deferred_setup_app( + tmp_path, + identifier="huggingface_hub", + trigger="huggingface_hub.inference._client", + app_imports="from huggingface_hub import InferenceClient", + patched="InferenceClient.chat_completion", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": True, + "patched": True, + "reported_in_event": True, + } diff --git a/tests/integrations/langchain/test_langchain.py b/tests/integrations/langchain/test_langchain.py index 0863aa5e8e..150889973e 100644 --- a/tests/integrations/langchain/test_langchain.py +++ b/tests/integrations/langchain/test_langchain.py @@ -38,6 +38,7 @@ ) from sentry_sdk.integrations.stdlib import StdlibIntegration from sentry_sdk.utils import package_version +from tests.integrations.utils import run_deferred_setup_app try: # langchain v1+ @@ -7338,3 +7339,22 @@ async def mock_aembed_query(self, text): # Data collection never gates non-PII attributes assert span_data[SPANDATA.GEN_AI_OPERATION_NAME] == "embeddings" assert span_data[SPANDATA.GEN_AI_REQUEST_MODEL] == "text-embedding-ada-002" + + +def test_deferred_setup_skips_deactivated_integration(tmp_path): + pytest.importorskip("google.genai") + + result = run_deferred_setup_app( + tmp_path, + identifier="google_genai", + trigger="google.genai", + app_imports="from google.genai.models import Models", + patched="Models.generate_content", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": False, + "patched": False, + "reported_in_event": False, + } diff --git a/tests/integrations/openai/test_openai.py b/tests/integrations/openai/test_openai.py index dae084c95c..c1247b1ce0 100644 --- a/tests/integrations/openai/test_openai.py +++ b/tests/integrations/openai/test_openai.py @@ -1,8 +1,4 @@ import json -import os -import subprocess -import sys -from textwrap import dedent import pytest @@ -81,6 +77,7 @@ from sentry_sdk.integrations.openai import OpenAIIntegration from sentry_sdk.integrations.stdlib import StdlibIntegration from sentry_sdk.utils import safe_serialize +from tests.integrations.utils import run_deferred_setup_app try: from unittest.mock import AsyncMock @@ -8277,51 +8274,34 @@ async def test_streaming_responses_api_ttft_async( assert ttft > 0 -def test_deferred_setup_on_import(tmp_path): - app = tmp_path / "app.py" - app.write_text( - dedent( - """ - import json - import sys - - import sentry_sdk - - events = [] - - def before_send(event, hint): - events.append(event) - return None - - sentry_sdk.init( - before_send=before_send, - _experiments={"deferred_auto_enabling_integrations": True}, - ) - imported_during_init = "openai" in sys.modules - - from openai.resources.chat.completions import Completions - - sentry_sdk.capture_message("hi") - - print(json.dumps({ - "imported_during_init": imported_during_init, - "integration_enabled": sentry_sdk.get_client().get_integration("openai") is not None, - "patched_by": Completions.create.__code__.co_filename, - "sdk_integrations": events[0]["sdk"]["integrations"], - })) - """ - ) - ) - - output = subprocess.check_output([sys.executable, str(app)]) - result = json.loads(output) - - assert result["imported_during_init"] is False - assert result["integration_enabled"] is True - assert result["patched_by"].endswith( - os.path.join("sentry_sdk", "integrations", "openai.py") - ) - assert "openai" in result["sdk_integrations"] +@pytest.mark.parametrize( + "local_agents_package", + [ + pytest.param(False, id="without_local_agents_package"), + pytest.param(True, id="with_local_agents_package"), + ], +) +def test_deferred_setup_on_import(tmp_path, local_agents_package): + if local_agents_package: + if package_version("openai-agents") is not None: + pytest.skip("openai-agents is installed") + (tmp_path / "agents").mkdir() + (tmp_path / "agents" / "__init__.py").write_text("") + + result = run_deferred_setup_app( + tmp_path, + identifier="openai", + trigger="openai", + app_imports="from openai.resources.chat.completions import Completions", + patched="Completions.create", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": True, + "patched": True, + "reported_in_event": True, + } def test_deferred_setup_when_already_imported(sentry_init): diff --git a/tests/integrations/openai_agents/test_openai_agents.py b/tests/integrations/openai_agents/test_openai_agents.py index b36436592b..7059bdd21b 100644 --- a/tests/integrations/openai_agents/test_openai_agents.py +++ b/tests/integrations/openai_agents/test_openai_agents.py @@ -75,6 +75,7 @@ from sentry_sdk.integrations.openai_agents.utils import _set_input_data, safe_serialize from sentry_sdk.integrations.stdlib import StdlibIntegration from sentry_sdk.utils import package_version, parse_version +from tests.integrations.utils import run_deferred_setup_app OPENAI_VERSION = package_version("openai") @@ -7678,3 +7679,20 @@ async def test_runner_run_streamed_with_starting_agent_kwarg( (transaction,) = events assert transaction["transaction"] == "test_agent workflow" + + +def test_deferred_setup_on_import(tmp_path): + result = run_deferred_setup_app( + tmp_path, + identifier="openai_agents", + trigger="agents", + app_imports="from agents.run import DEFAULT_AGENT_RUNNER", + patched="DEFAULT_AGENT_RUNNER.run", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": True, + "patched": True, + "reported_in_event": True, + } diff --git a/tests/integrations/pydantic_ai/test_pydantic_ai.py b/tests/integrations/pydantic_ai/test_pydantic_ai.py index c67a399d40..46238196f6 100644 --- a/tests/integrations/pydantic_ai/test_pydantic_ai.py +++ b/tests/integrations/pydantic_ai/test_pydantic_ai.py @@ -27,6 +27,7 @@ from sentry_sdk.integrations.pydantic_ai.spans.ai_client import _set_input_messages from sentry_sdk.integrations.pydantic_ai.spans.utils import _set_usage_data from sentry_sdk.utils import package_version +from tests.integrations.utils import run_deferred_setup_app PYDANTIC_AI_VERSION = package_version("pydantic-ai") @@ -4869,3 +4870,20 @@ def add_numbers(a: int, b: int) -> int: assert tool_messages[0]["content"] == [ {"type": "text", "text": expected_tool_return} ] + + +def test_deferred_setup_on_import(tmp_path): + result = run_deferred_setup_app( + tmp_path, + identifier="pydantic_ai", + trigger="pydantic_ai", + app_imports="from pydantic_ai import Agent", + patched="Agent.run", + ) + + assert result == { + "imported_during_init": False, + "integration_enabled": True, + "patched": True, + "reported_in_event": True, + } diff --git a/tests/integrations/utils.py b/tests/integrations/utils.py index 2f0cbbaff7..d08f6b8fa4 100644 --- a/tests/integrations/utils.py +++ b/tests/integrations/utils.py @@ -1,3 +1,8 @@ +import json +import subprocess +import sys +from textwrap import dedent + import pytest from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE @@ -118,3 +123,60 @@ id="data_collection_queues_on_with_no_pii", ), ] + + +def run_deferred_setup_app( + tmp_path, identifier, trigger, app_imports, patched, before_init="" +): + """ + Runs an app in a fresh interpreter that calls ``init()`` with deferred setup of + auto-enabling integrations on, and only afterwards runs ``app_imports``. + ``before_init`` runs before ``init()`` and can append to ``integrations``. + """ + app = tmp_path / "app.py" + app.write_text( + dedent( + """ + import json + import os + import sys + + import sentry_sdk + + integrations = [] + {before_init} + + events = [] + + def before_send(event, hint): + events.append(event) + return None + + sentry_sdk.init( + before_send=before_send, + integrations=integrations, + _experiments={{"deferred_auto_enabling_integrations": True}}, + ) + imported_during_init = {trigger!r} in sys.modules + + {app_imports} + + sentry_sdk.capture_message("hi") + + print(json.dumps({{ + "imported_during_init": imported_during_init, + "integration_enabled": sentry_sdk.get_client().get_integration({identifier!r}) is not None, + "patched": os.path.join("sentry_sdk", "integrations") in {patched}.__code__.co_filename, + "reported_in_event": {identifier!r} in events[0]["sdk"]["integrations"], + }})) + """ + ).format( + identifier=identifier, + trigger=trigger, + app_imports=app_imports, + patched=patched, + before_init=before_init, + ) + ) + + return json.loads(subprocess.check_output([sys.executable, str(app)])) diff --git a/tests/test_ai_integration_deactivation.py b/tests/test_ai_integration_deactivation.py index 3c13ef7546..1c5064f9c9 100644 --- a/tests/test_ai_integration_deactivation.py +++ b/tests/test_ai_integration_deactivation.py @@ -2,6 +2,7 @@ from sentry_sdk import get_client from sentry_sdk.integrations import _INTEGRATION_DEACTIVATES +from tests.integrations.utils import run_deferred_setup_app try: from sentry_sdk.integrations.langchain import LangchainIntegration @@ -41,13 +42,7 @@ pytestmark = pytest.mark.skipif( - not ( - has_langchain - and has_openai - and has_anthropic - and has_openai_agents - and has_pydantic_ai - ), + not (has_langchain and has_openai and has_anthropic), reason="Requires langchain, openai, and anthropic packages to be installed", ) @@ -130,6 +125,10 @@ def test_user_can_override_with_both_explicit_integrations( assert AnthropicIntegration in integration_types +@pytest.mark.skipif( + not (has_openai_agents and has_pydantic_ai), + reason="Requires openai-agents and pydantic-ai", +) def test_disabling_integrations_allows_openai_and_anthropic( sentry_init, reset_integrations ): @@ -249,3 +248,86 @@ def test_only_auto_enabling_integrations_without_defaults( if LangchainIntegration in integration_types: assert OpenAIIntegration not in integration_types assert AnthropicIntegration not in integration_types + + +@pytest.mark.parametrize( + "explicit_target", + [ + pytest.param(False, id="target_auto_enabled"), + pytest.param(True, id="target_in_integrations"), + ], +) +@pytest.mark.parametrize( + "deactivator_trigger, target, target_integration, target_imports, patched", + [ + pytest.param( + "agents", + "openai", + "OpenAIIntegration", + "from openai.resources.chat.completions import Completions", + "Completions.create", + id="deferred_auto_enabling_integrations-openai_agents-openai", + marks=pytest.mark.skipif( + not has_openai_agents, reason="Requires openai-agents" + ), + ), + pytest.param( + "pydantic_ai", + "openai", + "OpenAIIntegration", + "from openai.resources.chat.completions import Completions", + "Completions.create", + id="deferred_auto_enabling_integrations-pydantic_ai-openai", + marks=pytest.mark.skipif( + not has_pydantic_ai, reason="Requires pydantic-ai" + ), + ), + pytest.param( + "pydantic_ai", + "anthropic", + "AnthropicIntegration", + "from anthropic.resources import Messages", + "Messages.create", + id="deferred_auto_enabling_integrations-pydantic_ai-anthropic", + marks=pytest.mark.skipif( + not has_pydantic_ai, reason="Requires pydantic-ai" + ), + ), + ], +) +def test_deactivator_imported_after_target( + tmp_path, + deactivator_trigger, + target, + target_integration, + target_imports, + patched, + explicit_target, +): + hidden_deactivators = sorted( + {"agents", "langchain_core", "pydantic_ai"} - {deactivator_trigger} + ) + before_init = ( + f"for name in {hidden_deactivators!r}:\n sys.modules[name] = None\n" + ) + if explicit_target: + before_init += ( + f"from sentry_sdk.integrations.{target} import {target_integration}\n" + f"integrations.append({target_integration}())\n" + ) + + result = run_deferred_setup_app( + tmp_path, + identifier=target, + trigger=target, + app_imports=f"{target_imports}\nimport {deactivator_trigger}", + patched=patched, + before_init=before_init, + ) + + assert result == { + "imported_during_init": explicit_target, + "integration_enabled": explicit_target, + "patched": explicit_target, + "reported_in_event": explicit_target, + } diff --git a/tox.ini b/tox.ini index 42a737b34f..fb11e26410 100644 --- a/tox.ini +++ b/tox.ini @@ -458,6 +458,8 @@ deps = integration_deactivation: openai integration_deactivation: anthropic integration_deactivation: langchain + {py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation: openai-agents + {py3.10,py3.11,py3.12,py3.13,py3.14}-integration_deactivation: pydantic-ai-slim # === Integrations ===