@datadog/mobile-react-native 2.3.3 → 2.3.5

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 (82) hide show
  1. package/android/src/main/kotlin/com/datadog/reactnative/DdRumImplementation.kt +9 -0
  2. package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
  3. package/android/src/newarch/kotlin/com/datadog/reactnative/DdRum.kt +8 -0
  4. package/android/src/oldarch/kotlin/com/datadog/reactnative/DdRum.kt +8 -0
  5. package/datadog-sourcemaps.gradle +13 -2
  6. package/ios/Sources/DdRum.mm +11 -0
  7. package/ios/Sources/DdRumImplementation.swift +7 -0
  8. package/ios/Sources/SdkVersion.swift +1 -1
  9. package/jest/mock.js +9 -1
  10. package/lib/commonjs/DdSdkReactNative.js +11 -4
  11. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  12. package/lib/commonjs/index.js +16 -0
  13. package/lib/commonjs/index.js.map +1 -1
  14. package/lib/commonjs/rum/DdRum.js +26 -12
  15. package/lib/commonjs/rum/DdRum.js.map +1 -1
  16. package/lib/commonjs/rum/types.js.map +1 -1
  17. package/lib/commonjs/sdk/DatadogProvider/Buffer/BoundedBuffer.js +14 -6
  18. package/lib/commonjs/sdk/DatadogProvider/Buffer/BoundedBuffer.js.map +1 -1
  19. package/lib/commonjs/specs/NativeDdRum.js.map +1 -1
  20. package/lib/commonjs/trace/DdTrace.js +2 -2
  21. package/lib/commonjs/trace/DdTrace.js.map +1 -1
  22. package/lib/commonjs/utils/Timer.js +4 -4
  23. package/lib/commonjs/utils/Timer.js.map +1 -1
  24. package/lib/commonjs/utils/time-provider/DefaultTimeProvider.js +41 -0
  25. package/lib/commonjs/utils/time-provider/DefaultTimeProvider.js.map +1 -0
  26. package/lib/commonjs/utils/{TimeProvider.js → time-provider/TimeProvider.js} +8 -25
  27. package/lib/commonjs/utils/time-provider/TimeProvider.js.map +1 -0
  28. package/lib/commonjs/version.js +1 -1
  29. package/lib/commonjs/version.js.map +1 -1
  30. package/lib/module/DdSdkReactNative.js +11 -4
  31. package/lib/module/DdSdkReactNative.js.map +1 -1
  32. package/lib/module/index.js +3 -1
  33. package/lib/module/index.js.map +1 -1
  34. package/lib/module/rum/DdRum.js +25 -11
  35. package/lib/module/rum/DdRum.js.map +1 -1
  36. package/lib/module/rum/types.js.map +1 -1
  37. package/lib/module/sdk/DatadogProvider/Buffer/BoundedBuffer.js +14 -6
  38. package/lib/module/sdk/DatadogProvider/Buffer/BoundedBuffer.js.map +1 -1
  39. package/lib/module/specs/NativeDdRum.js.map +1 -1
  40. package/lib/module/trace/DdTrace.js +2 -2
  41. package/lib/module/trace/DdTrace.js.map +1 -1
  42. package/lib/module/utils/Timer.js +4 -4
  43. package/lib/module/utils/Timer.js.map +1 -1
  44. package/lib/module/utils/time-provider/DefaultTimeProvider.js +36 -0
  45. package/lib/module/utils/time-provider/DefaultTimeProvider.js.map +1 -0
  46. package/lib/module/utils/{TimeProvider.js → time-provider/TimeProvider.js} +8 -25
  47. package/lib/module/utils/time-provider/TimeProvider.js.map +1 -0
  48. package/lib/module/version.js +1 -1
  49. package/lib/module/version.js.map +1 -1
  50. package/lib/typescript/DdSdkReactNative.d.ts +5 -1
  51. package/lib/typescript/index.d.ts +5 -1
  52. package/lib/typescript/rum/DdRum.d.ts +4 -0
  53. package/lib/typescript/rum/types.d.ts +4 -0
  54. package/lib/typescript/sdk/DatadogProvider/Buffer/BoundedBuffer.d.ts +2 -0
  55. package/lib/typescript/specs/NativeDdRum.d.ts +4 -0
  56. package/lib/typescript/utils/Timer.d.ts +1 -1
  57. package/lib/typescript/utils/time-provider/DefaultTimeProvider.d.ts +7 -0
  58. package/lib/typescript/utils/{TimeProvider.d.ts → time-provider/TimeProvider.d.ts} +4 -6
  59. package/lib/typescript/version.d.ts +1 -1
  60. package/package.json +2 -2
  61. package/src/DdSdkReactNative.tsx +10 -4
  62. package/src/__tests__/DdSdkReactNative.test.tsx +2 -2
  63. package/src/__tests__/mock.test.ts +1 -1
  64. package/src/index.tsx +7 -0
  65. package/src/rum/DdRum.ts +24 -12
  66. package/src/rum/__mocks__/MockTimeProvider.ts +26 -0
  67. package/src/rum/__tests__/DdRum.test.ts +39 -13
  68. package/src/rum/__tests__/DdRumTimeProvider.test.ts +23 -0
  69. package/src/rum/types.ts +5 -0
  70. package/src/sdk/DatadogProvider/Buffer/BoundedBuffer.ts +22 -14
  71. package/src/sdk/DatadogProvider/__tests__/initialization.test.tsx +6 -5
  72. package/src/sdk/DatadogProvider/__tests__/initializationModes.test.tsx +5 -5
  73. package/src/specs/NativeDdRum.ts +5 -0
  74. package/src/trace/DdTrace.ts +2 -2
  75. package/src/utils/Timer.ts +5 -5
  76. package/src/utils/__tests__/TimeProvider.test.tsx +24 -7
  77. package/src/utils/__tests__/Timer.test.ts +38 -22
  78. package/src/utils/time-provider/DefaultTimeProvider.tsx +31 -0
  79. package/src/utils/{TimeProvider.tsx → time-provider/TimeProvider.tsx} +13 -27
  80. package/src/version.ts +1 -1
  81. package/lib/commonjs/utils/TimeProvider.js.map +0 -1
  82. package/lib/module/utils/TimeProvider.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"sources":["version.ts"],"names":["version"],"mappings":"AAAA;AACA,OAAO,MAAMA,OAAO,GAAG,OAAhB","sourcesContent":["// generated by genversion\nexport const version = '2.3.3';\n"]}
