AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): stop Sentry initialising its LaunchDarkly integration (#14895)
Bentlyc57024e94fdb
diff --git a/autogpt_platform/backend/backend/util/metrics_test.py b/autogpt_platform/backend/backend/util/metrics_test.pyindex eeb13071b5..0f3e2ea1cc 100644--- a/autogpt_platform/backend/backend/util/metrics_test.py+++ b/autogpt_platform/backend/backend/util/metrics_test.py@@ -13,14 +13,16 @@ import jsonimport sysimport sentry_sdk+from ldclient.client import LDClientfrom sentry_sdk.consts import DEFAULT_OPTIONSfrom sentry_sdk.serializer import serialize+from sentry_sdk.transport import Transportfrom sentry_sdk.utils import event_from_exception, json_dumps# Imported at module scope on purpose: AppProcess calls sentry_init() in its# class body, so the guard has to hold at collection time, not just in a test.import backend.util.process-from backend.util import metrics+from backend.util import feature_flag, metricsfrom backend.util.exceptions import InsufficientBalanceErrorfrom backend.util.metrics import (_FALKORDB_DRIVER_LOGGER,@@ -409,3 +411,59 @@ def test_sentry_init_skipped_in_subprocess_spawned_by_pytest(monkeypatch) -> Nonmetrics.sentry_init()assert calls == []+++def test_sentry_init_never_hooks_or_opens_a_launchdarkly_client(monkeypatch) -> None:+ """Sentry once asked feature_flag.get_client() for a LaunchDarklyIntegration,+ which initialised an LD client in every process that set up Sentry. LD bills+ per connection, so sentry_init() must neither hook LD nor create a client+ (SECRT-2708)."""+ calls = _spy_on_sentry_init(monkeypatch)+ monkeypatch.delitem(sys.modules, "pytest")+ monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)+ monkeypatch.setattr(feature_flag.settings.secrets, "launch_darkly_sdk_key", "sdk-x")+ ld_inits: list[str] = []+ monkeypatch.setattr(+ feature_flag, "initialize_launchdarkly", lambda: ld_inits.append("init")+ )+ monkeypatch.setattr(feature_flag, "_init_attempted", False)++ metrics.sentry_init()++ assert len(calls) == 1+ integration_names = {type(i).__name__ for i in calls[0]["integrations"]}+ assert "LaunchDarklyIntegration" not in integration_names+ assert ld_inits == []+++class _DiscardTransport(Transport):+ def capture_envelope(self, envelope) -> None:+ pass+++def test_sentry_sdk_setup_from_sentry_init_hooks_no_launchdarkly(monkeypatch) -> None:+ """The test above stops at the arguments sentry_init() passes. This builds a+ real SDK client from them, with sentry-sdk's default and auto-enabling+ integrations, and checks that SDK setup neither enables the LaunchDarkly+ integration nor constructs an LD client."""+ calls = _spy_on_sentry_init(monkeypatch)+ monkeypatch.delitem(sys.modules, "pytest")+ monkeypatch.delenv("PYTEST_CURRENT_TEST", raising=False)+ monkeypatch.setattr(feature_flag.settings.secrets, "launch_darkly_sdk_key", "sdk-x")+ monkeypatch.setattr(feature_flag, "_init_attempted", False)+ ld_clients: list[str] = []++ def _refuse_ld_client(self, *args, **kwargs) -> None:+ ld_clients.append("created")+ raise RuntimeError("Sentry setup constructed a LaunchDarkly client")++ monkeypatch.setattr(LDClient, "__init__", _refuse_ld_client)++ metrics.sentry_init()+ client = sentry_sdk.Client(**calls[0], transport=_DiscardTransport)+ try:+ assert "fastapi" in client.integrations+ assert "launchdarkly" not in client.integrations+ finally:+ client.close()+ assert ld_clients == []diff --git a/autogpt_platform/backend/poetry.lock b/autogpt_platform/backend/poetry.lockindex 3b2517fd6a..8e1c8b6d98 100644--- a/autogpt_platform/backend/poetry.lock+++ b/autogpt_platform/backend/poetry.lock@@ -7116,7 +7116,6 @@ files = [anthropic = {version = ">=0.16", optional = true, markers = "extra == \"anthropic\""}certifi = "*"fastapi = {version = ">=0.79.0", optional = true, markers = "extra == \"fastapi\""}-launchdarkly-server-sdk = {version = ">=9.8.0", optional = true, markers = "extra == \"launchdarkly\""}openai = {version = ">=1.0.0", optional = true, markers = "extra == \"openai\""}sqlalchemy = {version = ">=1.2", optional = true, markers = "extra == \"sqlalchemy\""}tiktoken = {version = ">=0.3.0", optional = true, markers = "extra == \"openai\""}@@ -8935,4 +8934,4 @@ cffi = ["cffi (>=1.17,<2.0) ; platform_python_implementation != \"PyPy\" and pyt[metadata]lock-version = "2.1"python-versions = ">=3.10,<3.14"-content-hash = "3fafab08269ed623ad4df93407e39d5e5aaefd6f6c97a7cfe29a41ea58d77809"+content-hash = "b0e521f9ac5ca2e76866dff633eabc71e5798e68ea94054470f4c6928b2b3a34"diff --git a/autogpt_platform/backend/pyproject.toml b/autogpt_platform/backend/pyproject.tomlindex 6bf6d1be61..7920b5b02e 100644--- a/autogpt_platform/backend/pyproject.toml+++ b/autogpt_platform/backend/pyproject.toml@@ -70,7 +70,7 @@ python-multipart = ">=0.0.22,<0.0.33"redis = "^8.1.0"regex = ">=2025.9.18,<2027.0.0"replicate = "^1.0.6"-sentry-sdk = {extras = ["anthropic", "fastapi", "launchdarkly", "openai", "sqlalchemy"], version = "^2.44.0"}+sentry-sdk = {extras = ["anthropic", "fastapi", "openai", "sqlalchemy"], version = "^2.44.0"}slack-sdk = "^3.41.0"sqlalchemy = "^2.0.40"strenum = "^0.4.9"