@datadog/mobile-react-native 2.3.6 → 2.4.1

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 (117) hide show
  1. package/DatadogSDKReactNative.podspec +7 -7
  2. package/android/build.gradle +4 -4
  3. package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
  4. package/android/src/test/kotlin/com/datadog/tools/unit/MockRumMonitor.kt +2 -0
  5. package/android/src/test/kotlin/com/datadog/tools/unit/forge/TelemetryConfigurationEventForgeryFactory.kt +15 -0
  6. package/android/src/test/resources/input/complete-configuration.json +5 -1
  7. package/datadog-configuration.schema.json +54 -9
  8. package/ios/Sources/DatadogSDKWrapper.swift +3 -3
  9. package/ios/Sources/SdkVersion.swift +1 -1
  10. package/lib/commonjs/DdSdkReactNative.js +6 -12
  11. package/lib/commonjs/DdSdkReactNative.js.map +1 -1
  12. package/lib/commonjs/DdSdkReactNativeConfiguration.js +7 -4
  13. package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
  14. package/lib/commonjs/index.js +2 -0
  15. package/lib/commonjs/index.js.map +1 -1
  16. package/lib/commonjs/polyfills.js +31 -0
  17. package/lib/commonjs/polyfills.js.map +1 -0
  18. package/lib/commonjs/rum/DdRum.js +3 -3
  19. package/lib/commonjs/rum/DdRum.js.map +1 -1
  20. package/lib/commonjs/rum/instrumentation/interactionTracking/DdEventsInterceptor.js +14 -9
  21. package/lib/commonjs/rum/instrumentation/interactionTracking/DdEventsInterceptor.js.map +1 -1
  22. package/lib/commonjs/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js +2 -6
  23. package/lib/commonjs/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js.map +1 -1
  24. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js +182 -0
  25. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js.map +1 -0
  26. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js +10 -57
  27. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js.map +1 -1
  28. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js +18 -10
  29. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js.map +1 -1
  30. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js +4 -2
  31. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js.map +1 -1
  32. package/lib/commonjs/sdk/GlobalState/GlobalState.js +38 -0
  33. package/lib/commonjs/sdk/GlobalState/GlobalState.js.map +1 -0
  34. package/lib/commonjs/specs/NativeDdLogs.js +1 -0
  35. package/lib/commonjs/specs/NativeDdLogs.js.map +1 -1
  36. package/lib/commonjs/specs/NativeDdRum.js +1 -0
  37. package/lib/commonjs/specs/NativeDdRum.js.map +1 -1
  38. package/lib/commonjs/specs/NativeDdSdk.js +1 -0
  39. package/lib/commonjs/specs/NativeDdSdk.js.map +1 -1
  40. package/lib/commonjs/specs/NativeDdTrace.js +1 -0
  41. package/lib/commonjs/specs/NativeDdTrace.js.map +1 -1
  42. package/lib/commonjs/version.js +1 -1
  43. package/lib/commonjs/version.js.map +1 -1
  44. package/lib/module/DdSdkReactNative.js +5 -12
  45. package/lib/module/DdSdkReactNative.js.map +1 -1
  46. package/lib/module/DdSdkReactNativeConfiguration.js +7 -4
  47. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  48. package/lib/module/index.js +5 -0
  49. package/lib/module/index.js.map +1 -1
  50. package/lib/module/polyfills.js +28 -0
  51. package/lib/module/polyfills.js.map +1 -0
  52. package/lib/module/rum/DdRum.js +2 -2
  53. package/lib/module/rum/DdRum.js.map +1 -1
  54. package/lib/module/rum/instrumentation/interactionTracking/DdEventsInterceptor.js +14 -9
  55. package/lib/module/rum/instrumentation/interactionTracking/DdEventsInterceptor.js.map +1 -1
  56. package/lib/module/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js +2 -6
  57. package/lib/module/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js.map +1 -1
  58. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js +169 -0
  59. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js.map +1 -0
  60. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js +3 -54
  61. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js.map +1 -1
  62. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js +13 -8
  63. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js.map +1 -1
  64. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js +3 -2
  65. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js.map +1 -1
  66. package/lib/module/sdk/GlobalState/GlobalState.js +29 -0
  67. package/lib/module/sdk/GlobalState/GlobalState.js.map +1 -0
  68. package/lib/module/specs/NativeDdLogs.js +1 -0
  69. package/lib/module/specs/NativeDdLogs.js.map +1 -1
  70. package/lib/module/specs/NativeDdRum.js +1 -0
  71. package/lib/module/specs/NativeDdRum.js.map +1 -1
  72. package/lib/module/specs/NativeDdSdk.js +1 -0
  73. package/lib/module/specs/NativeDdSdk.js.map +1 -1
  74. package/lib/module/specs/NativeDdTrace.js +1 -0
  75. package/lib/module/specs/NativeDdTrace.js.map +1 -1
  76. package/lib/module/version.js +1 -1
  77. package/lib/module/version.js.map +1 -1
  78. package/lib/typescript/DdSdkReactNative.d.ts +0 -5
  79. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +5 -1
  80. package/lib/typescript/index.d.ts +1 -0
  81. package/lib/typescript/rum/instrumentation/interactionTracking/DdEventsInterceptor.d.ts +12 -3
  82. package/lib/typescript/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.d.ts +2 -3
  83. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.d.ts +111 -0
  84. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.d.ts +1 -24
  85. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.d.ts +2 -0
  86. package/lib/typescript/sdk/GlobalState/GlobalState.d.ts +12 -0
  87. package/lib/typescript/version.d.ts +1 -1
  88. package/package.json +2 -2
  89. package/src/DdSdkReactNative.tsx +7 -11
  90. package/src/DdSdkReactNativeConfiguration.tsx +10 -4
  91. package/src/__tests__/DdSdkReactNative.test.tsx +3 -2
  92. package/src/__tests__/DdSdkReactNativeConfiguration.test.ts +7 -2
  93. package/src/__tests__/mock.test.ts +0 -1
  94. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +32 -0
  95. package/src/index.tsx +4 -0
  96. package/src/polyfills.js +37 -0
  97. package/src/rum/DdRum.ts +2 -2
  98. package/src/rum/__tests__/DdRum.test.ts +3 -3
  99. package/src/rum/instrumentation/interactionTracking/DdEventsInterceptor.tsx +23 -7
  100. package/src/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.tsx +3 -4
  101. package/src/rum/instrumentation/resourceTracking/__tests__/__utils__/XMLHttpRequestMock.ts +1 -0
  102. package/src/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.tsx +227 -0
  103. package/src/rum/instrumentation/resourceTracking/distributedTracing/__tests__/__utils__/TracingIdentifierUtils.ts +64 -0
  104. package/src/rum/instrumentation/resourceTracking/distributedTracing/__tests__/distributedTracing.test.ts +314 -42
  105. package/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.tsx +4 -59
  106. package/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.ts +33 -19
  107. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.ts +7 -2
  108. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts +134 -11
  109. package/src/sdk/DatadogProvider/__tests__/initialization.test.tsx +2 -2
  110. package/src/sdk/DatadogProvider/__tests__/initializationModes.test.tsx +3 -2
  111. package/src/sdk/FileBasedConfiguration/__tests__/FileBasedConfiguration.test.ts +3 -0
  112. package/src/sdk/GlobalState/GlobalState.tsx +26 -0
  113. package/src/specs/NativeDdLogs.ts +1 -0
  114. package/src/specs/NativeDdRum.ts +1 -0
  115. package/src/specs/NativeDdSdk.ts +1 -0
  116. package/src/specs/NativeDdTrace.ts +1 -0
  117. package/src/version.ts +1 -1
