@datadog/mobile-react-native 1.0.0-beta4 → 1.0.0-rc4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/DatadogSDKReactNative.podspec +1 -1
  2. package/README.md +73 -1
  3. package/android/build.gradle +2 -3
  4. package/android/src/main/java/com/datadog/reactnative/DdRum.kt +25 -24
  5. package/android/src/main/java/com/datadog/reactnative/DdTrace.kt +6 -6
  6. package/ios/Sources/DdRum.m +10 -9
  7. package/ios/Sources/DdRum.swift +24 -24
  8. package/ios/Sources/DdTrace.m +2 -2
  9. package/ios/Sources/DdTrace.swift +6 -6
  10. package/ios/Sources/QueueHolder.swift +1 -1
  11. package/lib/commonjs/DdSdkReactNative.js +57 -0
  12. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  13. package/lib/commonjs/DdSdkReactNativeConfiguration.js +8 -0
  14. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  15. package/lib/commonjs/NativeSdkVerbosity.js +26 -0
  16. package/lib/commonjs/NativeSdkVerbosity.js.map +1 -0
  17. package/lib/commonjs/ProxyConfiguration.js +53 -0
  18. package/lib/commonjs/ProxyConfiguration.js.map +1 -0
  19. package/lib/commonjs/Timer.js +99 -0
  20. package/lib/commonjs/Timer.js.map +1 -0
  21. package/lib/commonjs/TrackingConsent.js +6 -0
  22. package/lib/commonjs/TrackingConsent.js.map +1 -1
  23. package/lib/commonjs/foundation.js +88 -8
  24. package/lib/commonjs/foundation.js.map +1 -1
  25. package/lib/commonjs/index.js +14 -0
  26. package/lib/commonjs/index.js.map +1 -1
  27. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +51 -11
  28. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  29. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +38 -6
  30. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  31. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +103 -18
  32. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  33. package/lib/commonjs/types.js.map +1 -1
  34. package/lib/commonjs/version.js +10 -0
  35. package/lib/commonjs/version.js.map +1 -0
  36. package/lib/module/DdSdkReactNative.js +56 -1
  37. package/lib/module/DdSdkReactNative.js.map +1 -1
  38. package/lib/module/DdSdkReactNativeConfiguration.js +9 -1
  39. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  40. package/lib/module/NativeSdkVerbosity.js +18 -0
  41. package/lib/module/NativeSdkVerbosity.js.map +1 -0
  42. package/lib/module/ProxyConfiguration.js +46 -0
  43. package/lib/module/ProxyConfiguration.js.map +1 -0
  44. package/lib/module/Timer.js +90 -0
  45. package/lib/module/Timer.js.map +1 -0
  46. package/lib/module/TrackingConsent.js +6 -0
  47. package/lib/module/TrackingConsent.js.map +1 -1
  48. package/lib/module/foundation.js +89 -3
  49. package/lib/module/foundation.js.map +1 -1
  50. package/lib/module/index.js +2 -1
  51. package/lib/module/index.js.map +1 -1
  52. package/lib/module/rum/instrumentation/DdEventsInterceptor.js +51 -11
  53. package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  54. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +37 -6
  55. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  56. package/lib/module/rum/instrumentation/DdRumResourceTracking.js +96 -18
  57. package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  58. package/lib/module/types.js.map +1 -1
  59. package/lib/module/version.js +3 -0
  60. package/lib/module/version.js.map +1 -0
  61. package/lib/typescript/DdSdkReactNative.d.ts +11 -0
  62. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +6 -0
  63. package/lib/typescript/NativeSdkVerbosity.d.ts +9 -0
  64. package/lib/typescript/ProxyConfiguration.d.ts +56 -0
  65. package/lib/typescript/Timer.d.ts +20 -0
  66. package/lib/typescript/index.d.ts +2 -1
  67. package/lib/typescript/rum/instrumentation/DdEventsInterceptor.d.ts +2 -0
  68. package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +2 -0
  69. package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +4 -0
  70. package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +2 -2
  71. package/lib/typescript/types.d.ts +25 -24
  72. package/lib/typescript/version.d.ts +1 -0
  73. package/package.json +12 -13
  74. package/src/DdSdkReactNative.tsx +47 -0
  75. package/src/DdSdkReactNativeConfiguration.tsx +7 -1
  76. package/src/NativeSdkVerbosity.tsx +15 -0
  77. package/src/ProxyConfiguration.tsx +37 -0
  78. package/src/Timer.ts +96 -0
  79. package/src/TrackingConsent.tsx +6 -0
  80. package/src/__tests__/DdRumWrapper.test.tsx +91 -0
  81. package/src/__tests__/DdSdkReactNative.test.tsx +541 -0
  82. package/src/__tests__/Timer.test.ts +145 -0
  83. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +222 -0
  84. package/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx +314 -0
  85. package/src/__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx +677 -0
  86. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +188 -0
  87. package/src/__tests__/rum/instrumentation/TraceIdentifier.test.tsx +33 -0
  88. package/src/foundation.tsx +83 -3
  89. package/src/index.tsx +3 -2
  90. package/src/rum/instrumentation/DdEventsInterceptor.tsx +40 -10
  91. package/src/rum/instrumentation/DdRumErrorTracking.tsx +56 -25
  92. package/src/rum/instrumentation/DdRumResourceTracking.tsx +92 -22
  93. package/src/rum/instrumentation/DdRumXhr.tsx +6 -5
  94. package/src/types.tsx +25 -24
  95. package/src/version.ts +2 -0
  96. package/android/.project +0 -17
  97. package/android/.settings/org.eclipse.buildship.core.prefs +0 -13
  98. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  99. package/android/gradle/wrapper/gradle-wrapper.properties +0 -6
  100. package/android/gradlew +0 -185
  101. package/android/gradlew.bat +0 -104
  102. package/ios/DdSdkReactNative.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +0 -8
  103. package/ios/Tests/ModulesTest.swift +0 -43
