Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move application of event processors to shared function #2594

Merged
merged 6 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(

Check warning on line 28 in dart/lib/src/event_processor/run_event_processors.dart

View check run for this annotation

Codecov / codecov/patch

dart/lib/src/event_processor/run_event_processors.dart#L28

Added line #L28 was not covered by tests
SentryLevel.error,
'An exception occurred while processing event by a processor',
exception: exception,
stackTrace: stackTrace,
);
if (options.automatedTestMode) {

Check warning on line 34 in dart/lib/src/event_processor/run_event_processors.dart

View check run for this annotation

Codecov / codecov/patch

dart/lib/src/event_processor/run_event_processors.dart#L34

Added line #L34 was not covered by tests
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
Loading