Skip to content

Commit

Permalink
Feat: Add enableAppHangTracking and appHangTimeoutInterval. (#602)
Browse files Browse the repository at this point in the history
* add enableAppHangTracking flag

* add changelog

* nit changelog

* Update src/options.ts

* Update src/options.ts

---------

Co-authored-by: Kryštof Woldřich <31292499+krystofwoldrich@users.noreply.github.com>
  • Loading branch information
lucas-zimerman and krystofwoldrich authored Mar 20, 2024
1 parent d4f8a18 commit 24f6900
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
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 options for iOS: enableAppHangTracking and appHangTimeoutInterval, allowing users to define the App hang timeout or completly disabling it. ([#602](https://github.com/getsentry/sentry-capacitor/pull/602))

### Fixes

- Native integration would not properly disable when no DSN was provided ([#584](https://github.com/getsentry/sentry-capacitor/pull/584))
Expand Down
11 changes: 11 additions & 0 deletions src/nativeOptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Capacitor } from '@capacitor/core';

import type { CapacitorOptions } from './options';

/**
Expand Down Expand Up @@ -34,5 +36,14 @@ export function FilterNativeOptions(options: CapacitorOptions): CapacitorOptions
tracesSampleRate: options.tracesSampleRate,
// tunnel: options.tunnel: Only handled on the JavaScript Layer.
enableCaptureFailedRequests: options.enableCaptureFailedRequests,
...iOSParameters(options)
};
}

function iOSParameters(options: CapacitorOptions): CapacitorOptions
{
return Capacitor.getPlatform() === 'ios' ? {
enableAppHangTracking: options.enableAppHangTracking,
appHangTimeoutInterval: options.appHangTimeoutInterval
} : {}
}
22 changes: 22 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ export interface BaseCapacitorOptions{
* @default false
*/
enableCaptureFailedRequests?: boolean;

/**
* When enabled, the SDK tracks when the application stops responding for a specific amount of
* time defined by the `appHangTimeoutInterval` option.
*
* iOS only
*
* @default true
*/
enableAppHangTracking?: boolean;

/**
* The minimum amount of time an app should be unresponsive to be classified as an App Hanging.
* The actual amount may be a little longer.
* Avoid using values lower than 100ms, which may cause a lot of app hangs events being transmitted.
* Value should be in seconds.
*
* iOS only
*
* @default 2
*/
appHangTimeoutInterval?: number;
}

/**
Expand Down
39 changes: 39 additions & 0 deletions test/nativeOptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { Capacitor } from '@capacitor/core';
import type { Instrumenter, StackParser } from '@sentry/types';
import type { CapacitorOptions } from 'src';

import { FilterNativeOptions } from '../src/nativeOptions';

// Mock the Capacitor module
jest.mock('@capacitor/core', () => ({
Capacitor: {
getPlatform: jest.fn()
}
}));


describe('nativeOptions', () => {

test('Use value of enableOutOfMemoryTracking on enableWatchdogTerminationTracking when set true', async () => {
Expand Down Expand Up @@ -99,4 +109,33 @@ describe('nativeOptions', () => {

expect(keysFilter.toString()).toBe('');
});

test('Should include iOS parameters when running on iOS', async () => {
(Capacitor.getPlatform as jest.Mock).mockReturnValue('ios');

const expectedOptions: CapacitorOptions = {
environment: 'abc',
// iOS parameters
enableAppHangTracking: true,
appHangTimeoutInterval: 123
};
const nativeOptions = FilterNativeOptions(expectedOptions);
expect(JSON.stringify(nativeOptions)).toEqual(JSON.stringify(expectedOptions));
});

test('Should not include iOS parameters when running on android', async () => {
(Capacitor.getPlatform as jest.Mock).mockReturnValue('android');

const expectedOptions = {
environment: 'abc',
};
const nativeOptions = FilterNativeOptions({
...expectedOptions, ...{
appHangTimeoutInterval: 123,
enableAppHangTracking: true
}
});
expect(nativeOptions).toEqual(expectedOptions);
});

});

0 comments on commit 24f6900

Please sign in to comment.