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

log: Enrich logger with context from call

Clémence Lesné78546d9c92e1
diff --git a/app/helpers/monitoring.py b/app/helpers/monitoring.py
index 5687c02..204dbec 100644
--- a/app/helpers/monitoring.py
+++ b/app/helpers/monitoring.py
@@ -6,6 +6,7 @@ from opentelemetry.instrumentation.aiohttp_client import AioHttpClientInstrument
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
from opentelemetry.trace.span import INVALID_SPAN
from opentelemetry.util.types import AttributeValue
+from structlog.contextvars import bind_contextvars
VERSION = environ.get("VERSION", "0.0.0-unknown")
@@ -33,6 +34,10 @@ def span_attribute(key: str, value: AttributeValue) -> None:
Returns None.
"""
+ # Enrich logging
+ bind_contextvars(**{key: value})
+
+ # Enrich span
span = trace.get_current_span()
if span == INVALID_SPAN:
return
diff --git a/app/main.py b/app/main.py
index 652935c..b08185e 100644
--- a/app/main.py
+++ b/app/main.py
@@ -542,7 +542,7 @@ async def communicationservices_wss_post(
# Accept connection
await websocket.accept()
- logger.info("WebSocket connection established for call %s", call.call_id)
+ logger.info("WebSocket connection established")
# Client SDK
automation_client = await _use_automation_client()
@@ -665,7 +665,7 @@ async def _communicationservices_event_worker(
automation_client = await _use_automation_client()
# Log
- logger.debug("Call event received %s for call %s", event_type, call.call_id)
+ logger.debug("Call event received %s", event_type)
match event_type:
case "Microsoft.Communication.CallConnected": # Call answered
@@ -754,9 +754,13 @@ async def training_event(
Returns None.
"""
call = CallStateModel.model_validate_json(training.content)
- logger.debug("Training event received for call %s", call)
+
+ # Enrich span
span_attribute(CallAttributes.CALL_ID, str(call.call_id))
span_attribute(CallAttributes.CALL_PHONE_NUMBER, call.initiate.phone_number)
+
+ logger.debug("Training event received")
+
await call.trainings(cache_only=False) # Get trainings by advance to populate cache
@@ -773,9 +777,13 @@ async def post_event(
if not call:
logger.warning("Call %s not found", post.content)
return
- logger.debug("Post event received for call %s", call.call_id)
+
+ # Enrich span
span_attribute(CallAttributes.CALL_ID, str(call.call_id))
span_attribute(CallAttributes.CALL_PHONE_NUMBER, call.initiate.phone_number)
+
+ logger.debug("Post event received")
+
await on_end_call(call)