1
+ {"version":3,"sources":["version.ts"],"names":["version"],"mappings":"AAAA;AACA,OAAO,MAAMA,OAAO,GAAG,OAAhB","sourcesContent":["// generated by genversion\nexport const version = '2.3.5';\n"]}
@@ -11,9 +11,9 @@ export declare class DdSdkReactNative {
11
11
  private static readonly DD_SDK_VERSION;
12
12
  private static readonly DD_VERSION;
13
13
  private static readonly DD_VERSION_SUFFIX;
14
- private static wasInitialized;
15
14
  private static wasAutoInstrumented;
16
15
  private static features?;
16
+ private static _isInitialized;
17
17
  /**
18
18
  * Initializes the Datadog SDK.
19
19
  * @param configuration the configuration for the SDK library
@@ -57,6 +57,10 @@ export declare class DdSdkReactNative {
57
57
  * @returns a Promise
58
58
  */
59
59
  static clearAllData: () => Promise<void>;
60
+ /**
61
+ * Returns `true` if the SDK was initialized, `false` otherwise.
62
+ */
63
+ static isInitialized(): boolean;
60
64
  private static buildConfiguration;
61
65
  private static enableFeatures;
62
66
  }
@@ -11,4 +11,8 @@ import { RumActionType, ErrorSource, PropagatorType } from './rum/types';
11
11
  import { DatadogProvider } from './sdk/DatadogProvider/DatadogProvider';
12
12
  import { FileBasedConfiguration } from './sdk/FileBasedConfiguration/FileBasedConfiguration';
13
13
  import { DdTrace } from './trace/DdTrace';
14
- export { DatadogProvider, DatadogProviderConfiguration, FileBasedConfiguration, InitializationMode, DdLogs, DdTrace, DdRum, RumActionType, ErrorSource, DdSdkReactNativeConfiguration, DdSdkReactNative, InternalLog, ProxyConfiguration, ProxyType, TrackingConsent, SdkVerbosity, VitalsUpdateFrequency, PropagatorType, UploadFrequency, BatchSize, DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER, DATADOG_GRAPH_QL_OPERATION_NAME_HEADER, DATADOG_GRAPH_QL_VARIABLES_HEADER };
14
+ import { DefaultTimeProvider } from './utils/time-provider/DefaultTimeProvider';
15
+ import { TimeProvider } from './utils/time-provider/TimeProvider';
16
+ import type { Timestamp } from './utils/time-provider/TimeProvider';
17
+ export { DatadogProvider, DatadogProviderConfiguration, FileBasedConfiguration, InitializationMode, DdLogs, DdTrace, DdRum, RumActionType, ErrorSource, DdSdkReactNativeConfiguration, DdSdkReactNative, InternalLog, ProxyConfiguration, ProxyType, TrackingConsent, SdkVerbosity, VitalsUpdateFrequency, PropagatorType, UploadFrequency, BatchSize, TimeProvider, DefaultTimeProvider, DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER, DATADOG_GRAPH_QL_OPERATION_NAME_HEADER, DATADOG_GRAPH_QL_VARIABLES_HEADER };
18
+ export type { Timestamp };
@@ -1,4 +1,5 @@
1
1
  import type { GestureResponderEvent } from 'react-native';
