@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.
- package/DatadogSDKReactNative.podspec +7 -7
- package/android/build.gradle +4 -4
- package/android/src/main/kotlin/com/datadog/reactnative/SdkVersion.kt +1 -1
- package/android/src/test/kotlin/com/datadog/tools/unit/MockRumMonitor.kt +2 -0
- package/android/src/test/kotlin/com/datadog/tools/unit/forge/TelemetryConfigurationEventForgeryFactory.kt +15 -0
- package/android/src/test/resources/input/complete-configuration.json +5 -1
- package/datadog-configuration.schema.json +54 -9
- package/ios/Sources/DatadogSDKWrapper.swift +3 -3
- package/ios/Sources/SdkVersion.swift +1 -1
- package/lib/commonjs/DdSdkReactNative.js +6 -12
- package/lib/commonjs/DdSdkReactNative.js.map +1 -1
- package/lib/commonjs/DdSdkReactNativeConfiguration.js +7 -4
- package/lib/commonjs/DdSdkReactNativeConfiguration.js.map +1 -1
- package/lib/commonjs/index.js +2 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/polyfills.js +31 -0
- package/lib/commonjs/polyfills.js.map +1 -0
- package/lib/commonjs/rum/DdRum.js +3 -3
- package/lib/commonjs/rum/DdRum.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/interactionTracking/DdEventsInterceptor.js +14 -9
- package/lib/commonjs/rum/instrumentation/interactionTracking/DdEventsInterceptor.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js +2 -6
- package/lib/commonjs/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js +182 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js.map +1 -0
- package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js +10 -57
- package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js +18 -10
- package/lib/commonjs/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js.map +1 -1
- package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js +4 -2
- package/lib/commonjs/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js.map +1 -1
- package/lib/commonjs/sdk/GlobalState/GlobalState.js +38 -0
- package/lib/commonjs/sdk/GlobalState/GlobalState.js.map +1 -0
- package/lib/commonjs/specs/NativeDdLogs.js +1 -0
- package/lib/commonjs/specs/NativeDdLogs.js.map +1 -1
- package/lib/commonjs/specs/NativeDdRum.js +1 -0
- package/lib/commonjs/specs/NativeDdRum.js.map +1 -1
- package/lib/commonjs/specs/NativeDdSdk.js +1 -0
- package/lib/commonjs/specs/NativeDdSdk.js.map +1 -1
- package/lib/commonjs/specs/NativeDdTrace.js +1 -0
- package/lib/commonjs/specs/NativeDdTrace.js.map +1 -1
- package/lib/commonjs/version.js +1 -1
- package/lib/commonjs/version.js.map +1 -1
- package/lib/module/DdSdkReactNative.js +5 -12
- package/lib/module/DdSdkReactNative.js.map +1 -1
- package/lib/module/DdSdkReactNativeConfiguration.js +7 -4
- package/lib/module/DdSdkReactNativeConfiguration.js.map +1 -1
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/polyfills.js +28 -0
- package/lib/module/polyfills.js.map +1 -0
- package/lib/module/rum/DdRum.js +2 -2
- package/lib/module/rum/DdRum.js.map +1 -1
- package/lib/module/rum/instrumentation/interactionTracking/DdEventsInterceptor.js +14 -9
- package/lib/module/rum/instrumentation/interactionTracking/DdEventsInterceptor.js.map +1 -1
- package/lib/module/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js +2 -6
- package/lib/module/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.js.map +1 -1
- package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js +169 -0
- package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.js.map +1 -0
- package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js +3 -54
- package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.js.map +1 -1
- package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js +13 -8
- package/lib/module/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.js.map +1 -1
- package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js +3 -2
- package/lib/module/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.js.map +1 -1
- package/lib/module/sdk/GlobalState/GlobalState.js +29 -0
- package/lib/module/sdk/GlobalState/GlobalState.js.map +1 -0
- package/lib/module/specs/NativeDdLogs.js +1 -0
- package/lib/module/specs/NativeDdLogs.js.map +1 -1
- package/lib/module/specs/NativeDdRum.js +1 -0
- package/lib/module/specs/NativeDdRum.js.map +1 -1
- package/lib/module/specs/NativeDdSdk.js +1 -0
- package/lib/module/specs/NativeDdSdk.js.map +1 -1
- package/lib/module/specs/NativeDdTrace.js +1 -0
- package/lib/module/specs/NativeDdTrace.js.map +1 -1
- package/lib/module/version.js +1 -1
- package/lib/module/version.js.map +1 -1
- package/lib/typescript/DdSdkReactNative.d.ts +0 -5
- package/lib/typescript/DdSdkReactNativeConfiguration.d.ts +5 -1
- package/lib/typescript/index.d.ts +1 -0
- package/lib/typescript/rum/instrumentation/interactionTracking/DdEventsInterceptor.d.ts +12 -3
- package/lib/typescript/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.d.ts +2 -3
- package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.d.ts +111 -0
- package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.d.ts +1 -24
- package/lib/typescript/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.d.ts +2 -0
- package/lib/typescript/sdk/GlobalState/GlobalState.d.ts +12 -0
- package/lib/typescript/version.d.ts +1 -1
- package/package.json +2 -2
- package/src/DdSdkReactNative.tsx +7 -11
- package/src/DdSdkReactNativeConfiguration.tsx +10 -4
- package/src/__tests__/DdSdkReactNative.test.tsx +3 -2
- package/src/__tests__/DdSdkReactNativeConfiguration.test.ts +7 -2
- package/src/__tests__/mock.test.ts +0 -1
- package/src/__tests__/rum/instrumentation/DdRumUserInteractionTracking.test.tsx +32 -0
- package/src/index.tsx +4 -0
- package/src/polyfills.js +37 -0
- package/src/rum/DdRum.ts +2 -2
- package/src/rum/__tests__/DdRum.test.ts +3 -3
- package/src/rum/instrumentation/interactionTracking/DdEventsInterceptor.tsx +23 -7
- package/src/rum/instrumentation/interactionTracking/DdRumUserInteractionTracking.tsx +3 -4
- package/src/rum/instrumentation/resourceTracking/__tests__/__utils__/XMLHttpRequestMock.ts +1 -0
- package/src/rum/instrumentation/resourceTracking/distributedTracing/TracingIdentifier.tsx +227 -0
- package/src/rum/instrumentation/resourceTracking/distributedTracing/__tests__/__utils__/TracingIdentifierUtils.ts +64 -0
- package/src/rum/instrumentation/resourceTracking/distributedTracing/__tests__/distributedTracing.test.ts +314 -42
- package/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracing.tsx +4 -59
- package/src/rum/instrumentation/resourceTracking/distributedTracing/distributedTracingHeaders.ts +33 -19
- package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/DatadogRumResource/ResourceReporter.ts +7 -2
- package/src/rum/instrumentation/resourceTracking/requestProxy/XHRProxy/__tests__/XHRProxy.test.ts +134 -11
- package/src/sdk/DatadogProvider/__tests__/initialization.test.tsx +2 -2
- package/src/sdk/DatadogProvider/__tests__/initializationModes.test.tsx +3 -2
- package/src/sdk/FileBasedConfiguration/__tests__/FileBasedConfiguration.test.ts +3 -0
- package/src/sdk/GlobalState/GlobalState.tsx +26 -0
- package/src/specs/NativeDdLogs.ts +1 -0
- package/src/specs/NativeDdRum.ts +1 -0
- package/src/specs/NativeDdSdk.ts +1 -0
- package/src/specs/NativeDdTrace.ts +1 -0
- package/src/version.ts +1 -1
package/src/polyfills.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
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 { version as reactNativeVersion } from 'react-native/package.json';
|
|
8
|
+
|
|
9
|
+
function applyBigIntPolyfill() {
|
|
10
|
+
const rnVersion = reactNativeVersion.split('.').map(Number);
|
|
11
|
+
const isRn63 = rnVersion[0] === 0 && rnVersion[1] === 63;
|
|
12
|
+
|
|
13
|
+
if (isRn63 && typeof BigInt === 'undefined') {
|
|
14
|
+
try {
|
|
15
|
+
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
|
|
16
|
+
global.BigInt = require('big-integer');
|
|
17
|
+
console.warn(
|
|
18
|
+
'React Native 0.63 does not support BigInt, which is required starting from v2.4.0 of Datadog React Native SDK. ' +
|
|
19
|
+
'The missing type has been polyfilled using `big-integer` to grant back-compatibility. ' +
|
|
20
|
+
'We strongly suggest updating to a greater version of React Native (>= 0.64).'
|
|
21
|
+
);
|
|
22
|
+
} catch (e) {
|
|
23
|
+
if (e instanceof Error && e.code === 'MODULE_NOT_FOUND') {
|
|
24
|
+
throw new Error(
|
|
25
|
+
'React Native 0.63 does not support BigInt, which is required starting from v2.4.0 of Datadog React Native SDK. ' +
|
|
26
|
+
'We strongly suggest updating to a greater version of React Native (>= 0.64).\n\n' +
|
|
27
|
+
'You can install `big-integer` to enable our polyfill that grants back-compatibility with RN 63:\n\n' +
|
|
28
|
+
'`yarn add big-integer`\n\nOR\n\n`npm install --save big-integer`'
|
|
29
|
+
);
|
|
30
|
+
} else {
|
|
31
|
+
throw e;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
applyBigIntPolyfill();
|
package/src/rum/DdRum.ts
CHANGED
|
@@ -6,12 +6,12 @@
|
|
|
6
6
|
|
|
7
7
|
import type { GestureResponderEvent } from 'react-native';
|
|
8
8
|
|
|
9
|
-
import { DdSdkReactNative } from '../DdSdkReactNative';
|
|
10
9
|
import { InternalLog } from '../InternalLog';
|
|
11
10
|
import { SdkVerbosity } from '../SdkVerbosity';
|
|
12
11
|
import type { DdNativeRumType } from '../nativeModulesTypes';
|
|
13
12
|
import { bufferVoidNativeCall } from '../sdk/DatadogProvider/Buffer/bufferNativeCall';
|
|
14
13
|
import { DdSdk } from '../sdk/DdSdk';
|
|
14
|
+
import { GlobalState } from '../sdk/GlobalState/GlobalState';
|
|
15
15
|
import { validateContext } from '../utils/argsUtils';
|
|
16
16
|
import { DefaultTimeProvider } from '../utils/time-provider/DefaultTimeProvider';
|
|
17
17
|
import type { TimeProvider } from '../utils/time-provider/TimeProvider';
|
|
@@ -284,7 +284,7 @@ class DdRumWrapper implements DdRumType {
|
|
|
284
284
|
};
|
|
285
285
|
|
|
286
286
|
async getCurrentSessionId(): Promise<string | undefined> {
|
|
287
|
-
if (!
|
|
287
|
+
if (!GlobalState.instance.isInitialized) {
|
|
288
288
|
return undefined;
|
|
289
289
|
}
|
|
290
290
|
return this.nativeRum.getCurrentSessionId();
|
|
@@ -7,11 +7,11 @@
|
|
|
7
7
|
|
|
8
8
|
import { NativeModules } from 'react-native';
|
|
9
9
|
|
|
10
|
-
import { DdSdkReactNative } from '../../DdSdkReactNative';
|
|
11
10
|
import { InternalLog } from '../../InternalLog';
|
|
12
11
|
import { SdkVerbosity } from '../../SdkVerbosity';
|
|
13
12
|
import { BufferSingleton } from '../../sdk/DatadogProvider/Buffer/BufferSingleton';
|
|
14
13
|
import { DdSdk } from '../../sdk/DdSdk';
|
|
14
|
+
import { GlobalState } from '../../sdk/GlobalState/GlobalState';
|
|
15
15
|
import { DdRum } from '../DdRum';
|
|
16
16
|
import type { ActionEventMapper } from '../eventMappers/actionEventMapper';
|
|
17
17
|
import type { ErrorEventMapper } from '../eventMappers/errorEventMapper';
|
|
@@ -929,7 +929,7 @@ describe('DdRum', () => {
|
|
|
929
929
|
|
|
930
930
|
describe('DdRum.getCurrentSessionId', () => {
|
|
931
931
|
it('calls the native API if SDK is initialized', async () => {
|
|
932
|
-
|
|
932
|
+
GlobalState.instance.isInitialized = true;
|
|
933
933
|
const sessionId = await DdRum.getCurrentSessionId();
|
|
934
934
|
expect(NativeModules.DdRum.getCurrentSessionId).toHaveBeenCalled();
|
|
935
935
|
expect(sessionId).toBe('test-session-id');
|
|
@@ -938,7 +938,7 @@ describe('DdRum', () => {
|
|
|
938
938
|
|
|
939
939
|
describe('DdRum.getCurrentSessionId', () => {
|
|
940
940
|
it('returns undefined if SDK is not initialized', async () => {
|
|
941
|
-
|
|
941
|
+
GlobalState.instance.isInitialized = false;
|
|
942
942
|
const sessionId = await DdRum.getCurrentSessionId();
|
|
943
943
|
expect(
|
|
944
944
|
NativeModules.DdRum.getCurrentSessionId
|
|
@@ -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
|
-
|
|
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
|
-
|
|
79
|
-
if (
|
|
80
|
-
|
|
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
|
-
|
|
72
|
+
options
|
|
74
73
|
);
|
|
75
74
|
|
|
76
75
|
const original = React.createElement;
|
|
@@ -18,6 +18,7 @@ export class XMLHttpRequestMock implements XMLHttpRequest {
|
|
|
18
18
|
public requestHeaders: Map<string, string> = new Map();
|
|
19
19
|
public responseHeaders: Map<string, string> = new Map();
|
|
20
20
|
|
|
21
|
+
// eslint-disable-next-line no-empty-function
|
|
21
22
|
constructor() {}
|
|
22
23
|
responseText: string;
|
|
23
24
|
responseURL: string;
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
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
|
+
export class TracingIdentifierUtils {
|
|
8
|
+
/**
|
|
9
|
+
* Extracts the Unix timestamp from the 128-bit hex string representation.
|
|
10
|
+
* @param idHex - The 128-bit ID as a hexadecimal string.
|
|
11
|
+
* @returns The Unix timestamp as a number.
|
|
12
|
+
*/
|
|
13
|
+
public static extractTimestamp(idHex: string): number {
|
|
14
|
+
// Extract the first 8 characters which represent the 32-bit Unix timestamp
|
|
15
|
+
const timestampHex = idHex.substring(0, 8);
|
|
16
|
+
|
|
17
|
+
// Convert the hexadecimal string to a number
|
|
18
|
+
const timestamp = parseInt(timestampHex, 16);
|
|
19
|
+
|
|
20
|
+
return timestamp;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Checks if a string representation of an ID in a given radix is within 32 bits.
|
|
25
|
+
* @param idString - The string representation of the ID.
|
|
26
|
+
* @param radix - Optional base to use for the conversion (default is 10).
|
|
27
|
+
* @returns True if the ID is within 32 bits, otherwise false.
|
|
28
|
+
*/
|
|
29
|
+
public static isWithin32Bits(
|
|
30
|
+
idString: string,
|
|
31
|
+
radix: number = 10
|
|
32
|
+
): boolean {
|
|
33
|
+
const bigIntValue = BigInt(parseInt(idString, radix));
|
|
34
|
+
return bigIntValue < BigInt(1) << BigInt(32);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Checks if a string representation of an ID in a given radix is within 64 bits.
|
|
39
|
+
* @param idString - The string representation of the ID.
|
|
40
|
+
* @param radix - Optional base to use for the conversion (default is 10).
|
|
41
|
+
* @returns True if the ID is within 64 bits, otherwise false.
|
|
42
|
+
*/
|
|
43
|
+
public static isWithin64Bits(
|
|
44
|
+
idString: string,
|
|
45
|
+
radix: number = 10
|
|
46
|
+
): boolean {
|
|
47
|
+
const bigIntValue = BigInt(parseInt(idString, radix));
|
|
48
|
+
return bigIntValue < BigInt(1) << BigInt(64);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Checks if a string representation of an ID in a given radix is within 128 bits.
|
|
53
|
+
* @param idString - The string representation of the ID.
|
|
54
|
+
* @param radix - Optional base to use for the conversion (default is 10).
|
|
55
|
+
* @returns True if the ID is within 128 bits, otherwise false.
|
|
56
|
+
*/
|
|
57
|
+
public static isWithin128Bits(
|
|
58
|
+
idString: string,
|
|
59
|
+
radix: number = 10
|
|
60
|
+
): boolean {
|
|
61
|
+
const bigIntValue = BigInt(parseInt(idString, radix));
|
|
62
|
+
return bigIntValue < BigInt(1) << BigInt(128);
|
|
63
|
+
}
|
|
64
|
+
}
|