@@ -0,0 +1,188 @@
1
+ /*
2
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
+ * Copyright 2016-Present Datadog, Inc.
5
+ */
6
+
7
+ import React from 'react';
8
+ import { DdRumUserInteractionTracking } from '../../../rum/instrumentation/DdRumUserInteractionTracking'
9
+ import { DdEventsInterceptor } from '../../../rum/instrumentation/DdEventsInterceptor'
10
+ import { View, Text, Button, TouchableOpacity, TouchableHighlight, TouchableNativeFeedback, TouchableWithoutFeedback } from 'react-native';
11
+ import { render, fireEvent } from '@testing-library/react-native';
12
+ import { StyleSheet } from 'react-native';
13
+
14
+ const styles = StyleSheet.create({
15
+ button: {
16
+ alignItems: "center",
17
+ backgroundColor: "#DDDDDD",
18
+ padding: 10
19
+ },
20
+ });
21
+
22
+ jest.mock('../../../rum/instrumentation/DdEventsInterceptor');
23
+
24
+
25
+ // Silence the warning https://github.com/facebook/react-native/issues/11094#issuecomment-263240420
26
+ jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper')
27
+ jest.useFakeTimers()
28
+
29
+ let originalMethod: Function
30
+
31
+ beforeEach(() => {
32
+ originalMethod = React.createElement
33
+ jest.setTimeout(20000)
34
+ })
35
+
36
+ afterEach(() => {
37
+ DdRumUserInteractionTracking['isTracking'] = false
38
+ React.createElement = originalMethod
39
+ })
40
+
41
+ // Because the way RN decouples the component we cannot assert how many times the `interceptOnPress` function was called.
42
+ // This problem is being handled at the EventInterceptor level.
43
+
44
+ it('M intercept and send a RUM event W onPress { Button component }', async () => {
45
+ // GIVEN
46
+ DdRumUserInteractionTracking.startTracking()
47
+ const { getByText } = render((
48
+ <View>
49
+ <Button
50
+ title="Click me"
51
+ accessibilityLabel="click_me_button"
52
+ onPress={(event) => { }}
53
+ />
54
+ </View>
55
+ ))
56
+ const testButton = getByText("Click me")
57
+
58
+ // WHEN
59
+ fireEvent(testButton, "press")
60
+
61
+ // THEN
62
+ const currentMockedEventsInterceptor = DdEventsInterceptor.mock.instances[DdEventsInterceptor.mock.instances.length - 1];
63
+ const mockedInterceptOnPressFunction = currentMockedEventsInterceptor.interceptOnPress;
64
+ expect(mockedInterceptOnPressFunction).toBeCalled()
65
+ })
66
+
67
+ it('M intercept only once W startTracking { called multiple times }', async () => {
68
+ // GIVEN
69
+ DdRumUserInteractionTracking.startTracking()
70
+ DdRumUserInteractionTracking.startTracking()
71
+ DdRumUserInteractionTracking.startTracking()
72
+ const { getByText } = render((
73
+ <View>
74
+ <Button
75
+ title="Click me"
76
+ accessibilityLabel="click_me_button"
77
+ onPress={(event) => { }}
78
+ />
79
+ </View>
80
+ ))
81
+ const testButton = getByText("Click me")
82
+
83
+ // WHEN
84
+ fireEvent(testButton, "press")
85
+
86
+ // THEN
87
+ const currentMockedEventsInterceptor = DdEventsInterceptor.mock.instances[DdEventsInterceptor.mock.instances.length - 1];
88
+ const mockedInterceptOnPressFunction = currentMockedEventsInterceptor.interceptOnPress;
89
+ expect(mockedInterceptOnPressFunction).toBeCalled()
90
+ })
91
+
92
+ it('M intercept and send a RUM event W onPress { TouchableOpacity component }', async () => {
93
+ // GIVEN
94
+ DdRumUserInteractionTracking.startTracking()
95
+ const { getByText } = render((
96
+ <View>
97
+ <TouchableOpacity
98
+ style={styles.button}
99
+ accessibilityLabel="click_me_touchable_opacity"
100
+ onPress={(event) => { }}
101
+ >
102
+ <Text>Click me</Text>
103
+ </TouchableOpacity>
104
+ </View>
105
+ ))
106
+ const testButton = getByText("Click me")
107
+
108
+ // WHEN
109
+ fireEvent(testButton, "press")
110
+
111
+ // THEN
112
+ const currentMockedEventsInterceptor = DdEventsInterceptor.mock.instances[DdEventsInterceptor.mock.instances.length - 1];
113
+ const mockedInterceptOnPressFunction = currentMockedEventsInterceptor.interceptOnPress;
114
+ expect(mockedInterceptOnPressFunction).toBeCalled()
115
+ })
116
+
117
+ it('M intercept and send a RUM event W onPress { TouchableHighlight component }', async () => {
118
+ // GIVEN
119
+ DdRumUserInteractionTracking.startTracking()
120
+ const { getByText } = render((
121
+ <View>
122
+ <TouchableHighlight
123
+ onPress={(event) => { }}
124
+ underlayColor="white">
125
+ <View style={styles.button}>
126
+ <Text>Click me</Text>
127
+ </View>
128
+ </TouchableHighlight>
129
+ </View>
130
+ ))
131
+ const testButton = getByText("Click me")
132
+
133
+ // WHEN
134
+ fireEvent(testButton, "press")
135
+
136
+ // THEN
137
+ const currentMockedEventsInterceptor = DdEventsInterceptor.mock.instances[DdEventsInterceptor.mock.instances.length - 1];
138
+ const mockedInterceptOnPressFunction = currentMockedEventsInterceptor.interceptOnPress;
139
+ expect(mockedInterceptOnPressFunction).toBeCalled()
140
+ })
141
+
142
+ it('M intercept and send a RUM event W onPress { TouchableNativeFeedback component }', async () => {
143
+ // GIVEN
144
+ DdRumUserInteractionTracking.startTracking()
145
+ const { getByText } = render((
146
+ <View>
147
+ <TouchableNativeFeedback
148
+ onPress={(event) => { }}>
149
+ <View style={styles.button}>
150
+ <Text style={styles.button}>Click me</Text>
151
+ </View>
152
+ </TouchableNativeFeedback>
153
+ </View>
154
+ ))
155
+ const testButton = getByText("Click me")
156
+
157
+ // WHEN
158
+ fireEvent(testButton, "press")
159
+
160
+ // THEN
161
+ const currentMockedEventsInterceptor = DdEventsInterceptor.mock.instances[DdEventsInterceptor.mock.instances.length - 1];
162
+ const mockedInterceptOnPressFunction = currentMockedEventsInterceptor.interceptOnPress;
163
+ expect(mockedInterceptOnPressFunction).toBeCalled()
164
+ })
165
+
166
+ it('M intercept and send a RUM event W onPress { TouchableWithoutFeedback component }', async () => {
167
+ // GIVEN
168
+ DdRumUserInteractionTracking.startTracking()
169
+ const { getByText } = render((
170
+ <View>
171
+ <TouchableWithoutFeedback
172
+ onPress={(event) => { }}>
173
+ <View style={styles.button}>
174
+ <Text style={styles.button}>Click me</Text>
175
+ </View>
176
+ </TouchableWithoutFeedback>
177
+ </View>
178
+ ))
179
+ const testButton = getByText("Click me")
180
+
181
+ // WHEN
182
+ fireEvent(testButton, "press")
183
+
184
+ // THEN
185
+ const currentMockedEventsInterceptor = DdEventsInterceptor.mock.instances[DdEventsInterceptor.mock.instances.length - 1];
186
+ const mockedInterceptOnPressFunction = currentMockedEventsInterceptor.interceptOnPress;
187
+ expect(mockedInterceptOnPressFunction).toBeCalled()
188
+ })
@@ -0,0 +1,33 @@
1
+ import { generateTraceId } from '../../../rum/instrumentation/TraceIdentifier'
2
+
3
+
4
+ it('M return an unique identifier W toString', async () => {
5
+ // GIVEN
6
+ const generatedIds = new Set<String>()
7
+ const iterations = 100
8
+ var counter = iterations
9
+
10
+ // WHEN
11
+ while (counter-- > 0) {
12
+ generatedIds.add(generateTraceId())
13
+ }
14
+
15
+ // THEN
16
+ expect(generatedIds.size).toBe(iterations)
17
+ })
18
+
19
+ it('M return an 64 bits positive integer W toString', async () => {
20
+ let iterations = 100
21
+ while (iterations-- > 0) {
22
+ // GIVEN
23
+ const id = generateTraceId()
24
+
25
+ // THEN
26
+ expect(id).toMatch(/[1-9]{1,19}/)
27
+ // should be less than the max 64 bits integer
28
+ if (id.length == 19) {
29
+ expect(id < "9223372036854775807").toBeTruthy()
30
+ }
31
+ }
32
+
33
+ })
@@ -7,9 +7,89 @@
7
7
  import { NativeModules } from 'react-native';
