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

fix(ios): Don't pass enableTracing from RN to sentry-cocoa options #3042

Merged
merged 10 commits into from
May 5, 2023
Merged
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Unreleased

### Fixes

- Fix `event.origin` and `event.environment` on unhandled exception on iOS ([#3041](https://github.com/getsentry/sentry-react-native/pull/3041))
- Don't pass `enableTracing` from RN to `sentry-cocoa` options ([#3042](https://github.com/getsentry/sentry-react-native/pull/3042))

## 5.4.1

### Fixes
Expand Down
52 changes: 43 additions & 9 deletions RNSentryTester/RNSentryTesterTests/RNSentry+initNativeSdk.mm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import <Sentry/SentryOptions.h>
#import <Sentry/SentryEvent.h>
#import "RNSentry.h"

@interface RNSentryInitNativeSdkTests : XCTestCase
Expand All @@ -17,8 +18,11 @@ - (void)testCreateOptionsWithDictionaryRemovesPerformanceProperties
NSDictionary *_Nonnull mockedReactNativeDictionary = @{
@"dsn": @"https://[email protected]/123456",
@"beforeSend": @"will_be_overwritten",
@"enableNativeCrashHandling": @YES,

@"tracesSampleRate": @1,
@"tracesSampler": ^(SentrySamplingContext *_Nonnull samplingContext) {
return @1;
},
@"enableTracing": @YES,
};
SentryOptions* actualOptions = [rnSentry createOptionsWithDictionary:mockedReactNativeDictionary error:&error];

Expand All @@ -27,6 +31,7 @@ - (void)testCreateOptionsWithDictionaryRemovesPerformanceProperties
XCTAssertNotNil(actualOptions.beforeSend, @"Before send is overwriten by the native RNSentry implementation");
XCTAssertEqual(actualOptions.tracesSampleRate, nil, @"Traces sample rate should not be passed to native");
XCTAssertEqual(actualOptions.tracesSampler, nil, @"Traces sampler should not be passed to native");
XCTAssertEqual(actualOptions.enableTracing, false, @"EnableTracing should not be passed to native");
}

- (void)testCreateOptionsWithDictionaryNativeCrashHandlingDefault
Expand All @@ -43,7 +48,7 @@ - (void)testCreateOptionsWithDictionaryNativeCrashHandlingDefault
XCTAssertEqual([actualOptions.integrations containsObject:@"SentryCrashIntegration"], true, @"Did not set native crash handling");
}

- (void)testCreateOptionsWithDictionaryPerformanceTrackingDefault
- (void)testCreateOptionsWithDictionaryAutoPerformanceTracingDefault
{
RNSentry * rnSentry = [[RNSentry alloc] init];
NSError* error = nil;
Expand Down Expand Up @@ -72,15 +77,14 @@ - (void)testCreateOptionsWithDictionaryNativeCrashHandlingEnabled
XCTAssertEqual([actualOptions.integrations containsObject:@"SentryCrashIntegration"], true, @"Did not set native crash handling");
}

- (void)testCreateOptionsWithDictionaryPerformanceTrackingEnabled
- (void)testCreateOptionsWithDictionaryAutoPerformanceTracingEnabled
{
RNSentry * rnSentry = [[RNSentry alloc] init];
NSError* error = nil;

NSDictionary *_Nonnull mockedReactNativeDictionary = @{
@"dsn": @"https://[email protected]/123456",
@"enableAutoPerformanceTracing": @YES,

};
SentryOptions* actualOptions = [rnSentry createOptionsWithDictionary:mockedReactNativeDictionary error:&error];
XCTAssertNotNil(actualOptions, @"Did not create sentry options");
Expand All @@ -96,23 +100,21 @@ - (void)testCreateOptionsWithDictionaryNativeCrashHandlingDisabled
NSDictionary *_Nonnull mockedReactNativeDictionary = @{
@"dsn": @"https://[email protected]/123456",
@"enableNativeCrashHandling": @NO,

};
SentryOptions* actualOptions = [rnSentry createOptionsWithDictionary:mockedReactNativeDictionary error:&error];
XCTAssertNotNil(actualOptions, @"Did not create sentry options");
XCTAssertNil(error, @"Should not pass no error");
XCTAssertEqual([actualOptions.integrations containsObject:@"SentryCrashIntegration"], false, @"Did not disable native crash handling");
}