@@ -1,6 +1,14 @@
1
- import { TraceIdentifier } from '../distributedTracing';
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
+ */
2
6
 
3
- describe('TraceIdentifier', () => {
7
+ import { TracingIdentifier, TracingIdFormat } from '../TracingIdentifier';
8
+
9
+ import { TracingIdentifierUtils } from './__utils__/tracingIdentifierUtils';
10
+
11
+ describe('TracingIdentifier', () => {
4
12
  it('M return an unique identifier W toString', async () => {
5
13
  // GIVEN
6
14
  const generatedIds = new Set<string>();
@@ -9,63 +17,327 @@ describe('TraceIdentifier', () => {
9
17
 
10
18
  // WHEN
11
19
  while (counter-- > 0) {
12
- generatedIds.add(new TraceIdentifier().toString(10));
20
+ generatedIds.add(
21
+ TracingIdentifier.createTraceId().toString(
22
+ TracingIdFormat.decimal
23
+ )
24
+ );
25
+ generatedIds.add(
26
+ TracingIdentifier.createSpanId().toString(
27
+ TracingIdFormat.decimal
28
+ )
29
+ );
13
30
  }
14
31
 
15
32
  // THEN
16
- expect(generatedIds.size).toBe(iterations);
33
+ expect(generatedIds.size).toBe(iterations * 2);
17
34
  });
18
35
 
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);
36
+ describe('Trace IDs', () => {
37
+ it('M return a valid 128 bits HEX string W toString(.hex)', async () => {
38
+ let iterations = 100;
39
+ while (iterations-- > 0) {
40
+ // GIVEN
41
+ const id = TracingIdentifier.createTraceId();
42
+ const idStr128 = id.toString(TracingIdFormat.hex);
24
43
 
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();
44
+ // THEN
45
+ expect(idStr128).toMatch(/^[0-9a-f]{8}[0]{8}[0-9a-f]{16}$/);
46
+ expect(idStr128.length).toBeLessThanOrEqual(32);
47
+
48
+ expect(
49
+ TracingIdentifierUtils.isWithin128Bits(idStr128, 16)
50
+ ).toBe(true);
30
51
  }
31
- }
32
- });
52
+ });
33
53
 
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);
54
+ it('M return a valid 64 bits HEX string W toString(.lowHex)', () => {
55
+ let iterations = 100;
56
+ while (iterations-- > 0) {
57
+ const tracingId = TracingIdentifier.createTraceId();
58
+ const idHex = tracingId.toString(TracingIdFormat.lowHex);
41
59
 
42
- // THEN
43
- expect(id).toMatch(/[1-9a-f]{1,16}/);
44
- expect(paddedId).toMatch(/[0-9a-f]{16}/);
45
- }
46
- });
60
+ expect(idHex).toMatch(/^[0-9a-f]{1,}$/);
61
+ expect(idHex.length).toBeLessThanOrEqual(16);
62
+
63
+ expect(TracingIdentifierUtils.isWithin64Bits(idHex, 16)).toBe(
64
+ true
65
+ );
66
+ }
67
+ });
68
+
69
+ it('M return a valid 64 bits HEX string W toString(.highHex)', () => {
70
+ let iterations = 100;
71
+ while (iterations-- > 0) {
72
+ const tracingId = TracingIdentifier.createTraceId();
73
+ const idHex = tracingId.toString(TracingIdFormat.highHex);
74
+
75
+ expect(idHex).toMatch(/^[0-9a-f]{1,}$/);
76
+ expect(idHex.length).toBeLessThanOrEqual(16);
77
+
78
+ expect(TracingIdentifierUtils.isWithin64Bits(idHex, 16)).toBe(
79
+ true
80
+ );
81
+ }
82
+ });
83
+
84
+ it('M return a valid 128 bits HEX 32 string W toString(.paddedHex)', async () => {
85
+ let iterations = 100;
86
+ while (iterations-- > 0) {
87
+ // GIVEN
88
+ const id = TracingIdentifier.createTraceId();
89
+ const idStr128 = id.toString(TracingIdFormat.paddedHex);
90
+
91
+ // THEN
92
+ expect(idStr128).toMatch(/^[0-9a-f]{8}[0]{8}[0-9a-f]{16}$/);
93
+ expect(
94
+ TracingIdentifierUtils.isWithin128Bits(idStr128, 16)
95
+ ).toBe(true);
96
+ }
97
+ });
98
+
99
+ it('M return a valid 64 bits HEX 16 string W paddedLowHex', () => {
100
+ let iterations = 100;
101
+ while (iterations-- > 0) {
102
+ const tracingId = TracingIdentifier.createTraceId();
103
+ const idHex = tracingId.toString(TracingIdFormat.paddedLowHex);
104
+
105
+ expect(idHex).toMatch(/^[0-9a-f]{16}$/);
106
+ expect(TracingIdentifierUtils.isWithin64Bits(idHex, 16)).toBe(
107
+ true
108
+ );
109
+ }
110
+ });
111
+
112
+ it('M return a valid 64 bits HEX 16 string W paddedHighHex', () => {
113
+ const tracingId = TracingIdentifier.createTraceId();
114
+ const idHex = tracingId.toString(TracingIdFormat.paddedHighHex);
115
+
116
+ expect(idHex).toMatch(/^[0-9a-f]{8}[0]{8}$/);
117
+ expect(TracingIdentifierUtils.isWithin64Bits(idHex, 16)).toBe(true);
118
+ });
119
+
120
+ it('M return a valid 128 bits integer W toString(.decimal)', async () => {
121
+ let iterations = 100;
122
+ while (iterations-- > 0) {
123
+ // GIVEN
124
+ const id = TracingIdentifier.createTraceId();
125
+ const idDecimal = id.toString(TracingIdFormat.decimal);
126
+
127
+ // THEN
128
+ expect(TracingIdentifierUtils.isWithin128Bits(idDecimal)).toBe(
129
+ true
130
+ );
131
+ }
132
+ });
133
+
134
+ it('M return a valid 64 bits low and high part integer W toString(.lowDecimal) & toString(.highDecimal)', async () => {
135
+ let iterations = 100;
136
+ while (iterations-- > 0) {
137
+ // GIVEN
138
+ const id = TracingIdentifier.createTraceId();
139
+ const idStrLow64 = id.toString(TracingIdFormat.lowDecimal);
140
+ const idStrHigh64 = id.toString(TracingIdFormat.highDecimal);
141
+
142
+ // THEN
143
+ expect(TracingIdentifierUtils.isWithin64Bits(idStrLow64)).toBe(
144
+ true
145
+ );
146
+ expect(TracingIdentifierUtils.isWithin64Bits(idStrHigh64)).toBe(
147
+ true
148
+ );
149
+ }
150
+ });
151
+
152
+ it('M return a valid timestamp in the high part of the 128 bits ID w toString(.paddedHex)', () => {
153
+ const tracingId = TracingIdentifier.createTraceId();
154
+ const idHex = tracingId.toString(TracingIdFormat.paddedHex);
155
+ const timestamp = TracingIdentifierUtils.extractTimestamp(idHex);
156
+
157
+ const currentUnixTime = Math.floor(Date.now() / 1000);
158
+ const fiveMinutesInSeconds = 5 * 60;
159
+
160
+ expect(timestamp).toBeGreaterThan(
161
+ currentUnixTime - fiveMinutesInSeconds
162
+ );
163
+ expect(timestamp).toBeLessThan(
164
+ currentUnixTime + fiveMinutesInSeconds
165
+ );
166
+ });
47
167
 
