Skip to content

Commit 265a0a4

Browse files
Merge branch 'hotfix' into UT-10888-backend
2 parents e624781 + ca8af37 commit 265a0a4

File tree

14 files changed

+157
-104
lines changed

14 files changed

+157
-104
lines changed

.github/workflows/docker-build-and-push.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
uses: docker/setup-buildx-action@v1
3333

3434
- name: Log in to Azure Container Registry
35-
if: ${{ inputs.push == true && (github.ref_name == 'main' || github.ref_name == 'dev' || github.ref_name == 'demo' || github.ref_name == 'hotfix') }}
35+
if: ${{ github.event_name == 'push' && (github.ref_name == 'main' || github.ref_name == 'dev' || github.ref_name == 'demo' || github.ref_name == 'hotfix') }}
3636
uses: azure/docker-login@v2
3737
with:
3838
login-server: ${{ secrets.ACR_LOGIN_SERVER }}

src/backend/agents/base_agent.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Step,
2222
StepStatus,
2323
)
24-
from azure.monitor.events.extension import track_event
24+
from event_utils import track_event_if_configured
2525

2626

2727
class BaseAgent(RoutedAgent):
@@ -105,7 +105,7 @@ async def handle_action_request(
105105
)
106106
)
107107

108-
track_event(
108+
track_event_if_configured(
109109
"Base agent - Added into the cosmos",
110110
{
111111
"session_id": message.session_id,
@@ -119,7 +119,7 @@ async def handle_action_request(
119119

120120
except Exception as e:
121121
logging.exception(f"Error during LLM call: {e}")
122-
track_event(
122+
track_event_if_configured(
123123
"Base agent - Error during llm call, captured into the cosmos",
124124
{
125125
"session_id": message.session_id,
@@ -138,7 +138,7 @@ async def handle_action_request(
138138
step.agent_reply = result
139139
await self._model_context.update_step(step)
140140

141-
track_event(
141+
track_event_if_configured(
142142
"Base agent - Updated step and updated into the cosmos",
143143
{
144144
"status": StepStatus.completed,

src/backend/agents/group_chat_manager.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
StepStatus,
2323
)
2424

25-
from azure.monitor.events.extension import track_event
25+
from event_utils import track_event_if_configured
2626

2727

2828
@default_subscription
@@ -62,7 +62,7 @@ async def handle_input_task(
6262
)
6363
)
6464

65-
track_event(
65+
track_event_if_configured(
6666
"Group Chat Manager - Received and added input task into the cosmos",
6767
{
6868
"session_id": message.session_id,
@@ -164,7 +164,7 @@ class Step(BaseDataModel):
164164
step.status = StepStatus.rejected
165165
step.human_approval_status = HumanFeedbackStatus.rejected
166166
self._memory.update_step(step)
167-
track_event(
167+
track_event_if_configured(
168168
"Group Chat Manager - Steps has been rejected and updated into the cosmos",
169169
{
170170
"status": StepStatus.rejected,
@@ -188,7 +188,7 @@ class Step(BaseDataModel):
188188
step.status = StepStatus.rejected
189189
step.human_approval_status = HumanFeedbackStatus.rejected
190190
self._memory.update_step(step)
191-
track_event(
191+
track_event_if_configured(
192192
"Group Chat Manager - Step has been rejected and updated into the cosmos",
193193
{
194194
"status": StepStatus.rejected,
@@ -213,7 +213,7 @@ async def _update_step_status(
213213
step.human_feedback = received_human_feedback
214214
step.status = StepStatus.completed
215215
await self._memory.update_step(step)
216-
track_event(
216+
track_event_if_configured(
217217
"Group Chat Manager - Received human feedback, Updating step and updated into the cosmos",
218218
{
219219
"status": StepStatus.completed,
@@ -241,7 +241,7 @@ async def _execute_step(self, session_id: str, step: Step):
241241
# Update step status to 'action_requested'
242242
step.status = StepStatus.action_requested
243243
await self._memory.update_step(step)
244-
track_event(
244+
track_event_if_configured(
245245
"Group Chat Manager - Update step to action_requested and updated into the cosmos",
246246
{
247247
"status": StepStatus.action_requested,
@@ -304,7 +304,7 @@ async def _execute_step(self, session_id: str, step: Step):
304304
)
305305
)
306306

307-
track_event(
307+
track_event_if_configured(
308308
f"Group Chat Manager - Requesting {step.agent.value.title()} to perform the action and added into the cosmos",
309309
{
310310
"session_id": session_id,
@@ -338,7 +338,7 @@ async def _execute_step(self, session_id: str, step: Step):
338338
logging.info(
339339
"Marking the step as complete - Since we have received the human feedback"
340340
)
341-
track_event(
341+
track_event_if_configured(
342342
"Group Chat Manager - Steps completed - Received the human feedback and updated into the cosmos",
343343
{
344344
"session_id": session_id,

src/backend/agents/human.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
AgentMessage,
1313
Step,
1414
)
15-
from azure.monitor.events.extension import track_event
15+
from event_utils import track_event_if_configured
1616

1717

1818
@default_subscription
@@ -57,7 +57,7 @@ async def handle_step_feedback(
5757
)
5858
)
5959
logging.info(f"HumanAgent received feedback for step: {step}")
60-
track_event(
60+
track_event_if_configured(
6161
f"Human Agent - Received feedback for step: {step} and added into the cosmos",
6262
{
6363
"session_id": message.session_id,
@@ -81,7 +81,7 @@ async def handle_step_feedback(
8181
)
8282
logging.info(f"HumanAgent sent approval request for step: {step}")
8383

84-
track_event(
84+
track_event_if_configured(
8585
f"Human Agent - Approval request sent for step {step} and added into the cosmos",
8686
{
8787
"session_id": message.session_id,

src/backend/agents/planner.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
HumanFeedbackStatus,
2727
)
2828

29-
from azure.monitor.events.extension import track_event
29+
from event_utils import track_event_if_configured
3030

3131

3232
@default_subscription
@@ -74,7 +74,7 @@ async def handle_input_task(self, message: InputTask, ctx: MessageContext) -> Pl
7474
)
7575
logging.info(f"Plan generated: {plan.summary}")
7676

77-
track_event(
77+
track_event_if_configured(
7878
f"Planner - Generated a plan with {len(steps)} steps and added plan into the cosmos",
7979
{
8080
"session_id": message.session_id,
@@ -101,7 +101,7 @@ async def handle_input_task(self, message: InputTask, ctx: MessageContext) -> Pl
101101
f"Additional information requested: {plan.human_clarification_request}"
102102
)
103103

104-
track_event(
104+
track_event_if_configured(
105105
"Planner - Additional information requested and added into the cosmos",
106106
{
107107
"session_id": message.session_id,
@@ -138,7 +138,7 @@ async def handle_plan_clarification(
138138
)
139139
)
140140

141-
track_event(
141+
track_event_if_configured(
142142
"Planner - Store HumanAgent clarification and added into the cosmos",
143143
{
144144
"session_id": message.session_id,
@@ -160,7 +160,7 @@ async def handle_plan_clarification(
160160
)
161161
logging.info("Plan updated with HumanClarification.")
162162

163-
track_event(
163+
track_event_if_configured(
164164
"Planner - Updated with HumanClarification and added into the cosmos",
165165
{
166166
"session_id": message.session_id,
@@ -254,7 +254,7 @@ class StructuredOutputPlan(BaseModel):
254254
structured_plan = StructuredOutputPlan(**parsed_result)
255255

256256
if not structured_plan.steps:
257-
track_event(
257+
track_event_if_configured(
258258
"Planner agent - No steps found",
259259
{
260260
"session_id": self._session_id,
@@ -282,7 +282,7 @@ class StructuredOutputPlan(BaseModel):
282282
# Store the plan in memory
283283
await self._memory.add_plan(plan)
284284

285-
track_event(
285+
track_event_if_configured(
286286
"Planner - Initial plan and added into the cosmos",
287287
{
288288
"session_id": self._session_id,
@@ -308,7 +308,7 @@ class StructuredOutputPlan(BaseModel):
308308
human_approval_status=HumanFeedbackStatus.requested,
309309
)
310310
await self._memory.add_step(step)
311-
track_event(
311+
track_event_if_configured(
312312
"Planner - Added planned individual step into the cosmos",
313313
{
314314
"plan_id": plan.id,
@@ -326,7 +326,7 @@ class StructuredOutputPlan(BaseModel):
326326

327327
except Exception as e:
328328
logging.exception(f"Error in create_structured_plan: {e}")
329-
track_event(
329+
track_event_if_configured(
330330
f"Planner - Error in create_structured_plan: {e} into the cosmos",
331331
{
332332
"session_id": self._session_id,

src/backend/app.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,29 @@
1919
AgentMessage,
2020
PlanWithSteps,
2121
)
22+
2223
from src.backend.utils import (
2324
initialize_runtime_and_context,
2425
retrieve_all_agent_tools,
2526
rai_success,
2627
)
28+
29+
from utils import initialize_runtime_and_context, retrieve_all_agent_tools, rai_success
30+
from event_utils import track_event_if_configured
31+
2732
from fastapi.middleware.cors import CORSMiddleware
2833
from azure.monitor.opentelemetry import configure_azure_monitor
29-
from azure.monitor.events.extension import track_event
3034

31-
configure_azure_monitor(
32-
connection_string=os.getenv("APPLICATIONINSIGHTS_INSTRUMENTATION_KEY")
33-
)
35+
36+
# Check if the Application Insights Instrumentation Key is set in the environment variables
37+
instrumentation_key = os.getenv("APPLICATIONINSIGHTS_INSTRUMENTATION_KEY")
38+
if instrumentation_key:
39+
# Configure Application Insights if the Instrumentation Key is found
40+
configure_azure_monitor(connection_string=instrumentation_key)
41+
logging.info("Application Insights configured with the provided Instrumentation Key")
42+
else:
43+
# Log a warning if the Instrumentation Key is not found
44+
logging.warning("No Application Insights Instrumentation Key found. Skipping configuration")
3445

3546
# Configure logging
3647
logging.basicConfig(level=logging.INFO)
@@ -117,7 +128,7 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
117128
if not rai_success(input_task.description):
118129
print("RAI failed")
119130

120-
track_event(
131+
track_event_if_configured(
121132
"RAI failed",
122133
{
123134
"status": "Plan not created",
@@ -133,7 +144,7 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
133144
user_id = authenticated_user["user_principal_id"]
134145

135146
if not user_id:
136-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
147+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
137148

138149
raise HTTPException(status_code=400, detail="no user")
139150
if not input_task.session_id:
@@ -154,7 +165,7 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
154165
logging.info(f"Plan created: {plan.summary}")
155166

156167
# Log custom event for successful input task processing
157-
track_event(
168+
track_event_if_configured(
158169
"InputTaskProcessed",
159170
{
160171
"status": (
@@ -239,7 +250,7 @@ async def human_feedback_endpoint(human_feedback: HumanFeedback, request: Reques
239250
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
240251
user_id = authenticated_user["user_principal_id"]
241252
if not user_id:
242-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
253+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
243254
raise HTTPException(status_code=400, detail="no user")
244255
# Initialize runtime and context
245256
runtime, _ = await initialize_runtime_and_context(
@@ -250,7 +261,7 @@ async def human_feedback_endpoint(human_feedback: HumanFeedback, request: Reques
250261
human_agent_id = AgentId("human_agent", human_feedback.session_id)
251262
await runtime.send_message(human_feedback, human_agent_id)
252263

253-
track_event(
264+
track_event_if_configured(
254265
"Completed Feedback received",
255266
{
256267
"status": "Feedback received",
@@ -316,7 +327,7 @@ async def human_clarification_endpoint(
316327
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
317328
user_id = authenticated_user["user_principal_id"]
318329
if not user_id:
319-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
330+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
320331
raise HTTPException(status_code=400, detail="no user")
321332
# Initialize runtime and context
322333
runtime, _ = await initialize_runtime_and_context(
@@ -327,7 +338,7 @@ async def human_clarification_endpoint(
327338
planner_agent_id = AgentId("planner_agent", human_clarification.session_id)
328339
await runtime.send_message(human_clarification, planner_agent_id)
329340

330-
track_event(
341+
track_event_if_configured(
331342
"Completed Human clarification on the plan",
332343
{
333344
"status": "Clarification received",
@@ -398,7 +409,7 @@ async def approve_step_endpoint(
398409
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
399410
user_id = authenticated_user["user_principal_id"]
400411
if not user_id:
401-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
412+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
402413
raise HTTPException(status_code=400, detail="no user")
403414
# Initialize runtime and context
404415
runtime, _ = await initialize_runtime_and_context(user_id=user_id)
@@ -413,7 +424,7 @@ async def approve_step_endpoint(
413424
)
414425
# Return a status message
415426
if human_feedback.step_id:
416-
track_event(
427+
track_event_if_configured(
417428
"Completed Human clarification with step_id",
418429
{
419430
"status": f"Step {human_feedback.step_id} - Approval:{human_feedback.approved}."
@@ -424,7 +435,7 @@ async def approve_step_endpoint(
424435
"status": f"Step {human_feedback.step_id} - Approval:{human_feedback.approved}."
425436
}
426437
else:
427-
track_event(
438+
track_event_if_configured(
428439
"Completed Human clarification without step_id",
429440
{"status": "All steps approved"},
430441
)
@@ -496,15 +507,15 @@ async def get_plans(
496507
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
497508
user_id = authenticated_user["user_principal_id"]
498509
if not user_id:
499-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
510+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
500511
raise HTTPException(status_code=400, detail="no user")
501512

502513
cosmos = CosmosBufferedChatCompletionContext(session_id or "", user_id)
503514

504515
if session_id:
505516
plan = await cosmos.get_plan_by_session(session_id=session_id)
506517
if not plan:
507-
track_event(
518+
track_event_if_configured(
508519
"GetPlanBySessionNotFound",
509520
{"status_code": 400, "detail": "Plan not found"},
510521
)
@@ -584,7 +595,7 @@ async def get_steps_by_plan(plan_id: str, request: Request) -> List[Step]:
584595
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
585596
user_id = authenticated_user["user_principal_id"]
586597
if not user_id:
587-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
598+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
588599
raise HTTPException(status_code=400, detail="no user")
589600
cosmos = CosmosBufferedChatCompletionContext("", user_id)
590601
steps = await cosmos.get_steps_by_plan(plan_id=plan_id)
@@ -642,7 +653,7 @@ async def get_agent_messages(session_id: str, request: Request) -> List[AgentMes
642653
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
643654
user_id = authenticated_user["user_principal_id"]
644655
if not user_id:
645-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
656+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
646657
raise HTTPException(status_code=400, detail="no user")
647658
cosmos = CosmosBufferedChatCompletionContext(session_id, user_id)
648659
agent_messages = await cosmos.get_data_by_type("agent_message")

src/backend/event_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import logging
2+
import os
3+
from azure.monitor.events.extension import track_event
4+
5+
6+
def track_event_if_configured(event_name: str, event_data: dict):
7+
instrumentation_key = os.getenv("APPLICATIONINSIGHTS_INSTRUMENTATION_KEY")
8+
if instrumentation_key:
9+
track_event(event_name, event_data)
10+
else:
11+
logging.warning(f"Skipping track_event for {event_name} as Application Insights is not configured")

0 commit comments

Comments
 (0)