@datadog/mobile-react-native 1.5.2 → 1.7.0

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 (119) hide show
  1. package/DatadogSDKReactNative.podspec +2 -2
  2. package/android/build.gradle +2 -1
  3. package/android/gradle.properties +1 -1
  4. package/android/src/main/kotlin/com/datadog/reactnative/DatadogSDKWrapper.kt +4 -0
  5. package/android/src/main/kotlin/com/datadog/reactnative/DatadogWrapper.kt +6 -1
  6. package/android/src/main/kotlin/com/datadog/reactnative/DdLogs.kt +162 -6
  7. package/android/src/main/kotlin/com/datadog/reactnative/DdRum.kt +25 -0
  8. package/android/src/main/kotlin/com/datadog/reactnative/DdSdk.kt +32 -0
  9. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfiguration.kt +11 -1
  10. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfigurationExt.kt +10 -0
  11. package/android/src/test/kotlin/com/datadog/reactnative/DdLogsTest.kt +242 -2
  12. package/android/src/test/kotlin/com/datadog/reactnative/DdRumTest.kt +27 -0
  13. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +126 -30
  14. package/android/src/test/kotlin/com/datadog/tools/unit/DdSdkConfigurationExt.kt +19 -1
  15. package/android/src/test/kotlin/com/datadog/tools/unit/GenericAssert.kt +2 -2
  16. package/android/src/test/kotlin/com/datadog/tools/unit/forge/DdSdkConfigurationForgeryFactory.kt +18 -1
  17. package/ios/Sources/Attributes.swift +7 -0
  18. package/ios/Sources/DdLogs.m +31 -0
  19. package/ios/Sources/DdLogs.swift +84 -4
  20. package/ios/Sources/DdRum.m +8 -0
  21. package/ios/Sources/DdRum.swift +17 -0
  22. package/ios/Sources/DdSdk.swift +40 -0
  23. package/ios/Sources/DdSdkConfiguration.swift +19 -1
  24. package/ios/Sources/RNDdSdkConfiguration.swift +11 -1
  25. package/lib/commonjs/DdSdkReactNative.js +6 -2
  26. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  27. package/lib/commonjs/DdSdkReactNativeConfiguration.js +45 -2
  28. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  29. package/lib/commonjs/InternalLog.js +6 -2
  30. package/lib/commonjs/InternalLog.js.map +1 -1
  31. package/lib/commonjs/errorUtils.js +20 -1
  32. package/lib/commonjs/errorUtils.js.map +1 -1
  33. package/lib/commonjs/index.js +12 -0
  34. package/lib/commonjs/index.js.map +1 -1
  35. package/lib/commonjs/logs/DdLogs.js +102 -11
  36. package/lib/commonjs/logs/DdLogs.js.map +1 -1
  37. package/lib/commonjs/logs/eventMapper.js.map +1 -1
  38. package/lib/commonjs/nativeModulesTypes.js.map +1 -1
  39. package/lib/commonjs/rum/DdRum.js +14 -0
  40. package/lib/commonjs/rum/DdRum.js.map +1 -1
  41. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +62 -46
  42. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  43. package/lib/commonjs/rum/instrumentation/interactionTracking/DdEventsInterceptor.js +2 -2
  44. package/lib/commonjs/rum/instrumentation/interactionTracking/DdEventsInterceptor.js.map +1 -1
  45. package/lib/commonjs/rum/instrumentation/interactionTracking/ShallowObjectEqualityChecker.js +9 -1
  46. package/lib/commonjs/rum/instrumentation/interactionTracking/ShallowObjectEqualityChecker.js.map +1 -1
  47. package/lib/commonjs/rum/types.js.map +1 -1
  48. package/lib/commonjs/types.js +4 -1
  49. package/lib/commonjs/types.js.map +1 -1
  50. package/lib/commonjs/version.js +1 -1
  51. package/lib/commonjs/version.js.map +1 -1
  52. package/lib/module/DdSdkReactNative.js +6 -2
  53. package/lib/module/DdSdkReactNative.js.map +1 -1
  54. package/lib/module/DdSdkReactNativeConfiguration.js +42 -1
  55. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  56. package/lib/module/InternalLog.js +2 -1
  57. package/lib/module/InternalLog.js.map +1 -1
  58. package/lib/module/errorUtils.js +15 -0
  59. package/lib/module/errorUtils.js.map +1 -1
  60. package/lib/module/index.js +2 -2
  61. package/lib/module/index.js.map +1 -1
  62. package/lib/module/logs/DdLogs.js +103 -12
  63. package/lib/module/logs/DdLogs.js.map +1 -1
  64. package/lib/module/logs/eventMapper.js.map +1 -1
  65. package/lib/module/nativeModulesTypes.js.map +1 -1
  66. package/lib/module/rum/DdRum.js +12 -0
  67. package/lib/module/rum/DdRum.js.map +1 -1
  68. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +63 -46
  69. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  70. package/lib/module/rum/instrumentation/interactionTracking/DdEventsInterceptor.js +2 -2
  71. package/lib/module/rum/instrumentation/interactionTracking/DdEventsInterceptor.js.map +1 -1
  72. package/lib/module/rum/instrumentation/interactionTracking/ShallowObjectEqualityChecker.js +9 -1
  73. package/lib/module/rum/instrumentation/interactionTracking/ShallowObjectEqualityChecker.js.map +1 -1
  74. package/lib/module/rum/types.js.map +1 -1
  75. package/lib/module/types.js +4 -1
  76. package/lib/module/types.js.map +1 -1
  77. package/lib/module/version.js +1 -1
  78. package/lib/module/version.js.map +1 -1
  79. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +47 -0
  80. package/lib/typescript/InternalLog.d.ts +1 -0
  81. package/lib/typescript/errorUtils.d.ts +2 -0
  82. package/lib/typescript/index.d.ts +2 -2
  83. package/lib/typescript/logs/DdLogs.d.ts +13 -5
  84. package/lib/typescript/logs/eventMapper.d.ts +5 -5
  85. package/lib/typescript/logs/types.d.ts +34 -8
  86. package/lib/typescript/nativeModulesTypes.d.ts +51 -4
  87. package/lib/typescript/rum/DdRum.d.ts +2 -0
  88. package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +3 -2
  89. package/lib/typescript/rum/instrumentation/interactionTracking/DdEventsInterceptor.d.ts +1 -1
  90. package/lib/typescript/rum/instrumentation/interactionTracking/ShallowObjectEqualityChecker.d.ts +5 -2
  91. package/lib/typescript/rum/types.d.ts +4 -0
  92. package/lib/typescript/types.d.ts +8 -1
  93. package/lib/typescript/version.d.ts +1 -1
  94. package/package.json +2 -2
  95. package/src/DdSdkReactNative.tsx +9 -1
  96. package/src/DdSdkReactNativeConfiguration.tsx +56 -1
  97. package/src/InternalLog.tsx +3 -1
  98. package/src/__tests__/DdSdkReactNativeConfiguration.test.ts +20 -3
  99. package/src/__tests__/errorUtils.test.ts +49 -0
  100. package/src/__tests__/rum/instrumentation/DdEventsInterceptor.test.tsx +14 -14
  101. package/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx +474 -109
  102. package/src/__tests__/rum/instrumentation/ShallowObjectEqualityChecker.test.ts +18 -0
  103. package/src/errorUtils.ts +15 -0
  104. package/src/index.tsx +6 -2
  105. package/src/logs/DdLogs.ts +166 -16
  106. package/src/logs/__tests__/DdLogs.test.ts +169 -0
  107. package/src/logs/__tests__/eventMapper.test.ts +34 -0
  108. package/src/logs/eventMapper.ts +15 -4
  109. package/src/logs/types.ts +36 -8
  110. package/src/nativeModulesTypes.ts +81 -4
  111. package/src/rum/DdRum.ts +17 -0
  112. package/src/rum/__tests__/DdRum.test.ts +12 -27
  113. package/src/rum/instrumentation/DdRumErrorTracking.tsx +44 -10
  114. package/src/rum/instrumentation/interactionTracking/DdEventsInterceptor.tsx +7 -4
  115. package/src/rum/instrumentation/interactionTracking/ShallowObjectEqualityChecker.ts +11 -3
  116. package/src/rum/types.ts +5 -0
  117. package/src/sdk/DatadogProvider/__tests__/initialization.test.tsx +5 -0
  118. package/src/types.tsx +5 -0
  119. package/src/version.ts +1 -1