48
- it('M return an 64 bits positive padded hex W toPaddedString(16, 32)', async () => {
49
- let iterations = 100;
50
- while (iterations-- > 0) {
168
+ it('M return valid string representations for zero ID w toString', () => {
51
169
  // GIVEN
52
- const id = new TraceIdentifier().toPaddedString(16, 32);
170
+ const tracingId = TracingIdentifier.createTraceId();
171
+ (tracingId as any)['id'] = BigInt(0);
53
172
 
54
173
  // THEN
55
- expect(id).not.toMatch(/[0]{32}/);
56
- expect(id).toMatch(/[0]{16}[0-9a-f]{16}/);
57
- }
174
+
175
+ // Decimal
176
+ expect(tracingId.toString(TracingIdFormat.decimal)).toBe('0');
177
+ expect(tracingId.toString(TracingIdFormat.lowDecimal)).toBe('0');
178
+ expect(tracingId.toString(TracingIdFormat.highDecimal)).toBe('0');
179
+
180
+ // Hex
181
+ expect(tracingId.toString(TracingIdFormat.hex)).toBe('0');
182
+ expect(tracingId.toString(TracingIdFormat.lowHex)).toBe('0');
183
+ expect(tracingId.toString(TracingIdFormat.highHex)).toBe('0');
184
+
185
+ // Padded Hex
186
+ expect(tracingId.toString(TracingIdFormat.paddedHex)).toBe(
187
+ '00000000000000000000000000000000'
188
+ );
189
+ expect(tracingId.toString(TracingIdFormat.paddedLowHex)).toBe(
190
+ '0000000000000000'
191
+ );
192
+ expect(tracingId.toString(TracingIdFormat.paddedHighHex)).toBe(
193
+ '0000000000000000'
194
+ );
195
+ });
58
196
  });
