Skip to content

Commit 4fd74a0

Browse files
Support enableAppHangTracking and appHangTimeoutInterval properties for App Hangs (#338)
* expose ios options * add changelog
1 parent e72bf80 commit 4fd74a0

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Features
6+
7+
- Add options for iOS: enableAppHangTracking and appHangTimeoutInterval, allowing users to define the App hang timeout or completly disabling it. ([#338](https://github.com/getsentry/sentry-cordova/pull/338))
8+
59
### Dependencies
610

711
- Bump `sentry-wizard` to 3.21.0 ([#544](https://github.com/getsentry/sentry-wizard/pull/544))

src/js/__tests__/sdk.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getGlobalObject } from '@sentry/utils';
22

33
import type { CordovaOptions } from '../options';
44
import * as Sdk from '../sdk';
5+
import { getPlatform } from '../utils';
56

67
const optionsTest: {
78
current?: CordovaOptions;
@@ -19,6 +20,15 @@ jest.mock('@sentry/core', () => {
1920
};
2021
});
2122

23+
jest.mock('../utils', () => {
24+
const util = jest.requireActual('../utils');
25+
26+
return {
27+
...util,
28+
getPlatform: jest.fn().mockReturnValue('android'),
29+
};
30+
});
31+
2232
describe('Tests SDK', () => {
2333
describe('init', () => {
2434
it('Uses SENTRY_RELEASE environment variable if present.', () => {
@@ -44,5 +54,42 @@ describe('Tests SDK', () => {
4454

4555
expect(optionsTest.current?.release).toBe('user-release');
4656
});
57+
58+
describe('ios Options', () => {
59+
60+
it('Should include iOS parameters when running on iOS', async () => {
61+
(getPlatform as jest.Mock).mockReturnValue('ios');
62+
63+
const expectedOptions: CordovaOptions = {
64+
environment: 'abc',
65+
// iOS parameters
66+
enableAppHangTracking: true,
67+
appHangTimeoutInterval: 123
68+
};
69+
70+
Sdk.init(expectedOptions);
71+
72+
expect(optionsTest.current?.appHangTimeoutInterval).toEqual(expectedOptions.appHangTimeoutInterval);
73+
expect(optionsTest.current?.enableAppHangTracking).toEqual(expectedOptions.enableAppHangTracking);
74+
});
75+
76+
it('Should not include iOS parameters when running on android', async () => {
77+
(getPlatform as jest.Mock).mockReturnValue('android');
78+
79+
const expectedOption = {
80+
environment: 'abc'
81+
}
82+
const unexpectedOptions = {
83+
appHangTimeoutInterval: 123,
84+
enableAppHangTracking: true
85+
};
86+
87+
Sdk.init({ ...unexpectedOptions, ...expectedOption });
88+
89+
expect(optionsTest.current).not.toContain(unexpectedOptions);
90+
expect(optionsTest.current?.environment).toEqual(expectedOption.environment);
91+
});
92+
})
93+
4794
});
4895
});

src/js/options.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,26 @@ export interface CordovaOptions
5050
* @deprecated The method will be removed on a major update, instead, use enableWatchdogTerminationTracking for the same result.
5151
* */
5252
enableOutOfMemoryTracking?: boolean;
53+
54+
/**
55+
* When enabled, the SDK tracks when the application stops responding for a specific amount of
56+
* time defined by the `appHangTimeoutInterval` option.
57+
*
58+
* iOS only
59+
*
60+
* @default true
61+
*/
62+
enableAppHangTracking?: boolean;
63+
64+
/**
65+
* The minimum amount of time an app should be unresponsive to be classified as an App Hanging.
66+
* The actual amount may be a little longer.
67+
* Avoid using values lower than 100ms, which may cause a lot of app hangs events being transmitted.
68+
* Value should be in seconds.
69+
*
70+
* iOS only
71+
*
72+
* @default 2
73+
*/
74+
appHangTimeoutInterval?: number;
5375
}

src/js/sdk.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { Cordova, EventOrigin, SdkInfo } from './integrations';
77
import type { CordovaOptions } from './options';
88
import { CordovaScope } from './scope';
99
import { makeCordovaTransport } from './transports/cordova';
10+
import { CordovaPlatformType } from './types';
11+
import { getPlatform } from './utils';
1012
import { NATIVE } from './wrapper';
1113
const DEFAULT_OPTIONS: CordovaOptions = {
1214
enableNative: true,
@@ -47,6 +49,11 @@ export function init(options: Partial<CordovaOptions>): void {
4749
if (finalOptions.enableNative === undefined) {
4850
finalOptions.enableNative = true;
4951
}
52+
53+
if (getPlatform() !== CordovaPlatformType.Ios) {
54+
delete finalOptions.appHangTimeoutInterval;
55+
delete finalOptions.enableAppHangTracking;
56+
}
5057
}
5158

5259
// Initialize a new hub using our scope with native sync

0 commit comments

Comments
 (0)