8
8
  import { DdSdkConfiguration, DdSdkType, DdLogsType, DdTraceType, DdRumType } from './types';
9
9
 
10
+ class DdLogsWrapper implements DdLogsType {
11
+
12
+ private nativeLogs: DdLogsType = NativeModules.DdLogs;
13
+
14
+ debug(message: string, context: object = {}): Promise<void> {
15
+ return this.nativeLogs.debug(message, context);
16
+ }
17
+
18
+ info(message: string, context: object = {}): Promise<void> {
19
+ return this.nativeLogs.info(message, context);
20
+ }
21
+
22
+ warn(message: string, context: object = {}): Promise<void> {
23
+ return this.nativeLogs.warn(message, context);
24
+ }
25
+
26
+ error(message: string, context: object = {}): Promise<void> {
27
+ return this.nativeLogs.error(message, context);
28
+ }
29
+
30
+ }
31
+
32
+ class DdTraceWrapper implements DdTraceType {
33
+
34
+ private nativeTrace: DdTraceType = NativeModules.DdTrace;
35
+
36
+ startSpan(operation: string, context: object = {}, timestampMs: number = Date.now()): Promise<string> {
37
+ return this.nativeTrace.startSpan(operation, context, timestampMs);
38
+ }
39
+
40
+ finishSpan(spanId: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
41
+ return this.nativeTrace.finishSpan(spanId, context, timestampMs);
42
+ }
43
+
44
+ }
45
+
46
+ class DdRumWrapper implements DdRumType {
47
+
48
+ private nativeRum: DdRumType = NativeModules.DdRum;
49
+
50
+ startView(key: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
51
+ return this.nativeRum.startView(key, name, context, timestampMs);
52
+ }
53
+
54
+ stopView(key: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
55
+ return this.nativeRum.stopView(key, context, timestampMs);
56
+ }
57
+
58
+ startAction(type: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
59
+ return this.nativeRum.startAction(type, name, context, timestampMs);
60
+ }
61
+
62
+ stopAction(context: object = {}, timestampMs: number = Date.now()): Promise<void> {
63
+ return this.nativeRum.stopAction(context, timestampMs);
64
+ }
65
+
66
+ addAction(type: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
67
+ return this.nativeRum.addAction(type, name, context, timestampMs);
68
+ }
69
+
70
+ startResource(key: string, method: string, url: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
71
+ return this.nativeRum.startResource(key, method, url, context, timestampMs);
72
+ }
73
+
74
+ stopResource(key: string, statusCode: number, kind: string, size: number = -1, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
75
+ return this.nativeRum.stopResource(key, statusCode, kind, size, context, timestampMs);
76
+ }
77
+
78
+ addError(message: string, source: string, stacktrace: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
79
+ let updatedContext: any = context;
80
+ updatedContext["_dd.error.source_type"] = "react-native";
81
+ return this.nativeRum.addError(message, source, stacktrace, updatedContext, timestampMs);
82
+ }
83
+
84
+ addTiming(name: string): Promise<void> {
85
+ return this.nativeRum.addTiming(name);
86
+ }
87
+
88
+ }
89
+
10
90
  const DdSdk: DdSdkType = NativeModules.DdSdk;