2
+ import type { TimeProvider } from '../utils/time-provider/TimeProvider';
2
3
  import type { ActionEventMapper } from './eventMappers/actionEventMapper';
3
4
  import type { ErrorEventMapper } from './eventMappers/errorEventMapper';
4
5
  import type { ResourceEventMapper } from './eventMappers/resourceEventMapper';
@@ -9,6 +10,7 @@ declare class DdRumWrapper implements DdRumType {
9
10
  private errorEventMapper;
10
11
  private resourceEventMapper;
11
12
  private actionEventMapper;
13
+ private timeProvider;
12
14
  startView: (key: string, name: string, context?: object, timestampMs?: number) => Promise<void>;
13
15
  stopView: (key: string, context?: object, timestampMs?: number) => Promise<void>;
14
16
  startAction: (type: RumActionType, name: string, context?: object, timestampMs?: number) => Promise<void>;
@@ -18,6 +20,7 @@ declare class DdRumWrapper implements DdRumType {
18
20
  context?: object,
19
21
  timestampMs?: number
20
22
  ] | [context?: object, timestampMs?: number]) => Promise<void>;
23
+ setTimeProvider: (timeProvider: TimeProvider) => void;
21
24
  private callNativeStopAction;
22
25
  private getStopActionNativeCallArgs;
23
26
  addAction: (type: RumActionType, name: string, context?: object, timestampMs?: number, actionContext?: GestureResponderEvent) => Promise<void>;
@@ -27,6 +30,7 @@ declare class DdRumWrapper implements DdRumType {
27
30
  addTiming: (name: string) => Promise<void>;
28
31
  stopSession: () => Promise<void>;
29
32
  addFeatureFlagEvaluation: (name: string, value: unknown) => Promise<void>;
33
+ getCurrentSessionId(): Promise<string | undefined>;
30
34
  registerErrorEventMapper(errorEventMapper: ErrorEventMapper): void;
31
35
  unregisterErrorEventMapper(): void;
32
36
  registerResourceEventMapper(resourceEventMapper: ResourceEventMapper): void;
@@ -88,6 +88,10 @@ export type DdRumType = {
88
88
  * Stops the current RUM Session.
89
89
  */
90
90
  stopSession(): Promise<void>;
91
+ /**
92
+ * Returns current session ID, or undefined if unavailable.
93
+ */
94
+ getCurrentSessionId(): Promise<string | undefined>;
91
95
  };
92
96
  /**
93
97
  * Describe the type of a RUM Action.
@@ -9,6 +9,8 @@ export declare class BoundedBuffer extends DatadogBuffer {
9
9
  addCallbackReturningId: (callback: () => Promise<string>) => Promise<string>;
10
10
  addCallbackWithId: (callback: (id: string) => Promise<void>, bufferId: string) => Promise<void>;
11
11
  drain: () => Promise<void>;
12
+ private logBufferOverflow;
13
+ private logRandomIdGenerationError;
12
14
  private generateRandomBufferId;
13
15
  private addTelemetryEvent;
14
16
  private drainTelemetry;
@@ -87,6 +87,10 @@ export interface Spec extends TurboModule {
87
87
  * @param value: The value of the feature flag, encapsulated in an Object to accept all types
88
88
  */
89
89
  addFeatureFlagEvaluation(name: string, value: Object): Promise<void>;
90
+ /**
91
+ * Get current Session ID, or `undefined` if not available.
92
+ */
93
+ getCurrentSessionId(): Promise<string | undefined>;
90
94
  }
91
95
  declare const _default: Spec | null;
92
96
  export default _default;
@@ -1,4 +1,4 @@
1
- import { TimeProvider } from './TimeProvider';
1
+ import type { TimeProvider } from './time-provider/TimeProvider';
2
2
  /**
3
3
  * Simple timer which records time ticks. Shouldn't be re-used once stopped.
4
4
  * All timestamps/durations returned are in milliseconds.
@@ -0,0 +1,7 @@
1
+ import { TimeProvider } from './TimeProvider';
2
+ import type { Timestamp } from './TimeProvider';
3
+ export declare class DefaultTimeProvider extends TimeProvider {
4
+ private canUsePerformanceNow;
5
+ getTimestamp(): Timestamp;
6
+ private performanceNow;
7
+ }
@@ -3,18 +3,16 @@
3
3
  */
4
4
  export type Timestamp = {
5
5
  unix: number;
6
- react_native: number | null;
6
+ reactNative: number | null;
7
7
  };
8
8
  /**
9
9
  * Simple class providing timestamps in milliseconds.
10
10
  * If available, it will use the `performance.now()` method, and will fallback on `Date.now()` otherwise.
11
11
  */
