-
-
Notifications
You must be signed in to change notification settings - Fork 277
Refacto : add a Transport class #123
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
Changes from 1 commit
a10d740
623c904
0dcb613
c97c98b
477fdac
b8fb322
fd92e3f
0944c68
46829fc
01d4e19
fbd001e
4d39531
34fb528
2437fa4
57798f5
789e154
1aa05d6
9895142
cb5e309
e563d2c
71b53c8
be3f0a2
43b798c
19dc91d
cec1f21
909d4f9
8b600dc
4ff58a6
5493e19
621d6c4
e11f653
88526e2
796b633
fb8b9e0
1dbcba4
8b56bce
daeb29e
098d58c
2833a94
35671d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,15 @@ | |
| // 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 'transport/transport.dart'; | ||
| import 'version.dart'; | ||
|
|
||
| SentryClient createSentryClient(SentryOptions options) => | ||
|
|
@@ -41,19 +42,19 @@ class SentryBrowserClient extends SentryClient { | |
| ); | ||
| } | ||
|
|
||
| SentryBrowserClient._(SentryOptions options, {String origin, String platform}) | ||
| SentryBrowserClient._(SentryOptions options, | ||
| {String origin, @required String platform}) | ||
| : super.base( | ||
| options, | ||
| transport: Transport( | ||
| dsn: options.dsn, | ||
| httpClient: options.httpClient, | ||
| clock: options.clock, | ||
| compressPayload: false, | ||
| sdk: Sdk(name: browserSdkName, version: sdkVersion), | ||
|
||
| headersBuilder: SentryClient.buildHeaders, | ||
marandaneto marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| platform: platform, | ||
|
||
| ), | ||
| 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)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| import 'dart:async'; | ||
| import 'dart:convert'; | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
| import 'package:sentry/sentry.dart'; | ||
|
|
@@ -9,8 +8,8 @@ import 'client_stub.dart' | |
| if (dart.library.io) 'io_client.dart'; | ||
| import 'protocol.dart'; | ||
| import 'stack_trace.dart'; | ||
| import 'transport/transport.dart'; | ||
| import 'utils.dart'; | ||
| import 'version.dart'; | ||
|
|
||
| /// Logs crash reports and events to the Sentry.io service. | ||
| abstract class SentryClient { | ||
|
|
@@ -22,36 +21,15 @@ abstract class SentryClient { | |
|
|
||
| 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; | ||
| @required this.transport, | ||
| }); | ||
|
|
||
| @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; | ||
| final Transport transport; | ||
|
||
|
|
||
| /// Information about the current user. | ||
| /// | ||
|
|
@@ -68,51 +46,16 @@ abstract class SentryClient { | |
| /// 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, { | ||
| StackFrameFilter stackFrameFilter, | ||
| Scope scope, | ||
| }) 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), | ||
| }; | ||
|
|
||
| if (options.environmentAttributes != null) { | ||
|
|
@@ -132,22 +75,8 @@ abstract class SentryClient { | |
| ), | ||
| 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 transport.send(data); | ||
| } | ||
|
|
||
| /// Reports the [throwable] and optionally its [stackTrace] to Sentry.io. | ||
|
|
@@ -188,15 +117,22 @@ abstract class SentryClient { | |
| options.httpClient?.close(); | ||
| } | ||
|
|
||
| @override | ||
| String toString() => '$SentryClient("$postUri")'; | ||
| SentryEvent _processEvent( | ||
| SentryEvent event, { | ||
| dynamic hint, | ||
| List<EventProcessor> eventProcessors, | ||
| }) { | ||
| for (final processor in eventProcessors) { | ||
| event = processor(event, hint); | ||
|
||
| } | ||
| return event; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still missing the execution of the beforeSendCallback -> |
||
| } | ||
|
|
||
| @protected | ||
| List<int> bodyEncoder(Map<String, dynamic> data, Map<String, String> headers); | ||
| @override | ||
| String toString() => '$SentryClient("${options.dsn}")'; | ||
|
||
|
|
||
| @protected | ||
| @mustCallSuper | ||
| Map<String, String> buildHeaders(String authHeader) { | ||
| static Map<String, String> buildHeaders(String authHeader) { | ||
| final headers = { | ||
| 'Content-Type': 'application/json', | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import 'client.dart'; | |
| import 'protocol.dart'; | ||
| import 'scope.dart'; | ||
| import 'sentry_options.dart'; | ||
| import 'transport/transport.dart'; | ||
|
|
||
| class NoOpSentryClient implements SentryClient { | ||
| NoOpSentryClient._(); | ||
|
|
@@ -24,14 +25,7 @@ class NoOpSentryClient implements SentryClient { | |
| String origin; | ||
|
|
||
| @override | ||
| List<int> bodyEncoder( | ||
| Map<String, dynamic> data, | ||
| Map<String, String> headers, | ||
| ) => | ||
| []; | ||
|
|
||
| @override | ||
| Map<String, String> buildHeaders(String authHeader) => {}; | ||
| Transport transport; | ||
|
||
|
|
||
| @override | ||
| Future<SentryId> captureEvent(SentryEvent event, {stackFrameFilter, scope}) => | ||
|
|
@@ -51,29 +45,8 @@ class NoOpSentryClient implements SentryClient { | |
| }) => | ||
| Future.value(SentryId.empty()); | ||
|
|
||
| @override | ||
| String get clientId => 'No-op'; | ||
|
|
||
| @override | ||
| Future<void> close() async { | ||
| return; | ||
| } | ||
|
|
||
| @override | ||
| Uri get dsnUri => null; | ||
|
|
||
| @override | ||
| String get postUri => null; | ||
|
|
||
| @override | ||
| String get projectId => null; | ||
|
|
||
| @override | ||
| String get publicKey => null; | ||
|
|
||
| @override | ||
| Sdk get sdk => null; | ||
|
|
||
| @override | ||
| String get secretKey => null; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import 'dart:convert'; | ||
| import 'dart:io'; | ||
|
|
||
| List<int> bodyEncoder( | ||
| Map<String, dynamic> data, | ||
| Map<String, String> headers, { | ||
| bool compressPayload, | ||
| }) { | ||
| // [SentryIOClient] implement gzip compression | ||
| // gzip compression is not available on browser | ||
| var body = utf8.encode(json.encode(data)); | ||
| if (compressPayload) { | ||
| headers['Content-Encoding'] = 'gzip'; | ||
| body = gzip.encode(body); | ||
| } | ||
| return body; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import 'dart:convert'; | ||
|
|
||
| List<int> bodyEncoder( | ||
| Map<String, dynamic> data, | ||
| Map<String, String> headers, { | ||
| bool compressPayload, | ||
| }) { | ||
| // [SentryIOClient] implement gzip compression | ||
| // gzip compression is not available on browser | ||
| var body = utf8.encode(json.encode(data)); | ||
| return body; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.