Skip to content

Commit

Permalink
Move application of event processors to shared function (#2594)
Browse files Browse the repository at this point in the history
* Move application event processor to shared function

* remove unused import

---------

Co-authored-by: Giancarlo Buenaflor <[email protected]>
  • Loading branch information
denrase and buenaflor authored Jan 22, 2025
1 parent e23381b commit c01acd1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 87 deletions.
76 changes: 76 additions & 0 deletions dart/lib/src/event_processor/run_event_processors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import 'package:meta/meta.dart';

import '../client_reports/discard_reason.dart';
import '../event_processor.dart';
import '../hint.dart';
import '../protocol/sentry_event.dart';
import '../protocol/sentry_level.dart';
import '../protocol/sentry_transaction.dart';
import '../sentry_options.dart';
import '../transport/data_category.dart';

@internal
Future<SentryEvent?> runEventProcessors(
SentryEvent event,
Hint hint,
List<EventProcessor> eventProcessors,
SentryOptions options,
) async {
int spanCountBeforeEventProcessors =
event is SentryTransaction ? event.spans.length : 0;

SentryEvent? processedEvent = event;
for (final processor in eventProcessors) {
try {
final e = processor.apply(processedEvent!, hint);
processedEvent = e is Future<SentryEvent?> ? await e : e;
} catch (exception, stackTrace) {
options.logger(
SentryLevel.error,
'An exception occurred while processing event by a processor',
exception: exception,
stackTrace: stackTrace,
);
if (options.automatedTestMode) {
rethrow;
}
}

final discardReason = DiscardReason.eventProcessor;
if (processedEvent == null) {
options.recorder.recordLostEvent(discardReason, _getCategory(event));
if (event is SentryTransaction) {
// We dropped the whole transaction, the dropped count includes all child spans + 1 root span
options.recorder.recordLostEvent(
discardReason,
DataCategory.span,
count: spanCountBeforeEventProcessors + 1,
);
}
options.logger(SentryLevel.debug, 'Event was dropped by a processor');
break;
} else if (event is SentryTransaction &&
processedEvent is SentryTransaction) {
// If event processor removed only some spans we still report them as dropped
final spanCountAfterEventProcessors = processedEvent.spans.length;
final droppedSpanCount =
spanCountBeforeEventProcessors - spanCountAfterEventProcessors;
if (droppedSpanCount > 0) {
options.recorder.recordLostEvent(
discardReason,
DataCategory.span,
count: droppedSpanCount,
);
}
}
}

return processedEvent;
}

DataCategory _getCategory(SentryEvent event) {
if (event is SentryTransaction) {
return DataCategory.transaction;
}
return DataCategory.error;
}
29 changes: 2 additions & 27 deletions dart/lib/src/scope.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:collection';
import 'package:meta/meta.dart';

import 'event_processor.dart';
import 'event_processor/run_event_processors.dart';
import 'hint.dart';
import 'propagation_context.dart';
import 'protocol.dart';
Expand Down Expand Up @@ -341,33 +342,7 @@ class Scope {
}
}

SentryEvent? processedEvent = event;
for (final processor in _eventProcessors) {
try {
final e = processor.apply(processedEvent!, hint);
if (e is Future<SentryEvent?>) {
processedEvent = await e;
} else {
processedEvent = e;
}
} catch (exception, stackTrace) {
_options.logger(
SentryLevel.error,
'An exception occurred while processing event by a processor',
exception: exception,
stackTrace: stackTrace,
);
if (_options.automatedTestMode) {
rethrow;
}
}
if (processedEvent == null) {
_options.logger(SentryLevel.debug, 'Event was dropped by a processor');
break;
}
}

return processedEvent;
return await runEventProcessors(event, hint, _eventProcessors, _options);
}

/// Merge the scope contexts runtimes and the event contexts runtimes.
Expand Down
67 changes: 7 additions & 60 deletions dart/lib/src/sentry_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:meta/meta.dart';

import 'client_reports/client_report_recorder.dart';
import 'client_reports/discard_reason.dart';
import 'event_processor.dart';
import 'event_processor/run_event_processors.dart';
import 'hint.dart';
import 'protocol.dart';
import 'protocol/sentry_feedback.dart';
Expand Down Expand Up @@ -134,10 +134,11 @@ class SentryClient {
return _emptySentryId;
}

preparedEvent = await _runEventProcessors(
preparedEvent = await runEventProcessors(
preparedEvent,
hint,
eventProcessors: _options.eventProcessors,
_options.eventProcessors,
_options,
);

// dropped by event processors
Expand Down Expand Up @@ -374,10 +375,11 @@ class SentryClient {
return _emptySentryId;
}

preparedTransaction = await _runEventProcessors(
preparedTransaction = await runEventProcessors(
preparedTransaction,
hint,
eventProcessors: _options.eventProcessors,
_options.eventProcessors,
_options,
) as SentryTransaction?;

// dropped by event processors
Expand Down Expand Up @@ -551,61 +553,6 @@ class SentryClient {
return processedEvent;
}

Future<SentryEvent?> _runEventProcessors(
SentryEvent event,
Hint hint, {
required List<EventProcessor> eventProcessors,
}) async {
SentryEvent? processedEvent = event;
int spanCountBeforeEventProcessors =
event is SentryTransaction ? event.spans.length : 0;

for (final processor in eventProcessors) {
try {
final e = processor.apply(processedEvent!, hint);
if (e is Future<SentryEvent?>) {
processedEvent = await e;
} else {
processedEvent = e;
}
} catch (exception, stackTrace) {
_options.logger(
SentryLevel.error,
'An exception occurred while processing event by a processor',
exception: exception,
stackTrace: stackTrace,
);
if (_options.automatedTestMode) {
rethrow;
}
}

final discardReason = DiscardReason.eventProcessor;
if (processedEvent == null) {
_options.recorder.recordLostEvent(discardReason, _getCategory(event));
if (event is SentryTransaction) {
// We dropped the whole transaction, the dropped count includes all child spans + 1 root span
_options.recorder.recordLostEvent(discardReason, DataCategory.span,
count: spanCountBeforeEventProcessors + 1);
}
_options.logger(SentryLevel.debug, 'Event was dropped by a processor');
break;
} else if (event is SentryTransaction &&
processedEvent is SentryTransaction) {
// If event processor removed only some spans we still report them as dropped
final spanCountAfterEventProcessors = processedEvent.spans.length;
final droppedSpanCount =
spanCountBeforeEventProcessors - spanCountAfterEventProcessors;
if (droppedSpanCount > 0) {
_options.recorder.recordLostEvent(discardReason, DataCategory.span,
count: droppedSpanCount);
}
}
}

return processedEvent;
}

bool _sampleRate() {
if (_options.sampleRate != null && _random != null) {
return (_options.sampleRate! < _random!.nextDouble());
Expand Down

0 comments on commit c01acd1

Please sign in to comment.