@@ -473,15 +473,19 @@ def _process_results(
473473) -> ProcessedResults :
474474 """
475475 Used to process the results returned from clickhouse in two passes.
476- The first pass parses rows and builds attributes , and the second pass
477- sorts the attributes on each item .
476+ The first pass adds attributes to each row , and the second pass sorts
477+ the attributes and assembles the final items .
478478 """
479- items : list [GetTraceResponse .Item ] = []
480479 last_seen_timestamp_precise = 0.0
481480 last_seen_id = ""
481+ row_count = 0
482+
483+ # First pass: parse rows and build attribute dicts
484+ parsed_rows : list [tuple [str , Timestamp , dict [str , GetTraceResponse .Item .Attribute ]]] = []
482485
483486 with sentry_sdk .start_span (op = "function" , description = "add_attributes" ) as span :
484487 for row in data :
488+ row_count += 1
485489 id = row .pop ("id" )
486490 ts = row .pop ("timestamp" )
487491 arrays = row .pop ("attributes_array" , "{}" ) or "{}"
@@ -523,19 +527,26 @@ def add_attribute(key: str, value: Any) -> None:
523527 for int_key , int_value in integers .items ():
524528 add_attribute (int_key , int_value )
525529
526- items .append (
527- GetTraceResponse .Item (
528- id = id ,
529- timestamp = timestamp ,
530- attributes = attributes .values (),
531- )
532- )
530+ parsed_rows .append ((id , timestamp , attributes ))
531+
532+ span .set_data ("rows_processed" , row_count )
533+
534+ # Second pass: sort attributes and assemble items
535+ items : list [GetTraceResponse .Item ] = []
533536
534537 with sentry_sdk .start_span (op = "function" , description = "sort_attributes" ) as span :
535- for item in items :
536- item .attributes .sort (key = attrgetter ("key.name" ))
538+ for id , timestamp , attributes in parsed_rows :
539+ item = GetTraceResponse .Item (
540+ id = id ,
541+ timestamp = timestamp ,
542+ attributes = sorted (
543+ attributes .values (),
544+ key = attrgetter ("key.name" ),
545+ ),
546+ )
547+ items .append (item )
537548
538- span .set_data ("rows_processed " , len (items ))
549+ span .set_data ("rows_sorted " , len (parsed_rows ))
539550
540551 return ProcessedResults (
541552 items = items ,
0 commit comments