Skip to content

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.py
index 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 to
NodeExecutionStats 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.py
index 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 asynccontextmanager
from datetime import datetime, timedelta, timezone
from typing import TYPE_CHECKING, Any, Optional, TypeVar, cast
-import sentry_sdk
from pika.adapters.blocking_connection import BlockingChannel
from pika.spec import Basic, BasicProperties
from prometheus_client import Gauge, start_http_server
from 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_scope
from backend.blocks import get_block
from 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 tags
original_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 flow
raise
finally:
diff --git a/autogpt_platform/backend/backend/util/metrics.py b/autogpt_platform/backend/backend/util/metrics.py
index 2a072b3135..3348dd46d1 100644
--- a/autogpt_platform/backend/backend/util/metrics.py
+++ b/autogpt_platform/backend/backend/util/metrics.py
@@ -1,8 +1,10 @@
import logging
from enum import Enum
-import sentry_sdk
from 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_flush
from sentry_sdk.integrations import DidNotEnable
from sentry_sdk.integrations.asyncio import AsyncioIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
@@ -149,7 +151,7 @@ def sentry_init():
if AnthropicIntegration is not None
else []
)
- 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.py
index 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_sdk
import uvicorn
from fastapi import FastAPI, Request, responses
from prisma.errors import DataError, UniqueViolationError
from pydantic import BaseModel, TypeAdapter, create_model
+from sentry_sdk.api import capture_exception as _sentry_capture_exception
import backend.util.exceptions as exceptions
from 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 result
return result