@@ -28,4 +28,22 @@ describe('areObjectValuesEqual', () => {
28
28
  const objectB = { a: 22 };
29
29
  expect(areObjectShallowEqual(objectA, objectB)).toBe(true);
30
30
  });
31
+ it('returns false when one of the object is null', () => {
32
+ expect(areObjectShallowEqual(null, { a: 22 })).toBe(false);
33
+ expect(areObjectShallowEqual(null, undefined)).toBe(false);
34
+ expect(areObjectShallowEqual({ a: 22 }, null)).toBe(false);
35
+ expect(areObjectShallowEqual(undefined, null)).toBe(false);
36
+ });
37
+ it('returns false when one of the object is undefined', () => {
38
+ expect(areObjectShallowEqual(undefined, { a: 22 })).toBe(false);
39
+ expect(areObjectShallowEqual(undefined, null)).toBe(false);
40
+ expect(areObjectShallowEqual({ a: 22 }, undefined)).toBe(false);
41
+ expect(areObjectShallowEqual(null, undefined)).toBe(false);
42
+ });
43
+ it('returns true when both of the objects are null', () => {
44
+ expect(areObjectShallowEqual(null, null)).toBe(true);
45
+ });
46
+ it('returns true when both of the objects are undefined', () => {
47
+ expect(areObjectShallowEqual(undefined, undefined)).toBe(true);
48
+ });
31
49
  });
