Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/populate_tox/tox.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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 ===

Expand Down
29 changes: 24 additions & 5 deletions sentry_sdk/integrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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":
Expand Down
18 changes: 18 additions & 0 deletions tests/integrations/anthropic/test_anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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,
}
18 changes: 18 additions & 0 deletions tests/integrations/google_genai/test_google_genai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
18 changes: 18 additions & 0 deletions tests/integrations/huggingface_hub/test_huggingface_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
20 changes: 20 additions & 0 deletions tests/integrations/langchain/test_langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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+
Expand Down Expand Up @@ -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,
}
78 changes: 29 additions & 49 deletions tests/integrations/openai/test_openai.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import json
import os
import subprocess
import sys
from textwrap import dedent

import pytest

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
18 changes: 18 additions & 0 deletions tests/integrations/openai_agents/test_openai_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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,
}
18 changes: 18 additions & 0 deletions tests/integrations/pydantic_ai/test_pydantic_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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,
}
Loading
Loading