@@ -1058,7 +1058,6 @@ def _log_events_to_app_insights(
10581058 events : List [Dict [str , Any ]],
10591059 log_attributes : Dict [str , Any ],
10601060 data_source_item : Optional [Dict [str , Any ]] = None ,
1061- resource = None , # Resource to attach to LogRecords
10621061) -> None :
10631062 """
10641063 Log independent events directly to App Insights using OpenTelemetry logging.
@@ -1072,12 +1071,8 @@ def _log_events_to_app_insights(
10721071 :type log_attributes: Dict[str, Any]
10731072 :param data_source_item: Data source item containing trace_id, response_id, conversation_id
10741073 :type data_source_item: Optional[Dict[str, Any]]
1075- :param resource: Resource to attach to LogRecords for anonymization
1076- :type resource: Optional[Resource]
10771074 """
10781075
1079- from opentelemetry .sdk ._logs import LogRecord
1080-
10811076 try :
10821077 # Get the trace_id and other context from data source item
10831078 trace_id = None
@@ -1161,18 +1156,28 @@ def _log_events_to_app_insights(
11611156 # Anonymize IP address to prevent Azure GeoIP enrichment and location tracking
11621157 log_attributes ["http.client_ip" ] = "0.0.0.0"
11631158
1164- # Create a LogRecord and emit it, passing the resource for anonymization
1165- log_record = LogRecord (
1159+ # Create context with trace_id if present (for distributed tracing correlation)
1160+ ctx = None
1161+ if trace_id :
1162+ from opentelemetry import trace
1163+ from opentelemetry .trace import SpanContext , TraceFlags , NonRecordingSpan
1164+
1165+ span_context = SpanContext (
1166+ trace_id = trace_id ,
1167+ span_id = 0 ,
1168+ is_remote = False ,
1169+ trace_flags = TraceFlags (0x01 ),
1170+ )
1171+ span = NonRecordingSpan (span_context )
1172+ ctx = trace .set_span_in_context (span )
1173+
1174+ otel_logger .emit (
11661175 timestamp = time .time_ns (),
11671176 observed_timestamp = time .time_ns (),
11681177 body = EVALUATION_EVENT_NAME ,
11691178 attributes = log_attributes ,
1170- resource = resource , # Pass the anonymized resource
1179+ context = ctx ,
11711180 )
1172- if trace_id :
1173- log_record .trace_id = trace_id
1174-
1175- otel_logger .emit (log_record )
11761181
11771182 except Exception as e :
11781183 LOGGER .warning (f"Failed to log event { i } : { e } " )
@@ -1251,7 +1256,6 @@ def emit_eval_result_events_to_app_insights(app_insights_config: AppInsightsConf
12511256 events = result ["results" ],
12521257 log_attributes = log_attributes ,
12531258 data_source_item = result ["datasource_item" ] if "datasource_item" in result else None ,
1254- resource = anonymized_resource , # Pass the anonymized resource
12551259 )
12561260 # Force flush to ensure events are sent
12571261 logger_provider .force_flush ()
0 commit comments