59
197
 
60
- it('M return original string hex W toPaddedString(16, 10)', async () => {
61
- let iterations = 100;
62
- while (iterations-- > 0) {
198
+ describe('Span IDs', () => {
199
+ it('M return a valid 64 bits HEX string W toString(.hex)', async () => {
200
+ let iterations = 100;
201
+ while (iterations-- > 0) {
202
+ // GIVEN
203
+ const id = TracingIdentifier.createSpanId();
204
+ const idStr64 = id.toString(TracingIdFormat.hex);
205
+
206
+ // THEN
207
+ expect(idStr64).toMatch(/^[0-9a-f]{1,}$/);
208
+ expect(idStr64.length).toBeLessThanOrEqual(16);
209
+
210
+ expect(TracingIdentifierUtils.isWithin64Bits(idStr64, 16)).toBe(
211
+ true
212
+ );
213
+ }
214
+ });
215
+
216
+ it('M return a valid 32 bits HEX string W toString(.lowHex)', () => {
217
+ let iterations = 100;
218
+ while (iterations-- > 0) {
219
+ const tracingId = TracingIdentifier.createSpanId();
220
+ const idHex = tracingId.toString(TracingIdFormat.lowHex);
221
+
222
+ expect(idHex).toMatch(/^[0-9a-f]{1,}$/);
223
+ expect(idHex.length).toBeLessThanOrEqual(8);
224
+
225
+ expect(TracingIdentifierUtils.isWithin32Bits(idHex, 16)).toBe(
226
+ true
227
+ );
228
+ }
229
+ });
230
+
231
+ it('M return a valid 32 bits HEX string W toString(.highHex)', () => {
232
+ let iterations = 100;
233
+ while (iterations-- > 0) {
234
+ const tracingId = TracingIdentifier.createSpanId();
235
+ const idHex = tracingId.toString(TracingIdFormat.highHex);
236
+
237
+ expect(idHex).toMatch(/^[0-9a-f]{1,}$/);
238
+ expect(idHex.length).toBeLessThanOrEqual(8);
239
+
240
+ expect(TracingIdentifierUtils.isWithin32Bits(idHex, 16)).toBe(
241
+ true
242
+ );
243
+ }
244
+ });
245
+
246
+ it('M return a valid 64 bits HEX 16 string W toString(.paddedHex)', async () => {
247
+ let iterations = 100;
248
+ while (iterations-- > 0) {
249
+ // GIVEN
250
+ const id = TracingIdentifier.createSpanId();
251
+ const idStr128 = id.toString(TracingIdFormat.paddedHex);
252
+
253
+ // THEN
254
+ expect(idStr128).toMatch(/^[0-9a-f]{16}$/);
255
+ expect(
256
+ TracingIdentifierUtils.isWithin64Bits(idStr128, 16)
257
+ ).toBe(true);
258
+ }
259
+ });
260
+
261
+ it('M return a valid 64 bits HEX 8 string W paddedLowHex', () => {
262
+ let iterations = 100;
263
+ while (iterations-- > 0) {
264
+ const tracingId = TracingIdentifier.createSpanId();
265
+ const idHex = tracingId.toString(TracingIdFormat.paddedLowHex);
266
+
267
+ expect(idHex).toMatch(/^[0-9a-f]{8}$/);
268
+ expect(TracingIdentifierUtils.isWithin64Bits(idHex, 16)).toBe(
269
+ true
270
+ );
271
+ }
272
+ });
273
+
274
+ it('M return a valid 64 bits HEX 8 string W paddedHighHex', () => {
275
+ const tracingId = TracingIdentifier.createSpanId();
276
+ const idHex = tracingId.toString(TracingIdFormat.paddedHighHex);
277
+
278
+ expect(idHex).toMatch(/^[0-9a-f]{8}$/);
279
+ expect(TracingIdentifierUtils.isWithin64Bits(idHex, 16)).toBe(true);
280
+ });
281
+
282
+ it('M return a valid 64 bits integer W toString(.decimal)', async () => {
283
+ let iterations = 100;
284
+ while (iterations-- > 0) {
285
+ // GIVEN
286
+ const id = TracingIdentifier.createSpanId();
287
+ const idDecimal = id.toString(TracingIdFormat.decimal);
288
+
289
+ // THEN
290
+ expect(TracingIdentifierUtils.isWithin64Bits(idDecimal)).toBe(
291
+ true
292
+ );
293
+ }
294
+ });
295
+
296
+ it('M return a valid 32 bits low and high part integer W toString(.lowDecimal) & toString(.highDecimal)', async () => {
297
+ let iterations = 100;
298
+ while (iterations-- > 0) {
299
+ // GIVEN
300
+ const id = TracingIdentifier.createSpanId();
301
+ const idStrLow32 = id.toString(TracingIdFormat.lowDecimal);
302
+ const idStrHigh32 = id.toString(TracingIdFormat.highDecimal);
303
+
304
+ // THEN
305
+ expect(TracingIdentifierUtils.isWithin32Bits(idStrLow32)).toBe(
306
+ true
307
+ );
308
+ expect(TracingIdentifierUtils.isWithin32Bits(idStrHigh32)).toBe(
309
+ true
310
+ );
311
+ }
312
+ });
313
+
314
+ it('M return valid string representations for zero ID w toString', () => {
63
315
  // GIVEN
64
- const id = new TraceIdentifier().toPaddedString(16, 10);
316
+ const tracingId = TracingIdentifier.createSpanId();
317
+ (tracingId as any)['id'] = BigInt(0);
65
318
 
66
319
  // THEN
67
- expect(id).not.toMatch(/[0]{10}/);
68
- expect(id).toMatch(/[0-9a-f]{10}/);
69
- }
320
+
321
+ // Decimal
322
+ expect(tracingId.toString(TracingIdFormat.decimal)).toBe('0');
323
+ expect(tracingId.toString(TracingIdFormat.lowDecimal)).toBe('0');
324
+ expect(tracingId.toString(TracingIdFormat.highDecimal)).toBe('0');
325
+
326
+ // Hex
327
+ expect(tracingId.toString(TracingIdFormat.hex)).toBe('0');
328
+ expect(tracingId.toString(TracingIdFormat.lowHex)).toBe('0');
329
+ expect(tracingId.toString(TracingIdFormat.highHex)).toBe('0');
330
+
331
+ // Padded Hex
332
+ expect(tracingId.toString(TracingIdFormat.paddedHex)).toBe(
333
+ '0000000000000000'
334
+ );
335
+ expect(tracingId.toString(TracingIdFormat.paddedLowHex)).toBe(
336
+ '00000000'
337
+ );
338
+ expect(tracingId.toString(TracingIdFormat.paddedHighHex)).toBe(
339
+ '00000000'
340
+ );
341
+ });
70
342
  });
71
343
  });
