Skip to content
Merged
Show file tree
Hide file tree
Changes from 25 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- Ref: Sentry init with null and empty DSN and close method #126
- Ref: Hint is passed across Sentry static class, Hub and Client #124
- Ref: Remove stackFrameFilter in favor of beforeSendCallback #125
- Ref: added Transport #123

# `package:sentry` changelog

Expand Down
4 changes: 2 additions & 2 deletions dart/lib/sentry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
/// A pure Dart client for Sentry.io crash reporting.
export 'src/client.dart';
export 'src/protocol.dart';
export 'src/scope.dart';
export 'src/sentry.dart';
export 'src/sentry_options.dart';
export 'src/transport/transport.dart';
export 'src/version.dart';
export 'src/scope.dart';
export 'src/sentry_options.dart';
24 changes: 6 additions & 18 deletions dart/lib/src/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
// found in the LICENSE file.

/// A pure Dart client for Sentry.io crash reporting.
import 'dart:convert';
import 'dart:html' show window;

import 'package:http/browser_client.dart';
import 'package:meta/meta.dart';

import 'client.dart';
import 'protocol.dart';
import 'sentry_options.dart';
import 'version.dart';

Expand All @@ -32,7 +31,6 @@ class SentryBrowserClient extends SentryClient {
options.httpClient ??= BrowserClient();

// origin is necessary for sentry to resolve stacktrace
origin ??= '${window.location.origin}/';

return SentryBrowserClient._(
options,
Expand All @@ -41,19 +39,9 @@ class SentryBrowserClient extends SentryClient {
);
}

SentryBrowserClient._(SentryOptions options, {String origin, String platform})
: super.base(
options,
origin: origin,
sdk: Sdk(name: browserSdkName, version: sdkVersion),
platform: platform,
);

@override
List<int> bodyEncoder(
Map<String, dynamic> data,
Map<String, String> headers,
) =>
// Gzip compression is implicit on browser
utf8.encode(json.encode(data));
SentryBrowserClient._(
SentryOptions options, {
String origin,
@required String platform,
}) : super.base(options, origin: '${window.location.origin}/');
}
230 changes: 79 additions & 151 deletions dart/lib/src/client.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import 'dart:async';
import 'dart:convert';

import 'package:meta/meta.dart';
import 'package:sentry/sentry.dart';
import 'package:sentry/src/transport/noop_transport.dart';

import 'client_stub.dart'
if (dart.library.html) 'browser_client.dart'
if (dart.library.io) 'io_client.dart';
import 'protocol.dart';
import 'utils.dart';
import 'version.dart';

