diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fc02fb4..f842408e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/src/nativeOptions.ts b/src/nativeOptions.ts index d166e897..c2968d1a 100644 --- a/src/nativeOptions.ts +++ b/src/nativeOptions.ts @@ -1,3 +1,5 @@ +import { Capacitor } from '@capacitor/core'; + import type { CapacitorOptions } from './options'; /** @@ -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 + } : {} +} diff --git a/src/options.ts b/src/options.ts index 4992a0b0..5fb50039 100644 --- a/src/options.ts +++ b/src/options.ts @@ -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; } /** diff --git a/test/nativeOptions.test.ts b/test/nativeOptions.test.ts index ab1823d7..30e5d005 100644 --- a/test/nativeOptions.test.ts +++ b/test/nativeOptions.test.ts @@ -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 () => { @@ -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); + }); + });