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(transport): Add buffer size option to native transport #2578

Merged
merged 15 commits into from
Nov 4, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Add `maxQueueSize` option ([#2578](https://github.com/getsentry/sentry-react-native/pull/2578))

### Dependencies

- Bump JavaScript SDK from v7.16.0 to v7.17.4 ([#2582](https://github.com/getsentry/sentry-react-native/pull/2582), [#2598](https://github.com/getsentry/sentry-react-native/pull/2598))
Expand Down
3 changes: 3 additions & 0 deletions android/src/main/java/io/sentry/react/RNSentryModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ public void initNativeSdk(final ReadableMap rnOptions, Promise promise) {
if (rnOptions.hasKey("sendDefaultPii")) {
options.setSendDefaultPii(rnOptions.getBoolean("sendDefaultPii"));
}
if (rnOptions.hasKey("maxQueueSize")) {
options.setMaxQueueSize(rnOptions.getInt("maxQueueSize"));
}

options.setBeforeSend((event, hint) -> {
// React native internally throws a JavascriptException
Expand Down
9 changes: 4 additions & 5 deletions src/js/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BrowserClient, defaultStackParser, makeFetchTransport } from '@sentry/browser';
import { BrowserTransportOptions } from '@sentry/browser/types/transports/types';
import { FetchImpl } from '@sentry/browser/types/transports/utils';
import { BaseClient } from '@sentry/core';
import {
Expand All @@ -18,8 +17,8 @@ import { dateTimestampInSeconds, logger, SentryError } from '@sentry/utils';
import { Alert, LogBox, YellowBox } from 'react-native';

import { defaultSdkInfo } from './integrations/sdkinfo';
import { ReactNativeClientOptions } from './options';
import { NativeTransport } from './transports/native';
import { ReactNativeClientOptions, ReactNativeTransportOptions } from './options';
import { makeReactNativeTransport } from './transports/native';
import { createUserFeedbackEnvelope, items } from './utils/envelope';
import { mergeOutcomes } from './utils/outcome';
import { NATIVE } from './wrapper';
Expand All @@ -42,9 +41,9 @@ export class ReactNativeClient extends BaseClient<ReactNativeClientOptions> {
*/
public constructor(options: ReactNativeClientOptions) {
if (!options.transport) {
options.transport = (options: BrowserTransportOptions, nativeFetch?: FetchImpl): Transport => {
options.transport = (options: ReactNativeTransportOptions, nativeFetch?: FetchImpl): Transport => {
if (NATIVE.isNativeTransportAvailable()) {
return new NativeTransport();
return makeReactNativeTransport(options);
}
return makeFetchTransport(options, nativeFetch);
};
Expand Down
16 changes: 14 additions & 2 deletions src/js/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,29 @@ export interface BaseReactNativeOptions {
* @default 2
*/
appHangTimeoutInterval?: number;

/**
* The max queue size for capping the number of envelopes waiting to be sent by Transport.
*/
maxQueueSize?: number;
}

export interface ReactNativeTransportOptions extends BrowserTransportOptions {
/**
* @deprecated use `maxQueueSize` in the root of the SDK options.
*/
bufferSize?: number;
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Configuration options for the Sentry ReactNative SDK.
* @see ReactNativeFrontend for more information.
*/

export interface ReactNativeOptions extends Options<BrowserTransportOptions>, BaseBrowserOptions, BaseReactNativeOptions {
export interface ReactNativeOptions extends Options<ReactNativeTransportOptions>, BaseBrowserOptions, BaseReactNativeOptions {
}

export interface ReactNativeClientOptions extends ClientOptions<BrowserTransportOptions>, BaseBrowserOptions, BaseReactNativeOptions {
export interface ReactNativeClientOptions extends ClientOptions<ReactNativeTransportOptions>, BaseBrowserOptions, BaseReactNativeOptions {
}


Expand Down
9 changes: 8 additions & 1 deletion src/js/sdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ReactNativeClientOptions, ReactNativeOptions, ReactNativeWrapperOptions
import { ReactNativeScope } from './scope';
import { TouchEventBoundary } from './touchevents';
import { ReactNativeProfiler, ReactNativeTracing } from './tracing';
import { makeReactNativeTransport } from './transports/native';
import { DEFAULT_BUFFER_SIZE, makeReactNativeTransport } from './transports/native';
import { makeUtf8TextEncoder } from './transports/TextEncoder';
import { safeFactory, safeTracesSampler } from './utils/safe';
import { RN_GLOBAL_OBJ } from './utils/worldwide';
Expand All @@ -43,6 +43,7 @@ const DEFAULT_OPTIONS: ReactNativeOptions = {
textEncoder: makeUtf8TextEncoder(),
},
sendClientReports: true,
maxQueueSize: DEFAULT_BUFFER_SIZE,
};

/**
Expand All @@ -52,6 +53,10 @@ export function init(passedOptions: ReactNativeOptions): void {
const reactNativeHub = new Hub(undefined, new ReactNativeScope());
makeMain(reactNativeHub);

const maxQueueSize = passedOptions.maxQueueSize
// eslint-disable-next-line deprecation/deprecation
?? passedOptions.transportOptions?.bufferSize
?? DEFAULT_OPTIONS.maxQueueSize;
const options: ReactNativeClientOptions = {
...DEFAULT_OPTIONS,
...passedOptions,
Expand All @@ -60,7 +65,9 @@ export function init(passedOptions: ReactNativeOptions): void {
transportOptions: {
...DEFAULT_OPTIONS.transportOptions,
...(passedOptions.transportOptions ?? {}),
bufferSize: maxQueueSize,
},
maxQueueSize,
integrations: [],
stackParser: stackParserFromStackParserOptions(passedOptions.stackParser || defaultStackParser),
beforeBreadcrumb: safeFactory(passedOptions.beforeBreadcrumb, { loggerMessage: 'The beforeBreadcrumb threw an error' }),
Expand Down
16 changes: 14 additions & 2 deletions src/js/transports/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ import { makePromiseBuffer, PromiseBuffer } from '@sentry/utils';

import { NATIVE } from '../wrapper';

export const DEFAULT_BUFFER_SIZE = 30;

export type BaseNativeTransport = BaseTransportOptions

export interface BaseNativeTransportOptions {
bufferSize?: number;
}

/** Native Transport class implementation */
export class NativeTransport implements Transport {
/** A simple buffer holding all requests. */
protected readonly _buffer: PromiseBuffer<void> = makePromiseBuffer(30);
protected readonly _buffer: PromiseBuffer<void>;

public constructor(options: BaseNativeTransportOptions = {}) {
this._buffer = makePromiseBuffer(options.bufferSize || DEFAULT_BUFFER_SIZE);
}

/**
* Sends the envelope to the Store endpoint in Sentry.
Expand All @@ -35,4 +45,6 @@ export class NativeTransport implements Transport {
/**
* Creates a Native Transport.
*/
export function makeReactNativeTransport(): NativeTransport { return new NativeTransport(); }
export function makeReactNativeTransport(options: BaseNativeTransportOptions = {}): NativeTransport {
return new NativeTransport(options);
}
32 changes: 31 additions & 1 deletion test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jest.spyOn(logger, 'error');

import { initAndBind } from '@sentry/core';
import { getCurrentHub } from '@sentry/react';
import { Integration, Scope } from '@sentry/types';
import { BaseTransportOptions,ClientOptions, Integration, Scope } from '@sentry/types';

import { ReactNativeClientOptions } from '../src/js/options';
import { configureScope,flush, init, withScope } from '../src/js/sdk';
Expand Down Expand Up @@ -179,6 +179,36 @@ describe('Tests the SDK functionality', () => {
}
});
});

describe('transport options buffer size', () => {
const usedOptions = (): ClientOptions<BaseTransportOptions> | undefined => {
return mockedInitAndBind.mock.calls[0]?.[1];
}

it('uses default transport options buffer size', () => {
init({
tracesSampleRate: 0.5,
enableAutoPerformanceTracking: true,
});
expect(usedOptions()?.transportOptions?.bufferSize).toBe(30);
});

it('uses custom transport options buffer size', () => {
init({
transportOptions: {
bufferSize: 99,
},
});
expect(usedOptions()?.transportOptions?.bufferSize).toBe(99);
});

it('uses max queue size', () => {
init({
maxQueueSize: 88,
});
expect(usedOptions()?.transportOptions?.bufferSize).toBe(88);
});
});
});

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