@datadog/mobile-react-native 2.6.0 → 2.6.2

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 (114) hide show
  1. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfiguration.kt +8 -2
  2. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkConfigurationExt.kt +2 -1
  3. package/android/src/main/kotlin/com/datadog/reactnative/DdSdkNativeInitialization.kt +14 -0
  4. package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
  5. package/android/src/test/kotlin/com/datadog/reactnative/DdSdkTest.kt +49 -0
  6. package/android/src/test/kotlin/com/datadog/tools/unit/DdSdkConfigurationExt.kt +7 -0
  7. package/android/src/test/kotlin/com/datadog/tools/unit/forge/DdSdkConfigurationForgeryFactory.kt +8 -1
  8. package/ios/Sources/DdSdkConfiguration.swift +9 -1
  9. package/ios/Sources/DdSdkNativeInitialization.swift +3 -1
  10. package/ios/Sources/RNDdSdkConfiguration.swift +24 -2
  11. package/ios/Sources/SdkVersion.swift +1 -1
  12. package/jest/mock.js +5 -2
  13. package/lib/commonjs/DdSdkReactNative.js +1 -1
  14. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  15. package/lib/commonjs/DdSdkReactNativeConfiguration.js +20 -2
  16. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  17. package/lib/commonjs/index.js +40 -1
  18. package/lib/commonjs/index.js.map +1 -1
  19. package/lib/commonjs/logs/DdLogs.js +2 -2
  20. package/lib/commonjs/logs/DdLogs.js.map +1 -1
  21. package/lib/commonjs/rum/DdRum.js +14 -6
  22. package/lib/commonjs/rum/DdRum.js.map +1 -1
  23. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingContext.js +77 -0
  24. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingContext.js.map +1 -0
  25. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingIdentifier.js +44 -0
  26. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingIdentifier.js.map +1 -0
  27. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js +13 -11
  28. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js.map +1 -1
  29. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js +10 -4
  30. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js.map +1 -1
  31. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js +36 -3
  32. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js.map +1 -1
  33. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.js +1 -1
  34. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.js.map +1 -1
  35. package/lib/commonjs/rum/types.js.map +1 -1
  36. package/lib/commonjs/types.js +3 -1
  37. package/lib/commonjs/types.js.map +1 -1
  38. package/lib/commonjs/version.js +1 -1
  39. package/lib/module/DdSdkReactNative.js +1 -1
  40. package/lib/module/DdSdkReactNative.js.map +1 -1
  41. package/lib/module/DdSdkReactNativeConfiguration.js +19 -1
  42. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  43. package/lib/module/index.js +7 -3
  44. package/lib/module/index.js.map +1 -1
  45. package/lib/module/logs/DdLogs.js +2 -2
  46. package/lib/module/logs/DdLogs.js.map +1 -1
  47. package/lib/module/rum/DdRum.js +15 -7
  48. package/lib/module/rum/DdRum.js.map +1 -1
  49. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingContext.js +70 -0
  50. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingContext.js.map +1 -0
  51. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingIdentifier.js +37 -0
  52. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingIdentifier.js.map +1 -0
  53. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js +12 -10
  54. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js.map +1 -1
  55. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js +7 -3
  56. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js.map +1 -1
  57. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js +32 -1
  58. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js.map +1 -1
  59. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.js +2 -2
  60. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.js.map +1 -1
  61. package/lib/module/rum/types.js.map +1 -1
  62. package/lib/module/types.js +3 -1
  63. package/lib/module/types.js.map +1 -1
  64. package/lib/module/version.js +1 -1
  65. package/lib/typescript/DdSdkReactNative.d.ts.map +1 -1
  66. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +25 -0
  67. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts.map +1 -1
  68. package/lib/typescript/index.d.ts +6 -3
  69. package/lib/typescript/index.d.ts.map +1 -1
  70. package/lib/typescript/rum/DdRum.d.ts +7 -3
  71. package/lib/typescript/rum/DdRum.d.ts.map +1 -1
  72. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingContext.d.ts +45 -0
  73. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingContext.d.ts.map +1 -0
  74. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingIdentifier.d.ts +24 -0
  75. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingIdentifier.d.ts.map +1 -0
  76. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.d.ts +4 -3
  77. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.d.ts.map +1 -1
  78. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.d.ts +1 -0
  79. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.d.ts.map +1 -1
  80. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.d.ts +6 -1
  81. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.d.ts.map +1 -1
  82. package/lib/typescript/rum/types.d.ts +31 -6
  83. package/lib/typescript/rum/types.d.ts.map +1 -1
  84. package/lib/typescript/types.d.ts +4 -1
  85. package/lib/typescript/types.d.ts.map +1 -1
  86. package/lib/typescript/version.d.ts +1 -1
  87. package/package.json +5 -2
  88. package/src/DdSdkReactNative.tsx +3 -1
  89. package/src/DdSdkReactNativeConfiguration.tsx +31 -1
  90. package/src/__tests__/DdSdkReactNativeConfiguration.test.ts +6 -0
  91. package/src/index.tsx +16 -3
  92. package/src/logs/DdLogs.ts +2 -2
  93. package/src/rum/DdRum.ts +38 -15
  94. package/src/rum/__tests__/DdRum.test.ts +633 -10
  95. package/src/rum/__tests__/__utils__/TracingHeadersUtils.ts +166 -0
  96. package/src/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingContext.ts +81 -0
  97. package/src/rum/instrumentation/resourceTracking/distributedTracing/DatadogTracingIdentifier.ts +49 -0
  98. package/src/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.tsx +19 -13
  99. package/src/rum/instrumentation/resourceTracking/distributedTracing/__tests__/__utils__/TracingIdentifierUtils.ts +10 -6
  100. package/src/rum/instrumentation/resourceTracking/distributedTracing/__tests__/distributedTracing.test.ts +2 -0
  101. package/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.tsx +9 -3
  102. package/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.ts +71 -1
  103. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/XHRProxy.ts +2 -2
  104. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts +2 -1
  105. package/src/rum/types.ts +42 -7
  106. package/src/sdk/DatadogProvider/__tests__/initialization.test.tsx +2 -0
  107. package/src/sdk/FileBasedConfiguration/__tests__/FileBasedConfiguration.test.ts +4 -0
  108. package/src/types.tsx +5 -1
  109. package/src/version.ts +1 -1
  110. package/lib/commonjs/polyfills.js +0 -28
  111. package/lib/commonjs/polyfills.js.map +0 -1
  112. package/lib/module/polyfills.js +0 -26
  113. package/lib/module/polyfills.js.map +0 -1
  114. package/src/polyfills.js +0 -37
