@datadog/mobile-react-native 1.0.0-rc2 → 1.0.0-rc5

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 (108) hide show
  1. package/DatadogSDKReactNative.podspec +1 -1
  2. package/README.md +8 -121
  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 +63 -0
  12. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  13. package/lib/commonjs/DdSdkReactNativeConfiguration.js +9 -1
  14. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  15. package/lib/commonjs/InternalLog.js +51 -0
  16. package/lib/commonjs/InternalLog.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/SdkVerbosity.js +26 -0
  20. package/lib/commonjs/SdkVerbosity.js.map +1 -0
  21. package/lib/commonjs/Timer.js +99 -0
  22. package/lib/commonjs/Timer.js.map +1 -0
  23. package/lib/commonjs/foundation.js +102 -45
  24. package/lib/commonjs/foundation.js.map +1 -1
  25. package/lib/commonjs/index.js +28 -6
  26. package/lib/commonjs/index.js.map +1 -1
  27. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js +6 -2
  28. package/lib/commonjs/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  29. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js +61 -14
  30. package/lib/commonjs/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  31. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js +110 -17
  32. package/lib/commonjs/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  33. package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js +16 -4
  34. package/lib/commonjs/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
  35. package/lib/commonjs/rum/instrumentation/DdRumXhr.js +4 -0
  36. package/lib/commonjs/rum/instrumentation/NoOpEventsInterceptor.js +1 -1
  37. package/lib/commonjs/rum/instrumentation/NoOpEventsInterceptor.js.map +1 -1
  38. package/lib/commonjs/types.js.map +1 -1
  39. package/lib/commonjs/version.js +10 -0
  40. package/lib/commonjs/version.js.map +1 -0
  41. package/lib/module/DdSdkReactNative.js +54 -1
  42. package/lib/module/DdSdkReactNative.js.map +1 -1
  43. package/lib/module/DdSdkReactNativeConfiguration.js +9 -1
  44. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  45. package/lib/module/InternalLog.js +46 -0
  46. package/lib/module/InternalLog.js.map +1 -0
  47. package/lib/module/ProxyConfiguration.js +46 -0
  48. package/lib/module/ProxyConfiguration.js.map +1 -0
  49. package/lib/module/SdkVerbosity.js +18 -0
  50. package/lib/module/SdkVerbosity.js.map +1 -0
  51. package/lib/module/Timer.js +90 -0
  52. package/lib/module/Timer.js.map +1 -0
  53. package/lib/module/foundation.js +69 -44
  54. package/lib/module/foundation.js.map +1 -1
  55. package/lib/module/index.js +3 -1
  56. package/lib/module/index.js.map +1 -1
  57. package/lib/module/rum/instrumentation/DdEventsInterceptor.js +5 -1
  58. package/lib/module/rum/instrumentation/DdEventsInterceptor.js.map +1 -1
  59. package/lib/module/rum/instrumentation/DdRumErrorTracking.js +56 -14
  60. package/lib/module/rum/instrumentation/DdRumErrorTracking.js.map +1 -1
  61. package/lib/module/rum/instrumentation/DdRumResourceTracking.js +99 -17
  62. package/lib/module/rum/instrumentation/DdRumResourceTracking.js.map +1 -1
  63. package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js +12 -4
  64. package/lib/module/rum/instrumentation/DdRumUserInteractionTracking.js.map +1 -1
  65. package/lib/module/rum/instrumentation/DdRumXhr.js +1 -1
  66. package/lib/module/rum/instrumentation/NoOpEventsInterceptor.js +1 -1
  67. package/lib/module/rum/instrumentation/NoOpEventsInterceptor.js.map +1 -1
  68. package/lib/module/types.js.map +1 -1
  69. package/lib/module/version.js +3 -0
  70. package/lib/module/version.js.map +1 -0
  71. package/lib/typescript/DdSdkReactNative.d.ts +9 -0
  72. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +5 -2
  73. package/lib/typescript/InternalLog.d.ts +6 -0
  74. package/lib/typescript/ProxyConfiguration.d.ts +56 -0
  75. package/lib/typescript/SdkVerbosity.d.ts +9 -0
  76. package/lib/typescript/Timer.d.ts +20 -0
  77. package/lib/typescript/index.d.ts +3 -1
  78. package/lib/typescript/rum/instrumentation/DdRumErrorTracking.d.ts +2 -0
  79. package/lib/typescript/rum/instrumentation/DdRumResourceTracking.d.ts +4 -0
  80. package/lib/typescript/rum/instrumentation/DdRumXhr.d.ts +2 -2
  81. package/lib/typescript/types.d.ts +12 -11
  82. package/lib/typescript/version.d.ts +1 -0
  83. package/package.json +3 -3
  84. package/src/DdSdkReactNative.tsx +57 -8
  85. package/src/DdSdkReactNativeConfiguration.tsx +6 -3
  86. package/src/InternalLog.tsx +48 -0
  87. package/src/ProxyConfiguration.tsx +37 -0
  88. package/src/{NativeSdkVerbosity.tsx → SdkVerbosity.tsx} +2 -2
  89. package/src/Timer.ts +96 -0
  90. package/src/__tests__/DdRumWrapper.test.tsx +91 -0
  91. package/src/__tests__/DdSdkReactNative.test.tsx +213 -13
  92. package/src/__tests__/InternalLog.test.tsx +382 -0
  93. package/src/__tests__/Timer.test.ts +145 -0
  94. package/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx +155 -12
  95. package/src/__tests__/rum/instrumentation/DdRumResourceTracking.test.tsx +270 -26
  96. package/src/foundation.tsx +43 -33
  97. package/src/index.tsx +4 -2
  98. package/src/rum/instrumentation/DdRumErrorTracking.tsx +68 -27
  99. package/src/rum/instrumentation/DdRumResourceTracking.tsx +96 -22
  100. package/src/rum/instrumentation/DdRumUserInteractionTracking.tsx +4 -0
  101. package/src/rum/instrumentation/DdRumXhr.tsx +6 -5
  102. package/src/types.tsx +12 -11
  103. package/src/version.ts +2 -0
  104. package/lib/commonjs/NativeSdkVerbosity.js +0 -26
  105. package/lib/commonjs/NativeSdkVerbosity.js.map +0 -1
  106. package/lib/module/NativeSdkVerbosity.js +0 -18
  107. package/lib/module/NativeSdkVerbosity.js.map +0 -1
  108. package/lib/typescript/NativeSdkVerbosity.d.ts +0 -9
