Skip to content

Commit

Permalink
Merge pull request #765 from DataDog/marcosaia/RUM-6875/big-integer
Browse files Browse the repository at this point in the history
[RUM-6875] Use BigInteger from big-integer instead of bigint
  • Loading branch information
marco-saia-datadog authored Jan 7, 2025
2 parents 48965c6 + 389023b commit 97f26f3
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 63 deletions.
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Component,Origin,License,Copyright
prod,big-integer,Unlicense,"Free and unencumbered software released into the public domain."
prod,lodash.isequal,CC0,"Copyright jQuery Foundation and other contributors <https://jquery.org/>"
prod,react-native,MIT,"Copyright (c) Facebook, Inc. and its affiliates."
dev,@apollo/client,MIT,"Copyright (c) 2022 Apollo Graph, Inc. (Formerly Meteor Development Group, Inc.)"
Expand Down
3 changes: 3 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,8 @@
"android": {
"javaPackageName": "com.datadog.reactnative"
}
},
"dependencies": {
"big-integer": "^1.6.52"
}
}
1 change: 0 additions & 1 deletion packages/core/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
/* eslint-disable arca/import-ordering */

import './polyfills';
import {
DatadogProviderConfiguration,
DdSdkReactNativeConfiguration,
Expand Down
37 changes: 0 additions & 37 deletions packages/core/src/polyfills.js

This file was deleted.

5 changes: 3 additions & 2 deletions packages/core/src/rum/__tests__/DdRum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Copyright 2016-Present Datadog, Inc.
*/

import BigInt from 'big-integer';
import { NativeModules } from 'react-native';

import { InternalLog } from '../../InternalLog';
Expand Down Expand Up @@ -454,13 +455,13 @@ describe('DdRum', () => {
const traceUUID = DdRum.generateUUID(TracingIdType.trace);

expect(traceUUID).toBeDefined(); // Ensure the value is defined
expect(BigInt(traceUUID)).toBeGreaterThan(0n); // Ensure it's a valid positive number
expect(BigInt(traceUUID).greater(BigInt(0))).toBe(true); // Ensure it's a valid positive number
});
it('generates a valid span id in decimal format', () => {
const spanUUID = DdRum.generateUUID(TracingIdType.span);

expect(spanUUID).toBeDefined();
expect(BigInt(spanUUID)).toBeGreaterThan(0n);
expect(BigInt(spanUUID).greater(BigInt(0))).toBe(true);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* Copyright 2016-Present Datadog, Inc.
*/

import type { BigInteger } from 'big-integer';
import BigInt from 'big-integer';

/**
* Available formats for representing the {@link TracingIdentifier} as a string.
*/
Expand Down Expand Up @@ -85,13 +88,16 @@ export enum TracingIdType {
/**
* Value used to mask the low 64 bits of the trace identifier.
*/
const LOW_64BIT_MASK =
(BigInt('0xffffffff') << BigInt(32)) + BigInt('0xffffffff');
const LOW_64BIT_MASK: BigInteger = BigInt('ffffffff', 16)
.shiftLeft(32)
.add(BigInt('ffffffff', 16));

/**
* Value used to mask the low 32 bits of the trace identifier.
*/
const LOW_32BIT_MASK = (BigInt('0xffff') << BigInt(16)) + BigInt('0xffff');
const LOW_32BIT_MASK: BigInteger = BigInt('ffff', 16)
.shiftLeft(16)
.add(BigInt('ffff', 16));

/**
* A {@link TracingIdentifier} is a unique UUID that can be 64bit or 128bit, and provides
Expand All @@ -103,9 +109,9 @@ const LOW_32BIT_MASK = (BigInt('0xffff') << BigInt(16)) + BigInt('0xffff');
*/
export class TracingIdentifier {
/**
* Read-only generated ID as a {@link bigint}.
* Read-only generated ID as a {@link BigInteger}.
*/
readonly id: bigint;
readonly id: BigInteger;

/**
* Read-only type to determine whether the identifier is a {@link TraceId} or a {@link SpanId}.
Expand Down Expand Up @@ -140,9 +146,9 @@ export class TracingIdentifier {
/**
* Generates a unique ID with the given format.
* @param format - the desired format (64bit or 128bit).
* @returns the generated UUID as a {@link bigint}.
* @returns the generated UUID as a {@link BigInteger}.
*/
private generateUUID(type: TracingIdType): bigint {
private generateUUID(type: TracingIdType): BigInteger {
// Get the current Unix timestamp in seconds
const unixSeconds = Math.floor(Date.now() / 1000);

Expand All @@ -161,7 +167,7 @@ export class TracingIdentifier {

// If type is 'span' we return the generated 64 bit ID
if (type === TracingIdType.span) {
return BigInt(`0x${random64Hex}`);
return BigInt(random64Hex, 16);
}

// Convert parts to hexadecimal strings
Expand All @@ -171,7 +177,7 @@ export class TracingIdentifier {
// Combine parts to form the 128-bit ID
const hex128BitID = unixSecondsHex + zerosHex + random64Hex;

return BigInt(`0x${hex128BitID}`);
return BigInt(hex128BitID, 16);
}

/**
Expand All @@ -191,19 +197,19 @@ export class TracingIdentifier {
return this.id.toString(10);

case TracingIdFormat.lowDecimal:
return (this.id & lowTraceMask).toString(10);
return this.id.and(lowTraceMask).toString(10);

case TracingIdFormat.highDecimal:
return (this.id >> highTraceMask).toString(10);
return this.id.shiftRight(highTraceMask).toString(10);

case TracingIdFormat.hex:
return this.id.toString(16);

case TracingIdFormat.lowHex:
return (this.id & lowTraceMask).toString(16);
return this.id.and(lowTraceMask).toString(16);

case TracingIdFormat.highHex:
return (this.id >> highTraceMask).toString(16);
return this.id.shiftRight(highTraceMask).toString(16);

case TracingIdFormat.paddedHex:
return this.toString(TracingIdFormat.hex).padStart(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
import BigInt from 'big-integer';

export class TracingIdentifierUtils {
/**
Expand Down Expand Up @@ -30,8 +31,9 @@ export class TracingIdentifierUtils {
idString: string,
radix: number = 10
): boolean {
const bigIntValue = BigInt(parseInt(idString, radix));
return bigIntValue < BigInt(1) << BigInt(32);
return BigInt(parseInt(idString, radix)).lesser(
BigInt(1).shiftLeft(32)
);
}

/**
Expand All @@ -44,8 +46,9 @@ export class TracingIdentifierUtils {
idString: string,
radix: number = 10
): boolean {
const bigIntValue = BigInt(parseInt(idString, radix));
return bigIntValue < BigInt(1) << BigInt(64);
return BigInt(parseInt(idString, radix)).lesser(
BigInt(1).shiftLeft(64)
);
}

/**
Expand All @@ -58,7 +61,8 @@ export class TracingIdentifierUtils {
idString: string,
radix: number = 10
): boolean {
const bigIntValue = BigInt(parseInt(idString, radix));
return bigIntValue < BigInt(1) << BigInt(128);
return BigInt(parseInt(idString, radix)).lesser(
BigInt(1).shiftLeft(128)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Copyright 2016-Present Datadog, Inc.
*/

import BigInt from 'big-integer';

import { TracingIdentifier, TracingIdFormat } from '../TracingIdentifier';

import { TracingIdentifierUtils } from './__utils__/TracingIdentifierUtils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Copyright 2016-Present Datadog, Inc.
*/

import BigInt from 'big-integer';

import type { PropagatorType } from '../../../types';
import type { RegexMap } from '../requestProxy/interfaces/RequestProxy';

Expand All @@ -13,7 +15,7 @@ import type { Hostname } from './firstPartyHosts';
import { getPropagatorsForHost } from './firstPartyHosts';

const knuthFactor = BigInt('1111111111111111111');
const twoPow64 = BigInt('0x10000000000000000'); // 2n ** 64n
const twoPow64 = BigInt('10000000000000000', 16); // 2n ** 64n

export type DdRumResourceTracingAttributes =
| {
Expand Down Expand Up @@ -66,7 +68,7 @@ const generateTracingAttributesWithSampling = (
propagatorTypes: PropagatorType[]
): DdRumResourceTracingAttributes => {
const traceId = TracingIdentifier.createTraceId();
const hash = Number((traceId.id * knuthFactor) % twoPow64);
const hash = Number(traceId.id.multiply(knuthFactor).remainder(twoPow64));
const threshold = (tracingSamplingRate / 100) * Number(twoPow64);
const isSampled = hash <= threshold;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Copyright 2016-Present Datadog, Inc.
*/

import BigInt from 'big-integer';
import { Platform, NativeModules } from 'react-native';

import { InternalLog } from '../../../../../../InternalLog';
Expand Down Expand Up @@ -58,7 +59,7 @@ const flushPromises = () =>
let xhrProxy;

const hexToDecimal = (hex: string): string => {
return BigInt(`0x${hex}`).toString(10);
return BigInt(hex, 16).toString(10);
};

beforeEach(() => {
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2653,6 +2653,7 @@ __metadata:
resolution: "@datadog/mobile-react-native@workspace:packages/core"
dependencies:
"@testing-library/react-native": 7.0.2
big-integer: ^1.6.52
react-native-builder-bob: 0.26.0
peerDependencies:
react: ">=16.13.1"
Expand Down Expand Up @@ -7405,7 +7406,7 @@ __metadata:
languageName: node
linkType: hard

"big-integer@npm:1.6.x":
"big-integer@npm:1.6.x, big-integer@npm:^1.6.52":
version: 1.6.52
resolution: "big-integer@npm:1.6.52"
checksum: 6e86885787a20fed96521958ae9086960e4e4b5e74d04f3ef7513d4d0ad631a9f3bde2730fc8aaa4b00419fc865f6ec573e5320234531ef37505da7da192c40b
Expand Down

0 comments on commit 97f26f3

Please sign in to comment.