2222 format_stream_response )
2323from event_utils import track_event_if_configured
2424from azure .monitor .opentelemetry import configure_azure_monitor
25+ from opentelemetry import trace
26+ from opentelemetry .trace import Status , StatusCode
2527
2628bp = Blueprint ("routes" , __name__ , static_folder = "static" , template_folder = "static" )
2729
@@ -169,6 +171,10 @@ def init_openai_client():
169171 return azure_openai_client
170172 except Exception as e :
171173 logging .exception ("Exception in Azure OpenAI initialization" , e )
174+ span = trace .get_current_span ()
175+ if span is not None :
176+ span .record_exception (e )
177+ span .set_status (Status (StatusCode .ERROR , str (e )))
172178 azure_openai_client = None
173179 raise e
174180
@@ -188,6 +194,10 @@ def init_ai_search_client():
188194 return client
189195 except Exception as e :
190196 logging .exception ("Exception in Azure AI Client initialization" , e )
197+ span = trace .get_current_span ()
198+ if span is not None :
199+ span .record_exception (e )
200+ span .set_status (Status (StatusCode .ERROR , str (e )))
191201 raise e
192202
193203
@@ -213,6 +223,10 @@ def init_cosmosdb_client():
213223 )
214224 except Exception as e :
215225 logging .exception ("Exception in CosmosDB initialization" , e )
226+ span = trace .get_current_span ()
227+ if span is not None :
228+ span .record_exception (e )
229+ span .set_status (Status (StatusCode .ERROR , str (e )))
216230 cosmos_conversation_client = None
217231 raise e
218232 else :
@@ -375,9 +389,10 @@ async def send_chat_request(request_body, request_headers):
375389
376390 except Exception as e :
377391 logging .exception ("Exception in send_chat_request" )
378- track_event_if_configured ("ChatCompletionError" , {
379- "error" : str (e )
380- })
392+ span = trace .get_current_span ()
393+ if span is not None :
394+ span .record_exception (e )
395+ span .set_status (Status (StatusCode .ERROR , str (e )))
381396 raise e
382397
383398 return response , apim_request_id
@@ -439,10 +454,11 @@ async def conversation_internal(request_body, request_headers):
439454
440455 except Exception as ex :
441456 logging .exception (ex )
442- track_event_if_configured ("ConversationRequestFailed" , {
443- "error" : str (ex ),
444- "chat_type" : str (request_body .get ("chat_type" , "unknown" ))
445- })
457+ span = trace .get_current_span ()
458+ if span is not None :
459+ span .record_exception (ex )
460+ span .set_status (Status (StatusCode .ERROR , str (ex )))
461+
446462 if hasattr (ex , "status_code" ):
447463 return jsonify ({"error" : str (ex )}), ex .status_code
448464 else :
@@ -468,6 +484,10 @@ def get_frontend_settings():
468484 return jsonify (frontend_settings ), 200
469485 except Exception as e :
470486 logging .exception ("Exception in /frontend_settings" )
487+ span = trace .get_current_span ()
488+ if span is not None :
489+ span .record_exception (e )
490+ span .set_status (Status (StatusCode .ERROR , str (e )))
471491 return jsonify ({"error" : str (e )}), 500
472492
473493
@@ -540,6 +560,10 @@ async def add_conversation():
540560
541561 except Exception as e :
542562 logging .exception ("Exception in /history/generate" )
563+ span = trace .get_current_span ()
564+ if span is not None :
565+ span .record_exception (e )
566+ span .set_status (Status (StatusCode .ERROR , str (e )))
543567 return jsonify ({"error" : str (e )}), 500
544568
545569
@@ -598,6 +622,10 @@ async def update_conversation():
598622
599623 except Exception as e :
600624 logging .exception ("Exception in /history/update" )
625+ span = trace .get_current_span ()
626+ if span is not None :
627+ span .record_exception (e )
628+ span .set_status (Status (StatusCode .ERROR , str (e )))
601629 return jsonify ({"error" : str (e )}), 500
602630
603631
@@ -659,6 +687,10 @@ async def update_message():
659687
660688 except Exception as e :
661689 logging .exception ("Exception in /history/message_feedback" )
690+ span = trace .get_current_span ()
691+ if span is not None :
692+ span .record_exception (e )
693+ span .set_status (Status (StatusCode .ERROR , str (e )))
662694 return jsonify ({"error" : str (e )}), 500
663695
664696
@@ -711,6 +743,10 @@ async def delete_conversation():
711743 )
712744 except Exception as e :
713745 logging .exception ("Exception in /history/delete" )
746+ span = trace .get_current_span ()
747+ if span is not None :
748+ span .record_exception (e )
749+ span .set_status (Status (StatusCode .ERROR , str (e )))
714750 return jsonify ({"error" : str (e )}), 500
715751
716752
@@ -951,6 +987,10 @@ async def delete_all_conversations():
951987
952988 except Exception as e :
953989 logging .exception ("Exception in /history/delete_all" )
990+ span = trace .get_current_span ()
991+ if span is not None :
992+ span .record_exception (e )
993+ span .set_status (Status (StatusCode .ERROR , str (e )))
954994 return jsonify ({"error" : str (e )}), 500
955995
956996
@@ -987,6 +1027,10 @@ async def clear_messages():
9871027 )
9881028 except Exception as e :
9891029 logging .exception ("Exception in /history/clear_messages" )
1030+ span = trace .get_current_span ()
1031+ if span is not None :
1032+ span .record_exception (e )
1033+ span .set_status (Status (StatusCode .ERROR , str (e )))
9901034 return jsonify ({"error" : str (e )}), 500
9911035
9921036
@@ -1054,6 +1098,10 @@ async def generate_section_content():
10541098 return jsonify ({"section_content" : content }), 200
10551099 except Exception as e :
10561100 logging .exception ("Exception in /section/generate" )
1101+ span = trace .get_current_span ()
1102+ if span is not None :
1103+ span .record_exception (e )
1104+ span .set_status (Status (StatusCode .ERROR , str (e )))
10571105 return jsonify ({"error" : str (e )}), 500
10581106
10591107
@@ -1066,10 +1114,10 @@ async def get_document(filepath):
10661114 return jsonify (document ), 200
10671115 except Exception as e :
10681116 logging .exception ("Exception in /document/<filepath>" )
1069- track_event_if_configured ( "DocumentRetrieveFailed" , {
1070- "filepath" : filepath ,
1071- "error" : str (e )
1072- } )
1117+ span = trace . get_current_span ()
1118+ if span is not None :
1119+ span . record_exception (e )
1120+ span . set_status ( Status ( StatusCode . ERROR , str ( e )) )
10731121 return jsonify ({"error" : str (e )}), 500
10741122
10751123
@@ -1125,6 +1173,10 @@ async def get_section_content(request_body, request_headers):
11251173
11261174 except Exception as e :
11271175 logging .exception ("Exception in send_chat_request" )
1176+ span = trace .get_current_span ()
1177+ if span is not None :
1178+ span .record_exception (e )
1179+ span .set_status (Status (StatusCode .ERROR , str (e )))
11281180 raise e
11291181
11301182 return response .choices [0 ].message .content
@@ -1143,6 +1195,10 @@ def retrieve_document(filepath):
11431195 return document
11441196 except Exception as e :
11451197 logging .exception ("Exception in retrieve_document" )
1198+ span = trace .get_current_span ()
1199+ if span is not None :
1200+ span .record_exception (e )
1201+ span .set_status (Status (StatusCode .ERROR , str (e )))
11461202 raise e
11471203
11481204
0 commit comments