11
- const DdLogs: DdLogsType = NativeModules.DdLogs;
12
- const DdTrace: DdTraceType = NativeModules.DdTrace;
13
- const DdRum: DdRumType = NativeModules.DdRum;
91
+ const DdLogs: DdLogsType = new DdLogsWrapper();
92
+ const DdTrace: DdTraceType = new DdTraceWrapper();
93
+ const DdRum: DdRumType = new DdRumWrapper();
14
94
 
15
95
  export { DdSdkConfiguration, DdSdk, DdLogs, DdTrace, DdRum };
package/src/index.tsx CHANGED
@@ -8,6 +8,7 @@ import { DdLogs, DdTrace, DdRum } from './foundation';
8
8
 
9
9
  import { DdSdkReactNativeConfiguration } from './DdSdkReactNativeConfiguration';
10
10
  import { DdSdkReactNative } from './DdSdkReactNative';
11
- import { TrackingConsent } from './TrackingConsent'
11
+ import { ProxyConfiguration, ProxyType } from './ProxyConfiguration';
12
+ import { TrackingConsent } from './TrackingConsent';
12
13
 
13
- export { DdLogs, DdTrace, DdRum, DdSdkReactNativeConfiguration, DdSdkReactNative, TrackingConsent };
14
+ export { DdLogs, DdTrace, DdRum, DdSdkReactNativeConfiguration, DdSdkReactNative, ProxyConfiguration, ProxyType, TrackingConsent };
@@ -10,6 +10,7 @@ import { DdRum } from '../../foundation'
10
10
  export const UNKNOWN_TARGET_NAME = "unknown_target"