/// Logs crash reports and events to the Sentry.io service.
abstract class SentryClient {
Expand All @@ -19,38 +17,19 @@ abstract class SentryClient {
/// `dart:html` is available, otherwise it will throw an unsupported error.
factory SentryClient(SentryOptions options) => createSentryClient(options);

SentryClient.base(
this.options, {
String platform,
this.origin,
Sdk sdk,
}) : _dsn = Dsn.parse(options.dsn),
_platform = platform ?? sdkPlatform,
sdk = sdk ?? Sdk(name: sdkName, version: sdkVersion);

final Dsn _dsn;
SentryClient.base(this.options, {String origin}) {
if (options.transport is NoOpTransport) {
options.transport = Transport(
options: options,
sdkIdentifier: '${sdkName}/${sdkVersion}',
Copy link
Contributor

Choose a reason for hiding this comment

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

could this be replaced by options.sdkVersion.identifier? it looks duplicated now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

✅ my bad, I thought sdkVersion was just a String, I renamed it sdk, and define it in the ioClient & BrowserClient if null

origin: origin,
);
}
}

@protected
SentryOptions options;

/// The DSN URI.
@visibleForTesting
Uri get dsnUri => _dsn.uri;

/// The Sentry.io public key for the project.
@visibleForTesting
// ignore: invalid_use_of_visible_for_testing_member
String get publicKey => _dsn.publicKey;

/// The Sentry.io secret key for the project.
@visibleForTesting
// ignore: invalid_use_of_visible_for_testing_member
String get secretKey => _dsn.secretKey;

/// The ID issued by Sentry.io to your project.
///
/// Attached to the event payload.
String get projectId => _dsn.projectId;
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


/// Information about the current user.
///
Expand All @@ -64,88 +43,19 @@ 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.


/// Use for browser stacktrace
String origin;

/// Used by sentry to differentiate browser from io environment
final String _platform;

final Sdk sdk;

String get clientId => sdk.identifier;

@visibleForTesting
String get postUri {
final port = dsnUri.hasPort &&
((dsnUri.scheme == 'http' && dsnUri.port != 80) ||
(dsnUri.scheme == 'https' && dsnUri.port != 443))
? ':${dsnUri.port}'
: '';
final pathLength = dsnUri.pathSegments.length;
String apiPath;
if (pathLength > 1) {
// some paths would present before the projectID in the dsnUri
apiPath =
(dsnUri.pathSegments.sublist(0, pathLength - 1) + ['api']).join('/');
} else {
apiPath = 'api';
}
return '${dsnUri.scheme}://${dsnUri.host}$port/$apiPath/$projectId/store/';
}

/// Reports an [event] to Sentry.io.
Future<SentryId> captureEvent(
SentryEvent event, {
Scope scope,
dynamic hint,
}) async {
final now = options.clock();
var authHeader = 'Sentry sentry_version=6, sentry_client=$clientId, '
'sentry_timestamp=${now.millisecondsSinceEpoch}, sentry_key=$publicKey';
if (secretKey != null) {
authHeader += ', sentry_secret=$secretKey';
}

final headers = buildHeaders(authHeader);
event = _processEvent(event, eventProcessors: options.eventProcessors);

final data = <String, dynamic>{
'project': projectId,
'event_id': event.eventId.toString(),
'timestamp': formatDateAsIso8601WithSecondPrecision(event.timestamp),
};
event = _applyScope(event: event, scope: scope);

if (options.environmentAttributes != null) {
mergeAttributes(options.environmentAttributes.toJson(), into: data);
}
event = event.copyWith(platform: sdkPlatform);

// Merge the user context.
if (userContext != null) {
mergeAttributes(<String, dynamic>{'user': userContext.toJson()},
into: data);
}

mergeAttributes(
event.toJson(
origin: origin,
),
into: data,
);
mergeAttributes(<String, String>{'platform': _platform}, into: data);

final body = bodyEncoder(data, headers);

final response = await options.httpClient.post(
postUri,
headers: headers,
body: body,
);

if (response.statusCode != 200) {
return SentryId.empty();
}

final eventId = json.decode(response.body)['id'];
return eventId != null ? SentryId.fromId(eventId) : SentryId.empty();
return options.transport.send(event);
}

/// Reports the [throwable] and optionally its [stackTrace] to Sentry.io.
Expand Down Expand Up @@ -173,63 +83,81 @@ abstract class SentryClient {
dynamic hint,
}) {
final event = SentryEvent(
message: Message(
formatted,
template: template,
params: params,
),
message: Message(formatted, template: template, params: params),
level: level,
timestamp: options.clock(),
);

return captureEvent(event, scope: scope, hint: hint);
}

void close() {
options.httpClient?.close();
}

@override
String toString() => '$SentryClient("$postUri")';

@protected
List<int> bodyEncoder(Map<String, dynamic> data, Map<String, String> headers);

@protected
@mustCallSuper
Map<String, String> buildHeaders(String authHeader) {
final headers = {
'Content-Type': 'application/json',
};

if (authHeader != null) {
headers['X-Sentry-Auth'] = authHeader;
SentryEvent _processEvent(
SentryEvent event, {
dynamic hint,
List<EventProcessor> eventProcessors,
}) {
for (final processor in eventProcessors) {
try {
event = processor(event, hint);
} catch (err) {
options.logger(
SentryLevel.error,
'An exception occurred while processing event by a processor : $err',
);
}
if (event == null) {
options.logger(SentryLevel.debug, 'Event was dropped by a processor');
break;
}
}

return headers;
return event;
Copy link
Contributor

Choose a reason for hiding this comment

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

}
}

/// A response from Sentry.io.
///
/// If [isSuccessful] the [eventId] field will contain the ID assigned to the
/// captured event by the Sentry.io backend. Otherwise, the [error] field will
/// contain the description of the error.
@immutable
class SentryResponse {
const SentryResponse.success({@required this.eventId})
: isSuccessful = true,
error = null;

const SentryResponse.failure(this.error)
: isSuccessful = false,
eventId = null;

/// Whether event was submitted successfully.
final bool isSuccessful;

/// The ID Sentry.io assigned to the submitted event for future reference.
final String eventId;

/// Error message, if the response is not successful.
final String error;
SentryEvent _applyScope({
@required SentryEvent event,
@required Scope scope,
}) {
if (scope != null) {
// Merge the scope transaction.
if (event.transaction == null) {
event = event.copyWith(transaction: scope.transaction);
}

// Merge the user context.
if (event.userContext == null) {
event = event.copyWith(userContext: scope.user);
}

// Merge the scope fingerprint.
if (event.fingerprint == null) {
event = event.copyWith(fingerprint: scope.fingerprint);
}

// Merge the scope breadcrumbs.
if (event.breadcrumbs == null) {
event = event.copyWith(breadcrumbs: scope.breadcrumbs);
}

// TODO add tests
// Merge the scope tags.
event = event.copyWith(
tags: scope.tags.map((key, value) => MapEntry(key, value))
..addAll(event.tags ?? {}));

// Merge the scope extra.
event = event.copyWith(
extra: scope.extra.map((key, value) => MapEntry(key, value))
..addAll(event.extra ?? {}));

// Merge the scope level.
if (scope.level != null) {
event = event.copyWith(level: scope.level);
}
}
return event;
}
}
Loading