Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
a10d740
remove the SentryResponse class
rxlabz Oct 21, 2020
623c904
refactor : add a Transport class
rxlabz Oct 21, 2020
0dcb613
refactor : move the clock initialization to Transport
rxlabz Oct 21, 2020
c97c98b
- refactor : applyScope
rxlabz Oct 21, 2020
477fdac
changelog
rxlabz Oct 21, 2020
b8fb322
fix merge conflicts
rxlabz Oct 22, 2020
fd92e3f
Merge branch 'feature/unified-api' into ref/transport
rxlabz Oct 22, 2020
0944c68
move the headers definition strategy to transport
rxlabz Oct 22, 2020
46829fc
- Instantiate Transport from options
rxlabz Oct 22, 2020
01d4e19
handle event dropping in event processors
rxlabz Oct 22, 2020
fbd001e
fix client._applyScope : merge event and scope tags and extra
rxlabz Oct 22, 2020
4d39531
remove client.toString()
rxlabz Oct 22, 2020
34fb528
fix a test
rxlabz Oct 22, 2020
2437fa4
Merge branch 'feature/unified-api' into ref/transport
rxlabz Oct 22, 2020
57798f5
handle the event serialization in the transport layer
rxlabz Oct 22, 2020
789e154
Merge remote-tracking branch 'origin/feature/unified-api' into ref/tr…
rxlabz Oct 22, 2020
1aa05d6
remove stackFrameFilter references
rxlabz Oct 22, 2020
9895142
Merge remote-tracking branch 'origin/feature/unified-api' into ref/tr…
rxlabz Oct 22, 2020
cb5e309
add a CredentialBuilder
rxlabz Oct 22, 2020
e563d2c
- headers configuration
rxlabz Oct 23, 2020
71b53c8
- fix scope.level precedence
rxlabz Oct 23, 2020
be3f0a2
buildHeaders and fix timestamp test
rxlabz Oct 23, 2020
43b798c
- add a isWeb helper to define the event.platform
rxlabz Oct 23, 2020
19dc91d
feat: apply sample rate
marandaneto Oct 23, 2020
cec1f21
changelog
marandaneto Oct 23, 2020
909d4f9
- add a isWeb helper to define the event.platform and sdkName
rxlabz Oct 23, 2020
8b600dc
- add a isWeb helper to define the event.platform and sdkName
rxlabz Oct 23, 2020
4ff58a6
Merge remote-tracking branch 'origin/feat/sample-rate' into ref/trans…
rxlabz Oct 23, 2020
5493e19
fix
marandaneto Oct 23, 2020
621d6c4
update tests
rxlabz Oct 23, 2020
e11f653
Merge remote-tracking branch 'origin/feat/sample-rate' into ref/trans…
rxlabz Oct 23, 2020
88526e2
remove the platform arg from Client ctor
rxlabz Oct 23, 2020
796b633
set client.options private
rxlabz Oct 23, 2020
fb8b9e0
remove event.platform redefinition
rxlabz Oct 23, 2020
1dbcba4
set options.sdk if null
rxlabz Oct 23, 2020
8b56bce
remove fields from Transport Api
rxlabz Oct 23, 2020
daeb29e
private version consts
rxlabz Oct 23, 2020
098d58c
remove transport from the Client ctor
rxlabz Oct 23, 2020
2833a94
remove options.environmentAttributes
rxlabz Oct 23, 2020
35671d4
event processing : platform
rxlabz Oct 23, 2020
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
Prev Previous commit
Next Next commit
feat: apply sample rate
  • Loading branch information
marandaneto committed Oct 23, 2020
commit 19dc91d210eb86892efdfd68c507d57c90a1ecfe
30 changes: 28 additions & 2 deletions dart/lib/src/client.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:math';

