Skip to content

Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!

Python

ops: Mark suppressed exceptions as OK

Clémence Lesnéf00ee70f4bcb
diff --git a/app/helpers/call_utils.py b/app/helpers/call_utils.py
index c2b568d..c3c6e27 100644
--- a/app/helpers/call_utils.py
+++ b/app/helpers/call_utils.py
@@ -3,7 +3,7 @@ import json
import re
import time
from collections.abc import AsyncGenerator, Awaitable, Callable, Generator
-from contextlib import asynccontextmanager, contextmanager, suppress
+from contextlib import asynccontextmanager, contextmanager
from enum import Enum
from typing import Any
@@ -55,6 +55,7 @@ from app.helpers.monitoring import (
call_stt_complete_latency,
counter_add,
gauge_set,
+ suppress,
)
from app.models.call import CallStateModel
from app.models.message import (
@@ -113,9 +114,7 @@ class ContextEnum(str, Enum):
"""Transfer failed"""
-def tts_sentence_split(
- text: str, include_last: bool
-) -> Generator[tuple[str, int]]:
+def tts_sentence_split(text: str, include_last: bool) -> Generator[tuple[str, int]]:
"""
Split a text into sentences.
diff --git a/app/helpers/monitoring.py b/app/helpers/monitoring.py
index 510b4c3..d2d8e21 100644
--- a/app/helpers/monitoring.py
+++ b/app/helpers/monitoring.py
@@ -1,3 +1,4 @@
+from contextlib import contextmanager
from enum import Enum
from os import environ
@@ -6,6 +7,7 @@ from opentelemetry import metrics, trace
from opentelemetry.instrumentation.aiohttp_client import AioHttpClientInstrumentor
from opentelemetry.metrics._internal.instrument import Counter, Gauge
from opentelemetry.semconv.attributes import service_attributes
+from opentelemetry.trace import Status, StatusCode
from opentelemetry.trace.span import INVALID_SPAN
from opentelemetry.util.types import AttributeValue
from structlog.contextvars import bind_contextvars, get_contextvars
@@ -169,3 +171,20 @@ def counter_add(
**get_contextvars(),
},
)
+
+
+@contextmanager
+def suppress(*exceptions):
+ """
+ Context manager to suppress exceptions, while also logging them properly in OTEL.
+
+ OTEL span will always be set to OK status, even if an exception occurs. But exception will still be recorded.
+ """
+ try:
+ # Try executing the block
+ yield
+ # If an exception occurs, set the span status to OK and record the exception
+ except exceptions as e:
+ span = trace.get_current_span()
+ span.set_status(Status(StatusCode.OK))
+ span.record_exception(e)
diff --git a/app/main.py b/app/main.py
index 4a934bf..cfccdbb 100644
--- a/app/main.py
+++ b/app/main.py
@@ -2,7 +2,7 @@ import asyncio
import json
import time
from base64 import b64decode, b64encode
-from contextlib import asynccontextmanager, suppress
+from contextlib import asynccontextmanager
from datetime import timedelta
from http import HTTPStatus
from os import getenv
@@ -65,6 +65,7 @@ from app.helpers.monitoring import (
call_frames_in_latency,
call_frames_out_latency,
gauge_set,
+ suppress,
tracer,
)
from app.helpers.pydantic_types.phone_numbers import PhoneNumber
diff --git a/app/persistence/ai_search.py b/app/persistence/ai_search.py
index 24ae49a..0ed403c 100644
--- a/app/persistence/ai_search.py
+++ b/app/persistence/ai_search.py
@@ -1,5 +1,3 @@
-from contextlib import suppress
-
from azure.core.exceptions import (
HttpResponseError,
ResourceExistsError,
@@ -50,6 +48,7 @@ from app.helpers.config_models.ai_search import AiSearchModel
from app.helpers.http import azure_transport
from app.helpers.identity import credential
from app.helpers.logging import logger
+from app.helpers.monitoring import suppress
from app.models.readiness import ReadinessEnum
from app.models.training import TrainingModel
from app.persistence.icache import ICache
diff --git a/app/persistence/cosmos_db.py b/app/persistence/cosmos_db.py
index 9c637dd..2382f5e 100644
--- a/app/persistence/cosmos_db.py
+++ b/app/persistence/cosmos_db.py
@@ -1,6 +1,6 @@
import asyncio
from collections.abc import AsyncGenerator
-from contextlib import asynccontextmanager, suppress
+from contextlib import asynccontextmanager
from typing import Any
from uuid import UUID, uuid4
@@ -16,6 +16,7 @@ from app.helpers.features import callback_timeout_hour
from app.helpers.http import azure_transport
from app.helpers.identity import credential
from app.helpers.logging import logger
+from app.helpers.monitoring import suppress
from app.models.call import CallStateModel
from app.models.readiness import ReadinessEnum
from app.persistence.icache import ICache
diff --git a/app/persistence/memory.py b/app/persistence/memory.py
index 738860f..3b5e01b 100644
--- a/app/persistence/memory.py
+++ b/app/persistence/memory.py
@@ -1,9 +1,9 @@
import hashlib
from collections import OrderedDict
-from contextlib import suppress
from datetime import UTC, datetime, timedelta
from app.helpers.config_models.cache import MemoryModel
+from app.helpers.monitoring import suppress
from app.models.readiness import ReadinessEnum
from app.persistence.icache import ICache