@@ -250,7 +250,7 @@ def on_dropped_event(self, reason):
250
250
# type: (str) -> None
251
251
return None
252
252
253
- def _flush_stats (self , force = False ):
253
+ def _fetch_pending_client_report (self , force = False ):
254
254
# type: (bool) -> None
255
255
if not self .options ["send_client_reports" ]:
256
256
return
@@ -269,7 +269,7 @@ def _flush_stats(self, force=False):
269
269
self ._discarded_events = defaultdict (int )
270
270
self ._last_event_loss_sent = time .time ()
271
271
272
- client_report = Item (
272
+ return Item (
273
273
PayloadRef (
274
274
json = {
275
275
"timestamp" : time .time (),
@@ -284,7 +284,12 @@ def _flush_stats(self, force=False):
284
284
),
285
285
type = "client_report" ,
286
286
)
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 ]))
288
293
289
294
def _check_disabled (self , category ):
290
295
# type: (str) -> bool
@@ -345,6 +350,15 @@ def _send_envelope(
345
350
if not envelope .items :
346
351
return None
347
352
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
+
348
362
body = io .BytesIO ()
349
363
with gzip .GzipFile (fileobj = body , mode = "w" ) as f :
350
364
envelope .serialize_into (f )
@@ -429,7 +443,7 @@ def send_event_wrapper():
429
443
self .on_dropped_event ("full_queue" )
430
444
self .record_lost_event ("queue_overflow" , "error" )
431
445
432
- self ._flush_stats ()
446
+ self ._flush_client_reports ()
433
447
434
448
def capture_envelope (
435
449
self , envelope # type: Envelope
@@ -448,7 +462,7 @@ def send_envelope_wrapper():
448
462
for item in envelope .items :
449
463
self .record_lost_event ("queue_overflow" , item .data_category )
450
464
451
- self ._flush_stats ()
465
+ self ._flush_client_reports ()
452
466
453
467
def flush (
454
468
self ,
@@ -459,7 +473,7 @@ def flush(
459
473
logger .debug ("Flushing HTTP transport" )
460
474
461
475
if timeout > 0 :
462
- self ._worker .submit (lambda : self ._flush_stats (force = True ))
476
+ self ._worker .submit (lambda : self ._flush_client_reports (force = True ))
463
477
self ._worker .flush (timeout , callback )
464
478
465
479
def kill (self ):
0 commit comments