@datadog/mobile-react-native 2.3.6 → 2.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 (68) hide show
  1. package/DatadogSDKReactNative.podspec +6 -6
  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 +2 -1
  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/rum/instrumentation/interactionTracking/DdEventsInterceptor.js +14 -9
  15. package/lib/commonjs/rum/instrumentation/interactionTracking/DdEventsInterceptor.js.map +1 -1
  16. package/lib/commonjs/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js +2 -6
  17. package/lib/commonjs/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js.map +1 -1
  18. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js +182 -0
  19. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js.map +1 -0
  20. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js +10 -57
  21. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js.map +1 -1
  22. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js +18 -10
  23. package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js.map +1 -1
  24. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js +4 -2
  25. package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js.map +1 -1
  26. package/lib/commonjs/version.js +1 -1
  27. package/lib/commonjs/version.js.map +1 -1
  28. package/lib/module/DdSdkReactNative.js +2 -1
  29. package/lib/module/DdSdkReactNative.js.map +1 -1
  30. package/lib/module/DdSdkReactNativeConfiguration.js +7 -4
  31. package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
  32. package/lib/module/rum/instrumentation/interactionTracking/DdEventsInterceptor.js +14 -9
  33. package/lib/module/rum/instrumentation/interactionTracking/DdEventsInterceptor.js.map +1 -1
  34. package/lib/module/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js +2 -6
  35. package/lib/module/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js.map +1 -1
  36. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js +169 -0
  37. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js.map +1 -0
  38. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js +3 -54
  39. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js.map +1 -1
  40. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js +13 -8
  41. package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js.map +1 -1
  42. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js +3 -2
  43. package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js.map +1 -1
  44. package/lib/module/version.js +1 -1
  45. package/lib/module/version.js.map +1 -1
  46. package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +5 -1
  47. package/lib/typescript/rum/instrumentation/interactionTracking/DdEventsInterceptor.d.ts +12 -3
  48. package/lib/typescript/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.d.ts +2 -3
  49. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.d.ts +111 -0
  50. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.d.ts +1 -24
  51. package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.d.ts +2 -0
  52. package/lib/typescript/version.d.ts +1 -1
  53. package/package.json +2 -2
  54. package/src/DdSdkReactNative.tsx +4 -1
  55. package/src/DdSdkReactNativeConfiguration.tsx +10 -4
  56. package/src/__tests__/DdSdkReactNativeConfiguration.test.ts +7 -2
  57. package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +32 -0
  58. package/src/rum/instrumentation/interactionTracking/DdEventsInterceptor.tsx +23 -7
  59. package/src/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.tsx +3 -4
  60. package/src/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.tsx +227 -0
  61. package/src/rum/instrumentation/resourceTracking/distributedTracing/__tests__/__utils__/TracingIdentifierUtils.ts +64 -0
  62. package/src/rum/instrumentation/resourceTracking/distributedTracing/__tests__/distributedTracing.test.ts +314 -42
  63. package/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.tsx +4 -59
  64. package/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.ts +33 -19
  65. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.ts +7 -2
  66. package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts +134 -11
  67. package/src/sdk/FileBasedConfiguration/__tests__/FileBasedConfiguration.test.ts +3 -0
  68. package/src/version.ts +1 -1