@@ -0,0 +1,166 @@
1
+ /* eslint-disable @typescript-eslint/ban-ts-comment */
2
+ /*
3
+ * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
4
+ * This product includes software developed at Datadog (https://www.datadoghq.com/).
5
+ * Copyright 2016-Present Datadog, Inc.
6
+ */
7
+
8
+ import type { DatadogTracingContext } from '../../instrumentation/resourceTracking/distributedTracing/DatadogTracingContext';
9
+ import { TracingIdFormat } from '../../instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
10
+ import { TracingIdentifierUtils } from '../../instrumentation/resourceTracking/distributedTracing/__tests__/__utils__/TracingIdentifierUtils';
11
+
12
+ type Header = { header: string; value: string };
13
+
14
+ export const verifyRumResourceContext = (
15
+ tracingContext: DatadogTracingContext,
16
+ resourceContext?: Record<string, string | number>
17
+ ) => {
18
+ const rumResourceContext =
19
+ resourceContext ?? tracingContext.getRumResourceContext();
20
+ const traceId = rumResourceContext['_dd.trace_id'];
21
+ const spanId = rumResourceContext['_dd.span_id'];
22
+
23
+ expect(traceId).toBe(
24
+ tracingContext.traceId?.toString(TracingIdFormat.paddedHex)
25
+ );
26
+
27
+ expect(spanId).toBe(
28
+ tracingContext.spanId?.toString(TracingIdFormat.decimal)
29
+ );
30
+ };
31
+
32
+ export const verifyDatadogHeaders = (
33
+ headersArray: Header[],
34
+ isSampled: boolean
35
+ ) => {
36
+ const headers = getHeadersMapFromArray(headersArray);
37
+
38
+ // x-datadog-origin
39
+ const originHeader = headers.get('x-datadog-origin') as string;
40
+ expect(originHeader).toBeDefined();
41
+ expect(originHeader).toBe('rum');
42
+
43
+ // x-datadog-sampling-priority
44
+ const samplingPriorityHeader = headers.get(
45
+ 'x-datadog-sampling-priority'
46
+ ) as string;
47
+ expect(samplingPriorityHeader).toBeDefined();
48
+ expect(samplingPriorityHeader).toBe(isSampled ? '1' : '0');
49
+
50
+ // x-datadog-trace-id
51
+ const traceIdHeader = headers.get('x-datadog-trace-id') as string;
52
+ expect(traceIdHeader).toBeDefined();
53
+ expect(traceIdHeader).toMatch(/^(?:\d+|\d*\.\d+)$/);
54
+ expect(TracingIdentifierUtils.isWithin64Bits(traceIdHeader)).toBe(true);
55
+
56
+ // x-datadog-parent-id
57
+ const parentIdHeader = headers.get('x-datadog-parent-id') as string;
58
+ expect(parentIdHeader).toBeDefined();
59
+ expect(parentIdHeader).toMatch(/^(?:\d+|\d*\.\d+)$/);
60
+ expect(TracingIdentifierUtils.isWithin64Bits(parentIdHeader)).toBe(true);
61
+
62
+ // x-datadog-tags
63
+ const tagsHeader = headers.get('x-datadog-tags') as string;
64
+ expect(tagsHeader).toBeDefined();
65
+ expect(tagsHeader).toMatch(/^_dd\.p\.tid=[0-9a-fA-F]{16}$/);
66
+ expect(
67
+ TracingIdentifierUtils.isWithin64Bits(tagsHeader.split('=')[1])
68
+ ).toBe(true);
69
+ };
70
+
71
+ export const verifyTraceContextHeaders = (
72
+ headersArray: Header[],
73
+ isSampled: boolean
74
+ ) => {
75
+ const headers = getHeadersMapFromArray(headersArray);
76
+
77
+ // traceparent (example: 00-67a9ce4b00000000566c8ec4106d55b4-b69743e07c0f3fb5-01)
78
+ const traceParentHeader = headers.get('traceparent') as string;
79
+ expect(traceParentHeader).toBeDefined();
80
+
81
+ const traceParentParts = traceParentHeader.split('-');
82
+ expect(traceParentParts).toHaveLength(4);
83
+
84
+ const version = traceParentParts[0];
85
+ expect(version).toBe('00');
86
+
87
+ const traceId = traceParentParts[1];
88
+ expect(traceId).toMatch(/^[0-9a-fA-F]{32}$/);
89
+ expect(TracingIdentifierUtils.isWithin128Bits(traceId, 16)).toBe(true);
90
+
91
+ const parentId = traceParentParts[2];
92
+ expect(parentId).toMatch(/^[0-9a-fA-F]{16}$/);
93
+ expect(TracingIdentifierUtils.isWithin64Bits(parentId, 16)).toBe(true);
94
+
95
+ const flags = traceParentParts[3];
96
+ expect(flags).toBe(isSampled ? '01' : '00');
97
+
98
+ // tracestate (example: dd=s:1;o:rum;p:b69743e07c0f3fb5)
99
+ const traceStateHeader = headers.get('tracestate') as string;
100
+ expect(traceStateHeader).toBeDefined();
101
+
102
+ const traceStateParts = traceStateHeader.split(';');
103
+ expect(traceStateParts).toHaveLength(3);
104
+
105
+ const datadogFlags = traceStateParts[0];
106
+ expect(datadogFlags.split('=')[1]).toBe(`s:${isSampled ? 1 : 0}`);
107
+
108
+ const origin = traceStateParts[1];
109
+ expect(origin.split(':')[1]).toBe('rum');
110
+
111
+ const parent = traceStateParts[2];
112
+ expect(parent.split(':')[1]).toMatch(/^[0-9a-fA-F]{16}$/);
113
+ expect(
114
+ TracingIdentifierUtils.isWithin64Bits(parent.split(':')[1], 16)
115
+ ).toBe(true);
116
+ };
117
+
118
+ export const verifyB3Headers = (headersArray: Header[], isSampled: boolean) => {
119
+ const headers = getHeadersMapFromArray(headersArray);
120
+
121
+ // b3 (example: 67a9d1b800000000b83b034110cf109f-934634763188a363-1)
122
+ const b3Header = headers.get('b3') as string;
123
+ expect(b3Header).toBeDefined();
124
+
125
+ const parts = b3Header.split('-');
126
+ expect(parts).toHaveLength(3);
127
+
128
+ const traceId = parts[0];
129
+ expect(traceId).toMatch(/^[0-9a-fA-F]{32}$/);
130
+ expect(TracingIdentifierUtils.isWithin128Bits(traceId, 16)).toBe(true);
131
+
132
+ const parentId = parts[1];
133
+ expect(parentId).toMatch(/^[0-9a-fA-F]{16}$/);
134
+ expect(TracingIdentifierUtils.isWithin64Bits(parentId, 16)).toBe(true);
135
+
136
+ const samplingFlag = parts[2];
137
+ expect(samplingFlag).toBe(isSampled ? '1' : '0');
138
+ };
139
+
140
+ export const verifyB3MultiHeaders = (
141
+ headersArray: Header[],
142
+ isSampled: boolean
143
+ ) => {
144
+ const headers = getHeadersMapFromArray(headersArray);
145
+
146
+ // X-B3-TraceId (example: 67a9d2cc00000000f1118fc009a8ea2f)
147
+ const traceIdHeader = headers.get('X-B3-TraceId') as string;
148
+ expect(traceIdHeader).toBeDefined();
149
+ expect(traceIdHeader).toMatch(/^[0-9a-fA-F]{32}$/);
150
+ expect(TracingIdentifierUtils.isWithin128Bits(traceIdHeader, 16));
151
+
152
+ // X-B3-SpanId (example: 64765d396cb90802)
153
+ const spanIdHeader = headers.get('X-B3-SpanId') as string;
154
+ expect(spanIdHeader).toBeDefined();
155
+ expect(spanIdHeader).toMatch(/^[0-9a-fA-F]{16}$/);
156
+ expect(TracingIdentifierUtils.isWithin64Bits(traceIdHeader, 16));
157
+
158
+ // X-B3-Sampled
159
+ const samplingHeader = headers.get('X-B3-Sampled') as string;
160
+ expect(samplingHeader).toBeDefined();
161
+ expect(samplingHeader).toBe(isSampled ? '1' : '0');
162
+ };
163
+
164
+ const getHeadersMapFromArray = (headers: Header[]): Map<string, string> => {
165
+ return new Map(headers.map(({ header, value }) => [header, value]));
166
+ };
@@ -0,0 +1,81 @@
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 { DatadogTracingIdentifier } from './DatadogTracingIdentifier';
8
+ import type { SpanId, TraceId } from './TracingIdentifier';
9
+
10
+ /**
11
+ * An object that contains the tracing attributes as headers for network requests and attributes
12
+ * for RUM Resources.
13
+ */
14
+ export class DatadogTracingContext {
15
+ private readonly headersForRequest: { header: string; value: string }[];
16
+ private readonly rumResourceContext: Record<string, string | number>;
17
+ readonly traceId?: DatadogTracingIdentifier;
18
+ readonly spanId?: DatadogTracingIdentifier;
19
+
20
+ constructor(
21
+ requestHeaders: { header: string; value: string }[],
22
+ resourceContext: Record<string, string | number>,
23
+ traceId: TraceId | undefined | void,
24
+ spanId: SpanId | undefined | void
25
+ ) {
26
+ this.headersForRequest = requestHeaders;
27
+ this.rumResourceContext = resourceContext;
28
+ this.traceId = traceId
29
+ ? new DatadogTracingIdentifier(traceId)
30
+ : undefined;
31
+ this.spanId = spanId ? new DatadogTracingIdentifier(spanId) : undefined;
32
+ }
33
+
34
+ /**
35
+ * Get the tracing headers to add to your network request.
36
+ * @returns the headers for the request as a key-value object.
37
+ */
38
+ getHeadersForRequest(): Record<string, string> {
39
+ return Object.fromEntries(
40
+ this.headersForRequest.map(({ header, value }) => [header, value])
41
+ );
42
+ }
43
+
44
+ /**
45
+ * Get the tracing headers to add to your network request, as an array of key-value objects.
46
+ * @returns the headers for the request as an array of key-value objects.
47
+ */
48
+ getHeadersForRequestAsArray(): { header: string; value: string }[] {
49
+ return [...this.headersForRequest];
50
+ }
51
+
52
+ /**
53
+ * Get the RUM Resource Context tracing attributes to your RUM Resource context.
54
+ * @returns the RUM Resource context tracing attributes.
55
+ */
56
+ getRumResourceContext(): Record<string, string | number> {
57
+ return { ...this.rumResourceContext };
58
+ }
59
+
60
+ /**
61
+ * Convenience method to inject the tracing headers to your request.
62
+ * @param inject a callback that is called for each header that should be added to your request.
63
+ */
64
+ injectHeadersForRequest(inject: (header: string, value: string) => void) {
65
+ this.headersForRequest.forEach(({ header, value }) =>
66
+ inject(header, value)
67
+ );
68
+ }
69
+
70
+ /**
71
+ * Convenience method to inject the RUM Resource attributes to your RUM Resource context.
72
+ * @param inject a callback that is called for each attribute that should be added to your RUM Resource context.
73
+ */
74
+ injectRumResourceContext(
75
+ inject: (attribute: string, value: string | number) => void
76
+ ) {
77
+ Object.keys(this.rumResourceContext).forEach((key: string) => {
78
+ inject(key, this.rumResourceContext[key]);
79
+ });
80
+ }
81
+ }
@@ -0,0 +1,49 @@
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 { BigInteger } from 'big-integer';
8
+
9
+ import type {
10
+ TraceId,
11
+ SpanId,
12
+ TracingIdentifier,
13
+ TracingIdType,
14
+ TracingIdFormat
15
+ } from './TracingIdentifier';
16
+
17
+ /**
18
+ * A read-only wrapper of {@link TracingIdentifier} for public API usage.
19
+ */
20
+ export class DatadogTracingIdentifier {
21
+ /**
22
+ * Read-only generated ID as a {@link BigInteger}.
23
+ */
24
+ get id(): BigInteger {
25
+ return this.uuid.id;
26
+ }
27
+
28
+ /**
29
+ * Read-only type to determine whether the identifier is a {@link TraceId} or a {@link SpanId}.
30
+ */
31
+ get type(): TracingIdType {
32
+ return this.uuid.type;
33
+ }
34
+
35
+ private uuid: TracingIdentifier;
36
+
37
+ public constructor(uuid: TraceId | SpanId) {
38
+ this.uuid = uuid;
39
+ }
40
+
41
+ /**
42
+ * Returns a string representation of the Tracing ID.
43
+ * @param format - The type of representation to use.
44
+ * @returns The ID as a string in the specified representation type.
45
+ */
46
+ toString(format: TracingIdFormat): string {
47
+ return this.uuid.toString(format);
48
+ }
49
+ }
@@ -4,6 +4,9 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
+ import type { BigInteger } from 'big-integer';
8
+ import BigInt from 'big-integer';
9
+
7
10
  /**
8
11
  * Available formats for representing the {@link TracingIdentifier} as a string.
9
12
  */
