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: App start time span no longer created if too long #3299

Merged
merged 10 commits into from
Oct 9, 2023
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

### Fixes

- App start time span no longer created if too long ([#3299](https://github.com/getsentry/sentry-react-native/pull/3299))

### Features

- Add Hermes Debug Info flag to React Native Context ([#3290](https://github.com/getsentry/sentry-react-native/pull/3290))
Expand Down
18 changes: 9 additions & 9 deletions src/js/tracing/reactnativetracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,15 @@ export class ReactNativeTracing implements Integration {
return;
}

const appStartDurationMilliseconds = this._appStartFinishTimestamp * 1000 - appStart.appStartTime;

// we filter out app start more than 60s.
// this could be due to many different reasons.
// we've seen app starts with hours, days and even months.
if (appStartDurationMilliseconds >= ReactNativeTracing._maxAppStart) {
return;
}

const appStartTimeSeconds = appStart.appStartTime / 1000;

lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
const op = appStart.isColdStart ? APP_START_COLD_OP : APP_START_WARM_OP;
Expand All @@ -385,15 +394,6 @@ export class ReactNativeTracing implements Integration {
endTimestamp: this._appStartFinishTimestamp,
});

const appStartDurationMilliseconds = this._appStartFinishTimestamp * 1000 - appStart.appStartTime;

// we filter out app start more than 60s.
// this could be due to many different reasons.
// we've seen app starts with hours, days and even months.
if (appStartDurationMilliseconds >= ReactNativeTracing._maxAppStart) {
return;
}

const measurement = appStart.isColdStart ? APP_START_COLD : APP_START_WARM;
transaction.setMeasurement(measurement, appStartDurationMilliseconds, 'millisecond');
}
Expand Down
37 changes: 37 additions & 0 deletions test/tracing/reactnativetracing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {
APP_START_WARM as APP_START_WARM_OP,
UI_LOAD,
} from '../../src/js/tracing';
import { APP_START_WARM as APP_SPAN_START_WARM } from '../../src/js/tracing/ops';
import { ReactNativeTracing } from '../../src/js/tracing/reactnativetracing';
import { getTimeOriginMilliseconds } from '../../src/js/tracing/utils';
import { NATIVE } from '../../src/js/wrapper';
Expand Down Expand Up @@ -209,6 +210,42 @@ describe('ReactNativeTracing', () => {
}
});

it('Does not add app start span if more than 60s', async () => {
const integration = new ReactNativeTracing();

const timeOriginMilliseconds = Date.now();
const appStartTimeMilliseconds = timeOriginMilliseconds - 65000;
const mockAppStartResponse: NativeAppStartResponse = {
isColdStart: false,
appStartTime: appStartTimeMilliseconds,
didFetchAppStart: false,
};

mockFunction(getTimeOriginMilliseconds).mockReturnValue(timeOriginMilliseconds);
mockFunction(NATIVE.fetchNativeAppStart).mockResolvedValue(mockAppStartResponse);

const mockHub = getMockHub();
integration.setupOnce(addGlobalEventProcessor, () => mockHub);

await jest.advanceTimersByTimeAsync(500);

const transaction = mockHub.getScope()?.getTransaction();

expect(transaction).toBeDefined();

if (transaction) {
expect(
// @ts-expect-error access private for test
transaction.spanRecorder,
).toBeDefined();

expect(
// @ts-expect-error access private for test
transaction.spanRecorder.spans.some(span => span.op == APP_SPAN_START_WARM),
).toBe(false);
}
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
});

it('Does not create app start transaction if didFetchAppStart == true', async () => {
const integration = new ReactNativeTracing();

Expand Down
Loading