Skip to content

Commit

Permalink
Add ignoreTransactions and ignoreErrors getsentry#1391
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin committed Aug 1, 2024
1 parent ffa37ac commit 9c8955f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
18 changes: 18 additions & 0 deletions dart/lib/src/sentry_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ class SentryClient {
dynamic stackTrace,
Hint? hint,
}) async {
if (_options.isIgnoredError(event)) {
_options.logger(
SentryLevel.debug,
'Error was ignored as specified in the ignoredErrors options.',
);
_options.recorder
.recordLostEvent(DiscardReason.beforeSend, _getCategory(event));
return _emptySentryId;
}

if (_options.containsIgnoredExceptionForType(event.throwable)) {
_options.logger(
SentryLevel.debug,
Expand Down Expand Up @@ -351,6 +361,14 @@ class SentryClient {
return _emptySentryId;
}

if (_options.isIgnoredTransaction(preparedTransaction)) {
_options.logger(
SentryLevel.debug,
'Transaction was ignored as specified in the ignoredTransactions options.',
);
return _emptySentryId;
}

preparedTransaction =
await _runBeforeSend(preparedTransaction, hint) as SentryTransaction?;

Expand Down
37 changes: 37 additions & 0 deletions dart/lib/src/sentry_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,43 @@ class SentryOptions {
/// sent. Events are picked randomly. Default is null (disabled)
double? sampleRate;

/// The ignoreErrors tells the SDK which errors should be not sent to the sentry server.
/// If an null or an empty list is used, the SDK will send all transactions.
Iterable<String>? ignoreErrors;

bool isIgnoredError(SentryEvent event) {
if (ignoreErrors == null ||
ignoreErrors!.isEmpty ||
event.message == null) {
return false;
}
String combinedRegexPattern = ignoreErrors!.join('|');

RegExp regExp = RegExp(combinedRegexPattern);

bool ignore = regExp.firstMatch(event.message!.formatted) != null;

return ignore;
}

/// The ignoreTransactions tells the SDK which transactions should be not sent to the sentry server.
/// If null or an empty list is used, the SDK will send all transactions.
Iterable<String>? ignoreTransactions;

bool isIgnoredTransaction(SentryTransaction transaction) {
if (ignoreTransactions == null || ignoreTransactions!.isEmpty) {
return false;
}

String combinedRegexPattern = ignoreTransactions!.join('|');

RegExp regExp = RegExp(combinedRegexPattern);

bool ignore = regExp.firstMatch(transaction.tracer.name) != null;

return ignore;
}

final List<String> _inAppExcludes = [];

/// A list of string prefixes of packages names that do not belong to the app, but rather third-party
Expand Down
36 changes: 36 additions & 0 deletions dart/test/sentry_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,42 @@ void main() {
});
});

group('SentryClient ignored errors', () {
late Fixture fixture;

setUp(() {
fixture = Fixture();
fixture.options.ignoreErrors = ["my-error", "error-.*"];
});

test('ignore Error "my-error"', () async {
final event = SentryEvent(message: SentryMessage("my-error"));

final client = fixture.getSut();
await client.captureEvent(event);

expect((fixture.transport).called(0), true);
});

test('ignore Error "error-foo"', () async {
final event = SentryEvent(message: SentryMessage("error-foo"));

final client = fixture.getSut();
await client.captureEvent(event);

expect((fixture.transport).called(0), true);
});

test('allow Error "warning"', () async {
final event = SentryEvent(message: SentryMessage("warning"));

final client = fixture.getSut();
await client.captureEvent(event);

expect((fixture.transport).called(1), true);
});
});

group('SentryClient ignored exceptions', () {
late Fixture fixture;

Expand Down

0 comments on commit 9c8955f

Please sign in to comment.