@@ -153,27 +153,41 @@ def _get_bottom_most_span(
153153
154154 # Check if one span is an ancestor of the other by walking parent pointers
155155 # Check if external_span is an ancestor of current_span
156+ current_is_descendant_of_external = False
156157 temp_id = current_span_id
157158 depth = 0
158159 while temp_id is not None and depth < 100 : # Prevent infinite loops
159160 if temp_id == external_span_id :
160- logger . info ( f"RESULT: current_span is a descendant of external_span (found at depth { depth } ) -> returning current_span" )
161- logger .info ("=" * 80 )
162- return current_span
161+ current_is_descendant_of_external = True
162+ logger .info (f"current_span is a descendant of external_span (found at depth { depth } )" )
163+ break
163164 temp_id = parent_map .get (temp_id )
164165 depth += 1
165166
166167 # Check if current_span is an ancestor of external_span
168+ external_is_descendant_of_current = False
167169 temp_id = external_span_id
168170 depth = 0
169171 while temp_id is not None and depth < 100 : # Prevent infinite loops
170172 if temp_id == current_span_id :
171- logger . info ( f"RESULT: external_span is a descendant of current_span (found at depth { depth } ) -> returning external_span" )
172- logger .info ("=" * 80 )
173- return external_span
173+ external_is_descendant_of_current = True
174+ logger .info (f"external_span is a descendant of current_span (found at depth { depth } )" )
175+ break
174176 temp_id = parent_map .get (temp_id )
175177 depth += 1
176178
179+ # If they're in the same hierarchy (one is ancestor of the other)
180+ if current_is_descendant_of_external :
181+ # current_span is deeper (descendant means further down the tree)
182+ logger .info ("RESULT: current_span is a descendant of external_span -> returning current_span (deeper)" )
183+ logger .info ("=" * 80 )
184+ return current_span
185+ elif external_is_descendant_of_current :
186+ # external_span is deeper (descendant means further down the tree)
187+ logger .info ("RESULT: external_span is a descendant of current_span -> returning external_span (deeper)" )
188+ logger .info ("=" * 80 )
189+ return external_span
190+
177191 # Neither is an ancestor of the other - they're in different branches
178192 # Use chain length as tiebreaker
179193 if len (current_chain ) > len (external_chain ):
0 commit comments