12
- export declare class TimeProvider {
13
- private canUsePerformanceNow;
12
+ export declare abstract class TimeProvider {
14
13
  /** Keeps an average offset between the unix time and the provided timestamp. */
15
- private baseOffset;
14
+ protected baseOffset: number;
16
15
  constructor();
17
- getTimestamp(): Timestamp;
16
+ abstract getTimestamp(): Timestamp;
18
17
  now(): number;
19
- private performanceNow;
20
18
  }
@@ -1 +1 @@
1
- export declare const version = "2.3.3";
1
+ export declare const version = "2.3.5";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datadog/mobile-react-native",
3
- "version": "2.3.3",
3
+ "version": "2.3.5",
4
4
  "description": "A client-side React Native module to interact with Datadog",
5
5
  "keywords": [
6
6
  "datadog",
@@ -97,5 +97,5 @@
97
97
  "javaPackageName": "com.datadog.reactnative"
98
98
  }
99
99
  },
100
- "gitHead": "f9db01676449a1e3e31ad32ba20d1bcf360556e5"
100
+ "gitHead": "d80e122e5f06e6d126f9e271f641b3cec9e21832"
101
101
  }
@@ -47,10 +47,9 @@ export class DdSdkReactNative {
47
47
  private static readonly DD_SDK_VERSION = '_dd.sdk_version';
48
48
  private static readonly DD_VERSION = '_dd.version';
49
49
  private static readonly DD_VERSION_SUFFIX = '_dd.version_suffix';
50
-
51
- private static wasInitialized = false;
52
50
  private static wasAutoInstrumented = false;
53
51
  private static features?: AutoInstrumentationConfiguration;
52
+ private static _isInitialized = false;
54
53
 
55
54
  /**
56
55
  * Initializes the Datadog SDK.
@@ -72,7 +71,7 @@ export class DdSdkReactNative {
72
71
  initializationModeForTelemetry: InitializationModeForTelemetry;
73
72
  }
74
73
  ): Promise<void> => {
75
- if (DdSdkReactNative.wasInitialized) {
74
+ if (DdSdkReactNative._isInitialized) {
76
75
  InternalLog.log(
77
76
  "Can't initialize Datadog, SDK was already initialized",
78
77
  SdkVerbosity.WARN
@@ -91,7 +90,7 @@ export class DdSdkReactNative {
91
90
  DdSdkReactNative.buildConfiguration(configuration, params)
92
91
  );
93
92
  InternalLog.log('Datadog SDK was initialized', SdkVerbosity.INFO);
94
- DdSdkReactNative.wasInitialized = true;
93
+ DdSdkReactNative._isInitialized = true;
95
94
  BufferSingleton.onInitialization();
96
95
  };
97
96
 
@@ -212,6 +211,13 @@ export class DdSdkReactNative {
212
211
  return DdSdk.clearAllData();
213
212
  };
214
213
 
214
+ /**
215
+ * Returns `true` if the SDK was initialized, `false` otherwise.
216
+ */
217
+ public static isInitialized() {
218
+ return DdSdkReactNative._isInitialized;
219
+ }
220
+
215
221
  private static buildConfiguration = (
216
222
  configuration: DdSdkReactNativeConfiguration,
217
223
  params: {
@@ -56,7 +56,7 @@ jest.mock('../rum/instrumentation/DdRumErrorTracking', () => {
56
56
  });
57
57
 
58
58
  beforeEach(async () => {
59
- DdSdkReactNative['wasInitialized'] = false;
59
+ DdSdkReactNative['_isInitialized'] = false;
60
60
  DdSdkReactNative['wasAutoInstrumented'] = false;
61
61
  NativeModules.DdSdk.initialize.mockClear();
62
62
  NativeModules.DdSdk.setAttributes.mockClear();
@@ -151,7 +151,7 @@ describe('DdSdkReactNative', () => {
151
151
  '_dd.sdk_version': sdkVersion
152
152
  });
153
153
 
154
- expect(DdSdkReactNative['wasInitialized']).toBe(false);
154
+ expect(DdSdkReactNative['_isInitialized']).toBe(false);
155
155
  expect(DdRumUserInteractionTracking.startTracking).toBeCalledTimes(
156
156
  0
157
157
  );
@@ -45,7 +45,7 @@ const privateProperties = {
45
45
  'DD_SDK_VERSION',
46
46
  'DD_VERSION',
47
47
  'DD_VERSION_SUFFIX',
48
- 'wasInitialized',
48
+ '_isInitialized',
49
49
  'wasAutoInstrumented',
50
50
  'initializeNativeSDK',
51
51
  '_initializeFromDatadogProviderWithConfigurationAsync',
package/src/index.tsx CHANGED
@@ -28,6 +28,9 @@ import { RumActionType, ErrorSource, PropagatorType } from './rum/types';
28
28
  import { DatadogProvider } from './sdk/DatadogProvider/DatadogProvider';
29
29
  import { FileBasedConfiguration } from './sdk/FileBasedConfiguration/FileBasedConfiguration';
30
30
  import { DdTrace } from './trace/DdTrace';
31
+ import { DefaultTimeProvider } from './utils/time-provider/DefaultTimeProvider';
32
+ import { TimeProvider } from './utils/time-provider/TimeProvider';
33
+ import type { Timestamp } from './utils/time-provider/TimeProvider';
31
34
 
32
35
  export {
33
36
  DatadogProvider,
@@ -50,7 +53,11 @@ export {
50
53
  PropagatorType,
51
54
  UploadFrequency,
52
55
  BatchSize,
56
+ TimeProvider,
57
+ DefaultTimeProvider,
53
58
  DATADOG_GRAPH_QL_OPERATION_TYPE_HEADER,
54
59
  DATADOG_GRAPH_QL_OPERATION_NAME_HEADER,
55
60
  DATADOG_GRAPH_QL_VARIABLES_HEADER
56
61
  };
62
+
63
+ export type { Timestamp };
package/src/rum/DdRum.ts CHANGED
@@ -6,12 +6,14 @@
6
6
 
7
7
  import type { GestureResponderEvent } from 'react-native';
8
8
 
9
+ import { DdSdkReactNative } from '../DdSdkReactNative';
9
10
  import { InternalLog } from '../InternalLog';
10
11
  import { SdkVerbosity } from '../SdkVerbosity';
11
12
  import type { DdNativeRumType } from '../nativeModulesTypes';
12
13
  import { bufferVoidNativeCall } from '../sdk/DatadogProvider/Buffer/bufferNativeCall';
13
14
  import { DdSdk } from '../sdk/DdSdk';
14
- import { TimeProvider } from '../utils/TimeProvider';
15
+ import { DefaultTimeProvider } from '../utils/time-provider/DefaultTimeProvider';
16
+ import type { TimeProvider } from '../utils/time-provider/TimeProvider';
15
17
 
16
18
  import type { ActionEventMapper } from './eventMappers/actionEventMapper';
17
19
  import { generateActionEventMapper } from './eventMappers/actionEventMapper';
@@ -26,8 +28,6 @@ import type {
26
28
  ErrorSource
27
29
  } from './types';
28
30
 
29
- const timeProvider = new TimeProvider();
30
-
31
31
  const generateEmptyPromise = () => new Promise<void>(resolve => resolve());
32
32
 
33
33
  class DdRumWrapper implements DdRumType {
@@ -38,12 +38,13 @@ class DdRumWrapper implements DdRumType {
38
38
  private errorEventMapper = generateErrorEventMapper(undefined);
39
39
  private resourceEventMapper = generateResourceEventMapper(undefined);
40
40
  private actionEventMapper = generateActionEventMapper(undefined);
41
+ private timeProvider: TimeProvider = new DefaultTimeProvider();
41
42
 
42
43
  startView = (
43
44
  key: string,
44
45
  name: string,
45
46
  context: object = {},
46
- timestampMs: number = timeProvider.now()
47
+ timestampMs: number = this.timeProvider.now()
47
48
  ): Promise<void> => {
48
49
  InternalLog.log(
49
50
  `Starting RUM View “${name}” #${key}`,
@@ -57,7 +58,7 @@ class DdRumWrapper implements DdRumType {
57
58
  stopView = (
58
59
  key: string,
59
60
  context: object = {},
60
- timestampMs: number = timeProvider.now()
61
+ timestampMs: number = this.timeProvider.now()
61
62
  ): Promise<void> => {
62
63
  InternalLog.log(`Stopping RUM View #${key}`, SdkVerbosity.DEBUG);
63
64
  return bufferVoidNativeCall(() =>
@@ -69,7 +70,7 @@ class DdRumWrapper implements DdRumType {
69
70
  type: RumActionType,
70
71
  name: string,
71
72
  context: object = {},
72
- timestampMs: number = timeProvider.now()
73
+ timestampMs: number = this.timeProvider.now()
73
74
  ): Promise<void> => {
74
75
  InternalLog.log(
75
76
  `Starting RUM Action “${name}” (${type})`,
@@ -100,6 +101,10 @@ class DdRumWrapper implements DdRumType {
100
101
  return this.callNativeStopAction(...nativeCallArgs);
101
102
  };
102
103
 
104
+ setTimeProvider = (timeProvider: TimeProvider): void => {
105
+ this.timeProvider = timeProvider;
106
+ };
107
+
103
108
  private callNativeStopAction = (
104
109
  type: RumActionType,
105
110
  name: string,
@@ -157,7 +162,7 @@ class DdRumWrapper implements DdRumType {
157
162
  args[0],
158
163
  args[1],
159
164
  args[2] || {},
160
- args[3] || timeProvider.now()
165
+ args[3] || this.timeProvider.now()
161
166
  ];
162
167
  }
163
168
  if (isOldStopActionAPI(args)) {
@@ -170,7 +175,7 @@ class DdRumWrapper implements DdRumType {
170
175
  type,
171
176
  name,
172
177
  args[0] || {},
173
- args[1] || timeProvider.now()
178
+ args[1] || this.timeProvider.now()
174
179
  ];
175
180
  }
176
181
  InternalLog.log(
@@ -191,7 +196,7 @@ class DdRumWrapper implements DdRumType {
191
196
  type: RumActionType,
192
197
  name: string,
193
198
  context: object = {},
194
- timestampMs: number = timeProvider.now(),
199
+ timestampMs: number = this.timeProvider.now(),
195
200
  actionContext?: GestureResponderEvent
196
201
  ): Promise<void> => {
197
202
  const mappedEvent = this.actionEventMapper.applyEventMapper({
@@ -223,7 +228,7 @@ class DdRumWrapper implements DdRumType {
223
228
  method: string,
224
229
  url: string,
225
230
  context: object = {},
226
- timestampMs: number = timeProvider.now()
231
+ timestampMs: number = this.timeProvider.now()
227
232
  ): Promise<void> => {
228
233
  InternalLog.log(
229
234
  `Starting RUM Resource #${key} ${method}: ${url}`,
@@ -240,7 +245,7 @@ class DdRumWrapper implements DdRumType {
240
245
  kind: ResourceKind,
241
246
  size: number = -1,
242
247
  context: object = {},
243
- timestampMs: number = timeProvider.now(),
248
+ timestampMs: number = this.timeProvider.now(),
244
249
  resourceContext?: XMLHttpRequest
245
250
  ): Promise<void> => {
246
251
  const mappedEvent = this.resourceEventMapper.applyEventMapper({
@@ -293,7 +298,7 @@ class DdRumWrapper implements DdRumType {
293
298
  source: ErrorSource,
294
299
  stacktrace: string,
295
300
  context: object = {},
296
- timestampMs: number = timeProvider.now()
301
+ timestampMs: number = this.timeProvider.now()
297
302
  ): Promise<void> => {
298
303
  const mappedEvent = this.errorEventMapper.applyEventMapper({
299
304
  message,
@@ -347,6 +352,13 @@ class DdRumWrapper implements DdRumType {
347
352
  );
348
353
  };
349
354
 
355
+ async getCurrentSessionId(): Promise<string | undefined> {
356
+ if (!DdSdkReactNative.isInitialized()) {
357
+ return undefined;
358
+ }
359
+ return this.nativeRum.getCurrentSessionId();
360
+ }
361
+
350
362
  registerErrorEventMapper(errorEventMapper: ErrorEventMapper) {
351
363
  this.errorEventMapper = generateErrorEventMapper(errorEventMapper);
352
364
  }
@@ -0,0 +1,26 @@
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 type { Timestamp } from '../../utils/time-provider/TimeProvider';
8
+ import { TimeProvider } from '../../utils/time-provider/TimeProvider';
9
+
10
+ export default class MockTimeProvider extends TimeProvider {
11
+ unixMockedTime: number;
12
+ reactNativeMockedTime: number | null;
13
+
14
+ constructor(unixMockedTime: number, reactNativeMockedTime: number | null) {
15
+ super();
16
+ this.unixMockedTime = unixMockedTime;
17
+ this.reactNativeMockedTime = reactNativeMockedTime;
18
+ }
19
+
20
+ getTimestamp(): Timestamp {
21
+ return {
22
+ unix: this.unixMockedTime,
23
+ reactNative: this.reactNativeMockedTime
24
+ };
25
+ }
26
+ }
@@ -7,6 +7,7 @@
7
7
 
8
8
  import { NativeModules } from 'react-native';
9
9
 
10
+ import { DdSdkReactNative } from '../../DdSdkReactNative';
10
11
  import { BufferSingleton } from '../../sdk/DatadogProvider/Buffer/BufferSingleton';
11
12
  import { DdSdk } from '../../sdk/DdSdk';
12
13
  import { DdRum } from '../DdRum';
@@ -15,9 +16,9 @@ import type { ErrorEventMapper } from '../eventMappers/errorEventMapper';
15
16
  import type { ResourceEventMapper } from '../eventMappers/resourceEventMapper';
16
17
  import { ErrorSource, PropagatorType, RumActionType } from '../types';
17
18
 
18
- jest.mock('../../utils/TimeProvider', () => {
19
+ jest.mock('../../utils/time-provider/DefaultTimeProvider', () => {
19
20
  return {
20
- TimeProvider: jest.fn().mockImplementation(() => {
21
+ DefaultTimeProvider: jest.fn().mockImplementation(() => {
21
22
  return { now: jest.fn().mockReturnValue(456) };
22
23
  })
23
24
  };
@@ -31,9 +32,14 @@ describe('DdRum', () => {
31
32
 
32
33
  describe('DdRum.stopAction', () => {
33
34
  test('calls the native SDK when called with new API', async () => {
34
- await DdRum.stopAction('scroll', 'page', { user: 'me' }, 123);
35
+ await DdRum.stopAction(
36
+ RumActionType.SCROLL,
37
+ 'page',
38
+ { user: 'me' },
39
+ 123
40
+ );
35
41
  expect(NativeModules.DdRum.stopAction).toHaveBeenCalledWith(
36
- 'scroll',
42
+ RumActionType.SCROLL,
37
43
  'page',
38
44
  { user: 'me' },
39
45
  123
@@ -41,9 +47,9 @@ describe('DdRum', () => {
41
47
  });
42
48
 
43
49
  test('calls the native SDK when called with new API with default values', async () => {
44
- await DdRum.stopAction('scroll', 'page');
50
+ await DdRum.stopAction(RumActionType.SCROLL, 'page');
45
51
  expect(NativeModules.DdRum.stopAction).toHaveBeenCalledWith(
46
- 'scroll',
52
+ RumActionType.SCROLL,
47
53
  'page',
48
54
  {},
49
55
  456
@@ -57,10 +63,10 @@ describe('DdRum', () => {
57
63
  });
58
64
 
59
65
  test('calls the native SDK when called with old API', async () => {
60
- await DdRum.startAction('scroll', 'page_old_api');
66
+ await DdRum.startAction(RumActionType.SCROLL, 'page_old_api');
61
67
  await DdRum.stopAction({ user: 'me' }, 789);
62
68
  expect(NativeModules.DdRum.stopAction).toHaveBeenCalledWith(
63
- 'scroll',
69
+ RumActionType.SCROLL,
64
70
  'page_old_api',
65
71
  { user: 'me' },
66
72
  789
@@ -71,10 +77,10 @@ describe('DdRum', () => {
71
77
  });
72
78
 
73
79
  test('calls the native SDK when called with old API with default values', async () => {
74
- await DdRum.startAction('scroll', 'page_old_api');
80
+ await DdRum.startAction(RumActionType.SCROLL, 'page_old_api');
75
81
  await DdRum.stopAction();
76
82
  expect(NativeModules.DdRum.stopAction).toHaveBeenCalledWith(
77
- 'scroll',
83
+ RumActionType.SCROLL,
78
84
  'page_old_api',
79
85
  {},
80
86
  456
@@ -85,7 +91,7 @@ describe('DdRum', () => {
85
91
  });
86
92
 
87
93
  test('cleans the action data when stopAction is called', async () => {
88
- await DdRum.startAction('scroll', 'page_old_api');
94
+ await DdRum.startAction(RumActionType.SCROLL, 'page_old_api');
89
95
  await DdRum.stopAction();
90
96
  await DdRum.stopAction();
91
97
  expect(NativeModules.DdRum.stopAction).toHaveBeenCalledTimes(1);
@@ -101,7 +107,7 @@ describe('DdRum', () => {
101
107
  it('M add error source type W addError()', async () => {
102
108
  // Given
103
109
  const message = 'Oops I did it again!';
104
- const source = 'SOURCE';
110
+ const source = ErrorSource.SOURCE;
105
111
  const stacktrace = 'doSomething() at ./path/to/file.js:67:3';
106
112
 
107
113
  // When
@@ -123,7 +129,7 @@ describe('DdRum', () => {
123
129
  it('M add error source type W addError() {with custom attributes}', async () => {
124
130
  // Given
125
131
  const message = 'Oops I did it again!';
126
- const source = 'SOURCE';
132
+ const source = ErrorSource.SOURCE;
127
133
  const stacktrace = 'doSomething() at ./path/to/file.js:67:3';
128
134
  const random = Math.random();
129
135
  const attributes = {
@@ -369,6 +375,26 @@ describe('DdRum', () => {
369
375
  });
370
376
  });
371
377
 
378
+ describe('DdRum.getCurrentSessionId', () => {
379
+ it('calls the native API if SDK is initialized', async () => {
380
+ DdSdkReactNative['_isInitialized'] = true;
381
+ const sessionId = await DdRum.getCurrentSessionId();
382
+ expect(NativeModules.DdRum.getCurrentSessionId).toHaveBeenCalled();
383
+ expect(sessionId).toBe('test-session-id');
384
+ });
385
+ });
386
+
387
+ describe('DdRum.getCurrentSessionId', () => {
388
+ it('returns undefined if SDK is not initialized', async () => {
389
+ DdSdkReactNative['_isInitialized'] = false;
390
+ const sessionId = await DdRum.getCurrentSessionId();
391
+ expect(
392
+ NativeModules.DdRum.getCurrentSessionId
393
+ ).toHaveBeenCalledTimes(0);
394
+ expect(sessionId).toBe(undefined);
395
+ });
396
+ });
397
+
372
398
  describe('PropagatorTypes', () => {
373
399
  it('matches with the native name of propagators', () => {
374
400
  /**
@@ -0,0 +1,23 @@
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 { DdRum } from '../DdRum';
8
+ import MockTimeProvider from '../__mocks__/MockTimeProvider';
9
+
10
+ jest.unmock('../../utils/time-provider/TimeProvider');
11
+
12
+ describe('DdRum', () => {
13
+ describe('setTimeProvider', () => {
14
+ it('overrides default time provider', async () => {
15
+ const mockTimeProvider = new MockTimeProvider(1000, 2000);
16
+ DdRum.setTimeProvider(mockTimeProvider);
17
+
18
+ const timestamp = DdRum['timeProvider'].getTimestamp();
19
+ expect(timestamp.unix).toBe(1000);
20
+ expect(timestamp.reactNative).toBe(2000);
21
+ });
22
+ });
23
+ });
package/src/rum/types.ts CHANGED
@@ -147,6 +147,11 @@ export type DdRumType = {
147
147
  * Stops the current RUM Session.
148
148
  */
149
149
  stopSession(): Promise<void>;
150
+
151
+ /**
152
+ * Returns current session ID, or undefined if unavailable.
153
+ */
154
+ getCurrentSessionId(): Promise<string | undefined>;
150
155
  };
151
156
 
152
157
  /**
@@ -54,7 +54,7 @@ export class BoundedBuffer extends DatadogBuffer {
54
54
  if (this.buffer.length < this.bufferSize) {
55
55
  this.buffer.push({ callback, _type: 'VOID' });
56
56
  } else {
57
- this.addTelemetryEvent('Buffer overflow', '', 'BufferOverflow');
57
+ this.logBufferOverflow();
58
58
  }
59
59
 
60
60
  return new Promise<void>(resolve => resolve(undefined));
@@ -71,21 +71,12 @@ export class BoundedBuffer extends DatadogBuffer {
71
71
  });
72
72
  this.idTable[bufferId] = null;
73
73
  } else {
74
- this.addTelemetryEvent('Buffer overflow', '', 'BufferOverflow');
74
+ this.logBufferOverflow();
75
75
  }
76
76
 
77
77
  return new Promise<string>(resolve => resolve(bufferId));
78
- } catch (error) {
79
- this.addTelemetryEvent(
80
- 'Could not generate enough random numbers',
81
- getErrorStackTrace(error),
82
- 'RandomIdGenerationError'
83
- );
84
- // Not using InternalLog here as it is not yet instantiated
85
- console.warn(
86
- `[Datadog] Could not generate enough random numbers for RUM buffer. Please check that Math.random is not overwritten. Math.random returns: ${Math.random()}`
87
- );
88
-
78
+ } catch (error: any) {
79
+ this.logRandomIdGenerationError(error);
89
80
  return new Promise<string>(resolve => resolve(''));
90
81
  }
91
82
  };
@@ -101,7 +92,7 @@ export class BoundedBuffer extends DatadogBuffer {
101
92
  _type: 'WITH_ID'
102
93
  });
103
94
  } else {
104
- this.addTelemetryEvent('Buffer overflow', '', 'BufferOverflow');
95
+ this.logBufferOverflow();
105
96
  }
106
97
 
107
98
  return new Promise<void>(resolve => resolve(undefined));
@@ -164,6 +155,23 @@ export class BoundedBuffer extends DatadogBuffer {
164
155
  this.drainTelemetry();
165
156
  };
166
157
 
158
+ private logBufferOverflow() {
159
+ this.addTelemetryEvent('Buffer overflow', '', 'BufferOverflow');
160
+ }
161
+
162
+ private logRandomIdGenerationError = (error: any): void => {
163
+ this.addTelemetryEvent(
164
+ 'Could not generate enough random numbers',
165
+ getErrorStackTrace(error),
166
+ 'RandomIdGenerationError'
167
+ );
168
+
169
+ // Not using InternalLog here as it is not yet instantiated
170
+ console.warn(
171
+ `[Datadog] Could not generate enough random numbers for RUM buffer. Please check that Math.random is not overwritten. Math.random returns: ${Math.random()}`
172
+ );
173
+ };
174
+
167
175
  private generateRandomBufferId = (): string => {
168
176
  let tries = 0;
169
177
  while (tries < 20) {