@@ -84,7 +84,8 @@ class SentryClient {
84
84
Hint ? hint,
85
85
}) async {
86
86
if (_sampleRate ()) {
87
- _recordLostEvent (event, DiscardReason .sampleRate);
87
+ _options.recorder
88
+ .recordLostEvent (DiscardReason .sampleRate, _getCategory (event));
88
89
_options.logger (
89
90
SentryLevel .debug,
90
91
'Event ${event .eventId .toString ()} was dropped due to sampling decision.' ,
@@ -403,7 +404,9 @@ class SentryClient {
403
404
SentryEvent event,
404
405
Hint hint,
405
406
) async {
406
- SentryEvent ? eventOrTransaction = event;
407
+ SentryEvent ? processedEvent = event;
408
+ final spanCountBeforeCallback =
409
+ event is SentryTransaction ? event.spans.length : 0 ;
407
410
408
411
final beforeSend = _options.beforeSend;
409
412
final beforeSendTransaction = _options.beforeSendTransaction;
@@ -412,18 +415,18 @@ class SentryClient {
412
415
try {
413
416
if (event is SentryTransaction && beforeSendTransaction != null ) {
414
417
beforeSendName = 'beforeSendTransaction' ;
415
- final e = beforeSendTransaction (event);
416
- if (e is Future <SentryTransaction ?>) {
417
- eventOrTransaction = await e ;
418
+ final callbackResult = beforeSendTransaction (event);
419
+ if (callbackResult is Future <SentryTransaction ?>) {
420
+ processedEvent = await callbackResult ;
418
421
} else {
419
- eventOrTransaction = e ;
422
+ processedEvent = callbackResult ;
420
423
}
421
424
} else if (beforeSend != null ) {
422
- final e = beforeSend (event, hint);
423
- if (e is Future <SentryEvent ?>) {
424
- eventOrTransaction = await e ;
425
+ final callbackResult = beforeSend (event, hint);
426
+ if (callbackResult is Future <SentryEvent ?>) {
427
+ processedEvent = await callbackResult ;
425
428
} else {
426
- eventOrTransaction = e ;
429
+ processedEvent = callbackResult ;
427
430
}
428
431
}
429
432
} catch (exception, stackTrace) {
@@ -438,15 +441,30 @@ class SentryClient {
438
441
}
439
442
}
440
443
441
- if (eventOrTransaction == null ) {
442
- _recordLostEvent (event, DiscardReason .beforeSend);
444
+ final discardReason = DiscardReason .beforeSend;
445
+ if (processedEvent == null ) {
446
+ _options.recorder.recordLostEvent (discardReason, _getCategory (event));
447
+ if (event is SentryTransaction ) {
448
+ // We dropped the whole transaction, the dropped count includes all child spans + 1 root span
449
+ _options.recorder.recordLostEvent (discardReason, DataCategory .span,
450
+ count: spanCountBeforeCallback + 1 );
451
+ }
443
452
_options.logger (
444
453
SentryLevel .debug,
445
454
'${event .runtimeType } was dropped by $beforeSendName callback' ,
446
455
);
456
+ } else if (event is SentryTransaction &&
457
+ processedEvent is SentryTransaction ) {
458
+ // If beforeSend removed only some spans we still report them as dropped
459
+ final spanCountAfterCallback = processedEvent.spans.length;
460
+ final droppedSpanCount = spanCountBeforeCallback - spanCountAfterCallback;
461
+ if (droppedSpanCount > 0 ) {
462
+ _options.recorder.recordLostEvent (discardReason, DataCategory .span,
463
+ count: droppedSpanCount);
464
+ }
447
465
}
448
466
449
- return eventOrTransaction ;
467
+ return processedEvent ;
450
468
}
451
469
452
470
Future <SentryEvent ?> _runEventProcessors (
@@ -455,6 +473,9 @@ class SentryClient {
455
473
required List <EventProcessor > eventProcessors,
456
474
}) async {
457
475
SentryEvent ? processedEvent = event;
476
+ int spanCountBeforeEventProcessors =
477
+ event is SentryTransaction ? event.spans.length : 0 ;
478
+
458
479
for (final processor in eventProcessors) {
459
480
try {
460
481
final e = processor.apply (processedEvent! , hint);
@@ -474,12 +495,29 @@ class SentryClient {
474
495
rethrow ;
475
496
}
476
497
}
498
+
499
+ final discardReason = DiscardReason .eventProcessor;
477
500
if (processedEvent == null ) {
478
- _recordLostEvent (event, DiscardReason .eventProcessor);
501
+ _options.recorder.recordLostEvent (discardReason, _getCategory (event));
502
+ if (event is SentryTransaction ) {
503
+ // We dropped the whole transaction, the dropped count includes all child spans + 1 root span
504
+ _options.recorder.recordLostEvent (discardReason, DataCategory .span,
505
+ count: spanCountBeforeEventProcessors + 1 );
506
+ }
479
507
_options.logger (SentryLevel .debug, 'Event was dropped by a processor' );
480
- break ;
508
+ } else if (event is SentryTransaction &&
509
+ processedEvent is SentryTransaction ) {
510
+ // If event processor removed only some spans we still report them as dropped
511
+ final spanCountAfterEventProcessors = processedEvent.spans.length;
512
+ final droppedSpanCount =
513
+ spanCountBeforeEventProcessors - spanCountAfterEventProcessors;
514
+ if (droppedSpanCount > 0 ) {
515
+ _options.recorder.recordLostEvent (discardReason, DataCategory .span,
516
+ count: droppedSpanCount);
517
+ }
481
518
}
482
519
}
520
+
483
521
return processedEvent;
484
522
}
485
523
@@ -490,14 +528,11 @@ class SentryClient {
490
528
return false ;
491
529
}
492
530
493
- void _recordLostEvent (SentryEvent event, DiscardReason reason) {
494
- DataCategory category;
531
+ DataCategory _getCategory (SentryEvent event) {
495
532
if (event is SentryTransaction ) {
496
- category = DataCategory .transaction;
497
- } else {
498
- category = DataCategory .error;
533
+ return DataCategory .transaction;
499
534
}
500
- _options.recorder. recordLostEvent (reason, category) ;
535
+ return DataCategory .error ;
501
536
}
502
537
503
538
Future <SentryId ?> _attachClientReportsAndSend (SentryEnvelope envelope) {
0 commit comments