Skip to content

Commit 4ed7e7b

Browse files
fix(tracing): Flush pending span buckets in segment finish order
SpanBatcher._pending_flush was a set, so when the flusher woke after several segments had finished, it sent their traces in hash order. Use an insertion-ordered dict instead. Fixes #8027
1 parent 860a060 commit 4ed7e7b

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

‎sentry_sdk/_span_batcher.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ def __init__(
5757

5858
self._last_full_flush: float = time.monotonic() # drives time-based flushes
5959
self._flush_event = threading.Event()
60-
self._pending_flush: set[str] = set() # buckets to be flushed
60+
# Buckets to be flushed, in the order they became ready. A dict, not a
61+
# set, so that traces are sent in the order their segments finished.
62+
self._pending_flush: dict[str, None] = {}
6163

6264
self._flusher: "Optional[threading.Thread]" = None
6365
self._flusher_pid: "Optional[int]" = None
@@ -83,7 +85,7 @@ def _reset_thread_state(self) -> None:
8385

8486
self._last_full_flush = time.monotonic()
8587
self._flush_event = threading.Event()
86-
self._pending_flush = set()
88+
self._pending_flush = {}
8789

8890
self._flusher = None
8991
self._flusher_pid = None
@@ -143,7 +145,7 @@ def add(self, span: "SpanJSON") -> None:
143145
or self._running_size[span["trace_id"]]
144146
>= self.MAX_BYTES_BEFORE_FLUSH
145147
):
146-
self._pending_flush.add(span["trace_id"])
148+
self._pending_flush[span["trace_id"]] = None
147149
notify = True
148150
else:
149151
notify = False

‎tests/tracing/test_span_batcher.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
import threading
34
import time
45
from unittest import mock
56

@@ -479,6 +480,42 @@ def test_trace_bucket_flushes_when_segment_ends(
479480
assert items[1].payload["name"] == "segment span"
480481

481482

483+
def test_pending_traces_flush_in_segment_finish_order(
484+
sentry_init, capture_envelopes, monkeypatch
485+
):
486+
"""Traces whose segments finish before the flusher wakes are sent in finish order."""
487+
monkeypatch.setattr(SpanBatcher, "FLUSH_WAIT_TIME", 100000)
488+
489+
# Hold the flusher thread back until every segment below has finished, so
490+
# all traces are pending when it runs.
491+
flusher_may_run = threading.Event()
492+
original_flush_loop = SpanBatcher._flush_loop
493+
494+
def delayed_flush_loop(self):
495+
flusher_may_run.wait()
496+
original_flush_loop(self)
497+
498+
monkeypatch.setattr(SpanBatcher, "_flush_loop", delayed_flush_loop)
499+
500+
sentry_init(traces_sample_rate=1.0, trace_lifecycle="stream")
501+
envelopes = capture_envelopes()
502+
503+
finished_trace_ids = []
504+
for i in range(10):
505+
sentry_sdk.traces.new_trace()
506+
with sentry_sdk.traces.start_span(name=f"segment {i}") as segment:
507+
with sentry_sdk.traces.start_span(name="child"):
508+
pass
509+
finished_trace_ids.append(segment.trace_id)
510+
511+
flusher_may_run.set()
512+
time.sleep(0.1)
513+
514+
assert [
515+
envelope.headers["trace"]["trace_id"] for envelope in envelopes
516+
] == finished_trace_ids
517+
518+
482519
@pytest.mark.skipif(
483520
sys.platform == "win32"
484521
or not hasattr(os, "fork")

0 commit comments

Comments
 (0)