@@ -6,21 +6,30 @@
6
6
 
7
7
  import { NativeModules } from 'react-native';
8
8
  import { DdSdkConfiguration, DdSdkType, DdLogsType, DdTraceType, DdRumType } from './types';
9
+ import {InternalLog} from "./InternalLog"
10
+ import {SdkVerbosity} from "./SdkVerbosity";
9
11
 
10
12
  class DdLogsWrapper implements DdLogsType {
11
13
 
12
14
  private nativeLogs: DdLogsType = NativeModules.DdLogs;
13
15
 
14
16
  debug(message: string, context: object = {}): Promise<void> {
17
+ InternalLog.log("Tracking debug log “" + message + "”", SdkVerbosity.DEBUG);
15
18
  return this.nativeLogs.debug(message, context);
16
19
  }
20
+
17
21
  info(message: string, context: object = {}): Promise<void> {
22
+ InternalLog.log("Tracking info log “" + message + "”", SdkVerbosity.DEBUG);
18
23
  return this.nativeLogs.info(message, context);
19
24
  }
25
+
20
26
  warn(message: string, context: object = {}): Promise<void> {
27
+ InternalLog.log("Tracking warn log “" + message + "”", SdkVerbosity.DEBUG);
21
28
  return this.nativeLogs.warn(message, context);
22
29
  }
30
+
23
31
  error(message: string, context: object = {}): Promise<void> {
32
+ InternalLog.log("Tracking error log “" + message + "”", SdkVerbosity.DEBUG);
24
33
  return this.nativeLogs.error(message, context);
25
34
  }
26
35
 
@@ -31,16 +40,15 @@ class DdTraceWrapper implements DdTraceType {
31
40
  private nativeTrace: DdTraceType = NativeModules.DdTrace;
32
41
 
33
42
  startSpan(operation: string, context: object = {}, timestampMs: number = Date.now()): Promise<string> {
34
- // TODO swap context and stacktrace in the native call, requires native bridges update
35
- // @ts-ignore
36
- return this.nativeTrace.startSpan(operation, timestampMs, context);
43
+ let spanId = this.nativeTrace.startSpan(operation, context, timestampMs);
44
+ InternalLog.log("Starting span “" + operation + "” #" + spanId, SdkVerbosity.DEBUG);
45
+ return spanId
37
46
  }
47
+
38
48
  finishSpan(spanId: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
39
- // TODO swap context and stacktrace in the native call, requires native bridges update
40
- // @ts-ignore
41
- return this.nativeTrace.finishSpan(spanId, timestampMs, context);
49
+ InternalLog.log("Finishing span #" + spanId, SdkVerbosity.DEBUG);
50
+ return this.nativeTrace.finishSpan(spanId, context, timestampMs);
42
51
  }
43
-
44
52
  }
45
53
 
46
54
  class DdRumWrapper implements DdRumType {
@@ -48,49 +56,51 @@ class DdRumWrapper implements DdRumType {
48
56
  private nativeRum: DdRumType = NativeModules.DdRum;
49
57
 
50
58
  startView(key: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
51
- // TODO swap context and stacktrace in the native call, requires native bridges update
52
- // @ts-ignore
53
- return this.nativeRum.startView(key, name, timestampMs, context);
59
+ InternalLog.log("Starting RUM View “" + name + "” #" + key, SdkVerbosity.DEBUG);
60
+ return this.nativeRum.startView(key, name, context, timestampMs);
54
61
  }
62
+
55
63
  stopView(key: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
56
- // TODO swap context and stacktrace in the native call, requires native bridges update
57
- // @ts-ignore
58
- return this.nativeRum.stopView(key, timestampMs, context);
64
+ InternalLog.log("Stopping RUM View #" + key, SdkVerbosity.DEBUG);
65
+ return this.nativeRum.stopView(key, context, timestampMs);
59
66
  }
67
+
60
68
  startAction(type: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
61
- // TODO swap context and stacktrace in the native call, requires native bridges update
62
- // @ts-ignore
63
- return this.nativeRum.startAction(type, name, timestampMs, context);
69
+ InternalLog.log("Starting RUM Action “" + name + "” (" + type + ")", SdkVerbosity.DEBUG);
70
+ return this.nativeRum.startAction(type, name, context, timestampMs);
64
71
  }
72
+
65
73
  stopAction(context: object = {}, timestampMs: number = Date.now()): Promise<void> {
66
- // TODO swap context and stacktrace in the native call, requires native bridges update
67
- // @ts-ignore
68
- return this.nativeRum.stopAction(timestampMs, context);
74
+ InternalLog.log("Stopping current RUM Action", SdkVerbosity.DEBUG);
75
+ return this.nativeRum.stopAction(context, timestampMs);
69
76
  }
77
+
70
78
  addAction(type: string, name: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
71
- // TODO swap context and stacktrace in the native call, requires native bridges update
72
- // @ts-ignore
73
- return this.nativeRum.addAction(type, name, timestampMs, context);
79
+ InternalLog.log("Adding RUM Action “" + name + "” (" + type + ")", SdkVerbosity.DEBUG);
80
+ return this.nativeRum.addAction(type, name, context, timestampMs);
74
81
  }
82
+
75
83
  startResource(key: string, method: string, url: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
76
- // TODO swap context and stacktrace in the native call, requires native bridges update
77
- // @ts-ignore
78
- return this.nativeRum.startResource(key, method, url, timestampMs, context);
84
+ InternalLog.log("Starting RUM Resource #" + key + " " + method + ": " + url, SdkVerbosity.DEBUG);
85
+ return this.nativeRum.startResource(key, method, url, context, timestampMs);
79
86
  }
80
- stopResource(key: string, statusCode: number, kind: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
81
- // TODO swap context and stacktrace in the native call, requires native bridges update
82
- // @ts-ignore
83
- return this.nativeRum.stopResource(key, statusCode, kind, timestampMs, context);
87
+
88
+ stopResource(key: string, statusCode: number, kind: string, size: number = -1, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
89
+ InternalLog.log("Stopping RUM Resource #" + key + " status:" + statusCode, SdkVerbosity.DEBUG);
90
+ return this.nativeRum.stopResource(key, statusCode, kind, size, context, timestampMs);
84
91
  }
92
+
85
93
  addError(message: string, source: string, stacktrace: string, context: object = {}, timestampMs: number = Date.now()): Promise<void> {
86
- // TODO swap context and stacktrace in the native call, requires native bridges update
87
- // @ts-ignore
88
- return this.nativeRum.addError(message, source, stacktrace, timestampMs, context);
94
+ InternalLog.log("Adding RUM Error “" + message + "”", SdkVerbosity.DEBUG);
95
+ let updatedContext: any = context;
96
+ updatedContext["_dd.error.source_type"] = "react-native";
97
+ return this.nativeRum.addError(message, source, stacktrace, updatedContext, timestampMs);
89
98
  }
99
+
90
100
  addTiming(name: string): Promise<void> {
101
+ InternalLog.log("Adding timing “" + name + "” to RUM View", SdkVerbosity.DEBUG);
91
102
  return this.nativeRum.addTiming(name);
92
103
  }
93
-
94
104
  }
95
105
 
96
106
  const DdSdk: DdSdkType = NativeModules.DdSdk;
package/src/index.tsx CHANGED
@@ -8,6 +8,8 @@ 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';
13
+ import { SdkVerbosity } from './SdkVerbosity'
12
14
 
13
- export { DdLogs, DdTrace, DdRum, DdSdkReactNativeConfiguration, DdSdkReactNative, TrackingConsent };
15
+ export { DdLogs, DdTrace, DdRum, DdSdkReactNativeConfiguration, DdSdkReactNative, ProxyConfiguration, ProxyType, TrackingConsent, SdkVerbosity };
@@ -4,9 +4,13 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
+ import type { ErrorHandlerCallback } from 'react-native';
7
8
  import { DdRum } from '../../foundation';
9
+ import {InternalLog} from "../../InternalLog";
10
+ import {SdkVerbosity} from "../../SdkVerbosity";
8
11
 
9
12
 
13
+ const EMPTY_MESSAGE = "Unknown Error"
10
14
  const EMPTY_STACK_TRACE = ""
11
15
  const TYPE_SOURCE = "SOURCE"
12
16
  const TYPE_CONSOLE = "CONSOLE"
@@ -16,13 +20,15 @@ const TYPE_CONSOLE = "CONSOLE"
16
20
  */
17
21
  export class DdRumErrorTracking {
18
22
 
19
- private static isTracking = false
23
+ private static isTracking = false;
20
24
 
21
- // eslint-disable-next-line
22
- private static defaultErrorHandler = (_error: any, _isFatal?: boolean) => {}
25
+ private static isInDefaultErrorHandler = false;
23
26
 
24
- // eslint-disable-next-line
25
- private static defaultConsoleError = (..._params: unknown[]) => {}
27
+ // eslint-disable-next-line
28
+ private static defaultErrorHandler: ErrorHandlerCallback = (_error: any, _isFatal?: boolean) => { }
29
+
30
+ // eslint-disable-next-line
31
+ private static defaultConsoleError = (..._params: unknown[]) => { }
26
32
 
27
33
  /**
28
34
  * Starts tracking errors and sends a RUM Error event every time an error is detected.
@@ -30,74 +36,109 @@ export class DdRumErrorTracking {
30
36
  static startTracking(): void {
31
37
  // extra safety to avoid wrapping the Error handler twice
32
38
  if (DdRumErrorTracking.isTracking) {
33
- console.log("DdRumErrorTracking already started");
39
+ InternalLog.log("Datadog SDK is already tracking errors", SdkVerbosity.WARN);
34
40
  return
35
41
  }
36
-
42
+
37
43
  if (ErrorUtils) {
38
44
  DdRumErrorTracking.defaultErrorHandler = ErrorUtils.getGlobalHandler();
39
45
  DdRumErrorTracking.defaultConsoleError = console.error;
40
46
 
41
-
47
+
42
48
  ErrorUtils.setGlobalHandler(DdRumErrorTracking.onGlobalError);
43
49
  console.error = DdRumErrorTracking.onConsoleError;
44
50
 
45
51
  DdRumErrorTracking.isTracking = true;
52
+ InternalLog.log("Datadog SDK is tracking errors", SdkVerbosity.INFO);
53
+ } else {
54
+ InternalLog.log("Datadog SDK cannot track errors, ErrorUtils is not defined", SdkVerbosity.ERROR);
46
55
  }
47
56
 
48
57
  }
49
58
 
50
59
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
51
- static onGlobalError(error: any, isFatal?: boolean): void {
60
+ static onGlobalError(error: any, isFatal?: boolean): void {
61
+ const message = DdRumErrorTracking.getErrorMessage(error);
62
+ const stacktrace = DdRumErrorTracking.getErrorStackTrace(error);
52
63
  DdRum.addError(
53
- String(error),
54
- TYPE_SOURCE,
55
- DdRumErrorTracking.getErrorStackTrace(error),
64
+ message,
65
+ TYPE_SOURCE,
66
+ stacktrace,
56
67
  { "_dd.error.is_crash": isFatal, "_dd.error.raw": error }
57
68
  ).then(() => {
58
- DdRumErrorTracking.defaultErrorHandler(error, isFatal);
69
+ DdRumErrorTracking.isInDefaultErrorHandler = true;
70
+ try {
71
+ DdRumErrorTracking.defaultErrorHandler(error, isFatal);
72
+ } finally {
73
+ DdRumErrorTracking.isInDefaultErrorHandler = false;
74
+ }
59
75
  });
60
76
  }
61
77
 
62
- static onConsoleError(...params: unknown[]): void {
63
- let stack: string = EMPTY_STACK_TRACE
78
+ static onConsoleError(...params: unknown[]): void {
79
+
80
+ if (DdRumErrorTracking.isInDefaultErrorHandler) {
81
+ return;
82
+ }
83
+
84
+ let stack: string = EMPTY_STACK_TRACE;
64
85
  for (let i = 0; i < params.length; i += 1) {
65
86
  const param = params[i];
66
- const paramStack = DdRumErrorTracking.getErrorStackTrace(param)
67
- if (paramStack != undefined && paramStack != EMPTY_STACK_TRACE){
87
+ const paramStack = DdRumErrorTracking.getErrorStackTrace(param);
88
+ if (paramStack != undefined && paramStack != EMPTY_STACK_TRACE) {
68
89
  stack = paramStack;
69
90
  break;
70
91
  }
71
92
  }
72
93
 
73
94
  const message = params.map((param) => {
74
- if (typeof param === 'string') { return param }
75
- else if (param instanceof Error) { return String(param) }
76
- else { return JSON.stringify(param)}
95
+ if (typeof param === 'string') { return param; }
96
+ else { return DdRumErrorTracking.getErrorMessage(param); }
77
97
  }).join(' ');
78
98
 
79
-
99
+
80
100
  DdRum.addError(
81
- message,
101
+ message,
82
102
  TYPE_CONSOLE,
83
103
  stack
84
104
  ).then(() => {
85
105
  DdRumErrorTracking.defaultConsoleError.apply(console, params);
86
106
  });
87
-
107
+
108
+ }
109
+
110
+ private static getErrorMessage(error: any | undefined): string {
111
+ let message = EMPTY_MESSAGE;
112
+ if (error == undefined) {
113
+ message = EMPTY_MESSAGE;
114
+ } else if (typeof error == 'object' && "message" in error){
115
+ message = String(error.message);
116
+ } else {
117
+ message = String(error);
118
+ }
119
+
120
+ return message
88
121
  }
89
122
 
90
123
  private static getErrorStackTrace(error: any | undefined): string {
91
124
  let stack = EMPTY_STACK_TRACE
125
+
92
126
  if (error == undefined) {
93
127
  stack = EMPTY_STACK_TRACE;
94
128
  } else if (typeof error === 'string') {
95
129
  stack = EMPTY_STACK_TRACE;
96
- } else if ('componentStack' in error) {
97
- stack = String(error.componentStack);
98
- } else if (('sourceURL' in error) && ('line' in error) && ('column' in error)) {
99
- stack = `at ${error.sourceURL}:${error.line}:${error.column}`;
130
+ } else if (typeof error === 'object') {
131
+ if ('componentStack' in error) {
132
+ stack = String(error.componentStack);
133
+ } else if ('stacktrace' in error) {
134
+ stack = String(error.stacktrace);
135
+ } else if ('stack' in error) {
136
+ stack = String(error.stack);
137
+ } else if (('sourceURL' in error) && ('line' in error) && ('column' in error)) {
138
+ stack = `at ${error.sourceURL}:${error.line}:${error.column}`;
139
+ }
100
140
  }
141
+
101
142
  return stack
102
143
  }
103
144
  }
@@ -6,15 +6,21 @@
6
6
 
7
7
  import { Platform } from 'react-native';
8
8
  import { DdRum } from '../../foundation'
9
+ import Timer from '../../Timer';
9
10
  import type { DdRumXhr } from './DdRumXhr'
10
- import { generateTraceId } from './TraceIdentifier';
11
+ import { generateTraceId } from './TraceIdentifier'
12
+ import {InternalLog} from "../../InternalLog"
13
+ import {SdkVerbosity} from "../../SdkVerbosity"
11
14
 
12
15
  export const TRACE_ID_HEADER_KEY = "x-datadog-trace-id"
13
16
  export const PARENT_ID_HEADER_KEY = "x-datadog-parent-id"
14
17
  export const ORIGIN_HEADER_KEY = "x-datadog-origin"
18
+ export const SAMPLING_PRIORITY_HEADER_KEY = "x-datadog-sampling-priority"
19
+ export const SAMPLED_HEADER_KEY = "x-datadog-sampled"
15
20
  export const ORIGIN_RUM = "rum"
16
21
 
17
- const MISSING_TIME = -1
22
+ const RESPONSE_START_LABEL = "response_start"
23
+ const MISSING_RESOURCE_SIZE = -1
18
24
 
19
25
  interface Timing {
20
26
  /**
@@ -38,11 +44,7 @@ interface ResourceTimings {
38
44
  }
39
45
 
40
46
  function createTimings(startTime: number,
41
- responseStartTime: number, responseEndTime: number): ResourceTimings | null {
42
-
43
- if (startTime === MISSING_TIME || responseStartTime === MISSING_TIME) {
44
- return null
45
- }
47
+ responseStartTime: number, responseEndTime: number): ResourceTimings {
46
48
 
47
49
  const firstByte = formatTiming(startTime, startTime, responseStartTime)
48
50
  const download = formatTiming(startTime, responseStartTime, responseEndTime)
@@ -74,6 +76,70 @@ function timeToNanos(durationMs: number): number {
74
76
  return +(durationMs * 1e6).toFixed(0)
75
77
  }
76
78
 
79
+ function byteLength(str: string): number {
80
+ // This is a weird trick, but it works.
81
+ // Output is the same as TextEncoder.encode(...).length
82
+ return unescape(encodeURI(str)).length;
83
+ }
84
+
85
+ function getResponseContentLengthFromHeader(xhr: XMLHttpRequest): number | null {
86
+ const contentLengthHeader = parseInt(xhr.getResponseHeader('Content-Length') ?? '', 10);
87
+ if (!isNaN(contentLengthHeader)) {
88
+ return contentLengthHeader;
89
+ }
90
+ return null;
91
+ }
92
+
93
+ export function calculateResponseSize(xhr: XMLHttpRequest): number {
94
+
95
+ const contentLengthHeader = getResponseContentLengthFromHeader(xhr);
96
+ if (contentLengthHeader != null) {
97
+ return contentLengthHeader as number;
98
+ }
99
+
100
+ const response = xhr.response
101
+ if (!response) {
102
+ return MISSING_RESOURCE_SIZE;
103
+ }
104
+
105
+ let size;
106
+ try {
107
+ switch (xhr.responseType) {
108
+ case '':
109
+ case 'text':
110
+ // String
111
+ size = byteLength(response);
112
+ break;
113
+ case 'blob':
114
+ size = response.size;
115
+ break;
116
+ case 'arraybuffer':
117
+ size = response.byteLength;
118
+ break;
119
+ case 'document':
120
+ // currently not supported by RN as of 0.66
121
+ // HTML Document or XML Document
122
+ break;
123
+ case 'json':
124
+ // plain JS object
125
+ // original size was lost, because this is the object which was parsed.
126
+ // We can only convert back to the string and calculate the size,
127
+ // which will roughly match original.
128
+ size = byteLength(JSON.stringify(response))
129
+ break;
130
+ default:
131
+ break;
132
+ }
133
+ } catch (e) {
134
+ console.error("Couldn't get resource size, because of the error", e);
135
+ }
136
+
137
+ if (typeof size !== 'number') {
138
+ return MISSING_RESOURCE_SIZE;
139
+ }
140
+ return size;
141
+ }
142
+
77
143
  /**
78
144
  * Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
79
145
  */
@@ -98,13 +164,15 @@ export class DdRumResourceTracking {
98
164
  static startTrackingInternal(xhrType: any): void {
99
165
  // extra safety to avoid proxying the XHR class twice
100
166
  if (DdRumResourceTracking.isTracking) {
167
+ InternalLog.log("Datadog SDK is already tracking XHR resources", SdkVerbosity.WARN);
101
168
  return
102
169
  }
103
170
 
104
171
  DdRumResourceTracking.originalXhrOpen = xhrType.prototype.open;
105
172
  DdRumResourceTracking.originalXhrSend = xhrType.prototype.send;
106
173
 
107
- DdRumResourceTracking.proxyXhr(xhrType)
174
+ DdRumResourceTracking.proxyXhr(xhrType);
175
+ InternalLog.log("Datadog SDK is tracking XHR resources", SdkVerbosity.INFO);
108
176
  }
109
177
 
110
178
 
@@ -131,10 +199,9 @@ export class DdRumResourceTracking {
131
199
  const traceId = generateTraceId()
132
200
  this._datadog_xhr = {
133
201
  method,
134
- startTime: MISSING_TIME,
135
- responseStartTime: MISSING_TIME,
136
202
  url: url,
137
203
  reported: false,
204
+ timer: new Timer(),
138
205
  spanId: spanId,
139
206
  traceId: traceId
140
207
  }
@@ -149,11 +216,12 @@ export class DdRumResourceTracking {
149
216
 
150
217
  if (this._datadog_xhr) {
151
218
  // keep track of start time
152
- this._datadog_xhr.startTime = Date.now()
219
+ this._datadog_xhr.timer.start()
153
220
  this.setRequestHeader(TRACE_ID_HEADER_KEY, this._datadog_xhr.traceId)
154
221
  this.setRequestHeader(PARENT_ID_HEADER_KEY, this._datadog_xhr.spanId)
155
222
  this.setRequestHeader(ORIGIN_HEADER_KEY, ORIGIN_RUM)
156
-
223
+ this.setRequestHeader(SAMPLING_PRIORITY_HEADER_KEY, "1")
224
+ this.setRequestHeader(SAMPLED_HEADER_KEY, "1")
157
225
  }
158
226
 
159
227
  DdRumResourceTracking.proxyOnReadyStateChange(this, xhrType);
@@ -171,9 +239,8 @@ export class DdRumResourceTracking {
171
239
  DdRumResourceTracking.reportXhr(xhrProxy);
172
240
  xhrProxy._datadog_xhr.reported = true;
173
241
  }
174
- } else if (xhrProxy.readyState === xhrType.HEADERS_RECEIVED
175
- && xhrProxy._datadog_xhr.responseStartTime === MISSING_TIME) {
176
- xhrProxy._datadog_xhr.responseStartTime = Date.now()
242
+ } else if (xhrProxy.readyState === xhrType.HEADERS_RECEIVED) {
243
+ xhrProxy._datadog_xhr.timer.recordTick(RESPONSE_START_LABEL)
177
244
  }
178
245
 
179
246
  if (originalOnreadystatechange) {
@@ -185,13 +252,13 @@ export class DdRumResourceTracking {
185
252
 
186
253
  private static reportXhr(xhrProxy: DdRumXhr): void {
187
254
 
255
+ const responseSize = calculateResponseSize(xhrProxy)
256
+
188
257
  const context = xhrProxy._datadog_xhr
189
258
 
190
- const key = context.startTime + "/"
191
- + context.method + "/"
192
- + context.startTime
259
+ const key = context.timer.startTime + "/" + context.method
193
260
 
194
- const responseEndTime = Date.now()
261
+ context.timer.stop()
195
262
 
196
263
  DdRum.startResource(
197
264
  key,
@@ -201,16 +268,23 @@ export class DdRumResourceTracking {
201
268
  "_dd.span_id": context.spanId,
202
269
  "_dd.trace_id": context.traceId
203
270
  },
204
- context.startTime
271
+ context.timer.startTime
205
272
  ).then(() => {
206
273
  DdRum.stopResource(
207
274
  key,
208
275
  xhrProxy.status,
209
276
  "xhr",
277
+ responseSize,
210
278
  {
211
- "_dd.resource_timings": createTimings(context.startTime, context.responseStartTime, responseEndTime),
279
+ "_dd.resource_timings": context.timer.hasTickFor(RESPONSE_START_LABEL) ?
280
+ createTimings(
281
+ context.timer.startTime,
282
+ context.timer.timeAt(RESPONSE_START_LABEL),
283
+ context.timer.stopTime
284
+ ) : null
212
285
  },
213
- responseEndTime);
286
+ context.timer.stopTime
287
+ );
214
288
  })
215
289
  }
216
290
 
@@ -8,6 +8,8 @@ import React from 'react'
8
8
  import { DdEventsInterceptor } from './DdEventsInterceptor'
9
9
  import NoOpEventsInterceptor from './NoOpEventsInterceptor'
10
10
  import type EventsInterceptor from './EventsInterceptor'
11
+ import {InternalLog} from "../../InternalLog"
12
+ import {SdkVerbosity} from "../../SdkVerbosity"
11
13
 
12
14
 
13
15
  const PROPERTY_FUNCTION_TYPE = "function"
@@ -29,6 +31,7 @@ export class DdRumUserInteractionTracking {
29
31
  static startTracking(): void {
30
32
  // extra safety to avoid wrapping more than 1 time this function
31
33
  if (DdRumUserInteractionTracking.isTracking) {
34
+ InternalLog.log("Datadog SDK is already tracking interactions", SdkVerbosity.WARN);
32
35
  return
33
36
  }
34
37
  DdRumUserInteractionTracking.eventsInterceptor = new DdEventsInterceptor()
@@ -45,6 +48,7 @@ export class DdRumUserInteractionTracking {
45
48
  return original(element, props, ...children)
46
49
  }
47
50
  DdRumUserInteractionTracking.isTracking = true
51
+ InternalLog.log("Datadog SDK is tracking interactions", SdkVerbosity.INFO);
48
52
  }
49
53
 
50
54
  }
@@ -4,6 +4,8 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
+ import type Timer from "../../Timer";
8
+
7
9
 
8
10
  export interface DdRumXhr extends XMLHttpRequest {
9
11
 
@@ -12,11 +14,10 @@ export interface DdRumXhr extends XMLHttpRequest {
12
14
  }
13
15
 
14
16
  export interface DdRumXhrContext {
15
- method: string
16
- url: string
17
- startTime: number,
18
- responseStartTime: number,
17
+ method: string,
18
+ url: string,
19
19
  reported: boolean,
20
+ timer: Timer,
20
21
  spanId: string,
21
22
  traceId: string
22
- }
23
+ }
package/src/types.tsx CHANGED
@@ -92,7 +92,7 @@ export type DdTraceType = {
92
92
  * Start a span, and returns a unique identifier for the span.
93
93
  * @param operation: The operation name of the span.
94
94
  * @param context: The additional context to send.
95
- * @param timestampMs: The timestamp when the operation started (in milliseconds).
95
+ * @param timestampMs: The timestamp when the operation started (in milliseconds). If not provided, current timestamp will be used.
96
96
  */
97
97
  startSpan(operation: string, context?: object, timestampMs?: number): Promise<string>;
98
98
 
@@ -100,7 +100,7 @@ export type DdTraceType = {
100
100
  * Finish a started span.
101
101
  * @param spanId: The unique identifier of the span.
102
102
  * @param context: The additional context to send.
103
- * @param timestampMs: The timestamp when the operation stopped (in milliseconds).
103
+ * @param timestampMs: The timestamp when the operation stopped (in milliseconds). If not provided, current timestamp will be used.
104
104
  */
105
105
  finishSpan(spanId: string, context?: object, timestampMs?: number): Promise<void>;
106
106
 
@@ -115,7 +115,7 @@ export type DdRumType = {
115
115
  * @param key: The view unique key identifier.
116
116
  * @param name: The view name.
117
117
  * @param context: The additional context to send.
118
- * @param timestampMs: The timestamp when the view started (in milliseconds).
118
+ * @param timestampMs: The timestamp when the view started (in milliseconds). If not provided, current timestamp will be used.
119
119
  */
120
120
  startView(key: string, name: string, context?: object, timestampMs?: number): Promise<void>;
121
121
 
@@ -123,7 +123,7 @@ export type DdRumType = {
123
123
  * Stop tracking a RUM View.
124
124
  * @param key: The view unique key identifier.
125
125
  * @param context: The additional context to send.
126
- * @param timestampMs: The timestamp when the view stopped (in milliseconds).
126
+ * @param timestampMs: The timestamp when the view stopped (in milliseconds). If not provided, current timestamp will be used.
127
127
  */
128
128
  stopView(key: string, context?: object, timestampMs?: number): Promise<void>;
129
129
 
@@ -132,14 +132,14 @@ export type DdRumType = {
132
132
  * @param type: The action type (tap, scroll, swipe, click, custom).
133
133
  * @param name: The action name.
134
134
  * @param context: The additional context to send.
135
- * @param timestampMs: The timestamp when the action started (in milliseconds).
135
+ * @param timestampMs: The timestamp when the action started (in milliseconds). If not provided, current timestamp will be used.
136
136
  */
137
137
  startAction(type: string, name: string, context?: object, timestampMs?: number): Promise<void>;
138
138
 
139
139
  /**
140
140
  * Stop tracking the ongoing RUM Action.
141
141
  * @param context: The additional context to send.
142
- * @param timestampMs: The timestamp when the action stopped (in milliseconds).
142
+ * @param timestampMs: The timestamp when the action stopped (in milliseconds). If not provided, current timestamp will be used.
143
143
  */
144
144
  stopAction(context?: object, timestampMs?: number): Promise<void>;
145
145
 
@@ -148,7 +148,7 @@ export type DdRumType = {
148
148
  * @param type: The action type (tap, scroll, swipe, click, custom).
149
149
  * @param name: The action name.
150
150
  * @param context: The additional context to send.
151
- * @param timestampMs: The timestamp when the action occurred (in milliseconds).
151
+ * @param timestampMs: The timestamp when the action occurred (in milliseconds). If not provided, current timestamp will be used.
152
152
  */
153
153
  addAction(type: string, name: string, context?: object, timestampMs?: number): Promise<void>;
154
154
 
@@ -158,7 +158,7 @@ export type DdRumType = {
158
158
  * @param method: The resource method (GET, POST, …).
159
159
  * @param url: The resource url.
160
160
  * @param context: The additional context to send.
161
- * @param timestampMs: The timestamp when the resource started (in milliseconds).
161
+ * @param timestampMs: The timestamp when the resource started (in milliseconds). If not provided, current timestamp will be used.
162
162
  */
163
163
  startResource(key: string, method: string, url: string, context?: object, timestampMs?: number): Promise<void>;
164
164
 
@@ -167,10 +167,11 @@ export type DdRumType = {
167
167
  * @param key: The resource unique key identifier.
168
168
  * @param statusCode: The resource status code.
169
169
  * @param kind: The resource's kind (xhr, document, image, css, font, …).
170
+ * @param size: The resource size in bytes. If size is unknown, use -1.
170
171
  * @param context: The additional context to send.
171
- * @param timestampMs: The timestamp when the resource stopped (in milliseconds).
172
+ * @param timestampMs: The timestamp when the resource stopped (in milliseconds). If not provided, current timestamp will be used.
172
173
  */
173
- stopResource(key: string, statusCode: number, kind: string, context?: object, timestampMs?: number): Promise<void>;
174
+ stopResource(key: string, statusCode: number, kind: string, size?: number, context?: object, timestampMs?: number): Promise<void>;
174
175
 
175
176
  /**
176
177
  * Add a RUM Error.
@@ -178,7 +179,7 @@ export type DdRumType = {
178
179
  * @param source: The error source (network, source, console, logger, …).
179
180
  * @param stacktrace: The error stacktrace.
180
181
  * @param context: The additional context to send.
181
- * @param timestampMs: The timestamp when the error occurred (in milliseconds).
182
+ * @param timestampMs: The timestamp when the error occurred (in milliseconds). If not provided, current timestamp will be used.
182
183
  */
183
184
  addError(message: string, source: string, stacktrace: string, context?: object, timestampMs?: number): Promise<void>;
184
185
 
package/src/version.ts ADDED
@@ -0,0 +1,2 @@
1
+ // generated by genversion
2
+ export const version = '1.0.0-rc5';