@@ -63,6 +63,7 @@ def __init__(self):
6363 self ._children_spans = defaultdict (
6464 list
6565 ) # type: DefaultDict[int, List[ReadableSpan]]
66+ self ._dropped_spans = defaultdict (lambda : 0 ) # type: DefaultDict[int, int]
6667
6768 def on_start (self , span , parent_context = None ):
6869 # type: (Span, Optional[Context]) -> None
@@ -143,12 +144,17 @@ def _flush_root_span(self, span):
143144 if not transaction_event :
144145 return
145146
147+ collected_spans , dropped_spans = self ._collect_children (span )
146148 spans = []
147- for child in self . _collect_children ( span ) :
149+ for child in collected_spans :
148150 span_json = self ._span_to_json (child )
149151 if span_json :
150152 spans .append (span_json )
153+
151154 transaction_event ["spans" ] = spans
155+ if dropped_spans > 0 :
156+ transaction_event ["_dropped_spans" ] = dropped_spans
157+
152158 # TODO-neel-potel sort and cutoff max spans
153159
154160 sentry_sdk .capture_event (transaction_event )
@@ -166,25 +172,29 @@ def _append_child_span(self, span):
166172 children_spans = self ._children_spans [span .parent .span_id ]
167173 if len (children_spans ) < max_spans :
168174 children_spans .append (span )
175+ else :
176+ self ._dropped_spans [span .parent .span_id ] += 1
169177
170178 def _collect_children (self , span ):
171- # type: (ReadableSpan) -> List[ReadableSpan]
179+ # type: (ReadableSpan) -> tuple[ List[ReadableSpan], int ]
172180 if not span .context :
173- return []
181+ return [], 0
174182
175183 children = []
184+ dropped_spans = 0
176185 bfs_queue = deque () # type: Deque[int]
177186 bfs_queue .append (span .context .span_id )
178187
179188 while bfs_queue :
180189 parent_span_id = bfs_queue .popleft ()
181190 node_children = self ._children_spans .pop (parent_span_id , [])
191+ dropped_spans += self ._dropped_spans .pop (parent_span_id , 0 )
182192 children .extend (node_children )
183193 bfs_queue .extend (
184194 [child .context .span_id for child in node_children if child .context ]
185195 )
186196
187- return children
197+ return children , dropped_spans
188198
189199 # we construct the event from scratch here
190200 # and not use the current Transaction class for easier refactoring
0 commit comments