@@ -412,6 +412,7 @@ private SentryTransaction processTransaction(
412412 final @ NotNull Hint hint ,
413413 final @ NotNull List <EventProcessor > eventProcessors ) {
414414 for (final EventProcessor processor : eventProcessors ) {
415+ final int spanCountBeforeProcessor = transaction .getSpans ().size ();
415416 try {
416417 transaction = processor .process (transaction , hint );
417418 } catch (Throwable e ) {
@@ -423,6 +424,7 @@ private SentryTransaction processTransaction(
423424 "An exception occurred while processing transaction by processor: %s" ,
424425 processor .getClass ().getName ());
425426 }
427+ final int spanCountAfterProcessor = transaction == null ? 0 : transaction .getSpans ().size ();
426428
427429 if (transaction == null ) {
428430 options
@@ -434,7 +436,25 @@ private SentryTransaction processTransaction(
434436 options
435437 .getClientReportRecorder ()
436438 .recordLostEvent (DiscardReason .EVENT_PROCESSOR , DataCategory .Transaction );
439+ // If we drop a transaction, we are also dropping all its spans (+1 for the root span)
440+ options
441+ .getClientReportRecorder ()
442+ .recordLostEvent (
443+ DiscardReason .EVENT_PROCESSOR , DataCategory .Span , spanCountBeforeProcessor + 1 );
437444 break ;
445+ } else if (spanCountAfterProcessor < spanCountBeforeProcessor ) {
446+ // If the callback removed some spans, we report it
447+ final int droppedSpanCount = spanCountBeforeProcessor - spanCountAfterProcessor ;
448+ options
449+ .getLogger ()
450+ .log (
451+ SentryLevel .DEBUG ,
452+ "%d spans were dropped by a processor: %s" ,
453+ droppedSpanCount ,
454+ processor .getClass ().getName ());
455+ options
456+ .getClientReportRecorder ()
457+ .recordLostEvent (DiscardReason .EVENT_PROCESSOR , DataCategory .Span , droppedSpanCount );
438458 }
439459 }
440460 return transaction ;
@@ -666,7 +686,9 @@ public void captureSession(final @NotNull Session session, final @Nullable Hint
666686 return SentryId .EMPTY_ID ;
667687 }
668688
689+ final int spanCountBeforeCallback = transaction .getSpans ().size ();
669690 transaction = executeBeforeSendTransaction (transaction , hint );
691+ final int spanCountAfterCallback = transaction == null ? 0 : transaction .getSpans ().size ();
670692
671693 if (transaction == null ) {
672694 options
@@ -675,7 +697,24 @@ public void captureSession(final @NotNull Session session, final @Nullable Hint
675697 options
676698 .getClientReportRecorder ()
677699 .recordLostEvent (DiscardReason .BEFORE_SEND , DataCategory .Transaction );
700+ // If we drop a transaction, we are also dropping all its spans (+1 for the root span)
701+ options
702+ .getClientReportRecorder ()
703+ .recordLostEvent (
704+ DiscardReason .BEFORE_SEND , DataCategory .Span , spanCountBeforeCallback + 1 );
678705 return SentryId .EMPTY_ID ;
706+ } else if (spanCountAfterCallback < spanCountBeforeCallback ) {
707+ // If the callback removed some spans, we report it
708+ final int droppedSpanCount = spanCountBeforeCallback - spanCountAfterCallback ;
709+ options
710+ .getLogger ()
711+ .log (
712+ SentryLevel .DEBUG ,
713+ "%d spans were dropped by beforeSendTransaction." ,
714+ droppedSpanCount );
715+ options
716+ .getClientReportRecorder ()
717+ .recordLostEvent (DiscardReason .BEFORE_SEND , DataCategory .Span , droppedSpanCount );
679718 }
680719
681720 try {
0 commit comments