2020 PlanWithSteps ,
2121)
2222from utils import initialize_runtime_and_context , retrieve_all_agent_tools , rai_success
23+ from event_utils import track_event_if_configured
2324from fastapi .middleware .cors import CORSMiddleware
2425from 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
3239logging .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" )
0 commit comments