@@ -0,0 +1,111 @@
1
+ /**
2
+ * Available formats for representing the {@link TracingIdentifier} as a string.
3
+ */
4
+ export declare enum TracingIdFormat {
5
+ /**
6
+ * Decimal string representation of the full tracing id.
7
+ */
8
+ decimal = 0,
9
+ /**
10
+ * The low bits of the tracing id as a decimal.
11
+ */
12
+ lowDecimal = 1,
13
+ /**
14
+ * The high bits of the tracing id as a decimal.
15
+ */
16
+ highDecimal = 2,
17
+ /**
18
+ * Hexadecimal string representation of the full tracing id.
19
+ */
20
+ hex = 3,
21
+ /**
22
+ * Hexadecimal string representation of the low bits of the tracing id.
23
+ */
24
+ lowHex = 4,
25
+ /**
26
+ * Hexadecimal string representation of the high bits of the tracing id.
27
+ */
28
+ highHex = 5,
29
+ /**
30
+ * Padded hexadecimal string representation of the full tracing id.
31
+ */
32
+ paddedHex = 6,
33
+ /**
34
+ * Padded hexadecimal string representation of the low bits of the tracing id.
35
+ */
36
+ paddedLowHex = 7,
37
+ /**
38
+ * Padded hexadecimal string representation of the high bits of the tracing id.
39
+ */
40
+ paddedHighHex = 8
41
+ }
42
+ /**
43
+ * A {@link TracingIdentifier} used for Traces (128 bit).
44
+ */
45
+ export type TraceId = TracingIdentifier & {
46
+ _brand: 'traceId';
47
+ };
48
+ /**
49
+ * A {@link TracingIdentifier} used for Spans (64 bit).
50
+ */
51
+ export type SpanId = TracingIdentifier & {
52
+ _brand: 'spanId';
53
+ };
54
+ /**
55
+ * The tracing identifier type.
56
+ */
57
+ export declare enum TracingIdType {
58
+ /**
59
+ * 128-bit UUID.
60
+ */
61
+ trace = 0,
62
+ /**
63
+ * 64-bit UUID.
64
+ */
65
+ span = 1
66
+ }
67
+ /**
68
+ * A {@link TracingIdentifier} is a unique UUID that can be 64bit or 128bit, and provides
69
+ * convenient methods to represent it as a HEX or DECIMAL string, and it allows the masking
70
+ * of its low or high bits.
71
+ *
72
+ * Create a new identifier by calling {@link TracingIdentifier.createTraceId()} or
73
+ * {@link TracingIdentifier.createSpanId()}.
74
+ */
75
+ export declare class TracingIdentifier {
76
+ /**
77
+ * Read-only generated ID as a {@link bigint}.
78
+ */
79
+ readonly id: bigint;
80
+ /**
81
+ * Read-only type to determine whether the identifier is a {@link TraceId} or a {@link SpanId}.
82
+ */
83
+ readonly type: TracingIdType;
84
+ /**
85
+ * Creates a new unique Trace ID.
86
+ * @returns the generated {@link TraceId}.
87
+ */
88
+ static createTraceId(): TraceId;
89
+ /**
90
+ * Creates a new unique Span ID.
91
+ * @returns the generated {@link SpanId}.
92
+ */
93
+ static createSpanId(): SpanId;
94
+ /**
95
+ * Private constructor to initialize the {@link TracingIdentifier} based on the given
96
+ * {@link TracingIdType}.
97
+ */
98
+ private constructor();
99
+ /**
100
+ * Generates a unique ID with the given format.
101
+ * @param format - the desired format (64bit or 128bit).
102
+ * @returns the generated UUID as a {@link bigint}.
103
+ */
104
+ private generateUUID;
105
+ /**
106
+ * Returns a string representation of the Tracing ID.
107
+ * @param format - The type of representation to use.
108
+ * @returns The ID as a string in the specified representation type.
109
+ */
110
+ toString(format: TracingIdFormat): string;
111
+ }
@@ -1,5 +1,6 @@
1
1
  import type { PropagatorType } from '../../../types';
2
2
  import type { RegexMap } from '../requestProxy/interfaces/RequestProxy';
3
+ import type { SpanId, TraceId } from './TracingIdentifier';
3
4
  import type { Hostname } from './firstPartyHosts';