package/src/errorUtils.ts CHANGED
@@ -6,6 +6,7 @@
6
6
 
7
7
  export const EMPTY_MESSAGE = 'Unknown Error';
8
8
  export const EMPTY_STACK_TRACE = '';
9
+ export const DEFAULT_ERROR_NAME = 'Error';
9
10
 
10
11
  export const getErrorMessage = (error: any | undefined): string => {
11
12
  let message = EMPTY_MESSAGE;
@@ -56,3 +57,17 @@ export const getErrorStackTrace = (error: any | undefined): string => {
56
57
  }
57
58
  return stack;
58
59
  };
60
+
61
+ export const getErrorName = (error: unknown): string => {
62
+ try {
63
+ if (typeof error !== 'object' || error === null) {
64
+ return DEFAULT_ERROR_NAME;
65
+ }
66
+ if (typeof (error as any).name === 'string') {
67
+ return (error as any).name;
68
+ }
69
+ } catch (e) {
70
+ // Do nothing
71
+ }
72
+ return DEFAULT_ERROR_NAME;
73
+ };
package/src/index.tsx CHANGED
@@ -8,7 +8,9 @@ import {
8
8
  DatadogProviderConfiguration,
9
9
  DdSdkReactNativeConfiguration,
10
10
  InitializationMode,
11
- VitalsUpdateFrequency
11
+ VitalsUpdateFrequency,
12
+ UploadFrequency,
13
+ BatchSize
12
14
  } from './DdSdkReactNativeConfiguration';
13
15
  import { DdSdkReactNative } from './DdSdkReactNative';
14
16
  import { InternalLog } from './InternalLog';
@@ -38,5 +40,7 @@ export {
38
40
  TrackingConsent,
39
41
  SdkVerbosity,
40
42
  VitalsUpdateFrequency,
41
- PropagatorType
43
+ PropagatorType,
44
+ UploadFrequency,
45
+ BatchSize
42
46
  };
@@ -6,53 +6,203 @@
6
6
 
7
7
  import { NativeModules } from 'react-native';
8
8
 
9
- import { InternalLog } from '../InternalLog';
9
+ import { DATADOG_MESSAGE_PREFIX, InternalLog } from '../InternalLog';
10
10
  import { SdkVerbosity } from '../SdkVerbosity';
11
11
  import type { DdNativeLogsType } from '../nativeModulesTypes';
12
12
 
13
13
  import { generateEventMapper } from './eventMapper';
14
- import type { DdLogsType, LogEventMapper } from './types';
14
+ import type {
15
+ DdLogsType,
16
+ LogArguments,
17
+ LogEventMapper,
18
+ LogWithErrorArguments,
19
+ NativeLogWithError
20
+ } from './types';
21
+
22
+ const SDK_NOT_INITIALIZED_MESSAGE = 'DD_INTERNAL_LOG_SENT_BEFORE_SDK_INIT';
15
23
 