- (void)testCreateOptionsWithDictionaryPerformanceTrackingDisabled
- (void)testCreateOptionsWithDictionaryAutoPerformanceTracingDisabled
{
RNSentry * rnSentry = [[RNSentry alloc] init];
NSError* error = nil;

NSDictionary *_Nonnull mockedReactNativeDictionary = @{
@"dsn": @"https://[email protected]/123456",
@"enableAutoPerformanceTracing": @NO,

};
SentryOptions* actualOptions = [rnSentry createOptionsWithDictionary:mockedReactNativeDictionary error:&error];
XCTAssertNotNil(actualOptions, @"Did not create sentry options");
Expand All @@ -126,12 +128,44 @@ - (void)testPassesErrorOnWrongDsn
NSError* error = nil;

NSDictionary *_Nonnull mockedReactNativeDictionary = @{
@"dsn": @"not_a_valid_dsn"
@"dsn": @"not_a_valid_dsn",
};
SentryOptions* actualOptions = [rnSentry createOptionsWithDictionary:mockedReactNativeDictionary error:&error];

XCTAssertNil(actualOptions, @"Created invalid sentry options");
XCTAssertNotNil(error, @"Did not created error on invalid dsn");
}

- (void)testEventFromSentryCocoaReactNativeHasOriginAndEnvironmentTags
{
RNSentry* rnSentry = [[RNSentry alloc] init];
SentryEvent* testEvent = [[SentryEvent alloc] init];
testEvent.sdk = @{
@"name": @"sentry.cocoa.react-native",
};

[rnSentry setEventOriginTag: testEvent];

XCTAssertEqual(testEvent.tags[@"event.origin"], @"ios");
XCTAssertEqual(testEvent.tags[@"event.environment"], @"native");
}

- (void)testEventFromSentryReactNativeOriginAndEnvironmentTagsAreOverwritten
{
RNSentry* rnSentry = [[RNSentry alloc] init];
SentryEvent* testEvent = [[SentryEvent alloc] init];
testEvent.sdk = @{
@"name": @"sentry.cocoa.react-native",
};
testEvent.tags = @{
@"event.origin": @"testEventOriginTag",
@"event.environment": @"testEventEnvironmentTag",
};

[rnSentry setEventOriginTag: testEvent];

XCTAssertEqual(testEvent.tags[@"event.origin"], @"ios");
XCTAssertEqual(testEvent.tags[@"event.environment"], @"native");
}

@end
2 changes: 2 additions & 0 deletions ios/RNSentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
- (SentryOptions *_Nullable)createOptionsWithDictionary:(NSDictionary *_Nonnull)options
error:(NSError *_Nullable*_Nonnull)errorPointer;

- (void)setEventOriginTag:(SentryEvent *)event;

@end
12 changes: 7 additions & 5 deletions ios/RNSentry.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ + (void)storeEnvelope:(SentryEnvelope *)envelope;

static bool didFetchAppStart;

static NSString* const nativeClientName = @"sentry.cocoa.react-native";

@implementation RNSentry {
bool sentHybridSdkDidBecomeActive;
}
Expand All @@ -56,9 +58,8 @@ + (BOOL)requiresMainQueueSetup {
return;
}

NSString *sdkName = @"sentry.cocoa.react-native";
NSString *sdkVersion = [PrivateSentrySDKOnly getSdkVersionString];
[PrivateSentrySDKOnly setSdkName: sdkName andVersionString: sdkVersion];
[PrivateSentrySDKOnly setSdkName: nativeClientName andVersionString: sdkVersion];

[SentrySDK startWithOptions:sentryOptions];

Expand Down Expand Up @@ -105,6 +106,7 @@ - (SentryOptions *_Nullable)createOptionsWithDictionary:(NSDictionary *_Nonnull)
// The user could tho initialize the SDK manually and set themselves.
[mutableOptions removeObjectForKey:@"tracesSampleRate"];
[mutableOptions removeObjectForKey:@"tracesSampler"];
[mutableOptions removeObjectForKey:@"enableTracing"];

SentryOptions *sentryOptions = [[SentryOptions alloc] initWithDict:mutableOptions didFailWithError:errorPointer];
if (*errorPointer != nil) {
Expand Down Expand Up @@ -137,9 +139,9 @@ - (void)setEventOriginTag:(SentryEvent *)event {
if (event.sdk != nil) {
NSString *sdkName = event.sdk[@"name"];

// If the event is from react native, it gets set there and we do not handle
// it here.
if ([sdkName isEqualToString:@"sentry.cocoa"]) {
// If the event is from react native, it gets set
// there and we do not handle it here.
if ([sdkName isEqual:nativeClientName]) {
[self setEventEnvironmentTag:event origin:@"ios" environment:@"native"];
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"jest-environment-jsdom": "^29.4.1",
"prettier": "^2.0.5",
"react": "18.2.0",
"react-native": "0.71.0",
"react-native": "0.71.7",
"replace-in-file": "^6.0.0",
"rimraf": "^4.1.1",
"ts-jest": "^29.0.5",
Expand Down
Loading