Skip to content

Commit 889aec4

Browse files
authored
Record number of dropped spans in POTel (#4092)
Closes #4067 Possible follow-up: #4103
1 parent 30e1071 commit 889aec4

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

sentry_sdk/integrations/opentelemetry/span_processor.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/tracing/test_misc.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_span_trimming(sentry_init, capture_events):
1515

1616
with start_span(name="hi"):
1717
for i in range(10):
18-
with start_span(op="foo{}".format(i)):
18+
with start_span(op=f"foo{i}"):
1919
pass
2020

2121
(event,) = events
@@ -29,7 +29,6 @@ def test_span_trimming(sentry_init, capture_events):
2929

3030
assert event["_meta"]["spans"][""]["len"] == 10
3131
assert "_dropped_spans" not in event
32-
assert "dropped_spans" not in event
3332

3433

3534
def test_span_data_scrubbing_and_trimming(sentry_init, capture_events):
@@ -42,7 +41,7 @@ def test_span_data_scrubbing_and_trimming(sentry_init, capture_events):
4241
span.set_data("datafoo", "databar")
4342

4443
for i in range(10):
45-
with start_span(op="foo{}".format(i)):
44+
with start_span(op=f"foo{i}"):
4645
pass
4746

4847
(event,) = events

0 commit comments

Comments
 (0)