Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- BREAKING CHANGE: `package:http` min version bumped to 0.12.0 #104
- BREAKING CHANGE: replace the `package:usage` by `package:uuid` #94
- BREAKING CHANGE: `Event.message` must now be an instance of `Message`
- BREAKING CHANGE: SentryClient must now be initialized with a SentryOptions #118
- By default no logger it set #63
- Added missing Contexts to Event.copyWith() #62
- remove the `package:args` dependency #94
Expand Down
60 changes: 12 additions & 48 deletions dart/lib/src/browser_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,15 @@ import 'dart:convert';
import 'dart:html' show window;

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

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

SentryClient createSentryClient({
@required String dsn,
Event environmentAttributes,
bool compressPayload,
Client httpClient,
dynamic clock,
UuidGenerator uuidGenerator,
}) =>
SentryBrowserClient(
dsn: dsn,
environmentAttributes: environmentAttributes,
httpClient: httpClient,
clock: clock,
uuidGenerator: uuidGenerator,
);
SentryClient createSentryClient(SentryOptions options) =>
SentryBrowserClient(options);

/// Logs crash reports and events to the Sentry.io service.
class SentryBrowserClient extends SentryClient {
Expand All @@ -53,49 +39,27 @@ class SentryBrowserClient extends SentryClient {
/// If [uuidGenerator] is provided, it is used to generate the "event_id"
/// field instead of the built-in random UUID v4 generator. This is useful in
/// tests.
factory SentryBrowserClient({
@required String dsn,
Event environmentAttributes,
Client httpClient,
dynamic clock,
UuidGenerator uuidGenerator,
String origin,
}) {
httpClient ??= BrowserClient();
clock ??= getUtcDateTime;
uuidGenerator ??= generateUuidV4WithoutDashes;
factory SentryBrowserClient(SentryOptions options, {String origin}) {
options.httpClient ??= BrowserClient();
options.clock ??= getUtcDateTime;
options.uuidGenerator ??= generateUuidV4WithoutDashes;

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

return SentryBrowserClient._(
httpClient: httpClient,
clock: clock,
uuidGenerator: uuidGenerator,
environmentAttributes: environmentAttributes,
dsn: dsn,
options,
origin: origin,
platform: browserPlatform,
);
}

SentryBrowserClient._({
Client httpClient,
dynamic clock,
UuidGenerator uuidGenerator,
Event environmentAttributes,
String dsn,
String platform,
String origin,
}) : super.base(
httpClient: httpClient,
clock: clock,
uuidGenerator: uuidGenerator,
environmentAttributes: environmentAttributes,
dsn: dsn,
platform: platform,
SentryBrowserClient._(SentryOptions options, {String origin, String platform})
: super.base(
options,
origin: origin,
sdk: Sdk(name: browserSdkName, version: sdkVersion),
platform: platform,
);

@override
Expand Down
75 changes: 20 additions & 55 deletions dart/lib/src/client.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:async';
import 'dart:convert';

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

Expand All @@ -22,60 +21,30 @@ abstract class SentryClient {
///
/// Creates an `SentryIOClient` if `dart:io` is available and a `SentryBrowserClient` if
/// `dart:html` is available, otherwise it will throw an unsupported error.
factory SentryClient({
@required String dsn,
Event environmentAttributes,
bool compressPayload,
Client httpClient,
dynamic clock,
UuidGenerator uuidGenerator,
}) =>
createSentryClient(
dsn: dsn,
environmentAttributes: environmentAttributes,
httpClient: httpClient,
clock: clock,
uuidGenerator: uuidGenerator,
compressPayload: compressPayload,
);

SentryClient.base({
this.httpClient,
dynamic clock,
UuidGenerator uuidGenerator,
String dsn,
this.environmentAttributes,
factory SentryClient(SentryOptions options) => createSentryClient(options);

SentryClient.base(
this.options, {
String platform,
this.origin,
Sdk sdk,
}) : _dsn = Dsn.parse(dsn),
_uuidGenerator = uuidGenerator ?? generateUuidV4WithoutDashes,
}) : _dsn = Dsn.parse(options.dsn),
_platform = platform ?? sdkPlatform,
sdk = sdk ?? Sdk(name: sdkName, version: sdkVersion) {
if (clock == null) {
_clock = getUtcDateTime;
if (options.clock == null) {
options.clock = getUtcDateTime;
} else {
_clock = (clock is ClockProvider ? clock : clock.get) as ClockProvider;
options.clock = (options.clock is ClockProvider
? options.clock
: options.clock.get) as ClockProvider;
}
}

@protected
final Client httpClient;

ClockProvider _clock;
final UuidGenerator _uuidGenerator;

/// Contains [Event] attributes that are automatically mixed into all events
/// captured through this client.
///
/// This event is designed to contain static values that do not change from
/// event to event, such as local operating system version, the version of
/// Dart/Flutter SDK, etc. These attributes have lower precedence than those
/// supplied in the even passed to [capture].
final Event environmentAttributes;

final Dsn _dsn;

@protected
SentryOptions options;

/// The DSN URI.
@visibleForTesting
Uri get dsnUri => _dsn.uri;
Expand Down Expand Up @@ -108,7 +77,7 @@ abstract class SentryClient {
User userContext;

/// Use for browser stacktrace
final String origin;
String origin;

/// Used by sentry to differentiate browser from io environment
final String _platform;
Expand Down Expand Up @@ -142,7 +111,7 @@ abstract class SentryClient {
StackFrameFilter stackFrameFilter,
Scope scope,
}) async {
final now = _clock();
final now = options.clock();
var authHeader = 'Sentry sentry_version=6, sentry_client=$clientId, '
'sentry_timestamp=${now.millisecondsSinceEpoch}, sentry_key=$publicKey';
if (secretKey != null) {
Expand All @@ -153,12 +122,12 @@ abstract class SentryClient {

final data = <String, dynamic>{
'project': projectId,
'event_id': _uuidGenerator(),
'event_id': options.uuidGenerator(),
'timestamp': formatDateAsIso8601WithSecondPrecision(now),
};

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

// Merge the user context.
Expand All @@ -178,17 +147,13 @@ abstract class SentryClient {

final body = bodyEncoder(data, headers);

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

if (response.statusCode != 200) {
/*var errorMessage = 'Sentry.io responded with HTTP ${response.statusCode}';
if (response.headers['x-sentry-error'] != null) {
errorMessage += ': ${response.headers['x-sentry-error']}';
}*/
return SentryId.empty();
}

Expand Down Expand Up @@ -222,7 +187,7 @@ abstract class SentryClient {
}

Future<void> close() async {
httpClient.close();
options.httpClient?.close();
}

@override
Expand Down
15 changes: 2 additions & 13 deletions dart/lib/src/client_stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

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

/// Implemented in `browser_client.dart` and `io_client.dart`.
SentryClient createSentryClient({
@required String dsn,
Event environmentAttributes,
bool compressPayload,
Client httpClient,
dynamic clock,
UuidGenerator uuidGenerator,
}) =>
SentryClient createSentryClient(SentryOptions options) =>
throw UnsupportedError(
'Cannot create a client without dart:html or dart:io.');
13 changes: 3 additions & 10 deletions dart/lib/src/hub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@ typedef ScopeCallback = void Function(Scope);

/// SDK API contract which combines a client and scope management
class Hub {
static SentryClient _getClient({SentryOptions fromOptions}) {
return SentryClient(
dsn: fromOptions.dsn,
environmentAttributes: fromOptions.environmentAttributes,
compressPayload: fromOptions.compressPayload,
httpClient: fromOptions.httpClient,
clock: fromOptions.clock,
uuidGenerator: fromOptions.uuidGenerator,
);
static SentryClient _getClient(SentryOptions options) {
return SentryClient(options);
}

final ListQueue<_StackItem> _stack = ListQueue();
Expand All @@ -36,7 +29,7 @@ class Hub {
}

Hub._(SentryOptions options) : _options = options {
_stack.add(_StackItem(_getClient(fromOptions: options), Scope(_options)));
_stack.add(_StackItem(_getClient(_options), Scope(_options)));
_isEnabled = true;
}

Expand Down
72 changes: 6 additions & 66 deletions dart/lib/src/io_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,13 @@
import 'dart:convert';
import 'dart:io';

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

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

SentryClient createSentryClient({
@required String dsn,
Event environmentAttributes,
bool compressPayload,
Client httpClient,
dynamic clock,
UuidGenerator uuidGenerator,
}) =>
SentryIOClient(
dsn: dsn,
environmentAttributes: environmentAttributes,
compressPayload: compressPayload,
httpClient: httpClient,
clock: clock,
uuidGenerator: uuidGenerator,
);
SentryClient createSentryClient(SentryOptions options) =>
SentryIOClient(options);

/// Logs crash reports and events to the Sentry.io service.
class SentryIOClient extends SentryClient {
Expand Down Expand Up @@ -57,52 +40,9 @@ class SentryIOClient extends SentryClient {
/// If [uuidGenerator] is provided, it is used to generate the "event_id"
/// field instead of the built-in random UUID v4 generator. This is useful in
/// tests.
factory SentryIOClient({
@required String dsn,
Event environmentAttributes,
bool compressPayload,
Client httpClient,
dynamic clock,
UuidGenerator uuidGenerator,
}) {
httpClient ??= Client();
clock ??= getUtcDateTime;
uuidGenerator ??= generateUuidV4WithoutDashes;
compressPayload ??= true;
factory SentryIOClient(SentryOptions options) => SentryIOClient._(options);

return SentryIOClient._(
httpClient: httpClient,
clock: clock,
uuidGenerator: uuidGenerator,
environmentAttributes: environmentAttributes,
dsn: dsn,
compressPayload: compressPayload,
platform: sdkPlatform,
);
}

SentryIOClient._({
Client httpClient,
dynamic clock,
UuidGenerator uuidGenerator,
Event environmentAttributes,
String dsn,
this.compressPayload = true,
String platform,
String origin,
}) : super.base(
httpClient: httpClient,
clock: clock,
uuidGenerator: uuidGenerator,
environmentAttributes: environmentAttributes,
dsn: dsn,
platform: platform,
origin: origin,
sdk: Sdk(name: sdkName, version: sdkVersion),
);

/// Whether to compress payloads sent to Sentry.io.
final bool compressPayload;
SentryIOClient._(SentryOptions options) : super.base(options);

@override
Map<String, String> buildHeaders(String authHeader) {
Expand All @@ -123,7 +63,7 @@ class SentryIOClient extends SentryClient {
// [SentryIOClient] implement gzip compression
// gzip compression is not available on browser
var body = utf8.encode(json.encode(data));
if (compressPayload) {
if (options.compressPayload) {
headers['Content-Encoding'] = 'gzip';
body = gzip.encode(body);
}
Expand Down
Loading