Skip to content
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

feat: Sync RN scope to native #902

Merged
merged 31 commits into from
Jun 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
eef05e7
meta: added changlog entry for option filter
jennmueng May 28, 2020
a83ac4a
feat: Added event.origin tags to determine where the event originated.
jennmueng May 29, 2020
3e7ffc6
build: bump sentry-cocoa to 1.5.1
jennmueng Jun 1, 2020
e19e318
feat: Added event.environment tag to Android events
jennmueng Jun 1, 2020
3e0ed4b
meta: changelog
jennmueng Jun 1, 2020
2ce04da
build: Added @sentry/hub as a dependency
jennmueng Jun 1, 2020
face8ea
test: Add a button to set test scope properties.
jennmueng Jun 2, 2020
13a7393
feat: Sync setting js tags to native scope
jennmueng Jun 2, 2020
ef59ab3
test: Add number cases to sample scope set
jennmueng Jun 2, 2020
44870f0
feat: Syncs setExtra/setExtras to native scope
jennmueng Jun 3, 2020
c60f4d7
test: Adds a tests sending a nested object as an extra
jennmueng Jun 3, 2020
5566f83
feat: Syncs setUser to native scope
jennmueng Jun 3, 2020
c32a3c7
feat: Syncs breadcumbs to native scope
jennmueng Jun 3, 2020
abe757d
test: Adds extensive breadcrumb tests to sample
jennmueng Jun 3, 2020
31dbac0
feat: Adds setContext syncs down to iOS
jennmueng Jun 4, 2020
e311c59
test: Adds context test to sample
jennmueng Jun 4, 2020
589eb69
ref: Removes else null case for Android setUser
jennmueng Jun 4, 2020
43fb0df
feat: Supports unknown fields in setUser
jennmueng Jun 4, 2020
d20ba3d
test: Adds unknown fields to sample app setUser
jennmueng Jun 4, 2020
7a877fa
feat: setContext instead calls setExtra on Android
jennmueng Jun 4, 2020
1f06e42
ref: Use configureScope on Android
jennmueng Jun 4, 2020
4639450
feat: clearBreadcrumbs clears on native as well
jennmueng Jun 4, 2020
0afb011
test: Adds clearBreadcrumb button to sample
jennmueng Jun 4, 2020
e5ca813
feat: Adds setRelease and setDist to sample scope test
jennmueng Jun 4, 2020
e748634
Merge branch 'master' into feat/native-synced-scope
jennmueng Jun 4, 2020
b50ccf9
ref: Removes commented code
jennmueng Jun 4, 2020
4864885
ref: Calls super for all extended scope methods
jennmueng Jun 4, 2020
2add368
ref: Moves setTag stringify to wrapper
jennmueng Jun 4, 2020
43ebe20
meta: changelog entry
jennmueng Jun 4, 2020
5030e88
ref: Alphabetical imports
jennmueng Jun 4, 2020
c0a3139
ref: Remove useless comments
jennmueng Jun 4, 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
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Extend Scope methods to set native scope too. #902

## 1.4.2

- Bump android 2.1.4 #891
Expand Down Expand Up @@ -138,7 +140,7 @@ New way to import and init the SDK:
import * as Sentry from "@sentry/react-native";

