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 14 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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Expose session timeout. #887
- Added `event.origin` and `event.environment` tags to determine where events originate from. #890

## 1.4.1

Expand Down Expand Up @@ -134,7 +135,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 @@ -370,7 +371,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
2 changes: 1 addition & 1 deletion RNSentry.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
s.preserve_paths = '*.js'

s.dependency 'React'
s.dependency 'Sentry', '~> 5.1.0'
s.dependency 'Sentry', '~> 5.1.1'

s.source_files = 'ios/RNSentry.{h,m}'
s.public_header_files = 'ios/RNSentry.h'
Expand Down
139 changes: 139 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 @@ -28,10 +31,15 @@
import io.sentry.android.core.AnrIntegration;
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 @@ -108,6 +116,27 @@ public void startWithOptions(final ReadableMap rnOptions, Promise promise) {
} catch (Exception e) {
// We do nothing
}

// Add on the correct event.origin tag.
// it needs to be here so we can determine where it originated from.
SdkVersion sdkVersion = event.getSdk();
jennmueng marked this conversation as resolved.
Show resolved Hide resolved
if (sdkVersion != null) {
String sdkName = sdkVersion.getName();
if (sdkName != null) {
if (sdkName.equals("sentry.javascript.react-native")) {
event.setTag("event.origin", "javascript");
} else if (sdkName.equals("sentry.java.android") || sdkName.equals("sentry.native")) {
event.setTag("event.origin", "android");

if (sdkName.equals("sentry.native")) {
event.setTag("event.environment", "native");
} else {
event.setTag("event.environment", "java");
}
}
}
}

return event;
});

Expand Down Expand Up @@ -190,4 +219,114 @@ private Level logLevel(int level) {
return Level.OFF;
}
}

@ReactMethod
public void setUser(final ReadableMap user) {
if (user == null) {
Sentry.setUser(null);
jennmueng marked this conversation as resolved.
Show resolved Hide resolved
} else {
User userInstance = new User();

// only support these defined keys for now.
jennmueng marked this conversation as resolved.
Show resolved Hide resolved
if (user.hasKey("email")) {
userInstance.setEmail(user.getString("email"));
} else {
userInstance.setEmail(null);
jennmueng marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

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

Sentry.setUser(userInstance);
}
}

@ReactMethod
public void addBreadcrumb(final ReadableMap breadcrumb) {
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")) {
jennmueng marked this conversation as resolved.
Show resolved Hide resolved
ReadableMap data = breadcrumb.getMap("data");
ReadableMapKeySetIterator it = data.keySetIterator();
while (it.hasNextKey()) {
String key = it.nextKey();
String value = data.getString(key);
jennmueng marked this conversation as resolved.
Show resolved Hide resolved

breadcrumbInstance.setData(key, value);
}
}

Sentry.addBreadcrumb(breadcrumbInstance);
}

// @ReactMethod
// public void setFingerprint(ReadableArray fingerprint) {
// ArrayList<String> fingerprintList = new ArrayList<String>();

// for (int i = 0; i < fingerprint.size(); i++) {
// fingerprintList.add(fingerprint.getString(i));
// }

// Sentry.setFingerprint(fingerprintList);
// }

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

@ReactMethod
public void setTag(String key, String value) {
Sentry.setTag(key, value);
}
}
82 changes: 82 additions & 0 deletions ios/RNSentry.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ + (BOOL)requiresMainQueueSetup {
return nil;
}

// set the event.origin tag to be ios if the event originated from the sentry-cocoa sdk.
if (event.sdk && [event.sdk[@"name"] isEqualToString:@"sentry.cocoa"]) {
NSMutableDictionary *newTags = [NSMutableDictionary new];
[newTags addEntriesFromDictionary:event.tags];
[newTags setValue:@"ios" forKey:@"event.origin"];
event.tags = newTags;
}

return event;
};

Expand Down Expand Up @@ -124,6 +132,80 @@ + (BOOL)requiresMainQueueSetup {
});
}

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

[userInstance setUserId:user[@"id"]];
[userInstance setEmail:user[@"email"]];
[userInstance setUsername:user[@"username"]];

[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(setExtra:(NSString *)key
extra:(NSString *)extra
)
{
[SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
[scope setExtraValue:extra forKey:key];
}];
}

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

// RCT_EXPORT_METHOD(setFingerprint:(NSArray<NSString *> *)fingerprint)
// {
// [SentrySDK configureScope:^(SentryScope * _Nonnull scope) {
// [scope setFingerprint:fingerprint];
// }];
// }
jennmueng marked this conversation as resolved.
Show resolved Hide resolved

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