Skip to content

Commit 1ccaf30

Browse files
committed
ref: attach pending client reports to the most recent envelope
1 parent ca94e19 commit 1ccaf30

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

sentry_sdk/transport.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def on_dropped_event(self, reason):
250250
# type: (str) -> None
251251
return None
252252

253-
def _flush_stats(self, force=False):
253+
def _fetch_pending_client_report(self, force=False):
254254
# type: (bool) -> None
255255
if not self.options["send_client_reports"]:
256256
return
@@ -269,7 +269,7 @@ def _flush_stats(self, force=False):
269269
self._discarded_events = defaultdict(int)
270270
self._last_event_loss_sent = time.time()
271271

272-
client_report = Item(
272+
return Item(
273273
PayloadRef(
274274
json={
275275
"timestamp": time.time(),
@@ -284,7 +284,12 @@ def _flush_stats(self, force=False):
284284
),
285285
type="client_report",
286286
)
287-
self.capture_envelope(Envelope(items=[client_report]))
287+
288+
def _flush_client_reports(self, force=False):
289+
# type: (bool) -> None
290+
client_report = self._fetch_pending_client_report(force=force)
291+
if client_report is not None:
292+
self.capture_envelope(Envelope(items=[client_report]))
288293

289294
def _check_disabled(self, category):
290295
# type: (str) -> bool
@@ -345,6 +350,15 @@ def _send_envelope(
345350
if not envelope.items:
346351
return None
347352

353+
# since we're already in the business of sending out an envelope here
354+
# check if we have one pending for the stats session envelopes so we
355+
# can attach it to this enveloped scheduled for sending. This will
356+
# currently typically attach the client report to the most recent
357+
# session update.
358+
client_report_item = self._fetch_pending_client_report()
359+
if client_report_item is not None:
360+
envelope.items.append(client_report_item)
361+
348362
body = io.BytesIO()
349363
with gzip.GzipFile(fileobj=body, mode="w") as f:
350364
envelope.serialize_into(f)
@@ -429,7 +443,7 @@ def send_event_wrapper():
429443
self.on_dropped_event("full_queue")
430444
self.record_lost_event("queue_overflow", "error")
431445

432-
self._flush_stats()
446+
self._flush_client_reports()
433447

434448
def capture_envelope(
435449
self, envelope # type: Envelope
@@ -448,7 +462,7 @@ def send_envelope_wrapper():
448462
for item in envelope.items:
449463
self.record_lost_event("queue_overflow", item.data_category)
450464

451-
self._flush_stats()
465+
self._flush_client_reports()
452466

453467
def flush(
454468
self,
@@ -459,7 +473,7 @@ def flush(
459473
logger.debug("Flushing HTTP transport")
460474

461475
if timeout > 0:
462-
self._worker.submit(lambda: self._flush_stats(force=True))
476+
self._worker.submit(lambda: self._flush_client_reports(force=True))
463477
self._worker.flush(timeout, callback)
464478

465479
def kill(self):

0 commit comments

Comments
 (0)