55from auth .auth_utils import get_authenticated_user_details
66from services .history_service import HistoryService
77from common .logging .event_utils import track_event_if_configured
8- from azure .monitor .opentelemetry import configure_azure_monitor
98from opentelemetry import trace
109from opentelemetry .trace import Status , StatusCode
1110
1211router = APIRouter ()
1312
1413logger = logging .getLogger (__name__ )
1514
16- # Check if the Application Insights Instrumentation Key is set in the environment variables
17- instrumentation_key = os .getenv ("APPLICATIONINSIGHTS_CONNECTION_STRING" )
18- if instrumentation_key :
19- # Configure Application Insights if the Instrumentation Key is found
20- configure_azure_monitor (connection_string = instrumentation_key )
21- logging .info ("Application Insights configured with the provided Instrumentation Key" )
22- else :
23- # Log a warning if the Instrumentation Key is not found
24- logging .warning ("No Application Insights Instrumentation Key found. Skipping configuration" )
25-
2615# Single instance of HistoryService (if applicable)
2716history_service = HistoryService ()
2817
@@ -41,6 +30,11 @@ async def update_conversation(request: Request):
4130 if not conversation_id :
4231 raise HTTPException (status_code = 400 , detail = "No conversation_id found" )
4332
33+ # Attach conversation_id to current span for Application Insights correlation
34+ span = trace .get_current_span ()
35+ if span and conversation_id :
36+ span .set_attribute ("conversation_id" , conversation_id )
37+
4438 # Call HistoryService to update conversation
4539 update_response = await history_service .update_conversation (user_id , request_json )
4640
@@ -65,6 +59,10 @@ async def update_conversation(request: Request):
6559 )
6660 except Exception as e :
6761 logger .exception ("Exception in /history/update: %s" , str (e ))
62+ track_event_if_configured ("ConversationUpdateError" , {
63+ "error" : str (e ),
64+ "error_type" : type (e ).__name__
65+ })
6866 span = trace .get_current_span ()
6967 if span is not None :
7068 span .record_exception (e )
@@ -126,6 +124,10 @@ async def update_message_feedback(request: Request):
126124
127125 except Exception as e :
128126 logger .exception ("Exception in /history/message_feedback: %s" , str (e ))
127+ track_event_if_configured ("MessageFeedbackError" , {
128+ "error" : str (e ),
129+ "error_type" : type (e ).__name__
130+ })
129131 span = trace .get_current_span ()
130132 if span is not None :
131133 span .record_exception (e )
@@ -150,6 +152,11 @@ async def delete_conversation(request: Request):
150152 })
151153 raise HTTPException (status_code = 400 , detail = "conversation_id is required" )
152154
155+ # Attach conversation_id to current span for Application Insights correlation
156+ span = trace .get_current_span ()
157+ if span and conversation_id :
158+ span .set_attribute ("conversation_id" , conversation_id )
159+
153160 # Delete conversation using HistoryService
154161 deleted = await history_service .delete_conversation (user_id , conversation_id )
155162 if deleted :
@@ -173,6 +180,10 @@ async def delete_conversation(request: Request):
173180 detail = f"Conversation { conversation_id } not found or user does not have permission." )
174181 except Exception as e :
175182 logger .exception ("Exception in /history/delete: %s" , str (e ))
183+ track_event_if_configured ("ConversationDeleteError" , {
184+ "error" : str (e ),
185+ "error_type" : type (e ).__name__
186+ })
176187 span = trace .get_current_span ()
177188 if span is not None :
178189 span .record_exception (e )
@@ -191,7 +202,7 @@ async def list_conversations(
191202 request_headers = request .headers )
192203 user_id = authenticated_user ["user_principal_id" ]
193204
194- logger .info (f" user_id: { user_id } , offset: { offset } , limit: { limit } " )
205+ logger .info ("Fetching conversations - user_id: %s , offset: %s , limit: %s" , user_id , offset , limit )
195206
196207 # Get conversations
197208 conversations = await history_service .get_conversations (user_id , offset = offset , limit = limit )
@@ -216,6 +227,10 @@ async def list_conversations(
216227
217228 except Exception as e :
218229 logger .exception ("Exception in /history/list: %s" , str (e ))
230+ track_event_if_configured ("ConversationsListError" , {
231+ "error" : str (e ),
232+ "error_type" : type (e ).__name__
233+ })
219234 span = trace .get_current_span ()
220235 if span is not None :
221236 span .record_exception (e )
@@ -241,6 +256,11 @@ async def get_conversation_messages(request: Request):
241256 })
242257 raise HTTPException (status_code = 400 , detail = "conversation_id is required" )
243258
259+ # Attach conversation_id to current span for Application Insights correlation
260+ span = trace .get_current_span ()
261+ if span and conversation_id :
262+ span .set_attribute ("conversation_id" , conversation_id )
263+
244264 # Get conversation details
245265 conversationMessages = await history_service .get_conversation_messages (user_id , conversation_id )
246266 if not conversationMessages :
@@ -266,6 +286,10 @@ async def get_conversation_messages(request: Request):
266286
267287 except Exception as e :
268288 logger .exception ("Exception in /history/read: %s" , str (e ))
289+ track_event_if_configured ("ConversationReadError" , {
290+ "error" : str (e ),
291+ "error_type" : type (e ).__name__
292+ })
269293 span = trace .get_current_span ()
270294 if span is not None :
271295 span .record_exception (e )
@@ -291,6 +315,12 @@ async def rename_conversation(request: Request):
291315 "user_id" : user_id
292316 })
293317 raise HTTPException (status_code = 400 , detail = "conversation_id is required" )
318+
319+ # Attach conversation_id to current span for Application Insights correlation
320+ span = trace .get_current_span ()
321+ if span and conversation_id :
322+ span .set_attribute ("conversation_id" , conversation_id )
323+
294324 if not title :
295325 track_event_if_configured ("RenameConversationValidationError" , {
296326 "error" : "title is required" ,
@@ -310,6 +340,10 @@ async def rename_conversation(request: Request):
310340
311341 except Exception as e :
312342 logger .exception ("Exception in /history/rename: %s" , str (e ))
343+ track_event_if_configured ("ConversationRenameError" , {
344+ "error" : str (e ),
345+ "error_type" : type (e ).__name__
346+ })
313347 span = trace .get_current_span ()
314348 if span is not None :
315349 span .record_exception (e )
@@ -351,6 +385,10 @@ async def delete_all_conversations(request: Request):
351385
352386 except Exception as e :
353387 logging .exception ("Exception in /history/delete_all: %s" , str (e ))
388+ track_event_if_configured ("AllConversationsDeleteError" , {
389+ "error" : str (e ),
390+ "error_type" : type (e ).__name__
391+ })
354392 span = trace .get_current_span ()
355393 if span is not None :
356394 span .record_exception (e )
@@ -377,6 +415,11 @@ async def clear_messages(request: Request):
377415 })
378416 raise HTTPException (status_code = 400 , detail = "conversation_id is required" )
379417
418+ # Attach conversation_id to current span for Application Insights correlation
419+ span = trace .get_current_span ()
420+ if span and conversation_id :
421+ span .set_attribute ("conversation_id" , conversation_id )
422+
380423 # Delete conversation messages from CosmosDB
381424 success = await history_service .clear_messages (user_id , conversation_id )
382425
@@ -400,6 +443,10 @@ async def clear_messages(request: Request):
400443
401444 except Exception as e :
402445 logger .exception ("Exception in /history/clear: %s" , str (e ))
446+ track_event_if_configured ("MessagesClearError" , {
447+ "error" : str (e ),
448+ "error_type" : type (e ).__name__
449+ })
403450 span = trace .get_current_span ()
404451 if span is not None :
405452 span .record_exception (e )
@@ -429,6 +476,10 @@ async def ensure_cosmos():
429476 status_code = 200 )
430477 except Exception as e :
431478 logger .exception ("Exception in /history/ensure: %s" , str (e ))
479+ track_event_if_configured ("CosmosDBEnsureError" , {
480+ "error" : str (e ),
481+ "error_type" : type (e ).__name__
482+ })
432483 span = trace .get_current_span ()
433484 if span is not None :
434485 span .record_exception (e )
0 commit comments