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

chore(sdk): Better DEX when native disabled #2897

Merged
merged 4 commits into from
Mar 15, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Fixes

- Fix use Fetch transport when option `enableNative` is `false` ([#2897](https://github.com/getsentry/sentry-react-native/pull/2897))
- Improve logs when `enableNative` is `false` ([#2897](https://github.com/getsentry/sentry-react-native/pull/2897))

### Dependencies

- Bump JavaScript SDK from v7.40.0 to v7.43.0 ([#2874](https://github.com/getsentry/sentry-react-native/pull/2874))
Expand Down
8 changes: 7 additions & 1 deletion src/js/sdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ export function init(passedOptions: ReactNativeOptions): void {
...DEFAULT_OPTIONS,
...passedOptions,
// If custom transport factory fails the SDK won't initialize
transport: passedOptions.transport || makeNativeTransportFactory() || makeFetchTransport,
transport: passedOptions.transport
|| makeNativeTransportFactory({
enableNative: passedOptions.enableNative !== undefined
? passedOptions.enableNative
: DEFAULT_OPTIONS.enableNative
})
|| makeFetchTransport,
transportOptions: {
...DEFAULT_OPTIONS.transportOptions,
...(passedOptions.transportOptions ?? {}),
Expand Down
4 changes: 4 additions & 0 deletions src/js/tracing/nativeframes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export class NativeFramesInstrumentation {
if (framesMetrics) {
transaction.setData('__startFrames', framesMetrics);
}
}).then(undefined, (error) => {
logger.error(`[ReactNativeTracing] Error while fetching native frames: ${error}`);
});

instrumentChildSpanFinish(transaction, (_: Span, endTimestamp?: number) => {
Expand Down Expand Up @@ -85,6 +87,8 @@ export class NativeFramesInstrumentation {
nativeFrames,
};
}
}).then(undefined, (error) => {
logger.error(`[ReactNativeTracing] Error while fetching native frames: ${error}`);
});
}

Expand Down
6 changes: 4 additions & 2 deletions src/js/transports/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ export function makeNativeTransport(options: BaseNativeTransportOptions = {}): N
/**
* Creates a Native Transport factory if the native transport is available.
*/
export function makeNativeTransportFactory(): typeof makeNativeTransport | null {
if (NATIVE.isNativeTransportAvailable()) {
export function makeNativeTransportFactory(
{ enableNative }: { enableNative?: boolean },
): typeof makeNativeTransport | null {
if (enableNative && NATIVE.isNativeAvailable()) {
return makeNativeTransport;
}
return null;
Expand Down
16 changes: 10 additions & 6 deletions src/js/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ interface SentryNativeWrapper {
): module is Spec;
_getBreadcrumbs(event: Event): Breadcrumb[] | undefined;

isNativeTransportAvailable(): boolean;
isNativeAvailable(): boolean;

initNativeSdk(options: ReactNativeOptions): PromiseLike<boolean>;
closeNativeSdk(): PromiseLike<void>;
Expand Down Expand Up @@ -267,10 +267,12 @@ export const NATIVE: SentryNativeWrapper = {

async fetchNativeAppStart(): Promise<NativeAppStartResponse | null> {
if (!this.enableNative) {
throw this._DisabledNativeError;
logger.warn(this._DisabledNativeError);
return null;
}
if (!this._isModuleLoaded(RNSentry)) {
throw this._NativeClientError;
logger.error(this._NativeClientError);
return null;
}

return RNSentry.fetchNativeAppStart();
Expand Down Expand Up @@ -468,16 +470,18 @@ export const NATIVE: SentryNativeWrapper = {
RNSentry.enableNativeFramesTracking();
},

isNativeTransportAvailable(): boolean {
isNativeAvailable(): boolean {
return this.enableNative && this._isModuleLoaded(RNSentry);
},

async captureScreenshot(): Promise<Screenshot[] | null> {
if (!this.enableNative) {
throw this._DisabledNativeError;
logger.warn(this._DisabledNativeError);
return null;
}
if (!this._isModuleLoaded(RNSentry)) {
throw this._NativeClientError;
logger.error(this._NativeClientError);
return null;
}

try {
Expand Down
20 changes: 18 additions & 2 deletions test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,32 @@ describe('Tests the SDK functionality', () => {
});

it('uses native transport', () => {
(NATIVE.isNativeTransportAvailable as jest.Mock).mockImplementation(() => true);
(NATIVE.isNativeAvailable as jest.Mock).mockImplementation(() => true);
init({});
expect(usedOptions()?.transport).toEqual(makeNativeTransport);
});

it('uses fallback fetch transport', () => {
(NATIVE.isNativeTransportAvailable as jest.Mock).mockImplementation(() => false);
(NATIVE.isNativeAvailable as jest.Mock).mockImplementation(() => false);
init({});
expect(usedOptions()?.transport).toEqual(makeFetchTransport);
});

it('checks sdk options first', () => {
(NATIVE.isNativeAvailable as jest.Mock).mockImplementation(() => true);
init({ enableNative: false });
expect(usedOptions()?.transport).toEqual(makeFetchTransport);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(NATIVE.isNativeAvailable).not.toBeCalled();
});

it('check both options and native availability', () => {
(NATIVE.isNativeAvailable as jest.Mock).mockImplementation(() => true);
init({ enableNative: true });
expect(usedOptions()?.transport).toEqual(makeNativeTransport);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(NATIVE.isNativeAvailable).toBeCalled();
});
});

describe('initIsSafe', () => {
Expand Down