Skip to content

Commit

Permalink
build(android): Bump sentry-android to 5.0.1 (#46)
Browse files Browse the repository at this point in the history
Bumps the Sentry Android SDK to 5.0.1.

Additional Changes:

- Updates the captureEnvelope method to get the outboxPath from HubAdapter. A recent fix in React Native.
- Adds the native SDK's packages to the sdk info on events sent from the JS layer.
- Moves the event origin tag into its own method to align with other sdks.
  • Loading branch information
jennmueng authored Jun 8, 2021
1 parent 1ce8b46 commit 69f8171
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 38 deletions.
3 changes: 1 addition & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ repositories {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':capacitor-android')
implementation 'io.sentry:sentry-android:4.3.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'io.sentry:sentry-android:5.0.1'
testImplementation "junit:junit:$junitVersion"
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
Expand Down
101 changes: 68 additions & 33 deletions android/src/main/java/io/sentry/capacitor/SentryCapacitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.google.gson.Gson;

import io.sentry.Breadcrumb;
import io.sentry.HubAdapter;
import io.sentry.Integration;
import io.sentry.Sentry;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.SentryEvent;
import io.sentry.UncaughtExceptionHandlerIntegration;
import io.sentry.android.core.AnrIntegration;
import io.sentry.android.core.NdkIntegration;
import io.sentry.android.core.SentryAndroid;
import io.sentry.protocol.SdkVersion;
import io.sentry.protocol.SentryPackage;
import io.sentry.protocol.User;

import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
Expand All @@ -38,7 +40,6 @@
public class SentryCapacitor extends Plugin {

final static Logger logger = Logger.getLogger("capacitor-sentry");
private SentryOptions sentryOptions;
private Context context;
private static PackageInfo packageInfo;

Expand Down Expand Up @@ -99,25 +100,8 @@ public void startWithOptions(final PluginCall capOptions) {

options.setBeforeSend(
(event, hint) -> {
// Add on the correct event.origin tag.
// it needs to be here so we can determine where it originated from.
SdkVersion sdkVersion = event.getSdk();
if (sdkVersion != null) {
String sdkName = sdkVersion.getName();
if (sdkName != null) {
if (sdkName.equals("sentry.javascript.capacitor")) {
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");
}
}
}
}
setEventOriginTag(event);
addPackages(event, options.getSdkVersion());

return event;
}
Expand All @@ -137,7 +121,6 @@ public void startWithOptions(final PluginCall capOptions) {
}

logger.info(String.format("Native Integrations '%s'", options.getIntegrations().toString()));
sentryOptions = options;
}
);

Expand Down Expand Up @@ -206,17 +189,25 @@ public void fetchRelease(PluginCall call) {

@PluginMethod
public void captureEnvelope(PluginCall call) {
String envelope = call.getString("envelope");
try {
File installation = new File(sentryOptions.getOutboxPath(), UUID.randomUUID().toString());
String envelope = call.getString("envelope");
final String outboxPath = HubAdapter.getInstance().getOptions().getOutboxPath();

if (outboxPath == null || outboxPath.isEmpty()) {
logger.info("Error when writing envelope, no outbox path is present.");
call.reject("Missing outboxPath");
return;
}

final File installation = new File(outboxPath, UUID.randomUUID().toString());

try (FileOutputStream out = new FileOutputStream(installation)) {
out.write(envelope.getBytes(Charset.forName("UTF-8")));
logger.info("Successfully captured envelope.");

JSObject resp = new JSObject();
resp.put("value", envelope);
call.resolve(resp);

} catch (Exception e) {
logger.info("Error writing envelope.");
call.reject(String.valueOf(e));
Expand All @@ -229,7 +220,7 @@ public void captureEnvelope(PluginCall call) {
return;
}
}

@PluginMethod
public void getStringBytesLength(PluginCall call) {
if (call.getData().has("string")) {
Expand Down Expand Up @@ -288,12 +279,13 @@ public void addBreadcrumb(final PluginCall breadcrumb) {

if (breadcrumb.getData().has("data")) {
JSObject data = breadcrumb.getObject("data");
HashMap<String, String> mappedData = new Gson().fromJson(data.toString(), HashMap.class);
Iterator<String> it = data.keys();

while (it.hasNext()) {
String key = it.next();
String value = data.getString(key);

for ( HashMap.Entry<String, String> entry: mappedData.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
breadcrumbInstance.setData(key, value);
breadcrumbInstance.setData(key, value);
}
}

Expand All @@ -316,7 +308,7 @@ public void setExtra(PluginCall call) {
String key = call.getString("key");
String extra = call.getString("extra");
scope.setExtra(key, extra);
});
});
}
call.resolve();
}
Expand All @@ -332,4 +324,47 @@ public void setTag(PluginCall call) {
}
call.resolve();
}

public void setEventOriginTag(SentryEvent event) {
SdkVersion sdk = event.getSdk();
if (sdk != null) {
switch (sdk.getName()) {
// If the event is from capacitor js, it gets set there and we do not handle it here.
case "sentry.native":
setEventEnvironmentTag(event, "android", "native");
break;
case "sentry.java.android":
setEventEnvironmentTag(event, "android", "java");
break;
default:
break;
}
}
}

private void setEventEnvironmentTag(SentryEvent event, String origin, String environment) {
event.setTag("event.origin", origin);
event.setTag("event.environment", environment);
}

public void addPackages(SentryEvent event, SdkVersion sdk) {
SdkVersion eventSdk = event.getSdk();
if (eventSdk != null && eventSdk.getName().equals("sentry.javascript.capacitor") && sdk != null) {
List<SentryPackage> sentryPackages = sdk.getPackages();
if (sentryPackages != null) {
for (SentryPackage sentryPackage : sentryPackages) {
eventSdk.addPackage(sentryPackage.getName(), sentryPackage.getVersion());
}
}

List<String> integrations = sdk.getIntegrations();
if (integrations != null) {
for (String integration : integrations) {
eventSdk.addIntegration(integration);
}
}

event.setSdk(eventSdk);
}
}
}
25 changes: 22 additions & 3 deletions src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ export const NATIVE = {
* Sending the event over the bridge to native
* @param event Event
*/
async sendEvent(event: Event): Promise<Response> {
async sendEvent(_event: Event): Promise<Response> {
if (!this.enableNative) {
throw this._DisabledNativeError;
}
if (!this.isNativeClientAvailable()) {
throw this._NativeClientError;
}

// Process and convert deprecated levels
event.level = event.level ? this._processLevel(event.level) : undefined;
const event = this._processLevels(_event);

const header = {
event_id: event.event_id,
Expand Down Expand Up @@ -259,6 +258,26 @@ export const NATIVE = {
return serialized;
},

/**
* Convert js severity level in event.level and event.breadcrumbs to more widely supported levels.
* @param event
* @returns Event with more widely supported Severity level strings
*/
_processLevels(event: Event): Event {
const processed: Event = {
...event,
level: event.level ? this._processLevel(event.level) : undefined,
breadcrumbs: event.breadcrumbs?.map(breadcrumb => ({
...breadcrumb,
level: breadcrumb.level
? this._processLevel(breadcrumb.level)
: undefined,
})),
};

return processed;
},

/**
* Convert js severity level which has critical and log to more widely supported levels.
* @param level
Expand Down

0 comments on commit 69f8171

Please sign in to comment.