@datadog/mobile-react-native 1.3.0 → 1.4.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 (99) hide show
  1. package/DatadogSDKReactNative.podspec +2 -2
  2. package/android/build.gradle +58 -64
  3. package/android/src/main/kotlin/com/datadog/reactnative/DdSdk.kt +36 -2
  4. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfigurationExt.kt +14 -0
  5. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +162 -16
  6. package/android/src/test/kotlin/com/datadog/tools/unit/MapExt.kt +7 -0
  7. package/ios/Sources/DdSdk.swift +2 -2
  8. package/ios/Sources/RNDdSdkConfiguration.swift +42 -0
  9. package/lib/commonjs/DdSdkReactNative.js +2 -2
  10. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  11. package/lib/commonjs/DdSdkReactNativeConfiguration.js +26 -1
  12. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  13. package/lib/commonjs/index.js +12 -0
  14. package/lib/commonjs/index.js.map +1 -1
  15. package/lib/commonjs/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js +9 -0
  16. package/lib/commonjs/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js.map +1 -1
  17. package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +1 -1
  18. package/lib/commonjs/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -1
  19. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js +59 -41
  20. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -1
  21. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracingHeaders.js +136 -0
  22. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/distributedTracingHeaders.js.map +1 -0
  23. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +39 -6
  24. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -1
  25. package/lib/commonjs/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -1
  26. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +3 -3
  27. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -1
  28. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +12 -21
  29. package/lib/commonjs/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -1
  30. package/lib/commonjs/rum/types.js +19 -1
  31. package/lib/commonjs/rum/types.js.map +1 -1
  32. package/lib/commonjs/version.js +1 -1
  33. package/lib/commonjs/version.js.map +1 -1
  34. package/lib/module/DdSdkReactNative.js +3 -3
  35. package/lib/module/DdSdkReactNative.js.map +1 -1
  36. package/lib/module/DdSdkReactNativeConfiguration.js +22 -0
  37. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  38. package/lib/module/index.js +3 -3
  39. package/lib/module/index.js.map +1 -1
  40. package/lib/module/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js +9 -0
  41. package/lib/module/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js.map +1 -1
  42. package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js +2 -2
  43. package/lib/module/rum/instrumentation/resourceTracking/DdRumResourceTracking.js.map +1 -1
  44. package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js +57 -35
  45. package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracing.js.map +1 -1
  46. package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracingHeaders.js +115 -0
  47. package/lib/module/rum/instrumentation/resourceTracking/domain/distributedTracingHeaders.js.map +1 -0
  48. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js +37 -3
  49. package/lib/module/rum/instrumentation/resourceTracking/domain/firstPartyHosts.js.map +1 -1
  50. package/lib/module/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.js.map +1 -1
  51. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js +3 -3
  52. package/lib/module/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.js.map +1 -1
  53. package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js +10 -15
  54. package/lib/module/rum/instrumentation/resourceTracking/implementation/XHRProxy.js.map +1 -1
  55. package/lib/module/rum/types.js +16 -0
  56. package/lib/module/rum/types.js.map +1 -1
  57. package/lib/module/version.js +1 -1
  58. package/lib/module/version.js.map +1 -1
  59. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +11 -3
  60. package/lib/typescript/index.d.ts +3 -3
  61. package/lib/typescript/rum/eventMappers/resourceEventMapper.d.ts +0 -1
  62. package/lib/typescript/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.d.ts +1 -0
  63. package/lib/typescript/rum/instrumentation/resourceTracking/DdRumResourceTracking.d.ts +2 -1
  64. package/lib/typescript/rum/instrumentation/resourceTracking/domain/distributedTracing.d.ts +32 -6
  65. package/lib/typescript/rum/instrumentation/resourceTracking/domain/distributedTracingHeaders.d.ts +21 -0
  66. package/lib/typescript/rum/instrumentation/resourceTracking/domain/firstPartyHosts.d.ts +4 -2
  67. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.d.ts +6 -1
  68. package/lib/typescript/rum/instrumentation/resourceTracking/domain/interfaces/RumResource.d.ts +0 -1
  69. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/XHRProxy.d.ts +0 -6
  70. package/lib/typescript/rum/instrumentation/resourceTracking/implementation/responseSize.d.ts +0 -1
  71. package/lib/typescript/rum/types.d.ts +17 -0
  72. package/lib/typescript/version.d.ts +1 -1
  73. package/package.json +2 -2
  74. package/src/DdSdkReactNative.tsx +8 -4
  75. package/src/DdSdkReactNativeConfiguration.tsx +36 -3
  76. package/src/__tests__/DdSdkReactNative.test.tsx +16 -4
  77. package/src/__tests__/DdSdkReactNativeConfiguration.test.ts +7 -6
  78. package/src/__tests__/rum/instrumentation/DdRumErrorTracking.test.tsx +2 -1
  79. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +130 -114
  80. package/src/index.tsx +6 -3
  81. package/src/rum/__tests__/DdRum.test.ts +17 -1
  82. package/src/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.tsx +8 -0
  83. package/src/rum/instrumentation/resourceTracking/DdRumResourceTracking.tsx +6 -3
  84. package/src/rum/instrumentation/resourceTracking/__tests__/DdRumResourceTracking.test.ts +15 -3
  85. package/src/rum/instrumentation/resourceTracking/domain/__tests__/distributedTracing.test.ts +64 -24
  86. package/src/rum/instrumentation/resourceTracking/domain/__tests__/firstPartyHosts.test.ts +19 -7
  87. package/src/rum/instrumentation/resourceTracking/domain/distributedTracing.tsx +82 -40
  88. package/src/rum/instrumentation/resourceTracking/domain/distributedTracingHeaders.ts +141 -0
  89. package/src/rum/instrumentation/resourceTracking/domain/firstPartyHosts.ts +43 -7
  90. package/src/rum/instrumentation/resourceTracking/domain/interfaces/RequestProxy.ts +8 -1
  91. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/ResourceReporter.ts +4 -4
  92. package/src/rum/instrumentation/resourceTracking/implementation/DatadogRumResource/__tests__/ResourceReporter.test.ts +2 -1
  93. package/src/rum/instrumentation/resourceTracking/implementation/XHRProxy.ts +8 -23
  94. package/src/rum/instrumentation/resourceTracking/implementation/__tests__/XHRProxy.test.ts +257 -74
  95. package/src/rum/types.ts +19 -0
  96. package/src/sdk/DatadogProvider/Buffer/__tests__/BufferSingleton.test.ts +2 -1
  97. package/src/sdk/DatadogProvider/__tests__/initialization.test.tsx +5 -4
  98. package/src/sdk/DatadogProvider/__tests__/initializationModes.test.tsx +6 -2
  99. package/src/version.ts +1 -1