@@ -7,6 +7,8 @@
7
7
  import type { PropagatorType } from '../../../types';
8
8
  import type { RegexMap } from '../requestProxy/interfaces/RequestProxy';
9
9
 
10
+ import { TracingIdentifier } from './TracingIdentifier';
11
+ import type { SpanId, TraceId } from './TracingIdentifier';
10
12
  import type { Hostname } from './firstPartyHosts';
11
13
  import { getPropagatorsForHost } from './firstPartyHosts';
12
14
 
@@ -62,8 +64,8 @@ const generateTracingAttributesWithSampling = (
62
64
  ): DdRumResourceTracingAttributes => {
63
65
  const isSampled = Math.random() * 100 <= tracingSamplingRate;
64
66
  const tracingAttributes: DdRumResourceTracingAttributes = {
65
- traceId: new TraceIdentifier() as TraceId,
66
- spanId: new TraceIdentifier() as SpanId,
67
+ traceId: TracingIdentifier.createTraceId(),
68
+ spanId: TracingIdentifier.createSpanId(),
67
69
  samplingPriorityHeader: isSampled ? '1' : '0',
68
70
  tracingStrategy: 'KEEP',
69
71
  rulePsr: tracingSamplingRate / 100,
@@ -72,60 +74,3 @@ const generateTracingAttributesWithSampling = (
72
74
 
73
75
  return tracingAttributes;
74
76
  };
75
-
76
- /**
77
- * Using branded types will ensure we don't accidentally use
78
- * traceId for spanId when generating headers.
79
- */
80
- export type TraceId = TraceIdentifier & {
81
- _brand: 'traceId';
82
- };
83
-
84
- export type SpanId = TraceIdentifier & {
85
- _brand: 'spanId';
86
- };
87
-
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;
96
-
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);
101
- }
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
- };
131
- }
@@ -6,11 +6,9 @@
6
6
 
