-
-
Notifications
You must be signed in to change notification settings - Fork 267
Support for new DSN format (without secretKey) and remove the quiver dependency. #20
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
# Name/Organization <email address> | ||
|
||
Google Inc. | ||
Simon Lightfoot <[email protected]> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,6 @@ import 'dart:io'; | |
|
||
import 'package:http/http.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:quiver/time.dart'; | ||
import 'package:usage/uuid/uuid.dart'; | ||
|
||
import 'src/stack_trace.dart'; | ||
|
@@ -20,6 +19,9 @@ import 'src/version.dart'; | |
|
||
export 'src/version.dart'; | ||
|
||
/// Used to provide timestamp for logging. | ||
typedef ClockProvider = DateTime Function(); | ||
|
||
/// Logs crash reports and events to the Sentry.io service. | ||
class SentryClient { | ||
/// Sentry.io client identifier for _this_ client. | ||
|
@@ -46,7 +48,9 @@ class SentryClient { | |
/// make HTTP calls to Sentry.io. This is useful in tests. | ||
/// | ||
/// If [clock] is provided, it is used to get time instead of the system | ||
/// clock. This is useful in tests. | ||
/// clock. This is useful in tests. Should be an implementation of ClockProvider. | ||
/// This parameter is dynamic to maintain backwards compatibility with | ||
/// previous use of Clock from the Quiver library. | ||
/// | ||
/// 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 | ||
|
@@ -56,36 +60,35 @@ class SentryClient { | |
Event environmentAttributes, | ||
bool compressPayload, | ||
Client httpClient, | ||
Clock clock, | ||
dynamic clock, | ||
UuidGenerator uuidGenerator, | ||
}) { | ||
httpClient ??= new Client(); | ||
clock ??= const Clock(_getUtcDateTime); | ||
clock ??= _getUtcDateTime; | ||
uuidGenerator ??= _generateUuidV4WithoutDashes; | ||
compressPayload ??= true; | ||
|
||
final ClockProvider clockProvider = | ||
clock is ClockProvider ? clock : clock.get; | ||
|
||
final Uri uri = Uri.parse(dsn); | ||
final List<String> userInfo = uri.userInfo.split(':'); | ||
|
||
assert(() { | ||
if (userInfo.length != 2) | ||
throw new ArgumentError( | ||
'Colon-separated publicKey:secretKey pair not found in the user info field of the DSN URI: $dsn'); | ||
|
||
if (uri.pathSegments.isEmpty) | ||
throw new ArgumentError( | ||
'Project ID not found in the URI path of the DSN URI: $dsn'); | ||
|
||
return true; | ||
}()); | ||
|
||
final String publicKey = userInfo.first; | ||
final String secretKey = userInfo.last; | ||
final String publicKey = userInfo[0]; | ||
final String secretKey = userInfo.length >= 2 ? userInfo[1] : null; | ||
final String projectId = uri.pathSegments.last; | ||
|
||
return new SentryClient._( | ||
httpClient: httpClient, | ||
clock: clock, | ||
clock: clockProvider, | ||
uuidGenerator: uuidGenerator, | ||
environmentAttributes: environmentAttributes, | ||
dsnUri: uri, | ||
|
@@ -98,20 +101,20 @@ class SentryClient { | |
|
||
SentryClient._({ | ||
@required Client httpClient, | ||
@required Clock clock, | ||
@required ClockProvider clock, | ||
@required UuidGenerator uuidGenerator, | ||
@required this.environmentAttributes, | ||
@required this.dsnUri, | ||
@required this.publicKey, | ||
@required this.secretKey, | ||
this.secretKey, | ||
@required this.compressPayload, | ||
@required this.projectId, | ||
}) : _httpClient = httpClient, | ||
_clock = clock, | ||
_uuidGenerator = uuidGenerator; | ||
|
||
final Client _httpClient; | ||
final Clock _clock; | ||
final ClockProvider _clock; | ||
final UuidGenerator _uuidGenerator; | ||
|
||
/// Contains [Event] attributes that are automatically mixed into all events | ||
|
@@ -161,21 +164,23 @@ class SentryClient { | |
|
||
/// Reports an [event] to Sentry.io. | ||
Future<SentryResponse> capture({@required Event event}) async { | ||
final DateTime now = _clock.now(); | ||
final DateTime now = _clock(); | ||
String authHeader = 'Sentry sentry_version=6, sentry_client=$sentryClient, ' | ||
'sentry_timestamp=${now.millisecondsSinceEpoch}, sentry_key=$publicKey'; | ||
if (secretKey != null) { | ||
authHeader += ', sentry_secret=$secretKey'; | ||
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. Let's add a unit-test for this case. 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. Added unit test for parsing and http header. |
||
} | ||
|
||
final Map<String, String> headers = <String, String>{ | ||
'User-Agent': '$sentryClient', | ||
'Content-Type': 'application/json', | ||
'X-Sentry-Auth': 'Sentry sentry_version=6, ' | ||
'sentry_client=$sentryClient, ' | ||
'sentry_timestamp=${now.millisecondsSinceEpoch}, ' | ||
'sentry_key=$publicKey, ' | ||
'sentry_secret=$secretKey', | ||
'X-Sentry-Auth': authHeader, | ||
}; | ||
|
||
final Map<String, dynamic> data = <String, dynamic>{ | ||
'project': projectId, | ||
'event_id': _uuidGenerator(), | ||
'timestamp': formatDateAsIso8601WithSecondPrecision(_clock.now()), | ||
'timestamp': formatDateAsIso8601WithSecondPrecision(now), | ||
'logger': defaultLoggerName, | ||
}; | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,11 @@ import 'dart:convert'; | |
import 'dart:io'; | ||
|
||
import 'package:http/http.dart'; | ||
import 'package:quiver/time.dart'; | ||
import 'package:sentry/sentry.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
const String _testDsn = 'https://public:[email protected]/1'; | ||
const String _testDsnWithoutSecret = 'https://[email protected]/1'; | ||
|
||
void main() { | ||
group('$SentryClient', () { | ||
|
@@ -24,9 +24,75 @@ void main() { | |
await client.close(); | ||
}); | ||
|
||
test('can parse DSN without secret', () async { | ||
final SentryClient client = new SentryClient(dsn: _testDsnWithoutSecret); | ||
expect(client.dsnUri, Uri.parse(_testDsnWithoutSecret)); | ||
expect(client.postUri, 'https://sentry.example.com/api/1/store/'); | ||
expect(client.publicKey, 'public'); | ||
expect(client.secretKey, null); | ||
expect(client.projectId, '1'); | ||
await client.close(); | ||
}); | ||
|
||
test('sends client auth header without secret', () async { | ||
final MockClient httpMock = new MockClient(); | ||
final ClockProvider fakeClockProvider = | ||
() => new DateTime.utc(2017, 1, 2); | ||
|
||
Map<String, String> headers; | ||
|
||
httpMock.answerWith((Invocation invocation) async { | ||
if (invocation.memberName == #close) { | ||
return null; | ||
} | ||
if (invocation.memberName == #post) { | ||
headers = invocation.namedArguments[#headers]; | ||
return new Response('{"id": "test-event-id"}', 200); | ||
} | ||
fail('Unexpected invocation of ${invocation.memberName} in HttpMock'); | ||
}); | ||
|
||
final SentryClient client = new SentryClient( | ||
dsn: _testDsnWithoutSecret, | ||
httpClient: httpMock, | ||
clock: fakeClockProvider, | ||
compressPayload: false, | ||
uuidGenerator: () => 'X' * 32, | ||
environmentAttributes: const Event( | ||
serverName: 'test.server.com', | ||
release: '1.2.3', | ||
environment: 'staging', | ||
), | ||
); | ||
|
||
try { | ||
throw new ArgumentError('Test error'); | ||
} catch (error, stackTrace) { | ||
final SentryResponse response = await client.captureException( | ||
exception: error, stackTrace: stackTrace); | ||
expect(response.isSuccessful, true); | ||
expect(response.eventId, 'test-event-id'); | ||
expect(response.error, null); | ||
} | ||
|
||
final Map<String, String> expectedHeaders = <String, String>{ | ||
'User-Agent': '$sdkName/$sdkVersion', | ||
'Content-Type': 'application/json', | ||
'X-Sentry-Auth': 'Sentry sentry_version=6, ' | ||
'sentry_client=${SentryClient.sentryClient}, ' | ||
'sentry_timestamp=${fakeClockProvider().millisecondsSinceEpoch}, ' | ||
'sentry_key=public', | ||
}; | ||
|
||
expect(headers, expectedHeaders); | ||
|
||
await client.close(); | ||
}); | ||
|
||
testCaptureException(bool compressPayload) async { | ||
final MockClient httpMock = new MockClient(); | ||
final Clock fakeClock = new Clock.fixed(new DateTime.utc(2017, 1, 2)); | ||
final ClockProvider fakeClockProvider = | ||
() => new DateTime.utc(2017, 1, 2); | ||
|
||
String postUri; | ||
Map<String, String> headers; | ||
|
@@ -47,7 +113,7 @@ void main() { | |
final SentryClient client = new SentryClient( | ||
dsn: _testDsn, | ||
httpClient: httpMock, | ||
clock: fakeClock, | ||
clock: fakeClockProvider, | ||
uuidGenerator: () => 'X' * 32, | ||
compressPayload: compressPayload, | ||
environmentAttributes: const Event( | ||
|
@@ -74,9 +140,7 @@ void main() { | |
'Content-Type': 'application/json', | ||
'X-Sentry-Auth': 'Sentry sentry_version=6, ' | ||
'sentry_client=${SentryClient.sentryClient}, ' | ||
'sentry_timestamp=${fakeClock | ||
.now() | ||
.millisecondsSinceEpoch}, ' | ||
'sentry_timestamp=${fakeClockProvider().millisecondsSinceEpoch}, ' | ||
'sentry_key=public, ' | ||
'sentry_secret=secret', | ||
}; | ||
|
@@ -133,7 +197,8 @@ void main() { | |
|
||
test('reads error message from the x-sentry-error header', () async { | ||
final MockClient httpMock = new MockClient(); | ||
final Clock fakeClock = new Clock.fixed(new DateTime(2017, 1, 2)); | ||
final ClockProvider fakeClockProvider = | ||
() => new DateTime.utc(2017, 1, 2); | ||
|
||
httpMock.answerWith((Invocation invocation) async { | ||
if (invocation.memberName == #close) { | ||
|
@@ -150,7 +215,7 @@ void main() { | |
final SentryClient client = new SentryClient( | ||
dsn: _testDsn, | ||
httpClient: httpMock, | ||
clock: fakeClock, | ||
clock: fakeClockProvider, | ||
uuidGenerator: () => 'X' * 32, | ||
compressPayload: false, | ||
environmentAttributes: const Event( | ||
|
@@ -176,7 +241,8 @@ void main() { | |
|
||
test('$Event userContext overrides client', () async { | ||
final MockClient httpMock = new MockClient(); | ||
final Clock fakeClock = new Clock.fixed(new DateTime(2017, 1, 2)); | ||
final ClockProvider fakeClockProvider = | ||
() => new DateTime.utc(2017, 1, 2); | ||
|
||
String loggedUserId; // used to find out what user context was sent | ||
httpMock.answerWith((Invocation invocation) async { | ||
|
@@ -211,7 +277,7 @@ void main() { | |
final SentryClient client = new SentryClient( | ||
dsn: _testDsn, | ||
httpClient: httpMock, | ||
clock: fakeClock, | ||
clock: fakeClockProvider, | ||
uuidGenerator: () => 'X' * 32, | ||
compressPayload: false, | ||
environmentAttributes: const Event( | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
References to in-scope identifiers should be wrapped in square brackets so that dartdoc can create links to them, in this case
[ClockProvider]
.