4
5
  export type DdRumResourceTracingAttributes = {
5
6
  tracingStrategy: 'KEEP';
@@ -19,27 +20,3 @@ export declare const getTracingAttributes: ({ hostname, firstPartyHostsRegexMap,
19
20
  firstPartyHostsRegexMap: RegexMap;
20
21
  tracingSamplingRate: number;
21
22
  }) => DdRumResourceTracingAttributes;
22
- /**
23
- * Using branded types will ensure we don't accidentally use
24
- * traceId for spanId when generating headers.
25
- */
26
- export type TraceId = TraceIdentifier & {
27
- _brand: 'traceId';
28
- };
29
- export type SpanId = TraceIdentifier & {
30
- _brand: 'spanId';
31
- };
32
- export declare class TraceIdentifier {
33
- private low;
34
- private high;
35
- constructor();
36
- toString: (radix: number) => string;
37
- /**
38
- * This function pads the trace with `0`.
39
- * It should not be used with a `length` lower than the trace, as we return the full trace in this case.
40
- * @param radix radix for the trace
41
- * @param length minimum length
42
- * @returns padded string
43
- */
44
- toPaddedString: (radix: number, length: number) => string;
45
- }
@@ -7,6 +7,8 @@ export declare const ORIGIN_HEADER_KEY = "x-datadog-origin";
7
7
  export declare const ORIGIN_RUM = "rum";
8
8
  export declare const TRACE_ID_HEADER_KEY = "x-datadog-trace-id";
9
9
  export declare const PARENT_ID_HEADER_KEY = "x-datadog-parent-id";
10
+ export declare const TAGS_HEADER_KEY = "x-datadog-tags";
11
+ export declare const DD_TRACE_ID_TAG = "_dd.p.tid";
10
12
  /**
11
13
  * OTel headers
12
14
  */
@@ -1 +1 @@
1
- export declare const version = "2.3.6";
1
+ export declare const version = "2.4.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datadog/mobile-react-native",
3
- "version": "2.3.6",
3
+ "version": "2.4.0",
4
4
  "description": "A client-side React Native module to interact with Datadog",
5
5
  "keywords": [
6
6
  "datadog",
@@ -97,5 +97,5 @@
97
97
  "javaPackageName": "com.datadog.reactnative"
98
98
  }
99
99
  },
100
- "gitHead": "79c58ebfe7957fc97241d2455ddc0e75276683ff"
100
+ "gitHead": "3717f76f94bb68f6d871f84e38db5ba606eb7b06"
101
101
  }
@@ -47,6 +47,7 @@ export class DdSdkReactNative {
47
47
  private static readonly DD_SDK_VERSION = '_dd.sdk_version';
48
48
  private static readonly DD_VERSION = '_dd.version';
49
49
  private static readonly DD_VERSION_SUFFIX = '_dd.version_suffix';
50
+
50
51
  private static wasAutoInstrumented = false;
51
52
  private static features?: AutoInstrumentationConfiguration;
52
53
  private static _isInitialized = false;
@@ -299,9 +300,11 @@ export class DdSdkReactNative {
299
300
  );
300
301
  return;
301
302
  }
303
+
302
304
  if (configuration.trackInteractions) {
303
305
  DdRumUserInteractionTracking.startTracking({
304
- actionNameAttribute: configuration.actionNameAttribute
306
+ actionNameAttribute: configuration.actionNameAttribute,
307
+ useAccessibilityLabel: configuration.useAccessibilityLabel
305
308
  });
306
309
  }
307
310
 
@@ -115,7 +115,8 @@ export const DEFAULTS = {
115
115
  trackBackgroundEvents: false,
116
116
  getCustomEndpoints: () => ({}),
117
117
  bundleLogsWithRum: true,
118
- bundleLogsWithTraces: true
118
+ bundleLogsWithTraces: true,
119
+ useAccessibilityLabel: true
119
120
  };
120
121
 