16
24
  const generateEmptyPromise = () => new Promise<void>(resolve => resolve());
17
25
 
26
+ /**
27
+ * We consider that if either one of `errorKind`, `errorMessage` or `stacktrace` is a string,
28
+ * then the log contains an error.
29
+ */
30
+ const isLogWithError = (
31
+ args: LogArguments | LogWithErrorArguments
32
+ ): args is LogWithErrorArguments => {
33
+ return (
34
+ typeof args[1] === 'string' ||
35
+ typeof args[2] === 'string' ||
36
+ typeof args[3] === 'string' ||
37
+ typeof args[4] === 'object'
38
+ );
39
+ };
40
+
18
41
  class DdLogsWrapper implements DdLogsType {
19
42
  private nativeLogs: DdNativeLogsType = NativeModules.DdLogs;
20
43
  private logEventMapper = generateEventMapper(undefined);
21
44
 
22
- debug(message: string, context: object = {}): Promise<void> {
23
- return this.log(message, context, 'debug');
45
+ debug(...args: LogArguments | LogWithErrorArguments): Promise<void> {
46
+ if (isLogWithError(args)) {
47
+ return this.logWithError(
48
+ args[0],
49
+ args[1],
50
+ args[2],
51
+ args[3],
52
+ args[4] || {},
53
+ 'debug'
54
+ );
55
+ }
56
+ return this.log(args[0], args[1] || {}, 'debug');
24
57
  }
25
58
 
26
- info(message: string, context: object = {}): Promise<void> {
27
- return this.log(message, context, 'info');
59
+ info(...args: LogArguments | LogWithErrorArguments): Promise<void> {
60
+ if (isLogWithError(args)) {
61
+ return this.logWithError(
62
+ args[0],
63
+ args[1],
64
+ args[2],
65
+ args[3],
66
+ args[4] || {},
67
+ 'info'
68
+ );
69
+ }
70
+ return this.log(args[0], args[1] || {}, 'info');
28
71
  }
29
72
 
30
- warn(message: string, context: object = {}): Promise<void> {
31
- return this.log(message, context, 'warn');
73
+ warn(...args: LogArguments | LogWithErrorArguments): Promise<void> {
74
+ if (isLogWithError(args)) {
75
+ return this.logWithError(
76
+ args[0],
77
+ args[1],
78
+ args[2],
79
+ args[3],
80
+ args[4] || {},
81
+ 'warn'
82
+ );
83
+ }
84
+ return this.log(args[0], args[1] || {}, 'warn');
32
85
  }
33
86
 
34
- error(message: string, context: object = {}): Promise<void> {
35
- return this.log(message, context, 'error');
87
+ error(...args: LogArguments | LogWithErrorArguments): Promise<void> {
88
+ if (isLogWithError(args)) {
89
+ return this.logWithError(
90
+ args[0],
91
+ args[1],
92
+ args[2],
93
+ args[3],
94
+ args[4] || {},
95
+ 'error'
96
+ );
97
+ }
98
+ return this.log(args[0], args[1] || {}, 'error');
36
99
  }
37
100
 
38
- private log = (
101
+ /**
102
+ * Since the InternalLog does not have a verbosity set yet in this case,
103
+ * we use console.warn to warn the user in dev mode.
104
+ */
105
+ private printLogDroppedSdkNotInitialized = (
39
106
  message: string,
40
- context: object,
41
- status: keyof DdNativeLogsType
42
- ): Promise<void> => {
107
+ status: 'debug' | 'info' | 'warn' | 'error'
108
+ ) => {
109
+ if (__DEV__) {
110
+ console.warn(
111
+ `${DATADOG_MESSAGE_PREFIX} Dropping ${status} log as the SDK is not initialized yet: "${message}"`
112
+ );
113
+ }
114
+ };
115
+
116
+ private printlogDroppedByMapper = (
117
+ message: string,
118
+ status: 'debug' | 'info' | 'warn' | 'error'
119
+ ) => {
43
120
  InternalLog.log(
44
- `Tracking ${status} log “${message}”`,
121
+ `${status} log dropped by log mapper: "${message}"`,
45
122
  SdkVerbosity.DEBUG
46
123
  );
124
+ };
125
+
126
+ private printLogTracked = (
127
+ message: string,
128
+ status: 'debug' | 'info' | 'warn' | 'error'
129
+ ) => {
130
+ InternalLog.log(
131
+ `Tracking ${status} log "${message}"`,
132
+ SdkVerbosity.DEBUG
133
+ );
134
+ };
135
+
136
+ private log = async (
137
+ message: string,
138
+ context: object,
139
+ status: 'debug' | 'info' | 'warn' | 'error'
140
+ ): Promise<void> => {
47
141
  const event = this.logEventMapper.applyEventMapper({
48
142
  message,
49
143
  context,
50
144
  status
51
145
  });
52
146
  if (!event) {
147
+ this.printlogDroppedByMapper(message, status);
53
148
  return generateEmptyPromise();
54
149
  }
55
- return this.nativeLogs[status](event.message, event.context);
150
+
151
+ this.printLogTracked(event.message, status);
152
+ try {
153
+ return await this.nativeLogs[status](event.message, event.context);
154
+ } catch (error) {
155
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
156
+ // @ts-ignore
157
+ if (error.message === SDK_NOT_INITIALIZED_MESSAGE) {
158
+ this.printLogDroppedSdkNotInitialized(message, status);
159
+ return generateEmptyPromise();
160
+ }
161
+
162
+ throw error;
163
+ }
164
+ };
165
+
166
+ private logWithError = async (
167
+ message: string,
168
+ errorKind: string | undefined,
169
+ errorMessage: string | undefined,
170
+ stacktrace: string | undefined,
171
+ context: object,
172
+ status: 'debug' | 'info' | 'warn' | 'error'
173
+ ): Promise<void> => {
174
+ const event = this.logEventMapper.applyEventMapper({
175
+ message,
176
+ errorKind,
177
+ errorMessage,
178
+ stacktrace,
179
+ context,
180
+ status
181
+ });
182
+ if (!event) {
183
+ this.printlogDroppedByMapper(message, status);
184
+ return generateEmptyPromise();
185
+ }
186
+
187
+ this.printLogTracked(event.message, status);
188
+ try {
189
+ return await this.nativeLogs[`${status}WithError`](
190
+ event.message,
191
+ (event as NativeLogWithError).errorKind,
192
+ (event as NativeLogWithError).errorMessage,
193
+ (event as NativeLogWithError).stacktrace,
194
+ { ...event.context, '_dd.error.source_type': 'react-native' }
195
+ );
196
+ } catch (error) {
197
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
198
+ // @ts-ignore
199
+ if (error.message === SDK_NOT_INITIALIZED_MESSAGE) {
200
+ this.printLogDroppedSdkNotInitialized(message, status);
201
+ return generateEmptyPromise();
202
+ }
203
+
204
+ throw error;
205
+ }
56
206
  };
57
207
 
58
208
  registerLogEventMapper(logEventMapper: LogEventMapper) {
@@ -6,9 +6,20 @@
6
6
 
7
7
  import { NativeModules } from 'react-native';
8
8
 
9
+ import { InternalLog } from '../../InternalLog';
10
+ import type { DdNativeLogsType } from '../../nativeModulesTypes';
9
11
  import { DdLogs } from '../DdLogs';
10
12
  import type { LogEventMapper } from '../types';
11
13
 
14
+ jest.mock('../../InternalLog', () => {
15
+ return {
16
+ InternalLog: {
17
+ log: jest.fn()
18
+ },
19
+ DATADOG_MESSAGE_PREFIX: 'DATADOG:'
20
+ };
21
+ });
22
+
12
23
  describe('DdLogs', () => {
13
24
  describe('log event mapper', () => {
14
25
  beforeEach(() => {
@@ -17,8 +28,63 @@ describe('DdLogs', () => {
17
28
  });
18
29
 
19
30
  it('registers event mapper and maps logs', async () => {
31
+ const logEventMapper: LogEventMapper = log => {
32
+ return {
33
+ message: 'new message',
34
+ context: { newContext: 'context' },
35
+ status: 'info',
36
+ userInfo: {}
37
+ };
38
+ };
39
+ DdLogs.registerLogEventMapper(logEventMapper);
40
+
41
+ await DdLogs.info('original message', {});
42
+ expect(
43
+ NativeModules.DdLogs.info
44
+ ).toHaveBeenCalledWith('new message', { newContext: 'context' });
45
+
46
+ expect(InternalLog.log).toHaveBeenNthCalledWith(
47
+ 1,
48
+ 'Tracking info log "new message"',
49
+ 'debug'
50
+ );
51
+
52
+ await DdLogs.debug(
53
+ 'original message',
54
+ 'TypeError',
55
+ 'error message',
56
+ 'stack',
57
+ {}
58
+ );
59
+ expect(NativeModules.DdLogs.debugWithError).toHaveBeenCalledWith(
60
+ 'new message',
61
+ undefined,
62
+ undefined,
63
+ undefined,
64
+ {
65
+ newContext: 'context',
66
+ '_dd.error.source_type': 'react-native'
67
+ }
68
+ );
69
+ expect(InternalLog.log).toHaveBeenNthCalledWith(
70
+ 2,
71
+ 'Tracking debug log "new message"',
72
+ 'debug'
73
+ );
74
+ });
75
+
76
+ it('registers event mapper and maps logs with errors', async () => {
20
77
  const logEventMapper: LogEventMapper = log => {
21
78
  log.message = 'new message';
79
+ if (log.errorKind) {
80
+ log.errorKind = 'NewErrorType';
81
+ }
82
+ if (log.errorMessage) {
83
+ log.errorMessage = 'new error message';
84
+ }
85
+ if (log.stacktrace) {
86
+ log.stacktrace = 'new stacktrace';
87
+ }
22
88
  log.context = { newContext: 'context' };
23
89
  return log;
24
90
  };
@@ -28,6 +94,23 @@ describe('DdLogs', () => {
28
94
  expect(
29
95
  NativeModules.DdLogs.info
30
96
  ).toHaveBeenCalledWith('new message', { newContext: 'context' });
97
+ await DdLogs.info(
98
+ 'original message',
99
+ 'TypeError',
100
+ 'error message',
101
+ 'stack',
102
+ {}
103
+ );
104
+ expect(NativeModules.DdLogs.infoWithError).toHaveBeenCalledWith(
105
+ 'new message',
106
+ 'NewErrorType',
107
+ 'new error message',
108
+ 'new stacktrace',
109
+ {
110
+ newContext: 'context',
111
+ '_dd.error.source_type': 'react-native'
112
+ }
113
+ );
31
114
  });
32
115
 
33
116
  it('sends initial log if no event mapper is registered', async () => {
@@ -46,6 +129,92 @@ describe('DdLogs', () => {
46
129
 
47
130
  await DdLogs.info('original message', {});
48
131
  expect(NativeModules.DdLogs.info).not.toHaveBeenCalled();
132
+ expect(InternalLog.log).toHaveBeenCalledWith(
133
+ 'info log dropped by log mapper: "original message"',
134
+ 'debug'
135
+ );
136
+ });
137
+ });
138
+
139
+ describe('log with error', () => {
140
+ beforeEach(() => {
141
+ jest.clearAllMocks();
142
+ DdLogs.unregisterLogEventMapper();
143
+ });
144
+ it.each([
145
+ ['kind', 'message', 'stacktrace', { context: 'value' }],
146
+ // 1 argument is undefined
147
+ [undefined, 'message', 'stacktrace', { context: 'value' }],
148
+ ['kind', undefined, 'stacktrace', { context: 'value' }],
149
+ ['kind', 'message', undefined, { context: 'value' }],
150
+ ['kind', 'message', 'stacktrace', undefined],
151
+ // 2 arguments are undefined
152
+ [undefined, undefined, 'stacktrace', { context: 'value' }],
153
+ [undefined, 'message', undefined, { context: 'value' }],
154
+ [undefined, 'message', 'stacktrace', undefined],
155
+ ['kind', undefined, undefined, { context: 'value' }],
156
+ ['kind', undefined, 'stacktrace', undefined],
157
+ ['kind', 'message', undefined, undefined],
158
+ // 3 arguments are undefined
159
+ [undefined, undefined, 'stacktrace', undefined],
160
+ [undefined, 'message', undefined, undefined],
161
+ ['kind', undefined, undefined, undefined],
162
+ [undefined, undefined, undefined, { context: 'value' }]
163
+ ])(
164
+ 'sends error info when provided for %s %s %s %s',
165
+ async (errorKind, errorMessage, stacktrace, context) => {
166
+ await DdLogs.info(
167
+ 'message',
168
+ errorKind,
169
+ errorMessage,
170
+ stacktrace,
171
+ context
172
+ );
173
+ expect(NativeModules.DdLogs.infoWithError).toHaveBeenCalledWith(
174
+ 'message',
175
+ errorKind,
176
+ errorMessage,
177
+ stacktrace,
178
+ {
179
+ ...(context || {}),
180
+ '_dd.error.source_type': 'react-native'
181
+ }
182
+ );
183
+ }
184
+ );
185
+
186
+ it('does not send error info when no error and no context is passed', async () => {
187
+ await DdLogs.info(
188
+ 'message',
189
+ undefined,
190
+ undefined,
191
+ undefined,
192
+ undefined
193
+ );
194
+ expect(NativeModules.DdLogs.info).toHaveBeenCalledWith(
195
+ 'message',
196
+ {}
197
+ );
198
+ });
199
+ });
200
+
201
+ describe.only('when SDK is not initialized', () => {
202
+ beforeEach(() => {
203
+ jest.clearAllMocks();
204
+ DdLogs.unregisterLogEventMapper();
205
+ });
206
+
207
+ it('does not crash and warns user', async () => {
208
+ (NativeModules.DdLogs.info as jest.MockedFunction<
209
+ DdNativeLogsType['debug']
210
+ >).mockRejectedValueOnce(
211
+ new Error('DD_INTERNAL_LOG_SENT_BEFORE_SDK_INIT')
212
+ );
213
+ const consoleSpy = jest.spyOn(console, 'warn');
214
+ await DdLogs.info('original message', {});
215
+ expect(consoleSpy).toHaveBeenCalledWith(
216
+ 'DATADOG: Dropping info log as the SDK is not initialized yet: "original message"'
217
+ );
49
218
  });
50
219
  });
51
220
  });
@@ -50,4 +50,38 @@ describe('formatRawLogToLogEvent', () => {
50
50
  attributes: { appType: 'student' }
51
51
  });
52
52
  });
53
+
54
+ it('formats a raw log with error attributes and with context, userInfo and attributes to a LogEvent', () => {
55
+ expect(
56
+ formatRawLogToLogEvent(
57
+ {
58
+ message: 'original',
59
+ errorKind: 'TypeError',
60
+ errorMessage: 'something went wrong',
61
+ stacktrace: 'stacktrace',
62
+ context: { loggedIn: true },
63
+ status: 'info'
64
+ },
65
+ {
66
+ userInfo: {
67
+ name: 'userName',
68
+ extraInfo: { loggedIn: true }
69
+ },
70
+ attributes: { appType: 'student' }
71
+ }
72
+ )
73
+ ).toEqual({
74
+ message: 'original',
75
+ errorKind: 'TypeError',
76
+ errorMessage: 'something went wrong',
77
+ stacktrace: 'stacktrace',
78
+ context: { loggedIn: true },
79
+ status: 'info',
80
+ userInfo: {
81
+ name: 'userName',
82
+ extraInfo: { loggedIn: true }
83
+ },
84
+ attributes: { appType: 'student' }
85
+ });
86
+ });
53
87
  });
@@ -8,18 +8,29 @@ import type { Attributes } from '../sdk/AttributesSingleton/types';
8
8
  import { EventMapper } from '../sdk/EventMappers/EventMapper';
9
9
  import type { UserInfo } from '../sdk/UserInfoSingleton/types';
10
10
 
11
- import type { LogEvent, LogEventMapper, NativeLog, RawLog } from './types';
11
+ import type {
12
+ LogEvent,
13
+ LogEventMapper,
14
+ NativeLog,
15
+ NativeLogWithError,
16
+ RawLog,
17
+ RawLogWithError
18
+ } from './types';
12
19
 
13
- export const formatLogEventToNativeLog = (logEvent: LogEvent): NativeLog => {
20
+ export const formatLogEventToNativeLog = (
21
+ logEvent: LogEvent
22
+ ): NativeLog | NativeLogWithError => {
14
23
  return logEvent;
15
24
  };
16
25
 
17
- export const formatRawLogToNativeEvent = (rawLog: RawLog): NativeLog => {
26
+ export const formatRawLogToNativeEvent = (
27
+ rawLog: RawLog | RawLogWithError
28
+ ): NativeLog | NativeLogWithError => {
18
29
  return rawLog;
19
30
  };
20
31
 
21
32
  export const formatRawLogToLogEvent = (
22
- rawLog: RawLog,
33
+ rawLog: RawLog | RawLogWithError,
23
34
  additionalInformation: {
24
35
  userInfo: UserInfo;
25
36
  attributes: Attributes;
package/src/logs/types.ts CHANGED
@@ -11,32 +11,32 @@ import type { UserInfo } from '../sdk/UserInfoSingleton/types';
11
11
  */
12
12
  export type DdLogsType = {
13
13
  /**
14
- * Send a log with level debug.
14
+ * Send a log with debug level.
15
15
  * @param message: The message to send.
16
16
  * @param context: The additional context to send.
17
17
  */
18
- debug(message: string, context?: object): Promise<void>;
18
+ debug(...args: LogArguments | LogWithErrorArguments): Promise<void>;
19
19
 
20
20
  /**
21
- * Send a log with level info.
21
+ * Send a log with info level.
22
22
  * @param message: The message to send.
23
23
  * @param context: The additional context to send.
24
24
  */
25
- info(message: string, context?: object): Promise<void>;
25
+ info(...args: LogArguments | LogWithErrorArguments): Promise<void>;
26
26
 
27
27
  /**
28
- * Send a log with level warn.
28
+ * Send a log with warn level.
29
29
  * @param message: The message to send.
30
30
  * @param context: The additional context to send.
31
31
  */
32
- warn(message: string, context?: object): Promise<void>;
32
+ warn(...args: LogArguments | LogWithErrorArguments): Promise<void>;
33
33
 
34
34
  /**
35
- * Send a log with level error.
35
+ * Send a log with error level.
36
36
  * @param message: The message to send.
37
37
  * @param context: The additional context to send.
38
38
  */
39
- error(message: string, context?: object): Promise<void>;
39
+ error(...args: LogArguments | LogWithErrorArguments): Promise<void>;
40
40
  };
41
41
 
42
42
  /**
@@ -47,6 +47,14 @@ export type RawLog = {
47
47
  context: object;
48
48
  status: LogStatus;
49
49
  };
50
+ export type RawLogWithError = {
51
+ message: string;
52
+ errorKind: string;
53
+ errorMessage: string;
54
+ stacktrace: string;
55
+ context: object;
56
+ status: LogStatus;
57
+ };
50
58
 
51
59
  /**
52
60
  * Log input for native SDKs
@@ -55,12 +63,22 @@ export type NativeLog = {
55
63
  message: string;
56
64
  context: object;
57
65
  };
66
+ export type NativeLogWithError = {
67
+ message: string;
68
+ errorKind: string;
69
+ errorMessage: string;
70
+ stacktrace: string;
71
+ context: object;
72
+ };
58
73
 
59
74
  export type LogStatus = 'debug' | 'info' | 'warn' | 'error';
60
75
 
61
76
  export type LogEvent = {
62
77
  message: string;
63
78
  context: object;
79
+ errorKind?: string;
80
+ errorMessage?: string;
81
+ stacktrace?: string;
64
82
  // readonly date: number; // TODO: RUMM-2446 & RUMM-2447
65
83
  readonly status: LogStatus;
66
84
  readonly userInfo: UserInfo;
@@ -68,3 +86,13 @@ export type LogEvent = {
68
86
  };
69
87
 
70
88
  export type LogEventMapper = (logEvent: LogEvent) => LogEvent | null;
89
+
90
+ export type LogArguments = [message: string, context?: object];
91
+
92
+ export type LogWithErrorArguments = [
93
+ message: string,
94
+ errorKind?: string,
95
+ errorMessage?: string,
96
+ stacktrace?: string,
97
+ context?: object
98
+ ];