import 'package:meta/meta.dart';
import 'package:sentry/sentry.dart';
Expand All @@ -17,13 +18,16 @@ abstract class SentryClient {
/// `dart:html` is available, otherwise it will throw an unsupported error.
factory SentryClient(SentryOptions options) => createSentryClient(options);

SentryClient.base(this.options, {@required this.transport});
SentryClient.base(this.options, {@required this.transport}) {
_random = options.sampleRate == null ? null : Random();
}

@protected
SentryOptions options;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could be private I believe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ fixed


// TODO: this should be removed and used options.transport
@visibleForTesting
final Transport transport;
Transport transport;

/// Information about the current user.
///
Expand All @@ -37,6 +41,8 @@ abstract class SentryClient {
/// * https://docs.sentry.io/learn/context/#capturing-the-user
User userContext;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be part of the SentryClient only as well, it belongs to the Scope.user

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Random _random;

/// Reports an [event] to Sentry.io.
Future<SentryId> captureEvent(
SentryEvent event, {
Expand All @@ -45,6 +51,11 @@ abstract class SentryClient {
}) async {
event = _processEvent(event, eventProcessors: options.eventProcessors);

// dropped by sampling or event processors
if (event == null) {
return Future.value(SentryId.empty());
}

event = _applyScope(event: event, scope: scope);

event = event.copyWith(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

projectId is not really necessaary, its part of the DSN URL
https://develop.sentry.dev/sdk/event-payloads/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ fixed

Expand Down Expand Up @@ -97,6 +108,14 @@ abstract class SentryClient {
dynamic hint,
List<EventProcessor> eventProcessors,
}) {
if (_sampleRate()) {
options.logger(
SentryLevel.debug,
'Event ${event.eventId.toString()} was dropped due to sampling decision.',
);
return null;
}

for (final processor in eventProcessors) {
try {
event = processor(event, hint);
Expand Down Expand Up @@ -157,4 +176,11 @@ abstract class SentryClient {
}
return event;
}

bool _sampleRate() {
if (options.sampleRate != null && _random != null) {
return (options.sampleRate < _random.nextDouble());
}
return true;
}
}
12 changes: 10 additions & 2 deletions dart/lib/src/sentry_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ class SentryOptions {

/// Configures the sample rate as a percentage of events to be sent in the range of 0.0 to 1.0. if
/// 1.0 is set it means that 100% of events are sent. If set to 0.1 only 10% of events will be
/// sent. Events are picked randomly. Default is 1.0 (disabled)
double sampleRate = 1.0;
/// sent. Events are picked randomly. Default is null (disabled)
double sampleRate;

/// A list of string prefixes of module names that do not belong to the app, but rather third-party
/// packages. Modules considered not to be part of the app will be hidden from stack traces by
Expand All @@ -119,6 +119,14 @@ class SentryOptions {
List<String> get inAppIncludes => List.unmodifiable(_inAppIncludes);

// TODO: transport, transportGate, connectionTimeoutMillis, readTimeoutMillis, hostnameVerifier, sslSocketFactory, proxy
Transport _transport;

Transport get transport => _transport;

set transport(Transport transport) {
// TODO: NoOp transport
_transport = transport;
}

/// Sets the distribution. Think about it together with release and environment
String dist;
Expand Down
2 changes: 2 additions & 0 deletions dart/test/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:sentry/src/protocol.dart';

class MockSentryClient extends Mock implements SentryClient {}

class MockTransport extends Mock implements Transport {}

final fakeDsn = 'https://[email protected]/1234567';

final fakeException = Exception('Error');
Expand Down
44 changes: 44 additions & 0 deletions dart/test/sentry_client_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:mockito/mockito.dart';
import 'package:sentry/sentry.dart';
import 'package:test/test.dart';

import 'mocks.dart';

void main() {
group('SentryClient sampling', () {
SentryOptions options;
Transport transport;

setUp(() {
options = SentryOptions(dsn: fakeDsn);
transport = MockTransport();
});

test('captures event, sample rate is 100% enabled', () {
options.sampleRate = 1.0;
final client = SentryClient(options);
client.transport = transport;
client.captureEvent(fakeEvent);

verify(client.transport.send(any)).called(1);
});

test('do not capture event, sample rate is 0% disabled', () {
options.sampleRate = 0.0;
final client = SentryClient(options);
client.transport = transport;
client.captureEvent(fakeEvent);

verifyNever(client.transport.send(any));
});

test('do not capture event, sample rate is null', () {
options.sampleRate = null;
final client = SentryClient(options);
client.transport = transport;
client.captureEvent(fakeEvent);

verifyNever(client.transport.send(any));
});
});
}