1919 AgentMessage ,
2020 PlanWithSteps ,
2121)
22+
2223from 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+
2732from fastapi .middleware .cors import CORSMiddleware
2833from 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
3647logging .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" )
0 commit comments