7
7
  import { PropagatorType } from '../../../types';
8
8
 
9
- import type {
10
- DdRumResourceTracingAttributes,
11
- SpanId,
12
- TraceId
13
- } from './distributedTracing';
9
+ import { TracingIdFormat } from './TracingIdentifier';
10
+ import type { TraceId, SpanId } from './TracingIdentifier';
11
+ import type { DdRumResourceTracingAttributes } from './distributedTracing';
14
12
 
15
13
  export const SAMPLING_PRIORITY_HEADER_KEY = 'x-datadog-sampling-priority';
16
14
  /**
@@ -20,6 +18,9 @@ export const ORIGIN_HEADER_KEY = 'x-datadog-origin';
20
18
  export const ORIGIN_RUM = 'rum';
21
19
  export const TRACE_ID_HEADER_KEY = 'x-datadog-trace-id';
22
20
  export const PARENT_ID_HEADER_KEY = 'x-datadog-parent-id';
21
+ export const TAGS_HEADER_KEY = 'x-datadog-tags';
22
+ export const DD_TRACE_ID_TAG = '_dd.p.tid';
23
+
23
24
  /**
24
25
  * OTel headers
25
26
  */
@@ -51,11 +52,21 @@ export const getTracingHeaders = (
51
52
  },
52
53
  {
53
54
  header: TRACE_ID_HEADER_KEY,
54
- value: tracingAttributes.traceId.toString(10)
55
+ value: tracingAttributes.traceId.toString(
56
+ TracingIdFormat.lowDecimal
57
+ )
55
58
  },