11
11
  const DEBOUNCE_EVENT_THRESHOLD_IN_MS = 10
12
12
  const HANDLE_EVENT_APP_EXECUTION_TIME_IN_MS = 1
13
+ const DD_ACTION_NAME_PROP = "dd-action-name"
13
14
 
14
15
  export class DdEventsInterceptor implements EventsInterceptor {
15
16
 
@@ -20,25 +21,54 @@ export class DdEventsInterceptor implements EventsInterceptor {
20
21
  const currentTime = Date.now()
21
22
  const timestampDifference = Math.abs(Date.now() - this.debouncingStartedTimestamp)
22
23
  if (timestampDifference > DEBOUNCE_EVENT_THRESHOLD_IN_MS) {
23
- const targetProperties = args[0]._targetInst
24
- this.handleTargetEvent(targetProperties)
24
+ const targetNode = args[0]._targetInst
25
+ this.handleTargetEvent(targetNode)
25
26
  // we add an approximated 1 millisecond for the execution time of the `handleTargetEvent` function
26
27
  this.debouncingStartedTimestamp = currentTime + HANDLE_EVENT_APP_EXECUTION_TIME_IN_MS
27
28
  }
28
29
  }
29
30
  }
30
31
 
31
- private handleTargetEvent(targetProperties: any | null) {
32
- if (targetProperties) {
33
- const resolvedTargetName = this.resolveTargetName(targetProperties);
34
- DdRum.addAction(RumActionType.TAP.valueOf(), resolvedTargetName, Date.now(), {})
32
+ private handleTargetEvent(targetNode: any | null) {
33
+ if (targetNode) {
34
+ const resolvedTargetName = this.resolveTargetName(targetNode);
35
+ DdRum.addAction(RumActionType.TAP.valueOf(), resolvedTargetName)
35
36
  }
36
37
  }
37
38
 
38
- private resolveTargetName(targetProperties: any): string {
39
- const accessibilityLabel = targetProperties.memoizedProps?.accessibilityLabel
40
- const elementType = targetProperties.elementType
41
- return accessibilityLabel ? accessibilityLabel : (elementType ? elementType : UNKNOWN_TARGET_NAME)
39
+ private resolveTargetName(targetNode: any): string {
40
+ const closestActionLabel = this.findClosestActionLabel(targetNode)
41
+ if (closestActionLabel != null) {
42
+ return closestActionLabel
43
+ }
44
+ const accessibilityLabel = targetNode.memoizedProps?.accessibilityLabel
45
+ if (accessibilityLabel != null) {
46
+ return accessibilityLabel
47
+ }
48
+ const elementTypeName = this.resolveElementTypeName(targetNode.elementType)
49
+ return elementTypeName ? elementTypeName : UNKNOWN_TARGET_NAME
50
+ }
51
+
52
+ private resolveElementTypeName(elementType: any): string | null {
53
+ let elementTypeName = null
54
+ if (typeof elementType === "string") {
55
+ elementTypeName = elementType
56
+ } else if (elementType && typeof elementType.name === "string") {
57
+ elementTypeName = elementType.name
58
+ }
59
+ return elementTypeName
60
+ }
61
+
62
+ private findClosestActionLabel(targetNode: any): string | null {
63
+ let currentNode = targetNode
64
+ while (currentNode) {
65
+ const props = currentNode.memoizedProps
66
+ if (props && props[DD_ACTION_NAME_PROP]) {
67
+ return props[DD_ACTION_NAME_PROP]
68
+ }
69
+ currentNode = currentNode.return
70
+ }
71
+ return null
42
72
  }
43
73
  }
44
74
 
@@ -4,9 +4,11 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
+ import type { ErrorHandlerCallback } from 'react-native';
7
8
  import { DdRum } from '../../foundation';
8
9
 
9
10
 
11
+ const EMPTY_MESSAGE = "Unknown Error"
10
12
  const EMPTY_STACK_TRACE = ""
11
13
  const TYPE_SOURCE = "SOURCE"
12
14
  const TYPE_CONSOLE = "CONSOLE"
@@ -16,13 +18,15 @@ const TYPE_CONSOLE = "CONSOLE"
16
18
  */
17
19
  export class DdRumErrorTracking {
18
20
 
19
- private static isTracking = false
21
+ private static isTracking = false;
22
+
23
+ private static isInDefaultErrorHandler = false;
20
24
 
21
25
  // eslint-disable-next-line
22
- private static defaultErrorHandler = (_error: any, _isFatal?: boolean) => {}
26
+ private static defaultErrorHandler: ErrorHandlerCallback = (_error: any, _isFatal?: boolean) => { }
23
27
 
24
28
  // eslint-disable-next-line
25
- private static defaultConsoleError = (..._params: unknown[]) => {}
29
+ private static defaultConsoleError = (..._params: unknown[]) => { }
26
30
 
27
31
  /**
28
32
  * Starts tracking errors and sends a RUM Error event every time an error is detected.
@@ -33,12 +37,12 @@ export class DdRumErrorTracking {
33
37
  console.log("DdRumErrorTracking already started");
34
38
  return
35
39
  }
36
-
40
+
37
41
  if (ErrorUtils) {
38
42
  DdRumErrorTracking.defaultErrorHandler = ErrorUtils.getGlobalHandler();
39
43
  DdRumErrorTracking.defaultConsoleError = console.error;
40
44
 
41
-
45
+
42
46
  ErrorUtils.setGlobalHandler(DdRumErrorTracking.onGlobalError);
43
47
  console.error = DdRumErrorTracking.onConsoleError;
44
48
 
@@ -48,59 +52,86 @@ export class DdRumErrorTracking {
48
52
  }
49
53
 
50
54
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
51
- static onGlobalError(error: any, isFatal?: boolean): void {
55
+ static onGlobalError(error: any, isFatal?: boolean): void {
56
+ const message = DdRumErrorTracking.getErrorMessage(error);
57
+ const stacktrace = DdRumErrorTracking.getErrorStackTrace(error);
52
58
  DdRum.addError(
53
- String(error),
54
- TYPE_SOURCE,
55
- DdRumErrorTracking.getErrorStackTrace(error),
56
- Date.now(),
59
+ message,
60
+ TYPE_SOURCE,
61
+ stacktrace,
57
62
  { "_dd.error.is_crash": isFatal, "_dd.error.raw": error }
58
63
  ).then(() => {
59
- DdRumErrorTracking.defaultErrorHandler(error, isFatal);
64
+ DdRumErrorTracking.isInDefaultErrorHandler = true;
65
+ try {
66
+ DdRumErrorTracking.defaultErrorHandler(error, isFatal);
67
+ } finally {
68
+ DdRumErrorTracking.isInDefaultErrorHandler = false;
69
+ }
60
70
  });
61
71
  }
62
72
 
63
- static onConsoleError(...params: unknown[]): void {
64
- let stack: string = EMPTY_STACK_TRACE
73
+ static onConsoleError(...params: unknown[]): void {
74
+
75
+ if (DdRumErrorTracking.isInDefaultErrorHandler) {
76
+ return;
77
+ }
78
+
79
+ let stack: string = EMPTY_STACK_TRACE;
65
80
  for (let i = 0; i < params.length; i += 1) {
66
81
  const param = params[i];
67
- const paramStack = DdRumErrorTracking.getErrorStackTrace(param)
68
- if (paramStack != undefined && paramStack != EMPTY_STACK_TRACE){
82
+ const paramStack = DdRumErrorTracking.getErrorStackTrace(param);
83
+ if (paramStack != undefined && paramStack != EMPTY_STACK_TRACE) {
69
84
  stack = paramStack;
70
85
  break;
71
86
  }
72
87
  }
73
88
 
74
- const message = params.map((param) => {
75
- if (typeof param === 'string') { return param }
76
- else if (param instanceof Error) { return String(param) }
77
- else { return JSON.stringify(param)}
89
+ const message = params.map((param) => {
90
+ if (typeof param === 'string') { return param; }
91
+ else { return DdRumErrorTracking.getErrorMessage(param); }
78
92
  }).join(' ');
79
93
 
80
-
94
+
81
95
  DdRum.addError(
82
- message,
96
+ message,
83
97
  TYPE_CONSOLE,
84
- stack,
85
- Date.now(),
86
- {}
98
+ stack
87
99
  ).then(() => {
88
100
  DdRumErrorTracking.defaultConsoleError.apply(console, params);
89
101
  });
90
-
102
+
103
+ }
104
+
105
+ private static getErrorMessage(error: any | undefined): string {
106
+ let message = EMPTY_MESSAGE;
107
+ if (error == undefined) {
108
+ message = EMPTY_MESSAGE;
109
+ } else if ("message" in error){
110
+ message = String(error.message);
111
+ } else {
112
+ message = String(error);
113
+ }
114
+
115
+ return message
91
116
  }
92
117
 
93
118
  private static getErrorStackTrace(error: any | undefined): string {
94
119
  let stack = EMPTY_STACK_TRACE
120
+
95
121
  if (error == undefined) {
96
122
  stack = EMPTY_STACK_TRACE;
97
123
  } else if (typeof error === 'string') {
98
124
  stack = EMPTY_STACK_TRACE;
99
125
  } else if ('componentStack' in error) {
100
126
  stack = String(error.componentStack);
127
+ } else if ('stacktrace' in error) {
128
+ stack = String(error.stacktrace);
129
+ } else if ('stack' in error) {
130
+ stack = String(error.stack);
101
131
  } else if (('sourceURL' in error) && ('line' in error) && ('column' in error)) {
102
132
  stack = `at ${error.sourceURL}:${error.line}:${error.column}`;
103
133
  }
134
+
104
135
  return stack
105
136
  }
106
137
  }