-
-
Notifications
You must be signed in to change notification settings - Fork 237
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
Support user context #17
Merged
yjbanov
merged 11 commits into
getsentry:master
from
dustin-graham:support_user_context
May 15, 2018
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
64dcd35
add support for user context
dustin-graham d94749c
upgrade usage of deprecated GZIP
dustin-graham 99b20c0
revert gzip usage upgrade
dustin-graham 750531a
revert gzip usage upgrade in test
dustin-graham f58b55e
add tests for user context
dustin-graham 1823205
fix formatting
dustin-graham 5e022d4
remove print statement
dustin-graham 2e01023
Make user context a mutable property for SentryClient
dustin-graham df7ebf8
Add additional dart docs and an assertion for User properties
dustin-graham c925605
fix build error
dustin-graham 3e568ca
resolve dartfmt issue
dustin-graham 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 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 |
---|---|---|
|
@@ -143,6 +143,14 @@ class SentryClient { | |
/// Attached to the event payload. | ||
final String projectId; | ||
|
||
/// The user data that will get sent with every logged event | ||
/// | ||
/// Note that a [Event.userContext] that is set on a logged [Event] | ||
/// will override the [User] context set here. | ||
/// | ||
/// see: https://docs.sentry.io/learn/context/#capturing-the-user | ||
User userContext; | ||
|
||
@visibleForTesting | ||
String get postUri => | ||
'${dsnUri.scheme}://${dsnUri.host}/api/$projectId/store/'; | ||
|
@@ -170,6 +178,10 @@ class SentryClient { | |
if (environmentAttributes != null) | ||
mergeAttributes(environmentAttributes.toJson(), into: data); | ||
|
||
// merge the user context | ||
if (userContext != null) { | ||
mergeAttributes({'user': userContext.toJson()}, into: data); | ||
} | ||
mergeAttributes(event.toJson(), into: data); | ||
|
||
List<int> body = utf8.encode(json.encode(data)); | ||
|
@@ -285,6 +297,7 @@ class Event { | |
this.tags, | ||
this.extra, | ||
this.fingerprint, | ||
this.userContext, | ||
}); | ||
|
||
/// The logger that logged the event. | ||
|
@@ -330,6 +343,12 @@ class Event { | |
/// they must be JSON-serializable. | ||
final Map<String, dynamic> extra; | ||
|
||
/// User information that is sent with the logged [Event] | ||
/// | ||
/// The value in this field overrides the user context | ||
/// set in [SentryClient.userContext] for this logged event. | ||
final User userContext; | ||
|
||
/// Used to deduplicate events by grouping ones with the same fingerprint | ||
/// together. | ||
/// | ||
|
@@ -389,9 +408,65 @@ class Event { | |
|
||
if (extra != null && extra.isNotEmpty) json['extra'] = extra; | ||
|
||
Map<String, dynamic> userContextMap; | ||
if (userContext != null && | ||
(userContextMap = userContext.toJson()).isNotEmpty) | ||
json['user'] = userContextMap; | ||
|
||
if (fingerprint != null && fingerprint.isNotEmpty) | ||
json['fingerprint'] = fingerprint; | ||
|
||
return json; | ||
} | ||
} | ||
|
||
/// An interface which describes the authenticated User for a request. | ||
/// You should provide at least either an id (a unique identifier for an | ||
/// authenticated user) or ip_address (their IP address). | ||
/// | ||
/// Conforms to the User Interface contract for Sentry | ||
/// https://docs.sentry.io/clientdev/interfaces/user/ | ||
/// | ||
/// The outgoing json representation is: | ||
/// ``` | ||
/// "user": { | ||
/// "id": "unique_id", | ||
/// "username": "my_user", | ||
/// "email": "[email protected]", | ||
/// "ip_address": "127.0.0.1", | ||
/// "subscription": "basic" | ||
/// } | ||
/// ``` | ||
class User { | ||
/// The unique ID of the user. | ||
final String id; | ||
|
||
/// The username of the user | ||
final String username; | ||
|
||
/// The email address of the user. | ||
final String email; | ||
|
||
/// The IP of the user. | ||
final String ipAddress; | ||
|
||
/// Any other user context information that may be helpful | ||
/// All other keys are stored as extra information but not | ||
/// specifically processed by sentry. | ||
final Map<String, dynamic> extras; | ||
|
||
/// At a minimum you must set an [id] or an [ipAddress] | ||
const User({this.id, this.username, this.email, this.ipAddress, this.extras}) | ||
: assert(id != null || ipAddress != null); | ||
|
||
/// produces a [Map] that can be serialized to JSON | ||
Map<String, dynamic> toJson() { | ||
return { | ||
"id": id, | ||
"username": username, | ||
"email": email, | ||
"ip_address": ipAddress, | ||
"extras": extras, | ||
}; | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -74,7 +74,9 @@ void main() { | |
'Content-Type': 'application/json', | ||
'X-Sentry-Auth': 'Sentry sentry_version=6, ' | ||
'sentry_client=${SentryClient.sentryClient}, ' | ||
'sentry_timestamp=${fakeClock.now().millisecondsSinceEpoch}, ' | ||
'sentry_timestamp=${fakeClock | ||
.now() | ||
.millisecondsSinceEpoch}, ' | ||
'sentry_key=public, ' | ||
'sentry_secret=secret', | ||
}; | ||
|
@@ -171,10 +173,82 @@ void main() { | |
|
||
await client.close(); | ||
}); | ||
|
||
test('$Event userContext overrides client', () async { | ||
final MockClient httpMock = new MockClient(); | ||
final Clock fakeClock = new Clock.fixed(new DateTime(2017, 1, 2)); | ||
|
||
String loggedUserId; // used to find out what user context was sent | ||
httpMock.answerWith((Invocation invocation) async { | ||
if (invocation.memberName == #close) { | ||
return null; | ||
} | ||
if (invocation.memberName == #post) { | ||
// parse the body and detect which user context was sent | ||
var bodyData = invocation.namedArguments[new Symbol("body")]; | ||
var decoded = new Utf8Codec().decode(bodyData); | ||
var decodedJson = new JsonDecoder().convert(decoded); | ||
loggedUserId = decodedJson['user']['id']; | ||
return new Response('', 401, headers: <String, String>{ | ||
'x-sentry-error': 'Invalid api key', | ||
}); | ||
} | ||
fail('Unexpected invocation of ${invocation.memberName} in HttpMock'); | ||
}); | ||
|
||
final clientUserContext = new User( | ||
id: "client_user", | ||
username: "username", | ||
email: "[email protected]", | ||
ipAddress: "127.0.0.1"); | ||
final eventUserContext = new User( | ||
id: "event_user", | ||
username: "username", | ||
email: "[email protected]", | ||
ipAddress: "127.0.0.1", | ||
extras: {"foo": "bar"}); | ||
|
||
final SentryClient client = new SentryClient( | ||
dsn: _testDsn, | ||
httpClient: httpMock, | ||
clock: fakeClock, | ||
uuidGenerator: () => 'X' * 32, | ||
compressPayload: false, | ||
environmentAttributes: const Event( | ||
serverName: 'test.server.com', | ||
release: '1.2.3', | ||
environment: 'staging', | ||
), | ||
); | ||
client.userContext = clientUserContext; | ||
|
||
try { | ||
throw new ArgumentError('Test error'); | ||
} catch (error, stackTrace) { | ||
final eventWithoutContext = | ||
new Event(exception: error, stackTrace: stackTrace); | ||
final eventWithContext = new Event( | ||
exception: error, | ||
stackTrace: stackTrace, | ||
userContext: eventUserContext); | ||
await client.capture(event: eventWithoutContext); | ||
expect(loggedUserId, clientUserContext.id); | ||
await client.capture(event: eventWithContext); | ||
expect(loggedUserId, eventUserContext.id); | ||
} | ||
|
||
await client.close(); | ||
}); | ||
}); | ||
|
||
group('$Event', () { | ||
test('serializes to JSON', () { | ||
final user = new User( | ||
id: "user_id", | ||
username: "username", | ||
email: "[email protected]", | ||
ipAddress: "127.0.0.1", | ||
extras: {"foo": "bar"}); | ||
expect( | ||
new Event( | ||
message: 'test-message', | ||
|
@@ -190,6 +264,7 @@ void main() { | |
'g': 2, | ||
}, | ||
fingerprint: <String>[Event.defaultFingerprint, 'foo'], | ||
userContext: user, | ||
).toJson(), | ||
<String, dynamic>{ | ||
'platform': 'dart', | ||
|
@@ -203,6 +278,13 @@ void main() { | |
'tags': {'a': 'b', 'c': 'd'}, | ||
'extra': {'e': 'f', 'g': 2}, | ||
'fingerprint': ['{{ default }}', 'foo'], | ||
'user': { | ||
'id': 'user_id', | ||
'username': 'username', | ||
'email': '[email protected]', | ||
'ip_address': '127.0.0.1', | ||
'extras': {'foo': 'bar'} | ||
}, | ||
}, | ||
); | ||
}); | ||
|
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.
Wow, did
dartfmt
do this?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.
it did. it appears that the travis rule runs dartfmt and fails if there is anything changed after running it. so after I ran dartfmt locally and uploaded it now passes.
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.
I should file a bug.