Sentry.init({
dsn: "DSN",
dsn: "DSN"
});
```

Expand Down Expand Up @@ -374,7 +376,7 @@ To activate it set:

```js
Sentry.config("___DSN___", {
deactivateStacktraceMerging: false,
jennmueng marked this conversation as resolved.
Show resolved Hide resolved
deactivateStacktraceMerging: false
});
```

Expand Down
126 changes: 126 additions & 0 deletions android/src/main/java/io/sentry/RNSentryModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.module.annotations.ReactModule;

import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
Expand All @@ -29,11 +32,14 @@
import io.sentry.android.core.NdkIntegration;
import io.sentry.android.core.SentryAndroid;
import io.sentry.core.Sentry;
import io.sentry.core.Breadcrumb;
import io.sentry.core.Integration;
import io.sentry.core.SentryLevel;
import io.sentry.core.SentryOptions;
import io.sentry.core.UncaughtExceptionHandlerIntegration;
import io.sentry.core.protocol.SdkVersion;
import io.sentry.core.protocol.SentryException;
import io.sentry.core.protocol.User;

@ReactModule(name = RNSentryModule.NAME)
public class RNSentryModule extends ReactContextBaseJavaModule {
Expand Down Expand Up @@ -213,4 +219,124 @@ private Level logLevel(int level) {
return Level.OFF;
}
}

@ReactMethod
public void setUser(final ReadableMap user, final ReadableMap otherUserKeys) {
Sentry.configureScope(scope -> {
if (user == null && otherUserKeys == null) {
scope.setUser(null);
} else {
User userInstance = new User();

if (user != null) {
if (user.hasKey("email")) {
userInstance.setEmail(user.getString("email"));
}

if (user.hasKey("id")) {
userInstance.setId(user.getString("id"));
}

if (user.hasKey("username")) {
userInstance.setUsername(user.getString("username"));
}

if (user.hasKey("ip_address")) {
userInstance.setIpAddress(user.getString("ip_address"));
}
}

if (otherUserKeys != null) {
HashMap<String, String> otherUserKeysMap = new HashMap<String, String>();
ReadableMapKeySetIterator it = otherUserKeys.keySetIterator();
while (it.hasNextKey()) {
String key = it.nextKey();
String value = otherUserKeys.getString(key);

otherUserKeysMap.put(key, value);
}

userInstance.setOthers(otherUserKeysMap);
}

scope.setUser(userInstance);
}
});
}

@ReactMethod
public void addBreadcrumb(final ReadableMap breadcrumb) {
Sentry.configureScope(scope -> {
Breadcrumb breadcrumbInstance = new Breadcrumb();

if (breadcrumb.hasKey("message")) {
breadcrumbInstance.setMessage(breadcrumb.getString("message"));
}

if (breadcrumb.hasKey("type")) {
breadcrumbInstance.setType(breadcrumb.getString("type"));
}

if (breadcrumb.hasKey("category")) {
breadcrumbInstance.setCategory(breadcrumb.getString("category"));
}

if (breadcrumb.hasKey("level")) {
switch (breadcrumb.getString("level")) {
case "fatal":
breadcrumbInstance.setLevel(SentryLevel.FATAL);
break;
case "warning":
breadcrumbInstance.setLevel(SentryLevel.WARNING);
break;
case "info":
breadcrumbInstance.setLevel(SentryLevel.INFO);
break;
case "debug":
breadcrumbInstance.setLevel(SentryLevel.DEBUG);
break;
case "error":
breadcrumbInstance.setLevel(SentryLevel.ERROR);
break;
default:
breadcrumbInstance.setLevel(SentryLevel.ERROR);
break;
}
}

if (breadcrumb.hasKey("data")) {
ReadableMap data = breadcrumb.getMap("data");
ReadableMapKeySetIterator it = data.keySetIterator();
while (it.hasNextKey()) {
String key = it.nextKey();
String value = data.getString(key);

breadcrumbInstance.setData(key, value);
}
}

scope.addBreadcrumb(breadcrumbInstance);
});
}

@ReactMethod
public void clearBreadcrumbs() {
Sentry.configureScope(scope -> {
scope.clearBreadcrumbs();
});
}

@ReactMethod
public void setExtra(String key, String extra) {
Sentry.configureScope(scope -> {
scope.setExtra(key, extra);
});
}

@ReactMethod
public void setTag(String key, String value) {
Sentry.configureScope(scope -> {
scope.setTag(key, value);
});
}
}
90 changes: 90 additions & 0 deletions ios/RNSentry.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,96 @@ + (BOOL)requiresMainQueueSetup {
});
}

RCT_EXPORT_METHOD(setUser:(NSDictionary *)user
otherUserKeys:(NSDictionary *)otherUserKeys
)
{
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
if (nil == user && nil == otherUserKeys) {
[scope setUser:nil];
} else {
SentryUser* userInstance = [[SentryUser alloc] init];

if (nil != user) {
[userInstance setUserId:user[@"id"]];
[userInstance setEmail:user[@"email"]];
[userInstance setUsername:user[@"username"]];
}

if (nil != otherUserKeys) {
[userInstance setData:otherUserKeys];
}

[scope setUser:userInstance];
}
}];
}

RCT_EXPORT_METHOD(addBreadcrumb:(NSDictionary *)breadcrumb)
{
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
SentryBreadcrumb* breadcrumbInstance = [[SentryBreadcrumb alloc] init];

NSString * levelString = breadcrumb[@"level"];
SentryLevel sentryLevel;
if ([levelString isEqualToString:@"fatal"]) {
sentryLevel = kSentryLevelFatal;
} else if ([levelString isEqualToString:@"warning"]) {
sentryLevel = kSentryLevelWarning;
} else if ([levelString isEqualToString:@"info"]) {
sentryLevel = kSentryLevelInfo;
} else if ([levelString isEqualToString:@"debug"]) {
sentryLevel = kSentryLevelDebug;
} else {
sentryLevel = kSentryLevelError;
}
[breadcrumbInstance setLevel:sentryLevel];

[breadcrumbInstance setCategory:breadcrumb[@"category"]];

[breadcrumbInstance setType:breadcrumb[@"type"]];

[breadcrumbInstance setMessage:breadcrumb[@"message"]];

[breadcrumbInstance setData:breadcrumb[@"data"]];

[scope addBreadcrumb:breadcrumbInstance];
}];
}

RCT_EXPORT_METHOD(clearBreadcrumbs) {
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
[scope clearBreadcrumbs];
}];
}

RCT_EXPORT_METHOD(setExtra:(NSString *)key
extra:(NSString *)extra
)
{
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
[scope setExtraValue:extra forKey:key];
}];
}

RCT_EXPORT_METHOD(setContext:(NSString *)key
context:(NSDictionary *)context
)
{
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
[scope setContextValue:context forKey:key];
}];
}

RCT_EXPORT_METHOD(setTag:(NSString *)key
value:(NSString *)value
)
{
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
[scope setTagValue:value forKey:key];
}];
}

RCT_EXPORT_METHOD(crash)
{
[SentrySDK crash];
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"@sentry/browser": "^5.15.5",
"@sentry/core": "^5.15.5",
"@sentry/hub": "^5.15.5",
"@sentry/integrations": "^5.15.5",
"@sentry/types": "^5.15.5",
"@sentry/utils": "^5.15.5",
Expand Down
Loading