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: Set event.origin and event.environment tags #204

Merged
merged 4 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- build(ios): Bump `sentry-cocoa` to 6.2.1 (#205)
- feat(android): Add Android native bridge, full scope sync, and cached events (#202)
- feat: Set `event.origin` and `event.environment` tags (#204)

## v1.0.0-rc.0

Expand Down
29 changes: 29 additions & 0 deletions src/android/io/sentry/SentryCordova.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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.User;

public class SentryCordova extends CordovaPlugin {
Expand Down Expand Up @@ -196,6 +197,12 @@ private void startWithOptions(final JSONObject jsonOptions, final CallbackContex
options.setEnableNdk(false);
}

options.setBeforeSend((event, hint) -> {
setEventOriginTag(event);

return event;
});

sentryOptions = options;
} catch (JSONException e) {
logger.severe("Error parsing options JSON sent over native bridge.");
Expand Down Expand Up @@ -380,5 +387,27 @@ private SentryLevel getSentryLevelFromString(String level) {
}
}

private void setEventOriginTag(SentryEvent event) {
SdkVersion sdk = event.getSdk();
if (sdk != null) {
switch (sdk.getName()) {
// If the event is from cordova 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);
}

private void crash() { Sentry.captureException(new RuntimeException("TEST - Sentry Silent Client Crash")); }
}
34 changes: 24 additions & 10 deletions src/ios/SentryCordova.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
#import <Cordova/CDVAvailability.h>
@import Sentry;

NSString *const SentryCordovaVersionString = @"1.0.0-rc.0";
NSString *const SentryCordovaSdkName = @"sentry-cordova";

@implementation SentryCordova

- (void)pluginInitialize {
Expand All @@ -14,9 +11,10 @@ - (void)pluginInitialize {
- (void)startWithOptions:(CDVInvokedUrlCommand *)command {
NSDictionary *options = [command.arguments objectAtIndex:0];

SentryBeforeSendEventCallback beforeSend =
^SentryEvent *(SentryEvent *event) {
SentryBeforeSendEventCallback beforeSend = ^SentryEvent *(SentryEvent *event) {
[self setReleaseVersionDist:event];
[self setEventOriginTag:event];

return event;
};
[options setValue:beforeSend forKey:@"beforeSend"];
Expand All @@ -39,6 +37,27 @@ - (void)startWithOptions:(CDVInvokedUrlCommand *)command {
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}

- (void)setEventOriginTag:(SentryEvent *)event {
if (event.sdk != nil) {
NSString *sdkName = event.sdk[@"name"];

// If the event is from cordova js, it gets set there and we do not handle it here.
if ([sdkName isEqualToString:@"sentry.cocoa"]) {
[self setEventEnvironmentTag:event origin:@"ios" environment:@"native"];
}
}
}

- (void)setEventEnvironmentTag:(SentryEvent *)event origin:(NSString *)origin environment:(NSString *)environment {
NSMutableDictionary *newTags = [NSMutableDictionary new];
if (nil != event.tags) {
[newTags addEntriesFromDictionary:event.tags];
}
[newTags setValue:origin forKey:@"event.origin"];
[newTags setValue:environment forKey:@"event.environment"];
event.tags = newTags;
}

- (void)setReleaseVersionDist:(SentryEvent *)event {
if (event.extra[@"__sentry_version"]) {
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
Expand All @@ -54,11 +73,6 @@ - (void)setReleaseVersionDist:(SentryEvent *)event {
event.dist =
[NSString stringWithFormat:@"%@", event.extra[@"__sentry_dist"]];
}
event.sdk = @{
@"name" : SentryCordovaSdkName,
@"version" : SentryCordovaVersionString,
@"integrations" : @[ @"sentry-cocoa" ]
};
}

- (void)captureEnvelope:(CDVInvokedUrlCommand *)command {
Expand Down
6 changes: 6 additions & 0 deletions src/js/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export class CordovaClient extends BaseClient<CordovaBackend, CordovaOptions> {
version: SDK_VERSION,
};

if (!event.tags) {
event.tags = {};
}
event.tags['event.origin'] = 'cordova';
event.tags['event.environment'] = 'javascript';

return super._prepareEvent(event, scope, hint);
}
}