56
59
  {
57
60
  header: PARENT_ID_HEADER_KEY,
58
- value: tracingAttributes.spanId.toString(10)
61
+ value: tracingAttributes.spanId.toString(
62
+ TracingIdFormat.decimal
63
+ )
64
+ },
65
+ {
66
+ header: TAGS_HEADER_KEY,
67
+ value: `${DD_TRACE_ID_TAG}=${tracingAttributes.traceId.toString(
68
+ TracingIdFormat.paddedHighHex
69
+ )}`
59
70
  }
60
71
  );
61
72
  break;
@@ -99,11 +110,15 @@ export const getTracingHeaders = (
99
110
  headers.push(
100
111
  {
101
112
  header: B3_MULTI_TRACE_ID_HEADER_KEY,
102
- value: tracingAttributes.traceId.toPaddedString(16, 32)
113
+ value: tracingAttributes.traceId.toString(
114
+ TracingIdFormat.paddedHex
115
+ )
103
116
  },
104
117
  {
105
118
  header: B3_MULTI_SPAN_ID_HEADER_KEY,
106
- value: tracingAttributes.spanId.toPaddedString(16, 16)
119
+ value: tracingAttributes.spanId.toString(
120
+ TracingIdFormat.paddedHex
121
+ )
107
122
  },
108
123
  {
109
124
  header: B3_MULTI_SAMPLED_HEADER_KEY,
@@ -129,10 +144,9 @@ const generateTraceContextHeader = ({
129
144
  isSampled: boolean;
130
145
  }) => {
131
146
  const flags = isSampled ? '01' : '00';
132
- return `${version}-${traceId.toPaddedString(
133
- 16,
134
- 32
135
- )}-${parentId.toPaddedString(16, 16)}-${flags}`;
147
+ return `${version}-${traceId.toString(
148
+ TracingIdFormat.paddedHex
149
+ )}-${parentId.toString(TracingIdFormat.paddedHex)}-${flags}`;
136
150
  };
137
151
 
138
152
  const generateTraceStateHeader = ({
@@ -144,7 +158,7 @@ const generateTraceStateHeader = ({
144
158
  }) => {
145
159
  const sampled = `s:${isSampled ? '1' : '0'}`;
146
160
  const origin = 'o:rum';
147
- const parent = `p:${parentId.toPaddedString(16, 16)}`;
161
+ const parent = `p:${parentId.toString(TracingIdFormat.paddedHex)}`;
148
162
 
149
163
  return `dd=${sampled};${origin};${parent}`;
150
164
  };
@@ -158,9 +172,9 @@ const generateB3Header = ({
158
172
  spanId: SpanId;
159
173
  isSampled: boolean;
160
174
  }) => {
161
- const flags = isSampled ? '1' : '0';
162
- return `${traceId.toPaddedString(16, 32)}-${spanId.toPaddedString(
163
- 16,
164
- 16
165
- )}-${flags}`;
175
+ return [
176
+ traceId.toString(TracingIdFormat.paddedHex),
177
+ spanId.toString(TracingIdFormat.paddedHex),
178
+ isSampled ? '1' : '0'
179
+ ].join('-');
166
180
  };
@@ -5,6 +5,7 @@
5
5
  */
6
6
 
7
7
  import { DdRum } from '../../../../../DdRum';
8
+ import { TracingIdFormat } from '../../../distributedTracing/TracingIdentifier';
8
9
  import type { RUMResource } from '../../interfaces/RumResource';
9
10
 
10
11
  import { createTimings } from './resourceTiming';
@@ -37,8 +38,12 @@ const formatResourceStartContext = (
37
38
  ): Record<string, string | number> => {
38
39
  const attributes: Record<string, string | number> = {};
39
40
  if (tracingAttributes.samplingPriorityHeader !== '0') {
40
- attributes['_dd.span_id'] = tracingAttributes.spanId.toString(10);
41
- attributes['_dd.trace_id'] = tracingAttributes.traceId.toString(10);
41
+ attributes['_dd.span_id'] = tracingAttributes.spanId.toString(
42
+ TracingIdFormat.decimal
43
+ );
44
+ attributes['_dd.trace_id'] = tracingAttributes.traceId.toString(
45
+ TracingIdFormat.paddedHex
46
+ );
42
47
  attributes['_dd.rule_psr'] = tracingAttributes.rulePsr;
43
48
  }
44
49