121
122
  /**
@@ -303,7 +304,8 @@ export class DdSdkReactNativeConfiguration {
303
304
  readonly trackInteractions: boolean = false,
304
305
  readonly trackResources: boolean = false,
305
306
  readonly trackErrors: boolean = false,
306
- readonly trackingConsent: TrackingConsent = DEFAULTS.trackingConsent
307
+ readonly trackingConsent: TrackingConsent = DEFAULTS.trackingConsent,
308
+ readonly useAccessibilityLabel: boolean = DEFAULTS.useAccessibilityLabel
307
309
  ) {}
308
310
  }
309
311
 
@@ -321,6 +323,7 @@ export type AutoInstrumentationConfiguration = {
321
323
  readonly errorEventMapper?: ErrorEventMapper | null;
322
324
  readonly resourceEventMapper?: ResourceEventMapper | null;
323
325
  readonly actionEventMapper?: ActionEventMapper | null;
326
+ readonly useAccessibilityLabel?: boolean;
324
327
  readonly actionNameAttribute?: string;
325
328
  };
326
329
 
@@ -337,6 +340,7 @@ export type AutoInstrumentationParameters = {
337
340
  readonly errorEventMapper: ErrorEventMapper | null;
338
341
  readonly resourceEventMapper: ResourceEventMapper | null;
339
342
  readonly actionEventMapper: ActionEventMapper | null;
343
+ readonly useAccessibilityLabel: boolean;
340
344
  readonly actionNameAttribute?: string;
341
345
  };
342
346
 
@@ -370,7 +374,8 @@ export const addDefaultValuesToAutoInstrumentationConfiguration = (
370
374
  actionEventMapper:
371
375
  features.actionEventMapper === undefined
372
376
  ? DEFAULTS.actionEventMapper
373
- : features.actionEventMapper
377
+ : features.actionEventMapper,
378
+ useAccessibilityLabel: DEFAULTS.useAccessibilityLabel
374
379
  };
375
380
  };
376
381
 
@@ -434,7 +439,8 @@ export const buildConfigurationFromPartialConfiguration = (
434
439
  features.trackInteractions,
435
440
  features.trackResources,
436
441
  features.trackErrors,
437
- configuration.trackingConsent
442
+ configuration.trackingConsent,
443
+ features.useAccessibilityLabel
438
444
  );
439
445
 
440
446
  (Object.keys(
@@ -62,6 +62,7 @@ describe('DdSdkReactNativeConfiguration', () => {
62
62
  "trackResources": false,
63
63
  "trackingConsent": "granted",
64
64
  "uploadFrequency": "AVERAGE",
65
+ "useAccessibilityLabel": true,
65
66
  "verbosity": undefined,
66
67
  "vitalsUpdateFrequency": "AVERAGE",
67
68
  }
@@ -81,7 +82,8 @@ describe('DdSdkReactNativeConfiguration', () => {
81
82
  errorEventMapper: event => event,
82
83
  resourceEventMapper: event => event,
83
84
  actionEventMapper: event => event,
84
- actionNameAttribute: 'testID'
85
+ actionNameAttribute: 'testID',
86
+ useAccessibilityLabel: true
85
87
  },
86
88
  {
87
89
  applicationId: 'fake-app-id',
@@ -166,6 +168,7 @@ describe('DdSdkReactNativeConfiguration', () => {
166
168
  "trackResources": true,
167
169
  "trackingConsent": "pending",
168
170
  "uploadFrequency": "FREQUENT",
171
+ "useAccessibilityLabel": true,
169
172
  "verbosity": "debug",
170
173
  "version": "1.4.5",
171
174
  "versionSuffix": "codepush-3",
@@ -181,7 +184,8 @@ describe('DdSdkReactNativeConfiguration', () => {
181
184
  trackErrors: false,
182
185
  trackInteractions: false,
183
186
  trackResources: false,
184
- resourceTracingSamplingRate: 0
187
+ resourceTracingSamplingRate: 0,
188
+ useAccessibilityLabel: false
185
189
  },
186
190
  {
187
191
  applicationId: '',
@@ -238,6 +242,7 @@ describe('DdSdkReactNativeConfiguration', () => {
238
242
  "trackResources": false,
239
243
  "trackingConsent": "granted",
240
244
  "uploadFrequency": "AVERAGE",
245
+ "useAccessibilityLabel": false,
241
246
  "verbosity": undefined,
242
247
  "version": "",
243
248
  "versionSuffix": "",
@@ -77,6 +77,38 @@ it('M intercept and send a RUM event W onPress { Button component }', async () =
77
77
  );
78
78
  });
79
79
 
80
+ it('M intercept and send a RUM event with elementType W onPress { Button component, useAccessibilityLabel = false }', async () => {
81
+ // GIVEN
82
+ DdRumUserInteractionTracking.startTracking({
83
+ useAccessibilityLabel: false
84
+ });
85
+
86
+ const { getByText } = render(
87
+ <View>
88
+ <Button title="Click me" onPress={event => {}} />
89
+ </View>
90
+ );
91
+ const testButton = getByText('Click me');
92
+
93
+ // WHEN
94
+ fireEvent(testButton, 'press', {
95
+ _targetInst: {
96
+ elementType: 'test_element_type',
97
+ memoizedProps: {
98
+ accessibilityLabel: 'click_me_button'
99
+ }
100
+ }
101
+ });
102
+
103
+ // THEN
104
+ expect(DdRum.addAction).toBeCalledWith(
105
+ 'TAP',
106
+ 'test_element_type',
107
+ expect.anything(),
108
+ expect.anything()
109
+ );
110
+ });
111
+
80
112
  it('M intercept and send a RUM event W onPress { custom action name prop used }', async () => {
81
113
  // GIVEN
82
114
  DdRumUserInteractionTracking.startTracking({
@@ -18,6 +18,17 @@ const DEBOUNCE_EVENT_THRESHOLD_IN_MS = 10;
18
18
  const HANDLE_EVENT_APP_EXECUTION_TIME_IN_MS = 1;
19
19
  const DD_ACTION_NAME_PROP = 'dd-action-name';
20
20
 
21
+ export type DdEventsInterceptorOptions = {
22
+ /**
23
+ * Specifies a custom prop to name RUM actions on elements having an `onPress` prop
24
+ * */
25
+ actionNameAttribute?: string;
26
+ /**
27
+ * Determines whether the accessibility label can be used to resolve the action name.
28
+ * */
29
+ useAccessibilityLabel?: boolean;
30
+ };
31
+
21
32
  export class DdEventsInterceptor implements EventsInterceptor {
22
33
  static ACTION_EVENT_DROPPED_DEBUG_MESSAGE =
23
34
  'An action event was dropped because either the `onPress` method arguments' +
@@ -29,11 +40,11 @@ export class DdEventsInterceptor implements EventsInterceptor {
29
40
  private debouncingStartedTimestamp = Number.MIN_VALUE;
30
41
 
31
42
  private actionNameAttribute?: string;
43
+ private useAccessibilityLabel: boolean;
32
44
 
33
- constructor({
34
- actionNameAttribute
35
- }: { actionNameAttribute?: string } = {}) {
36
- this.actionNameAttribute = actionNameAttribute;
45
+ constructor(options: DdEventsInterceptorOptions = {}) {
46
+ this.actionNameAttribute = options.actionNameAttribute;
47
+ this.useAccessibilityLabel = options.useAccessibilityLabel ?? true;
37
48
  }
38
49
 
39
50
  interceptOnPress(...args: any[]): void {
@@ -75,10 +86,15 @@ export class DdEventsInterceptor implements EventsInterceptor {
75
86
  if (closestActionLabel != null) {
76
87
  return closestActionLabel;
77
88
  }
78
- const accessibilityLabel = targetNode.memoizedProps?.accessibilityLabel;
79
- if (accessibilityLabel != null) {
80
- return accessibilityLabel;
89
+
90
+ if (this.useAccessibilityLabel) {
91
+ const accessibilityLabel =
92
+ targetNode.memoizedProps?.accessibilityLabel;
93
+ if (accessibilityLabel != null) {
94
+ return accessibilityLabel;
95
+ }
81
96
  }
97
+
82
98
  const elementTypeName = this.resolveElementTypeName(
83
99
  targetNode.elementType
84
100
  );
@@ -12,6 +12,7 @@ import { DdSdk } from '../../../sdk/DdSdk';
12
12
  import { getErrorMessage } from '../../../utils/errorUtils';
13
13
 
14
14
  import { DdEventsInterceptor } from './DdEventsInterceptor';
15
+ import type { DdEventsInterceptorOptions } from './DdEventsInterceptor';
15
16
  import type EventsInterceptor from './EventsInterceptor';
16
17
  import NoOpEventsInterceptor from './NoOpEventsInterceptor';
17
18
  import { areObjectShallowEqual } from './ShallowObjectEqualityChecker';
@@ -58,9 +59,7 @@ export class DdRumUserInteractionTracking {
58
59
  * Please note that we are only considering as valid - for - tracking only the user interactions that have
59
60
  * a visible output (either an UI state change or a Resource request)
60
61
  */
61
- static startTracking({
62
- actionNameAttribute
63
- }: { actionNameAttribute?: string } = {}): void {
62
+ static startTracking(options: DdEventsInterceptorOptions): void {
64
63
  // extra safety to avoid wrapping more than 1 time this function
65
64
  if (DdRumUserInteractionTracking.isTracking) {
66
65
  InternalLog.log(
@@ -70,7 +69,7 @@ export class DdRumUserInteractionTracking {
70
69
  return;
71
70
  }
72
71
  DdRumUserInteractionTracking.eventsInterceptor = new DdEventsInterceptor(
73
- { actionNameAttribute }
72
+ options
74
73
  );
75
74
 
76
75
  const original = React.createElement;
@@ -0,0 +1,227 @@
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
+ /**
8
+ * Available formats for representing the {@link TracingIdentifier} as a string.
9
+ */
10
+ export enum TracingIdFormat {
11
+ /**
12
+ * Decimal string representation of the full tracing id.
13
+ */
14
+ decimal,
15
+
16
+ /**
17
+ * The low bits of the tracing id as a decimal.
18
+ */
19
+ lowDecimal,
20
+
21
+ /**
22
+ * The high bits of the tracing id as a decimal.
23
+ */
24
+ highDecimal,
25
+
26
+ /**
27
+ * Hexadecimal string representation of the full tracing id.
28
+ */
29
+ hex,
30
+
31
+ /**
32
+ * Hexadecimal string representation of the low bits of the tracing id.
33
+ */
34
+ lowHex,
35
+
36
+ /**
37
+ * Hexadecimal string representation of the high bits of the tracing id.
38
+ */
39
+ highHex,
40
+
41
+ /**
42
+ * Padded hexadecimal string representation of the full tracing id.
43
+ */
44
+ paddedHex,
45
+
46
+ /**
47
+ * Padded hexadecimal string representation of the low bits of the tracing id.
48
+ */
49
+ paddedLowHex,
50
+
51
+ /**
52
+ * Padded hexadecimal string representation of the high bits of the tracing id.
53
+ */
54
+ paddedHighHex
55
+ }
56
+
57
+ /**
58
+ * A {@link TracingIdentifier} used for Traces (128 bit).
59
+ */
60
+ export type TraceId = TracingIdentifier & {
61
+ _brand: 'traceId';
62
+ };
63
+
64
+ /**
65
+ * A {@link TracingIdentifier} used for Spans (64 bit).
66
+ */
67
+ export type SpanId = TracingIdentifier & {
68
+ _brand: 'spanId';
69
+ };
70
+
71
+ /**
72
+ * The tracing identifier type.
73
+ */
74
+ export enum TracingIdType {
75
+ /**
76
+ * 128-bit UUID.
77
+ */
78
+ trace,
79
+ /**
80
+ * 64-bit UUID.
81
+ */
82
+ span
83
+ }
84
+
85
+ /**
86
+ * Value used to mask the low 64 bits of the trace identifier.
87
+ */
88
+ const LOW_64BIT_MASK =
89
+ (BigInt('0xffffffff') << BigInt(32)) + BigInt('0xffffffff');
90
+
91
+ /**
92
+ * Value used to mask the low 32 bits of the trace identifier.
93
+ */
94
+ const LOW_32BIT_MASK = (BigInt('0xffff') << BigInt(16)) + BigInt('0xffff');
95
+
96
+ /**
97
+ * A {@link TracingIdentifier} is a unique UUID that can be 64bit or 128bit, and provides
98
+ * convenient methods to represent it as a HEX or DECIMAL string, and it allows the masking
99
+ * of its low or high bits.
100
+ *
101
+ * Create a new identifier by calling {@link TracingIdentifier.createTraceId()} or
102
+ * {@link TracingIdentifier.createSpanId()}.
103
+ */
104
+ export class TracingIdentifier {
105
+ /**
106
+ * Read-only generated ID as a {@link bigint}.
107
+ */
108
+ readonly id: bigint;
109
+
110
+ /**
111
+ * Read-only type to determine whether the identifier is a {@link TraceId} or a {@link SpanId}.
112
+ */
113
+ readonly type: TracingIdType;
114
+
115
+ /**
116
+ * Creates a new unique Trace ID.
117
+ * @returns the generated {@link TraceId}.
118
+ */
119
+ public static createTraceId(): TraceId {
120
+ return new TracingIdentifier(TracingIdType.trace) as TraceId;
121
+ }
122
+
123
+ /**
124
+ * Creates a new unique Span ID.
125
+ * @returns the generated {@link SpanId}.
126
+ */
127
+ public static createSpanId(): SpanId {
128
+ return new TracingIdentifier(TracingIdType.span) as SpanId;
129
+ }
130
+
131
+ /**
132
+ * Private constructor to initialize the {@link TracingIdentifier} based on the given
133
+ * {@link TracingIdType}.
134
+ */
135
+ private constructor(type: TracingIdType) {
136
+ this.id = this.generateUUID(type);
137
+ this.type = type;
138
+ }
139
+
140
+ /**
141
+ * Generates a unique ID with the given format.
142
+ * @param format - the desired format (64bit or 128bit).
143
+ * @returns the generated UUID as a {@link bigint}.
144
+ */
145
+ private generateUUID(type: TracingIdType): bigint {
146
+ // Get the current Unix timestamp in seconds
147
+ const unixSeconds = Math.floor(Date.now() / 1000);
148
+
149
+ // Ensure the Unix timestamp is 32 bits
150
+ const unixSeconds32 = unixSeconds & 0xffffffff;
151
+
152
+ // 32 bits of zero
153
+ const zeros32 = 0;
154
+
155
+ // Generate 64 random bits using Math.random()
156
+ const random32Bit1 = Math.floor(Math.random() * 0xffffffff);
157
+ const random32Bit2 = Math.floor(Math.random() * 0xffffffff);
158
+ const random64Hex =
159
+ random32Bit1.toString(16).padStart(8, '0') +
160
+ random32Bit2.toString(16).padStart(8, '0');
161
+
162
+ // If type is 'span' we return the generated 64 bit ID
163
+ if (type === TracingIdType.span) {
164
+ return BigInt(`0x${random64Hex}`);
165
+ }
166
+
167
+ // Convert parts to hexadecimal strings
168
+ const unixSecondsHex = unixSeconds32.toString(16).padStart(8, '0');
169
+ const zerosHex = zeros32.toString(16).padStart(8, '0');
170
+
171
+ // Combine parts to form the 128-bit ID
172
+ const hex128BitID = unixSecondsHex + zerosHex + random64Hex;
173
+
174
+ return BigInt(`0x${hex128BitID}`);
175
+ }
176
+
177
+ /**
178
+ * Returns a string representation of the Tracing ID.
179
+ * @param format - The type of representation to use.
180
+ * @returns The ID as a string in the specified representation type.
181
+ */
182
+ public toString(format: TracingIdFormat): string {
183
+ const lowTraceMask =
184
+ this.type === TracingIdType.trace ? LOW_64BIT_MASK : LOW_32BIT_MASK;
185
+ const highTraceMask =
186
+ this.type === TracingIdType.trace ? BigInt(64) : BigInt(32);
187
+ const padding = this.type === TracingIdType.trace ? 32 : 16;
188
+
189
+ switch (format) {
190
+ case TracingIdFormat.decimal:
191
+ return this.id.toString(10);
192
+
193
+ case TracingIdFormat.lowDecimal:
194
+ return (this.id & lowTraceMask).toString(10);
195
+
196
+ case TracingIdFormat.highDecimal:
197
+ return (this.id >> highTraceMask).toString(10);
198
+
199
+ case TracingIdFormat.hex:
200
+ return this.id.toString(16);
201
+
202
+ case TracingIdFormat.lowHex:
203
+ return (this.id & lowTraceMask).toString(16);
204
+
205
+ case TracingIdFormat.highHex:
206
+ return (this.id >> highTraceMask).toString(16);
207
+
208
+ case TracingIdFormat.paddedHex:
209
+ return this.toString(TracingIdFormat.hex).padStart(
210
+ padding,
211
+ '0'
212
+ );
213
+
214
+ case TracingIdFormat.paddedLowHex:
215
+ return this.toString(TracingIdFormat.lowHex).padStart(
216
+ padding / 2,
217
+ '0'
218
+ );
219
+
220
+ case TracingIdFormat.paddedHighHex:
221
+ return this.toString(TracingIdFormat.highHex).padStart(
222
+ padding / 2,
223
+ '0'
224
+ );
225
+ }
226
+ }
227
+ }