@@ -85,13 +88,16 @@ export enum TracingIdType {
85
88
  /**
86
89
  * Value used to mask the low 64 bits of the trace identifier.
87
90
  */
88
- const LOW_64BIT_MASK =
89
- (BigInt('0xffffffff') << BigInt(32)) + BigInt('0xffffffff');
91
+ const LOW_64BIT_MASK: BigInteger = BigInt('ffffffff', 16)
92
+ .shiftLeft(32)
93
+ .add(BigInt('ffffffff', 16));
90
94
 
91
95
  /**
92
96
  * Value used to mask the low 32 bits of the trace identifier.
93
97
  */
94
- const LOW_32BIT_MASK = (BigInt('0xffff') << BigInt(16)) + BigInt('0xffff');
98
+ const LOW_32BIT_MASK: BigInteger = BigInt('ffff', 16)
99
+ .shiftLeft(16)
100
+ .add(BigInt('ffff', 16));
95
101
 
96
102
  /**
97
103
  * A {@link TracingIdentifier} is a unique UUID that can be 64bit or 128bit, and provides
@@ -103,9 +109,9 @@ const LOW_32BIT_MASK = (BigInt('0xffff') << BigInt(16)) + BigInt('0xffff');
103
109
  */
104
110
  export class TracingIdentifier {
105
111
  /**
106
- * Read-only generated ID as a {@link bigint}.
112
+ * Read-only generated ID as a {@link BigInteger}.
107
113
  */
108
- readonly id: bigint;
114
+ readonly id: BigInteger;
109
115
 
110
116
  /**
111
117
  * Read-only type to determine whether the identifier is a {@link TraceId} or a {@link SpanId}.
@@ -140,9 +146,9 @@ export class TracingIdentifier {
140
146
  /**
141
147
  * Generates a unique ID with the given format.
142
148
  * @param format - the desired format (64bit or 128bit).
143
- * @returns the generated UUID as a {@link bigint}.
149
+ * @returns the generated UUID as a {@link BigInteger}.
144
150
  */
145
- private generateUUID(type: TracingIdType): bigint {
151
+ private generateUUID(type: TracingIdType): BigInteger {
146
152
  // Get the current Unix timestamp in seconds
147
153
  const unixSeconds = Math.floor(Date.now() / 1000);
148
154
 
@@ -161,7 +167,7 @@ export class TracingIdentifier {
161
167
 
162
168
  // If type is 'span' we return the generated 64 bit ID
163
169
  if (type === TracingIdType.span) {
164
- return BigInt(`0x${random64Hex}`);
170
+ return BigInt(random64Hex, 16);
165
171
  }
166
172
 
167
173
  // Convert parts to hexadecimal strings
@@ -171,7 +177,7 @@ export class TracingIdentifier {
171
177
  // Combine parts to form the 128-bit ID
172
178
  const hex128BitID = unixSecondsHex + zerosHex + random64Hex;
173
179
 
174
- return BigInt(`0x${hex128BitID}`);
180
+ return BigInt(hex128BitID, 16);
175
181
  }
176
182
 
177
183
  /**
@@ -191,19 +197,19 @@ export class TracingIdentifier {
191
197
  return this.id.toString(10);
192
198
 
193
199
  case TracingIdFormat.lowDecimal:
194
- return (this.id & lowTraceMask).toString(10);
200
+ return this.id.and(lowTraceMask).toString(10);
195
201
 
196
202
  case TracingIdFormat.highDecimal:
197
- return (this.id >> highTraceMask).toString(10);
203
+ return this.id.shiftRight(highTraceMask).toString(10);
198
204
 
199
205
  case TracingIdFormat.hex:
200
206
  return this.id.toString(16);
201
207
 
202
208
  case TracingIdFormat.lowHex:
203
- return (this.id & lowTraceMask).toString(16);
209
+ return this.id.and(lowTraceMask).toString(16);
204
210
 
205
211
  case TracingIdFormat.highHex:
206
- return (this.id >> highTraceMask).toString(16);
212
+ return this.id.shiftRight(highTraceMask).toString(16);
207
213
 
208
214
  case TracingIdFormat.paddedHex:
209
215
  return this.toString(TracingIdFormat.hex).padStart(
@@ -3,6 +3,7 @@
3
3
  * This product includes software developed at Datadog (https://www.datadoghq.com/).
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
+ import BigInt from 'big-integer';
6
7
 
7
8
  export class TracingIdentifierUtils {
8
9
  /**
@@ -30,8 +31,9 @@ export class TracingIdentifierUtils {
30
31
  idString: string,
31
32
  radix: number = 10
32
33
  ): boolean {
33
- const bigIntValue = BigInt(parseInt(idString, radix));
34
- return bigIntValue < BigInt(1) << BigInt(32);
34
+ return BigInt(parseInt(idString, radix)).lesser(
35
+ BigInt(1).shiftLeft(32)
36
+ );
35
37
  }
36
38
 
37
39
  /**
@@ -44,8 +46,9 @@ export class TracingIdentifierUtils {
44
46
  idString: string,
45
47
  radix: number = 10
46
48
  ): boolean {
47
- const bigIntValue = BigInt(parseInt(idString, radix));
48
- return bigIntValue < BigInt(1) << BigInt(64);
49
+ return BigInt(parseInt(idString, radix)).lesser(
50
+ BigInt(1).shiftLeft(64)
51
+ );
49
52
  }
50
53
 
51
54
  /**
@@ -58,7 +61,8 @@ export class TracingIdentifierUtils {
58
61
  idString: string,
59
62
  radix: number = 10
60
63
  ): boolean {
61
- const bigIntValue = BigInt(parseInt(idString, radix));
62
- return bigIntValue < BigInt(1) << BigInt(128);
64
+ return BigInt(parseInt(idString, radix)).lesser(
65
+ BigInt(1).shiftLeft(128)
66
+ );
63
67
  }
64
68
  }
@@ -4,6 +4,8 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
+ import BigInt from 'big-integer';
8
+
7
9
  import { TracingIdentifier, TracingIdFormat } from '../TracingIdentifier';
8
10
 
9
11
  import { TracingIdentifierUtils } from './__utils__/TracingIdentifierUtils';
@@ -4,6 +4,8 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
+ import BigInt from 'big-integer';
8
+
7
9
  import type { PropagatorType } from '../../../types';
8
10
  import type { RegexMap } from '../requestProxy/interfaces/RequestProxy';
9
11
 
@@ -13,7 +15,7 @@ import type { Hostname } from './firstPartyHosts';
13
15
  import { getPropagatorsForHost } from './firstPartyHosts';
14
16
 
15
17
  const knuthFactor = BigInt('1111111111111111111');
16
- const twoPow64 = BigInt('0x10000000000000000'); // 2n ** 64n
18
+ const twoPow64 = BigInt('10000000000000000', 16); // 2n ** 64n
17
19
 
18
20
  export type DdRumResourceTracingAttributes =
19
21
  | {
@@ -61,12 +63,16 @@ export const getTracingAttributes = ({
61
63
  return DISCARDED_TRACE_ATTRIBUTES;
62
64
  };
63
65
 
64
- const generateTracingAttributesWithSampling = (
66
+ export const generateTracingAttributesWithSampling = (
65
67
  tracingSamplingRate: number,
66
68
  propagatorTypes: PropagatorType[]
67
69
  ): DdRumResourceTracingAttributes => {
70
+ if (!propagatorTypes || propagatorTypes.length === 0) {
71
+ return DISCARDED_TRACE_ATTRIBUTES;
72
+ }
73
+
68
74
  const traceId = TracingIdentifier.createTraceId();
69
- const hash = Number((traceId.id * knuthFactor) % twoPow64);
75
+ const hash = Number(traceId.id.multiply(knuthFactor).remainder(twoPow64));
70
76
  const threshold = (tracingSamplingRate / 100) * Number(twoPow64);
71
77
  const isSampled = hash <= threshold;
72
78
 
@@ -5,10 +5,18 @@
5
5
  */
6
6
 
7
7
  import { PropagatorType } from '../../../types';
8
+ import type { FirstPartyHost } from '../../../types';
9
+ import { URLHostParser } from '../requestProxy/XHRProxy/URLHostParser';
8
10
 
11
+ import { DatadogTracingContext } from './DatadogTracingContext';
9
12
  import { TracingIdFormat } from './TracingIdentifier';
10
13
  import type { TraceId, SpanId } from './TracingIdentifier';
14
+ import {
15
+ generateTracingAttributesWithSampling,
16
+ getTracingAttributes
17
+ } from './distributedTracing';
11
18
  import type { DdRumResourceTracingAttributes } from './distributedTracing';
19
+ import { firstPartyHostsRegexMapBuilder } from './firstPartyHosts';
12
20
 
13
21
  export const SAMPLING_PRIORITY_HEADER_KEY = 'x-datadog-sampling-priority';
14
22
  /**
@@ -31,7 +39,7 @@ export const B3_MULTI_TRACE_ID_HEADER_KEY = 'X-B3-TraceId';
31
39
  export const B3_MULTI_SPAN_ID_HEADER_KEY = 'X-B3-SpanId';
32
40
  export const B3_MULTI_SAMPLED_HEADER_KEY = 'X-B3-Sampled';
33
41
 
34
- export const getTracingHeaders = (
42
+ export const getTracingHeadersFromAttributes = (
35
43
  tracingAttributes: DdRumResourceTracingAttributes
36
44
  ): { header: string; value: string }[] => {
37
45
  const headers: { header: string; value: string }[] = [];
@@ -132,6 +140,68 @@ export const getTracingHeaders = (
132
140
  return headers;
133
141
  };
134
142
 
143
+ export const getTracingContext = (
144
+ url: string,
145
+ tracingSamplingRate: number,
146
+ firstPartyHosts: FirstPartyHost[]
147
+ ): DatadogTracingContext => {
148
+ const hostname = URLHostParser(url);
149
+ const firstPartyHostsRegexMap = firstPartyHostsRegexMapBuilder(
150
+ firstPartyHosts
151
+ );
152
+ const tracingAttributes = getTracingAttributes({
153
+ hostname,
154
+ firstPartyHostsRegexMap,
155
+ tracingSamplingRate
156
+ });
157
+
158
+ return getTracingContextForAttributes(
159
+ tracingAttributes,
160
+ tracingSamplingRate
161
+ );
162
+ };
163
+
164
+ export const getTracingContextForPropagators = (
165
+ propagators: PropagatorType[],
166
+ tracingSamplingRate: number
167
+ ): DatadogTracingContext => {
168
+ return getTracingContextForAttributes(
169
+ generateTracingAttributesWithSampling(tracingSamplingRate, propagators),
170
+ tracingSamplingRate
171
+ );
172
+ };
173
+
174
+ const getTracingContextForAttributes = (
175
+ tracingAttributes: DdRumResourceTracingAttributes,
176
+ tracingSamplingRate: number
177
+ ): DatadogTracingContext => {
178
+ const requestHeaders = getTracingHeadersFromAttributes(tracingAttributes);
179
+ const resourceContext: Record<string, string | number> = {};
180
+
181
+ const spanId = tracingAttributes.spanId;
182
+ if (spanId) {
183
+ resourceContext['_dd.span_id'] = spanId.toString(
184
+ TracingIdFormat.decimal
185
+ );
186
+ }
187
+
188
+ const traceId = tracingAttributes.traceId;
189
+ if (traceId) {
190
+ resourceContext['_dd.trace_id'] = traceId.toString(
191
+ TracingIdFormat.paddedHex
192
+ );
193
+ }
194
+
195
+ resourceContext['_dd.rule_psr'] = tracingSamplingRate / 100;
196
+
197
+ return new DatadogTracingContext(
198
+ requestHeaders,
199
+ resourceContext,
200
+ tracingAttributes.traceId,
201
+ tracingAttributes.spanId
202
+ );
203
+ };
204
+
135
205
  const generateTraceContextHeader = ({
136
206
  version,
137
207
  traceId,
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  import Timer from '../../../../../utils/Timer';
8
- import { getTracingHeaders } from '../../distributedTracing/distributedTracingHeaders';
8
+ import { getTracingHeadersFromAttributes } from '../../distributedTracing/distributedTracingHeaders';
9
9
  import type { DdRumResourceTracingAttributes } from '../../distributedTracing/distributedTracing';
10
10
  import { getTracingAttributes } from '../../distributedTracing/distributedTracing';
11
11
  import {
@@ -125,7 +125,7 @@ const proxySend = (providers: XHRProxyProviders): void => {
125
125
  // keep track of start time
126
126
  this._datadog_xhr.timer.start();
127
127
 
128
- const tracingHeaders = getTracingHeaders(
128
+ const tracingHeaders = getTracingHeadersFromAttributes(
129
129
  this._datadog_xhr.tracingAttributes
130
130
  );
131
131
  tracingHeaders.forEach(({ header, value }) => {
@@ -4,6 +4,7 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
+ import BigInt from 'big-integer';
7
8
  import { Platform, NativeModules } from 'react-native';
8
9
 
9
10
  import { InternalLog } from '../../../../../../InternalLog';
@@ -58,7 +59,7 @@ const flushPromises = () =>
58
59
  let xhrProxy;
59
60
 
60
61
  const hexToDecimal = (hex: string): string => {
61
- return BigInt(`0x${hex}`).toString(10);
62
+ return BigInt(hex, 16).toString(10);
62
63
  };
63
64
 
64
65
  beforeEach(() => {
package/src/rum/types.ts CHANGED
@@ -4,7 +4,8 @@
4
4
  * Copyright 2016-Present Datadog, Inc.
5
5
  */
6
6
 
7
- import type { TracingIdType } from './instrumentation/resourceTracking/distributedTracing/TracingIdentifier';
7
+ import type { DatadogTracingContext } from './instrumentation/resourceTracking/distributedTracing/DatadogTracingContext';
8
+ import type { DatadogTracingIdentifier } from './instrumentation/resourceTracking/distributedTracing/DatadogTracingIdentifier';
8
9
 
9
10
  /**
10
11
  * The entry point to use Datadog's RUM feature.
@@ -123,12 +124,6 @@ export type DdRumType = {
123
124
  timestampMs?: number
124
125
  ): Promise<void>;
125
126
 
126
- /**
127
- * Generate a new unique tracing ID.
128
- * @param type - The type of the tracing ID to generate. Trace (128-bit) or Span (64-bit).
129
- */
130
- generateUUID(type: TracingIdType): string;
131
-
132
127
  /**
133
128
  * Add a RUM Error.
134
129
  * @param message: The error message.
@@ -160,6 +155,46 @@ export type DdRumType = {
160
155
  * Returns current session ID, or undefined if unavailable.
161
156
  */
162
157
  getCurrentSessionId(): Promise<string | undefined>;
158
+
159
+ /**
160
+ * Gets the tracing context for the given url, tracingSamplingRate and firstPartyHosts.
161
+ * The returned {@link DatadogTracingContext} can be used to retrieve the tracing headers
162
+ * to append to your network request, and the attributes to add to your RUM Resource.
163
+ *
164
+ * See also `DdRum.getTracingContextForPropagators(...)` if you do not intend to use `firstPartyHosts`.
165
+ *
166
+ * @param url The request URL.
167
+ * @param tracingSamplingRate Percentage of tracing integrations for network calls between your app and your backend. Range `0`-`100`.
168
+ * @param firstPartyHosts List of your backends hosts with propagator types.
169
+ */
170
+ getTracingContext(
171
+ url: string,
172
+ tracingSamplingRate: number,
173
+ firstPartyHosts: FirstPartyHost[]
174
+ ): DatadogTracingContext;
175
+
176
+ /**
177
+ * Gets the tracing context for the given list of propagator types and tracing sampling rate.
178
+ *
179
+ * The returned {@link DatadogTracingContext} can be used to retrieve the tracing headers
180
+ * to append to your network request, and the attributes to add to your RUM Resource.
181
+ * @param propagators The list of propagators of type {@link PropagatorType}
182
+ * @param tracingSamplingRate Percentage of tracing integrations for network calls between your app and your backend. Range `0`-`100`.
183
+ */
184
+ getTracingContextForPropagators(
185
+ propagators: PropagatorType[],
186
+ tracingSamplingRate: number
187
+ ): DatadogTracingContext;
188
+
189
+ /**
190
+ * Generates a unique 128bit Trace ID.
191
+ */
192
+ generateTraceId(): DatadogTracingIdentifier;
193
+
194
+ /**
195
+ * Generates a unique 128bit Span ID.
196
+ */
197
+ generateSpanId(): DatadogTracingIdentifier;
163
198
  };
164
199
 
165
200
  /**