@@ -26,6 +26,7 @@ export class DdRumUserInteractionTracking {
26
26
  private static eventsInterceptor: EventsInterceptor = new NoOpEventsInterceptor();
27
27
  private static originalCreateElement = React.createElement;
28
28
  private static originalMemo = React.memo;
29
+ private static originalJsx = null;
29
30
 
30
31
  private static patchCreateElementFunction = (
31
32
  originalFunction: typeof React.createElement,
@@ -78,6 +79,7 @@ export class DdRumUserInteractionTracking {
78
79
  try {
79
80
  const jsxRuntime = getJsxRuntime();
80
81
  const originaljsx = jsxRuntime.jsx;
82
+ this.originalJsx = originaljsx;
81
83
  jsxRuntime.jsx = (
82
84
  ...args: Parameters<typeof React.createElement>
83
85
  ): ReturnType<typeof React.createElement> => {
@@ -88,6 +90,7 @@ export class DdRumUserInteractionTracking {
88
90
  }
89
91
 
90
92
  const originalMemo = React.memo;
93
+
91
94
  React.memo = (
92
95
  component: any,
93
96
  propsAreEqual?: (prevProps: any, newProps: any) => boolean
@@ -129,5 +132,10 @@ export class DdRumUserInteractionTracking {
129
132
  React.createElement = this.originalCreateElement;
130
133
  React.memo = this.originalMemo;
131
134
  DdRumUserInteractionTracking.isTracking = false;
135
+ if (this.originalJsx) {
136
+ const jsxRuntime = getJsxRuntime();
137
+ jsxRuntime.jsx = this.originalJsx;
138
+ this.originalJsx = null;
139
+ }
132
140
  }
133
141
  }
@@ -6,8 +6,9 @@
6
6
 
7
7
  import { InternalLog } from '../../../InternalLog';
8
8
  import { SdkVerbosity } from '../../../SdkVerbosity';
9
+ import type { FirstPartyHost } from '../../types';
9
10
 
10
- import { firstPartyHostsRegexBuilder } from './domain/firstPartyHosts';
11
+ import { firstPartyHostsRegexMapBuilder } from './domain/firstPartyHosts';
11
12
  import type { RequestProxy } from './domain/interfaces/RequestProxy';
12
13
  import { ResourceReporter } from './implementation/DatadogRumResource/ResourceReporter';
13
14
  import { filterDevResource } from './implementation/DatadogRumResource/internalDevResourceBlocklist';
@@ -28,7 +29,7 @@ export class DdRumResourceTracking {
28
29
  firstPartyHosts
29
30
  }: {
30
31
  tracingSamplingRate: number;
31
- firstPartyHosts: string[];
32
+ firstPartyHosts: FirstPartyHost[];
32
33
  }): void {
33
34
  // extra safety to avoid proxying the XHR class twice
34
35
  if (DdRumResourceTracking.isTracking) {
@@ -45,7 +46,9 @@ export class DdRumResourceTracking {
45
46
  });
46
47
  this.requestProxy.onTrackingStart({
47
48
  tracingSamplingRate,
48
- firstPartyHostsRegex: firstPartyHostsRegexBuilder(firstPartyHosts)
49
+ firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder(
50
+ firstPartyHosts
51
+ )
49
52
  });
50
53
 
51
54
  InternalLog.log(
@@ -7,13 +7,15 @@
7
7
  import { NativeModules } from 'react-native';
8
8
 
9
9
  import { BufferSingleton } from '../../../../sdk/DatadogProvider/Buffer/BufferSingleton';
10
+ import { PropagatorType } from '../../../types';
10
11
  import { DdRumResourceTracking } from '../DdRumResourceTracking';
11
12
 
12
13
  import { XMLHttpRequestMock } from './__utils__/XMLHttpRequestMock';
13
14
 
14
15
  const DdRum = NativeModules.DdRum;
15
16
 
16
- const flushPromises = () => new Promise(setImmediate);
17
+ const flushPromises = () =>
18
+ new Promise(jest.requireActual('timers').setImmediate);
17
19
 
18
20
  beforeEach(() => {
19
21
  DdRum.startResource.mockClear();
@@ -40,7 +42,12 @@ describe('DdRumResourceTracking', () => {
40
42
  global.XMLHttpRequest = XMLHttpRequestMock;
41
43
  DdRumResourceTracking.startTracking({
42
44
  tracingSamplingRate: 100,
43
- firstPartyHosts: ['example.com']
45
+ firstPartyHosts: [
46
+ {
47
+ match: 'example.com',
48
+ propagatorTypes: [PropagatorType.DATADOG]
49
+ }
50
+ ]
44
51
  });
45
52
 
46
53
  // WHEN
@@ -67,7 +74,12 @@ describe('DdRumResourceTracking', () => {
67
74
  global.XMLHttpRequest = XMLHttpRequestMock;
68
75
  DdRumResourceTracking.startTracking({
69
76
  tracingSamplingRate: 100,
70
- firstPartyHosts: ['example.com']
77
+ firstPartyHosts: [
78
+ {
79
+ match: 'example.com',
80
+ propagatorTypes: [PropagatorType.DATADOG]
81
+ }
82
+ ]
71
83
  });
72
84
 
73
85
  // WHEN
@@ -1,31 +1,71 @@
1
- import { generateTraceId } from '../distributedTracing';
1
+ import { TraceIdentifier } from '../distributedTracing';
2
2
 
3
- it('M return an unique identifier W toString', async () => {
4
- // GIVEN
5
- const generatedIds = new Set<string>();
6
- const iterations = 100;
7
- let counter = iterations;
3
+ describe('TraceIdentifier', () => {
4
+ it('M return an unique identifier W toString', async () => {
5
+ // GIVEN
6
+ const generatedIds = new Set<string>();
7
+ const iterations = 100;
8
+ let counter = iterations;
8
9
 
9
- // WHEN
10
- while (counter-- > 0) {
11
- generatedIds.add(generateTraceId());
12
- }
10
+ // WHEN
11
+ while (counter-- > 0) {
12
+ generatedIds.add(new TraceIdentifier().toString(10));
13
+ }
13
14
 
14
- // THEN
15
- expect(generatedIds.size).toBe(iterations);
16
- });
15
+ // THEN
16
+ expect(generatedIds.size).toBe(iterations);
17
+ });
17
18
 
18
- it('M return an 64 bits positive integer W toString', async () => {
19
- let iterations = 100;
20
- while (iterations-- > 0) {
21
- // GIVEN
22
- const id = generateTraceId();
19
+ it('M return an 64 bits positive integer W toString', async () => {
20
+ let iterations = 100;
21
+ while (iterations-- > 0) {
22
+ // GIVEN
23
+ const id = new TraceIdentifier().toString(10);
23
24
 
24
- // THEN
25
- expect(id).toMatch(/[1-9]{1,19}/);
26
- // should be less than the max 64 bits integer
27
- if (id.length === 19) {
28
- expect(id < '9223372036854775807').toBeTruthy();
25
+ // THEN
26
+ expect(id).toMatch(/[1-9]{1,19}/);
27
+ // should be less than the max 64 bits integer
28
+ if (id.length === 19) {
29
+ expect(id < '9223372036854775807').toBeTruthy();
30
+ }
31
+ }
32
+ });
33
+
34
+ it('M return an 64 bits positive hex W toString(16)', async () => {
35
+ let iterations = 100;
36
+ while (iterations-- > 0) {
37
+ // GIVEN
38
+ const trace = new TraceIdentifier();
39
+ const id = trace.toString(16);
40
+ const paddedId = trace.toPaddedString(16, 16);
41
+
42
+ // THEN
43
+ expect(id).toMatch(/[1-9a-f]{1,16}/);
44
+ expect(paddedId).toMatch(/[0-9a-f]{16}/);
45
+ }
46
+ });
47
+
48
+ it('M return an 64 bits positive padded hex W toPaddedString(16, 32)', async () => {
49
+ let iterations = 100;
50
+ while (iterations-- > 0) {
51
+ // GIVEN
52
+ const id = new TraceIdentifier().toPaddedString(16, 32);
53
+
54
+ // THEN
55
+ expect(id).not.toMatch(/[0]{32}/);
56
+ expect(id).toMatch(/[0]{16}[0-9a-f]{16}/);
57
+ }
58
+ });
59
+
60
+ it('M return original string hex W toPaddedString(16, 10)', async () => {
61
+ let iterations = 100;
62
+ while (iterations-- > 0) {
63
+ // GIVEN
64
+ const id = new TraceIdentifier().toPaddedString(16, 10);
65
+
66
+ // THEN
67
+ expect(id).not.toMatch(/[0]{10}/);
68
+ expect(id).toMatch(/[0-9a-f]{10}/);
29
69
  }
30
- }
70
+ });
31
71
  });
@@ -4,19 +4,31 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import { firstPartyHostsRegexBuilder } from '../firstPartyHosts';
7
+ import { PropagatorType } from '../../../../types';
8
+ import { firstPartyHostsRegexMapBuilder } from '../firstPartyHosts';
8
9
 
9
10
  describe('firstPartyHosts', () => {
10
- describe('firstPartyHostsRegexBuilder', () => {
11
+ describe('firstPartyHostsRegexMapBuilder', () => {
11
12
  it('returns a RegExp that matches hosts', () => {
12
- const regex = firstPartyHostsRegexBuilder(['api.example.com']);
13
- expect(regex.test('api.example.com')).toBe(true);
14
- expect(regex.test('api.myapi.com')).toBe(false);
13
+ const regexMap = firstPartyHostsRegexMapBuilder([
14
+ {
15
+ match: 'api.example.com',
16
+ propagatorTypes: [PropagatorType.DATADOG]
17
+ }
18
+ ]);
19
+ expect(regexMap[0].propagatorType).toBe('datadog');
20
+ expect(regexMap[0].regex.test('api.example.com')).toBe(true);
21
+ expect(regexMap[0].regex.test('api.myapi.com')).toBe(false);
15
22
  });
16
23
 
17
24
  it('escapes special characters in hosts', () => {
18
- const regex = firstPartyHostsRegexBuilder(['api.example.com']);
19
- expect(regex.test('apiiexample.com')).toBe(false);
25
+ const regexMap = firstPartyHostsRegexMapBuilder([
26
+ {
27
+ match: 'api.example.com',
28
+ propagatorTypes: [PropagatorType.DATADOG]
29
+ }
30
+ ]);
31
+ expect(regexMap[0].regex.test('apiiexample.com')).toBe(false);
20
32
  });
21
33
  });
22
34
  });
@@ -4,16 +4,20 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
+ import type { PropagatorType } from '../../../types';
8
+
7
9
  import type { Hostname } from './firstPartyHosts';
8
- import { isHostFirstParty } from './firstPartyHosts';
10
+ import { getPropagatorsForHost } from './firstPartyHosts';
11
+ import type { RegexMap } from './interfaces/RequestProxy';
9
12
 
10
13
  export type DdRumResourceTracingAttributes =
11
14
  | {
12
15
  tracingStrategy: 'KEEP';
13
- traceId: string;
14
- spanId: string;
15
- samplingPriorityHeader: '1';
16
+ traceId: TraceId;
17
+ spanId: SpanId;
18
+ samplingPriorityHeader: '1' | '0';
16
19
  rulePsr: number;
20
+ propagatorTypes: PropagatorType[];
17
21
  }
18
22
  | {
19
23
  tracingStrategy: 'DISCARD';
@@ -29,61 +33,99 @@ const DISCARDED_TRACE_ATTRIBUTES: DdRumResourceTracingAttributes = {
29
33
 
30
34
  export const getTracingAttributes = ({
31
35
  hostname,
32
- firstPartyHostsRegex,
36
+ firstPartyHostsRegexMap,
33
37
  tracingSamplingRate
34
38
  }: {
35
39
  hostname: Hostname | null;
36
- firstPartyHostsRegex: RegExp;
40
+ firstPartyHostsRegexMap: RegexMap;
37
41
  tracingSamplingRate: number;
38
42
  }): DdRumResourceTracingAttributes => {
39
43
  if (hostname === null) {
40
44
  return DISCARDED_TRACE_ATTRIBUTES;
41
45
  }
42
- if (isHostFirstParty(hostname, firstPartyHostsRegex)) {
43
- return generateTracingAttributesWithSampling(tracingSamplingRate);
46
+ const propagatorsForHost = getPropagatorsForHost(
47
+ hostname,
48
+ firstPartyHostsRegexMap
49
+ );
50
+ if (propagatorsForHost) {
51
+ return generateTracingAttributesWithSampling(
52
+ tracingSamplingRate,
53
+ propagatorsForHost
54
+ );
44
55
  }
45
56
  return DISCARDED_TRACE_ATTRIBUTES;
46
57
  };
47
58
 
48
59
  const generateTracingAttributesWithSampling = (
49
- tracingSamplingRate: number
60
+ tracingSamplingRate: number,
61
+ propagatorTypes: PropagatorType[]
50
62
  ): DdRumResourceTracingAttributes => {
51
- if (Math.random() * 100 <= tracingSamplingRate) {
52
- return {
53
- traceId: generateTraceId(),
54
- spanId: generateTraceId(),
55
- samplingPriorityHeader: '1',
56
- tracingStrategy: 'KEEP',
57
- rulePsr: tracingSamplingRate / 100
58
- };
59
- }
60
- return DISCARDED_TRACE_ATTRIBUTES;
63
+ const isSampled = Math.random() * 100 <= tracingSamplingRate;
64
+ const tracingAttributes: DdRumResourceTracingAttributes = {
65
+ traceId: new TraceIdentifier() as TraceId,
66
+ spanId: new TraceIdentifier() as SpanId,
67
+ samplingPriorityHeader: isSampled ? '1' : '0',
68
+ tracingStrategy: 'KEEP',
69
+ rulePsr: tracingSamplingRate / 100,
70
+ propagatorTypes
71
+ };
72
+
73
+ return tracingAttributes;
61
74
  };
62
75
 
63
- /*
64
- * This code was inspired from browser-sdk at (https://github.com/DataDog/browser-sdk/blob/master/packages/rum-core/src/domain/tracing/tracer.ts#L107)
76
+ /**
77
+ * Using branded types will ensure we don't accidentally use
78
+ * traceId for spanId when generating headers.
65
79
  */
66
- export function generateTraceId(): string {
67
- const radix = 10;
68
- const MAX_32_BITS_NUMBER = 4294967295; // 2^32-1
69
- let low: number = Math.floor(Math.random() * MAX_32_BITS_NUMBER);
70
- let high: number = Math.floor(Math.random() * MAX_32_BITS_NUMBER);
71
- let str = '';
80
+ export type TraceId = TraceIdentifier & {
81
+ _brand: 'traceId';
82
+ };
72
83
 
73
- while (high > 0 && low > 0) {
74
- // Create an intermediate value with the same modulo as the combined high and low value
75
- // but requiring 36 bits max (32 for the low value + 4 for the high part)
76
- const modH = high % radix;
77
- const temp = (modH << 32) + low;
78
- const digit = temp % radix;
84
+ export type SpanId = TraceIdentifier & {
85
+ _brand: 'spanId';
86
+ };
79
87
 
80
- // update the high value
81
- high = (high - modH) / radix; // we reuse the modH to avoid the need of a floor op
82
- // the low value reuses the previous temp value to account for the "missing mod" in the high update
83
- low = (temp - digit) / radix; // we reuse the digit to avoid the need of a floor op
88
+ /*
89
+ * This code was inspired from browser-sdk at (https://github.com/DataDog/browser-sdk/blob/0e9722d5b06f6d49264bc82cd254a207d647d66c/packages/rum-core/src/domain/tracing/tracer.ts#L190)
90
+ */
91
+ const MAX_32_BITS_NUMBER = 4294967295; // 2^32-1
92
+ const MAX_31_BITS_NUMBER = 2147483647; // 2^31-1
93
+ export class TraceIdentifier {
94
+ private low: number;
95
+ private high: number;
84
96
 
85
- // update the string from right to left
86
- str = digit.toString() + str;
97
+ constructor() {
98
+ // We need to have a 63 bits number max
99
+ this.high = Math.floor(Math.random() * MAX_31_BITS_NUMBER);
100
+ this.low = Math.floor(Math.random() * MAX_32_BITS_NUMBER);
87
101
  }
88
- return str;
102
+
103
+ toString = (radix: number) => {
104
+ let low = this.low;
105
+ let high = this.high;
106
+ let str = '';
107
+
108
+ while (high > 0 || low > 0) {
109
+ const mod = (high % radix) * (MAX_32_BITS_NUMBER + 1) + low;
110
+ high = Math.floor(high / radix);
111
+ low = Math.floor(mod / radix);
112
+ str = (mod % radix).toString(radix) + str;
113
+ }
114
+ return str;
115
+ };
116
+
117
+ /**
118
+ * This function pads the trace with `0`.
119
+ * It should not be used with a `length` lower than the trace, as we return the full trace in this case.
120
+ * @param radix radix for the trace
121
+ * @param length minimum length
122
+ * @returns padded string
123
+ */
124
+ toPaddedString = (radix: number, length: number) => {
125
+ const traceId = this.toString(radix);
126
+ if (traceId.length > length) {
127
+ return traceId;
128
+ }
129
+ return Array(length - traceId.length + 1).join('0') + traceId;
130
+ };
89
131
  }
@@ -0,0 +1,141 @@
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 { PropagatorType } from '../../../types';
8
+
9
+ import type {
10
+ DdRumResourceTracingAttributes,
11
+ SpanId,
12
+ TraceId
13
+ } from './distributedTracing';
14
+
15
+ export const SAMPLING_PRIORITY_HEADER_KEY = 'x-datadog-sampling-priority';
16
+ /**
17
+ * Datadog headers
18
+ */
19
+ export const ORIGIN_HEADER_KEY = 'x-datadog-origin';
20
+ export const ORIGIN_RUM = 'rum';
21
+ export const TRACE_ID_HEADER_KEY = 'x-datadog-trace-id';
22
+ export const PARENT_ID_HEADER_KEY = 'x-datadog-parent-id';
23
+ /**
24
+ * OTel headers
25
+ */
26
+ export const TRACECONTEXT_HEADER_KEY = 'traceparent';
27
+ export const B3_HEADER_KEY = 'b3';
28
+ export const B3_MULTI_TRACE_ID_HEADER_KEY = 'X-B3-TraceId';
29
+ export const B3_MULTI_SPAN_ID_HEADER_KEY = 'X-B3-SpanId';
30
+ export const B3_MULTI_SAMPLED_HEADER_KEY = 'X-B3-Sampled';
31
+
32
+ export const getTracingHeaders = (
33
+ tracingAttributes: DdRumResourceTracingAttributes
34
+ ): { header: string; value: string }[] => {
35
+ const headers: { header: string; value: string }[] = [];
36
+ if (tracingAttributes.tracingStrategy === 'DISCARD') {
37
+ return headers;
38
+ }
39
+ tracingAttributes.propagatorTypes.forEach(propagator => {
40
+ switch (propagator) {
41
+ case PropagatorType.DATADOG: {
42
+ headers.push(
43
+ {
44
+ header: ORIGIN_HEADER_KEY,
45
+ value: ORIGIN_RUM
46
+ },
47
+ {
48
+ header: SAMPLING_PRIORITY_HEADER_KEY,
49
+ value: tracingAttributes.samplingPriorityHeader
50
+ },
51
+ {
52
+ header: TRACE_ID_HEADER_KEY,
53
+ value: tracingAttributes.traceId.toString(10)
54
+ },
55
+ {
56
+ header: PARENT_ID_HEADER_KEY,
57
+ value: tracingAttributes.spanId.toString(10)
58
+ }
59
+ );
60
+ break;
61
+ }
62
+ case PropagatorType.TRACECONTEXT: {
63
+ headers.push({
64
+ header: TRACECONTEXT_HEADER_KEY,
65
+ value: generateTraceContextHeader({
66
+ version: '00',
67
+ traceId: tracingAttributes.traceId,
68
+ parentId: tracingAttributes.spanId,
69
+ isSampled:
70
+ tracingAttributes.samplingPriorityHeader === '1'
71
+ })
72
+ });
73
+ break;
74
+ }
75
+ case PropagatorType.B3: {
76
+ headers.push({
77
+ header: B3_HEADER_KEY,
78
+ value: generateB3Header({
79
+ traceId: tracingAttributes.traceId,
80
+ spanId: tracingAttributes.spanId,
81
+ isSampled:
82
+ tracingAttributes.samplingPriorityHeader === '1'
83
+ })
84
+ });
85
+ break;
86
+ }
87
+ case PropagatorType.B3MULTI: {
88
+ headers.push(
89
+ {
90
+ header: B3_MULTI_TRACE_ID_HEADER_KEY,
91
+ value: tracingAttributes.traceId.toPaddedString(16, 32)
92
+ },
93
+ {
94
+ header: B3_MULTI_SPAN_ID_HEADER_KEY,
95
+ value: tracingAttributes.spanId.toPaddedString(16, 16)
96
+ },
97
+ {
98
+ header: B3_MULTI_SAMPLED_HEADER_KEY,
99
+ value: tracingAttributes.samplingPriorityHeader
100
+ }
101
+ );
102
+ }
103
+ }
104
+ });
105
+
106
+ return headers;
107
+ };
108
+
109
+ const generateTraceContextHeader = ({
110
+ version,
111
+ traceId,
112
+ parentId,
113
+ isSampled
114
+ }: {
115
+ version: string;
116
+ traceId: TraceId;
117
+ parentId: SpanId;
118
+ isSampled: boolean;
119
+ }) => {
120
+ const flags = isSampled ? '01' : '00';
121
+ return `${version}-${traceId.toPaddedString(
122
+ 16,
123
+ 32
124
+ )}-${parentId.toPaddedString(16, 16)}-${flags}`;
125
+ };
126
+
127
+ const generateB3Header = ({
128
+ traceId,
129
+ spanId,
130
+ isSampled
131
+ }: {
132
+ traceId: TraceId;
133
+ spanId: SpanId;
134
+ isSampled: boolean;
135
+ }) => {
136
+ const flags = isSampled ? '1' : '0';
137
+ return `${traceId.toPaddedString(16, 32)}-${spanId.toPaddedString(
138
+ 16,
139
+ 16
140
+ )}-${flags}`;
141
+ };
@@ -6,6 +6,9 @@
6
6
 
7
7
  import { InternalLog } from '../../../../InternalLog';
8
8
  import { SdkVerbosity } from '../../../../SdkVerbosity';
9
+ import type { FirstPartyHost, PropagatorType } from '../../../types';
10
+
11
+ import type { RegexMap } from './interfaces/RequestProxy';
9
12
 
10
13
  export type Hostname = { _type: 'Hostname' } & string;
11
14
 
@@ -17,9 +20,36 @@ const escapeRegExp = (string: string) => {
17
20
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
18
21
  };
19
22
 
20
- export const firstPartyHostsRegexBuilder = (
21
- firstPartyHosts: string[]
22
- ): RegExp => {
23
+ export const firstPartyHostsRegexMapBuilder = (
24
+ firstPartyHosts: FirstPartyHost[]
25
+ ): RegexMap => {
26
+ const hostsMap: Record<PropagatorType, string[]> = {
27
+ datadog: [],
28
+ tracecontext: [],
29
+ b3: [],
30
+ b3multi: []
31
+ };
32
+
33
+ firstPartyHosts.forEach(host => {
34
+ host.propagatorTypes.forEach(propagatorType => {
35
+ hostsMap[propagatorType].push(host.match);
36
+ });
37
+ });
38
+
39
+ const regexMap: { regex: RegExp; propagatorType: PropagatorType }[] = [];
40
+ Object.entries(hostsMap).forEach(([propagatorType, hosts]) => {
41
+ if (hosts.length > 0) {
42
+ regexMap.push({
43
+ propagatorType: propagatorType as PropagatorType,
44
+ regex: firstPartyHostsRegexBuilder(hosts)
45
+ });
46
+ }
47
+ });
48
+
49
+ return regexMap;
50
+ };
51
+
52
+ const firstPartyHostsRegexBuilder = (firstPartyHosts: string[]): RegExp => {
23
53
  if (firstPartyHosts.length === 0) {
24
54
  return NO_MATCH_REGEX;
25
55
  }
@@ -44,9 +74,15 @@ export const firstPartyHostsRegexBuilder = (
44
74
  }
45
75
  };
46
76
 
47
- export const isHostFirstParty = (
77
+ export const getPropagatorsForHost = (
48
78
  hostname: Hostname,
49
- firstPartyHostsRegex: RegExp
50
- ): boolean => {
51
- return firstPartyHostsRegex.test(hostname);
79
+ firstPartyHostsRegexMap: RegexMap
80
+ ): PropagatorType[] | null => {
81
+ const matchedPropagatorTypes: PropagatorType[] = [];
82
+ firstPartyHostsRegexMap.forEach(({ regex, propagatorType }) => {
83
+ if (regex.test(hostname)) {
84
+ matchedPropagatorTypes.push(propagatorType);
85
+ }
86
+ });
87
+ return matchedPropagatorTypes.length > 0 ? matchedPropagatorTypes : null;
52
88
  };
@@ -4,11 +4,18 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
+ import type { PropagatorType } from '../../../../types';
8
+
7
9
  export interface RequestProxyOptions {
8
10
  tracingSamplingRate: number;
9
- firstPartyHostsRegex: RegExp;
11
+ firstPartyHostsRegexMap: RegexMap;
10
12
  }
11
13
 
14
+ export type RegexMap = {
15
+ regex: RegExp;
16
+ propagatorType: PropagatorType;
17
+ }[];
18
+
12
19
  export abstract class RequestProxy {
13
20
  abstract onTrackingStart: (context: RequestProxyOptions) => void;
14
21
  abstract onTrackingStop: () => void;
@@ -34,12 +34,12 @@ export class ResourceReporter {
34
34
 
35
35
  const formatResourceStartContext = (
36
36
  tracingAttributes: RUMResource['tracingAttributes']
37
- ): Record<string, unknown> | undefined => {
38
- return tracingAttributes.tracingStrategy === 'DISCARD'
37
+ ): Record<string, string | number> | undefined => {
38
+ return tracingAttributes.samplingPriorityHeader === '0'
39
39
  ? undefined
40
40
  : {
41
- '_dd.span_id': tracingAttributes.spanId,
42
- '_dd.trace_id': tracingAttributes.traceId,
41
+ '_dd.span_id': tracingAttributes.spanId.toString(10),
42
+ '_dd.trace_id': tracingAttributes.traceId.toString(10),
43
43
  '_dd.rule_psr': tracingAttributes.rulePsr
44
44
  };
45
45
  };
@@ -14,7 +14,8 @@ import { ResourceMockFactory } from './__utils__/ResourceMockFactory';
14
14
 
15
15
  const resourceMockFactory = new ResourceMockFactory();
16
16
  const DdRum = NativeModules.DdRum;
17
- const flushPromises = () => new Promise(setImmediate);
17
+ const flushPromises = () =>
18
+ new Promise(jest.requireActual('timers').setImmediate);
18
19
 
19
20
  beforeEach(() => {
20
21
  DdRum.startResource.mockClear();