Skip to content

Commit

Permalink
Merge pull request #538 from DataDog/louiszawadzki/RUM-1359/fix-dupli…
Browse files Browse the repository at this point in the history
…cate-rum-crashes-ios-devices

Fix duplicate RUM Errors
  • Loading branch information
louiszawadzki authored Oct 25, 2023
2 parents 1743791 + e95d7fb commit 6b7b5f9
Show file tree
Hide file tree
Showing 21 changed files with 67 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/DdSdkReactNative.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { ProxyType } from './ProxyConfiguration';
import { SdkVerbosity } from './SdkVerbosity';
import type { TrackingConsent } from './TrackingConsent';
import { DdLogs } from './logs/DdLogs';
import { adaptLongTaskThreshold } from './longTasksUtils';
import { DdRum } from './rum/DdRum';
import { DdRumErrorTracking } from './rum/instrumentation/DdRumErrorTracking';
import { DdRumUserInteractionTracking } from './rum/instrumentation/interactionTracking/DdRumUserInteractionTracking';
Expand All @@ -37,6 +36,7 @@ import { DdSdk } from './sdk/DdSdk';
import { UserInfoSingleton } from './sdk/UserInfoSingleton/UserInfoSingleton';
import type { UserInfo } from './sdk/UserInfoSingleton/types';
import { DdSdkConfiguration } from './types';
import { adaptLongTaskThreshold } from './utils/longTasksUtils';
import { version as sdkVersion } from './version';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type {
import { DdRumErrorTracking } from '../../../rum/instrumentation/DdRumErrorTracking';
import { BufferSingleton } from '../../../sdk/DatadogProvider/Buffer/BufferSingleton';

jest.mock('../../../utils/jsUtils');

const DdRum = NativeModules.DdRum as DdNativeRumType;
const DdLogs = NativeModules.DdLogs as DdNativeLogsType;

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/rum/DdRum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import type { GestureResponderEvent } from 'react-native';

import { InternalLog } from '../InternalLog';
import { SdkVerbosity } from '../SdkVerbosity';
import { TimeProvider } from '../TimeProvider';
import type { DdNativeRumType } from '../nativeModulesTypes';
import { bufferVoidNativeCall } from '../sdk/DatadogProvider/Buffer/bufferNativeCall';
import { DdSdk } from '../sdk/DdSdk';
import { TimeProvider } from '../utils/TimeProvider';

import type { ActionEventMapper } from './eventMappers/actionEventMapper';
import { generateActionEventMapper } from './eventMappers/actionEventMapper';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/rum/__tests__/DdRum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { ErrorEventMapper } from '../eventMappers/errorEventMapper';
import type { ResourceEventMapper } from '../eventMappers/resourceEventMapper';
import { ErrorSource, PropagatorType, RumActionType } from '../types';

jest.mock('../../TimeProvider', () => {
jest.mock('../../utils/TimeProvider', () => {
return {
TimeProvider: jest.fn().mockImplementation(() => {
return { now: jest.fn().mockReturnValue(456) };
Expand Down
17 changes: 13 additions & 4 deletions packages/core/src/rum/instrumentation/DdRumErrorTracking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import type { ErrorHandlerCallback } from 'react-native';

import { InternalLog } from '../../InternalLog';
import { SdkVerbosity } from '../../SdkVerbosity';
import { DdLogs } from '../../logs/DdLogs';
import {
getErrorMessage,
getErrorStackTrace,
EMPTY_STACK_TRACE,
getErrorName,
DEFAULT_ERROR_NAME
} from '../../errorUtils';
import { DdLogs } from '../../logs/DdLogs';
} from '../../utils/errorUtils';
import { executeWithDelay } from '../../utils/jsUtils';
import { DdRum } from '../DdRum';
import { ErrorSource } from '../types';

Expand Down Expand Up @@ -73,10 +74,18 @@ export class DdRumErrorTracking {
this.reportError(message, ErrorSource.SOURCE, stacktrace, errorName, {
'_dd.error.is_crash': isFatal,
'_dd.error.raw': error
}).then(() => {
}).then(async () => {
DdRumErrorTracking.isInDefaultErrorHandler = true;
try {
DdRumErrorTracking.defaultErrorHandler(error, isFatal);
// On real iOS devices, the crash context is not updated soon
// enough for the view update to contain the crash.
// Waiting for 50ms has low impact and ensures the crash context
// is updated before actually crashing the app.
await executeWithDelay(
() =>
DdRumErrorTracking.defaultErrorHandler(error, isFatal),
50
);
} finally {
DdRumErrorTracking.isInDefaultErrorHandler = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import React from 'react';

import { InternalLog } from '../../../InternalLog';
import { SdkVerbosity } from '../../../SdkVerbosity';
import { getErrorMessage } from '../../../errorUtils';
import { DdSdk } from '../../../sdk/DdSdk';
import { getErrorMessage } from '../../../utils/errorUtils';

import { DdEventsInterceptor } from './DdEventsInterceptor';
import type EventsInterceptor from './EventsInterceptor';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright 2016-Present Datadog, Inc.
*/

import Timer from '../../../../../Timer';
import Timer from '../../../../../utils/Timer';
import { getTracingHeaders } from '../../distributedTracing/distributedTracingHeaders';
import type { DdRumResourceTracingAttributes } from '../../distributedTracing/distributedTracing';
import { getTracingAttributes } from '../../distributedTracing/distributedTracing';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import { InternalLog } from '../../../InternalLog';
import { SdkVerbosity } from '../../../SdkVerbosity';
import { getErrorStackTrace } from '../../../errorUtils';
import { DdSdk } from '../../../sdk/DdSdk';
import { getErrorStackTrace } from '../../../utils/errorUtils';

import { DatadogBuffer } from './DatadogBuffer';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { NativeModules } from 'react-native';

import { InitializationMode } from '../../../DdSdkReactNativeConfiguration';
import { DdSdkReactNative } from '../../../DdSdkReactNative';
import { TimeProvider } from '../../../TimeProvider';
import { DdRum } from '../../../rum/DdRum';
import { RumActionType } from '../../../rum/types';
import { DdTrace } from '../../../trace/DdTrace';
import { TimeProvider } from '../../../utils/TimeProvider';
import { BufferSingleton } from '../Buffer/BufferSingleton';
import {
DatadogProvider,
Expand All @@ -24,7 +24,7 @@ import {
renderWithProvider
} from './__utils__/renderWithProvider';

jest.mock('../../../TimeProvider', () => {
jest.mock('../../../utils/TimeProvider', () => {
const now = jest.fn();
return {
TimeProvider: jest.fn().mockImplementation(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { NativeModules } from 'react-native';

import { InitializationMode } from '../../../DdSdkReactNativeConfiguration';
import { DdSdkReactNative } from '../../../DdSdkReactNative';
import { TimeProvider } from '../../../TimeProvider';
import { DdRumUserInteractionTracking } from '../../../rum/instrumentation/interactionTracking/DdRumUserInteractionTracking';
import { XMLHttpRequestMock } from '../../../rum/instrumentation/resourceTracking/__tests__/__utils__/XMLHttpRequestMock';
import { TimeProvider } from '../../../utils/TimeProvider';
import { BufferSingleton } from '../Buffer/BufferSingleton';
import {
DatadogProvider,
Expand All @@ -25,7 +25,7 @@ import {
renderWithProviderAndAnimation
} from './__utils__/renderWithProvider';

jest.mock('../../../TimeProvider', () => {
jest.mock('../../../utils/TimeProvider', () => {
const now = jest.fn();
return {
TimeProvider: jest.fn().mockImplementation(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/trace/DdTrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

import { InternalLog } from '../InternalLog';
import { SdkVerbosity } from '../SdkVerbosity';
import { TimeProvider } from '../TimeProvider';
import type { DdNativeTraceType } from '../nativeModulesTypes';
import {
bufferNativeCallReturningId,
bufferNativeCallWithId
} from '../sdk/DatadogProvider/Buffer/bufferNativeCall';
import type { DdTraceType } from '../types';
import { TimeProvider } from '../utils/TimeProvider';

const timeProvider = new TimeProvider();

Expand Down
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions packages/core/src/utils/__mocks__/jsUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

/**
* This mock simplifies the tests by removing the timeout.
*/
export const executeWithDelay = async <T>(
callback: () => T,
delay: number
): Promise<T> => {
return new Promise<T>((resolve, reject) => {
try {
resolve(callback());
} catch (e) {
reject(e);
}
});
};
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions packages/core/src/utils/jsUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/

export const executeWithDelay = async <T>(
callback: () => T,
delay: number
): Promise<T> => {
return new Promise<T>((resolve, reject) => {
setTimeout(() => {
try {
resolve(callback());
} catch (e) {
reject(e);
}
}, delay);
});
};
File renamed without changes.

0 comments on commit 6b7b5f9

Please sign in to comment.