Skip to content

Commit 8f5bc03

Browse files
Roopan-MicrosoftPrashant-MicrosoftKiran-Siluveru-MicrosoftMohan-MicrosoftHarmanpreet-Microsoft
authored
fix: Replace the track_event function with track_event_if_configured across various files in the backend (microsoft#58)
* fix: ui changes (microsoft#1) * fix: in progress status color after fetch task details (microsoft#5) * cancel notification message updated (microsoft#7) * Update task.js (microsoft#9) * Stages overflow issue fix (microsoft#10) * fix: added space to the agent (microsoft#13) * Approve reject buttons titles disabling buttons and (microsoft#15) * Fix: UX becomes damaged when chat outputs sample code for a task (microsoft#14) * task page UI updates * UI updated code for task * Task page UI updated code * status section UI update in task page * Added custom event (microsoft#24) * feat: added custom event * Logs updated * modify code * added exception logs * added exception logs for cosmos memory --------- Co-authored-by: Roopan P M <[email protected]> * fix: task with zero stages cannot show the page, spins forever and rai test prompt (microsoft#41) * Updated the workflow for build and push docker * updated the repo name * Update requirements.txt * Update requirements.txt * Update requirements.txt * feat: Integrated application insights instrumentation key into the bicep file (microsoft#42) * feat: Integrated application insights instrumentation key into bicep files * added application insights instrumentation key into env and readme file * updated json file * upgraded json file * Update docker-build-and-push.yml * Update docker-build-and-push.yml * Update docker-build-and-push.yml * Update docker-build-and-push.yml * Update docker-build-and-push.yml * Update docker-build-and-push.yml * pyLint issues fixed * lint issues fixed --------- Co-authored-by: Roopan-Microsoft <[email protected]> Co-authored-by: Roopan P M <[email protected]> * Update test.yml (microsoft#43) * Disabling Text Area functionality (microsoft#47) * fix: Usability and Alignments changes (microsoft#48) * Name update and padding removed * fix home page padding and cards height * remove gap in tasks section * Update docker-build-and-push.yml to debug * Update docker-build-and-push.yml * Update docker-build-and-push.yml to take the correct event name * Update docker-build-and-push.yml * fix: text area background color (microsoft#50) * fix: text area background color * Cursor hover style and Light dark color updated * feat: customize track events (microsoft#54) * feat: customized track event * pylint fix * pylint fix --------- Co-authored-by: Prashant-Microsoft <[email protected]> Co-authored-by: Kiran-Siluveru-Microsoft <[email protected]> Co-authored-by: Mohan-Microsoft <[email protected]> Co-authored-by: Harmanpreet-Microsoft <[email protected]>
1 parent ab9ff48 commit 8f5bc03

File tree

6 files changed

+60
-42
lines changed

6 files changed

+60
-42
lines changed

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: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,20 @@
2020
PlanWithSteps,
2121
)
2222
from utils import initialize_runtime_and_context, retrieve_all_agent_tools, rai_success
23+
from event_utils import track_event_if_configured
2324
from fastapi.middleware.cors import CORSMiddleware
2425
from azure.monitor.opentelemetry import configure_azure_monitor
25-
from azure.monitor.events.extension import track_event
2626

27-
configure_azure_monitor(
28-
connection_string=os.getenv("APPLICATIONINSIGHTS_INSTRUMENTATION_KEY")
29-
)
27+
28+
# Check if the Application Insights Instrumentation Key is set in the environment variables
29+
instrumentation_key = os.getenv("APPLICATIONINSIGHTS_INSTRUMENTATION_KEY")
30+
if instrumentation_key:
31+
# Configure Application Insights if the Instrumentation Key is found
32+
configure_azure_monitor(connection_string=instrumentation_key)
33+
logging.info("Application Insights configured with the provided Instrumentation Key")
34+
else:
35+
# Log a warning if the Instrumentation Key is not found
36+
logging.warning("No Application Insights Instrumentation Key found. Skipping configuration")
3037

3138
# Configure logging
3239
logging.basicConfig(level=logging.INFO)
@@ -113,7 +120,7 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
113120
if not rai_success(input_task.description):
114121
print("RAI failed")
115122

116-
track_event(
123+
track_event_if_configured(
117124
"RAI failed",
118125
{
119126
"status": "Plan not created",
@@ -129,7 +136,7 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
129136
user_id = authenticated_user["user_principal_id"]
130137

131138
if not user_id:
132-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
139+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
133140

134141
raise HTTPException(status_code=400, detail="no user")
135142
if not input_task.session_id:
@@ -150,7 +157,7 @@ async def input_task_endpoint(input_task: InputTask, request: Request):
150157
logging.info(f"Plan created: {plan.summary}")
151158

152159
# Log custom event for successful input task processing
153-
track_event(
160+
track_event_if_configured(
154161
"InputTaskProcessed",
155162
{
156163
"status": f"Plan created:\n {plan.summary}"
@@ -231,7 +238,7 @@ async def human_feedback_endpoint(human_feedback: HumanFeedback, request: Reques
231238
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
232239
user_id = authenticated_user["user_principal_id"]
233240
if not user_id:
234-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
241+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
235242
raise HTTPException(status_code=400, detail="no user")
236243
# Initialize runtime and context
237244
runtime, _ = await initialize_runtime_and_context(
@@ -242,7 +249,7 @@ async def human_feedback_endpoint(human_feedback: HumanFeedback, request: Reques
242249
human_agent_id = AgentId("human_agent", human_feedback.session_id)
243250
await runtime.send_message(human_feedback, human_agent_id)
244251

245-
track_event(
252+
track_event_if_configured(
246253
"Completed Feedback received",
247254
{
248255
"status": "Feedback received",
@@ -308,7 +315,7 @@ async def human_clarification_endpoint(
308315
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
309316
user_id = authenticated_user["user_principal_id"]
310317
if not user_id:
311-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
318+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
312319
raise HTTPException(status_code=400, detail="no user")
313320
# Initialize runtime and context
314321
runtime, _ = await initialize_runtime_and_context(
@@ -319,7 +326,7 @@ async def human_clarification_endpoint(
319326
planner_agent_id = AgentId("planner_agent", human_clarification.session_id)
320327
await runtime.send_message(human_clarification, planner_agent_id)
321328

322-
track_event(
329+
track_event_if_configured(
323330
"Completed Human clarification on the plan",
324331
{
325332
"status": "Clarification received",
@@ -390,7 +397,7 @@ async def approve_step_endpoint(
390397
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
391398
user_id = authenticated_user["user_principal_id"]
392399
if not user_id:
393-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
400+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
394401
raise HTTPException(status_code=400, detail="no user")
395402
# Initialize runtime and context
396403
runtime, _ = await initialize_runtime_and_context(user_id=user_id)
@@ -405,7 +412,7 @@ async def approve_step_endpoint(
405412
)
406413
# Return a status message
407414
if human_feedback.step_id:
408-
track_event(
415+
track_event_if_configured(
409416
"Completed Human clarification with step_id",
410417
{
411418
"status": f"Step {human_feedback.step_id} - Approval:{human_feedback.approved}."
@@ -416,7 +423,7 @@ async def approve_step_endpoint(
416423
"status": f"Step {human_feedback.step_id} - Approval:{human_feedback.approved}."
417424
}
418425
else:
419-
track_event(
426+
track_event_if_configured(
420427
"Completed Human clarification without step_id",
421428
{"status": "All steps approved"},
422429
)
@@ -488,15 +495,15 @@ async def get_plans(
488495
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
489496
user_id = authenticated_user["user_principal_id"]
490497
if not user_id:
491-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
498+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
492499
raise HTTPException(status_code=400, detail="no user")
493500

494501
cosmos = CosmosBufferedChatCompletionContext(session_id or "", user_id)
495502

496503
if session_id:
497504
plan = await cosmos.get_plan_by_session(session_id=session_id)
498505
if not plan:
499-
track_event(
506+
track_event_if_configured(
500507
"GetPlanBySessionNotFound",
501508
{"status_code": 400, "detail": "Plan not found"},
502509
)
@@ -576,7 +583,7 @@ async def get_steps_by_plan(plan_id: str, request: Request) -> List[Step]:
576583
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
577584
user_id = authenticated_user["user_principal_id"]
578585
if not user_id:
579-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
586+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
580587
raise HTTPException(status_code=400, detail="no user")
581588
cosmos = CosmosBufferedChatCompletionContext("", user_id)
582589
steps = await cosmos.get_steps_by_plan(plan_id=plan_id)
@@ -634,7 +641,7 @@ async def get_agent_messages(session_id: str, request: Request) -> List[AgentMes
634641
authenticated_user = get_authenticated_user_details(request_headers=request.headers)
635642
user_id = authenticated_user["user_principal_id"]
636643
if not user_id:
637-
track_event("UserIdNotFound", {"status_code": 400, "detail": "no user"})
644+
track_event_if_configured("UserIdNotFound", {"status_code": 400, "detail": "no user"})
638645
raise HTTPException(status_code=400, detail="no user")
639646
cosmos = CosmosBufferedChatCompletionContext(session_id, user_id)
640647
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)