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): use monkeypatch.setattr in test and use typed sentry_sdk imports
Zamil Majdy0c94d884d043
diff --git a/autogpt_platform/backend/backend/blocks/ai_condition_test.py b/autogpt_platform/backend/backend/blocks/ai_condition_test.pyindex 38d39a406a..5520963682 100644--- a/autogpt_platform/backend/backend/blocks/ai_condition_test.py+++ b/autogpt_platform/backend/backend/blocks/ai_condition_test.py@@ -163,7 +163,9 @@ class TestExceptionPropagation:class TestCacheTokenPropagation:@pytest.mark.asyncio- async def test_cache_tokens_propagated_to_stats(self):+ async def test_cache_tokens_propagated_to_stats(+ self, monkeypatch: pytest.MonkeyPatch+ ):"""cache_read_tokens and cache_creation_tokens must be forwarded toNodeExecutionStats so that usage dashboards count cached tokens."""block = AIConditionBlock()@@ -176,7 +178,7 @@ class TestCacheTokenPropagation:provider_cost=0.0012,)- block.llm_call = spy_llm # type: ignore[assignment]+ monkeypatch.setattr(block, "llm_call", spy_llm)input_data = _make_input()await _collect_outputs(block, input_data, credentials=TEST_CREDENTIALS)diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.pyindex 3a51682b51..bd718d168f 100644--- a/autogpt_platform/backend/backend/executor/manager.py+++ b/autogpt_platform/backend/backend/executor/manager.py@@ -10,11 +10,13 @@ from contextlib import asynccontextmanagerfrom datetime import datetime, timedelta, timezonefrom typing import TYPE_CHECKING, Any, Optional, TypeVar, cast-import sentry_sdkfrom pika.adapters.blocking_connection import BlockingChannelfrom pika.spec import Basic, BasicPropertiesfrom prometheus_client import Gauge, start_http_serverfrom redis.asyncio.lock import Lock as AsyncRedisLock+from sentry_sdk.api import capture_exception as _sentry_capture_exception+from sentry_sdk.api import flush as _sentry_flush+from sentry_sdk.api import get_current_scope as _sentry_get_current_scopefrom backend.blocks import get_blockfrom backend.blocks._base import BlockSchema@@ -393,7 +395,7 @@ async def execute_node(output_size = 0# sentry tracking nonsense to get user counts for blocks because isolation scopes don't work :(- scope = sentry_sdk.get_current_scope()+ scope = _sentry_get_current_scope()# save the tagsoriginal_user = scope._user@@ -428,8 +430,8 @@ async def execute_node(ex, (NotFoundError, GraphNotFoundError))if not is_expected:- sentry_sdk.capture_exception(error=ex, scope=scope)- sentry_sdk.flush()+ _sentry_capture_exception(error=ex, scope=scope)+ _sentry_flush()# Re-raise to maintain normal error flowraisefinally:diff --git a/autogpt_platform/backend/backend/util/metrics.py b/autogpt_platform/backend/backend/util/metrics.pyindex 2a072b3135..3348dd46d1 100644--- a/autogpt_platform/backend/backend/util/metrics.py+++ b/autogpt_platform/backend/backend/util/metrics.py@@ -1,8 +1,10 @@import loggingfrom enum import Enum-import sentry_sdkfrom pydantic import SecretStr+from sentry_sdk._init_implementation import init as _sentry_init+from sentry_sdk.api import capture_exception as _sentry_capture_exception+from sentry_sdk.api import flush as _sentry_flushfrom sentry_sdk.integrations import DidNotEnablefrom sentry_sdk.integrations.asyncio import AsyncioIntegrationfrom sentry_sdk.integrations.logging import LoggingIntegration@@ -149,7 +151,7 @@ def sentry_init():if AnthropicIntegration is not Noneelse [])- sentry_sdk.init(+ _sentry_init(dsn=sentry_dsn,traces_sample_rate=1.0,profiles_sample_rate=1.0,@@ -165,8 +167,8 @@ def sentry_init():def sentry_capture_error(error: BaseException):- sentry_sdk.capture_exception(error)- sentry_sdk.flush()+ _sentry_capture_exception(error)+ _sentry_flush()async def discord_send_alert(diff --git a/autogpt_platform/backend/backend/util/service.py b/autogpt_platform/backend/backend/util/service.pyindex a1da0c1a68..459e46f01c 100644--- a/autogpt_platform/backend/backend/util/service.py+++ b/autogpt_platform/backend/backend/util/service.py@@ -26,11 +26,11 @@ from typing import ()import httpx-import sentry_sdkimport uvicornfrom fastapi import FastAPI, Request, responsesfrom prisma.errors import DataError, UniqueViolationErrorfrom pydantic import BaseModel, TypeAdapter, create_model+from sentry_sdk.api import capture_exception as _sentry_capture_exceptionimport backend.util.exceptions as exceptionsfrom backend.monitoring.instrumentation import instrument_fastapi@@ -721,7 +721,7 @@ def get_service_client(logger.warning(f"RPC return type validation failed for {type(e).__name__}: {e}")- sentry_sdk.capture_exception(e)+ _sentry_capture_exception(e)return resultreturn result