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 4 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Features
- Add buffer size option to native transport ([#2578](https://github.com/getsentry/sentry-react-native/pull/2578))
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved

### Dependencies

- Bump Android SDK from v6.5.0 to v6.6.0 ([#2572](https://github.com/getsentry/sentry-react-native/pull/2572))
Expand Down
6 changes: 6 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,12 @@ public void initNativeSdk(final ReadableMap rnOptions, Promise promise) {
if (rnOptions.hasKey("sendDefaultPii")) {
options.setSendDefaultPii(rnOptions.getBoolean("sendDefaultPii"));
}
if (rnOptions.hasKey("transportOptions")) {
final ReadableMap transportOptions = rnOptions.getMap("transportOptions");
if (transportOptions.hasKey("bufferSize")) {
options.setMaxQueueSize(rnOptions.getInt("bufferSize"));
}
}
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved

options.setBeforeSend((event, hint) -> {
// React native internally throws a JavascriptException
Expand Down
4 changes: 2 additions & 2 deletions src/js/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Alert, LogBox, YellowBox } from 'react-native';

import { defaultSdkInfo } from './integrations/sdkinfo';
import { ReactNativeClientOptions } from './options';
import { NativeTransport } from './transports/native';
import { makeReactNativeTransport } from './transports/native';
import { createUserFeedbackEnvelope, items } from './utils/envelope';
import { mergeOutcomes } from './utils/outcome';
import { NATIVE } from './wrapper';
Expand All @@ -44,7 +44,7 @@ export class ReactNativeClient extends BaseClient<ReactNativeClientOptions> {
if (!options.transport) {
options.transport = (options: BrowserTransportOptions, nativeFetch?: FetchImpl): Transport => {
if (NATIVE.isNativeTransportAvailable()) {
return new NativeTransport();
return makeReactNativeTransport(options);
}
return makeFetchTransport(options, nativeFetch);
};
Expand Down
3 changes: 2 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 @@ -40,6 +40,7 @@ const DEFAULT_OPTIONS: ReactNativeOptions = {
enableOutOfMemoryTracking: true,
patchGlobalPromise: true,
transportOptions: {
bufferSize: DEFAULT_BUFFER_SIZE,
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved
textEncoder: makeUtf8TextEncoder(),
},
sendClientReports: true,
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);
}
25 changes: 24 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,29 @@ 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);
});
});
});

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