@@ -64,7 +64,7 @@ def __init__(self):
6464 self ._children_spans = defaultdict (
6565 list
6666 ) # type: DefaultDict[int, List[ReadableSpan]]
67- self ._dropped_spans = defaultdict (lambda : 0 )
67+ self ._dropped_spans = defaultdict (lambda : 0 ) # type: DefaultDict[int, int]
6868
6969 def on_start (self , span , parent_context = None ):
7070 # type: (Span, Optional[Context]) -> None
@@ -145,16 +145,13 @@ def _flush_root_span(self, span):
145145 if not transaction_event :
146146 return
147147
148+ collected_spans , dropped_spans = self ._collect_children (span )
148149 spans = []
149- for child in self . _collect_children ( span ) :
150+ for child in collected_spans :
150151 span_json = self ._span_to_json (child )
151152 if span_json :
152153 spans .append (span_json )
153154
154- dropped_spans = 0
155- if span in self ._dropped_spans :
156- dropped_spans = self ._dropped_spans .pop (span )
157-
158155 if dropped_spans == 0 :
159156 transaction_event ["spans" ] = spans
160157 else :
@@ -182,23 +179,25 @@ def _append_child_span(self, span):
182179 self ._dropped_spans [span .parent .span_id ] += 1
183180
184181 def _collect_children (self , span ):
185- # type: (ReadableSpan) -> List[ReadableSpan]
182+ # type: (ReadableSpan) -> tuple[ List[ReadableSpan], int ]
186183 if not span .context :
187184 return []
188185
189186 children = []
187+ dropped_spans = 0
190188 bfs_queue = deque () # type: Deque[int]
191189 bfs_queue .append (span .context .span_id )
192190
193191 while bfs_queue :
194192 parent_span_id = bfs_queue .popleft ()
195193 node_children = self ._children_spans .pop (parent_span_id , [])
194+ dropped_spans += self ._dropped_spans .pop (parent_span_id , 0 )
196195 children .extend (node_children )
197196 bfs_queue .extend (
198197 [child .context .span_id for child in node_children if child .context ]
199198 )
200199
201- return children
200+ return children , dropped_spans
202201
203202 # we construct the event from scratch here
204203 # and not use the current Transaction class for easier refactoring
0 commit comments