Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/core/datadog-configuration.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@
"type": "boolean"
},
"sessionSampleRate": {
"description": "Percentage of sampled RUM sessions. Range `0`-`100`.",
"description": "Percentage of sampled RUM sessions. Range `0`-`100`. (default `100`).",
"type": "integer"
},
"trackBackgroundEvents": {
Expand Down Expand Up @@ -253,7 +253,7 @@
"type": "boolean"
},
"telemetrySampleRate": {
"description": "The sampling rate for Internal Telemetry (info related to the work of the SDK internals). Range `0`-`100`.",
"description": "The sampling rate for Internal Telemetry (info related to the work of the SDK internals). Range `0`-`100` (default `20`).",
"type": "integer"
},
"customEndpoint": {
Expand Down Expand Up @@ -288,7 +288,7 @@
"type": "object",
"properties": {
"resourceTraceSampleRate": {
"description": "Percentage of tracing integrations for network calls between your app and your backend. Range `0`-`100`.",
"description": "Percentage of tracing integrations for network calls between your app and your backend. Range `0`-`100` (default `100`).",
"type": "number"
},
"customEndpoint": {
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/DdSdkReactNativeConfiguration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ export class RumConfiguration {

/**
* Percentage of sampled RUM sessions. Range `0`-`100`.
* Default is `100`.
*/
public sessionSampleRate: number = DEFAULTS.sessionSampleRate;

Expand Down Expand Up @@ -419,6 +420,7 @@ export class LogsConfiguration {
export class TraceConfiguration {
/**
* Percentage of tracing integrations for network calls between your app and your backend. Range `0`-`100`.
* Default is `100`.
*/
public resourceTraceSampleRate: number = DEFAULTS.resourceTraceSampleRate;

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/__tests__/DdSdkReactNative.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ describe('DdSdkReactNative', () => {
const fakeAppId = '1';
const fakeClientToken = '2';
const fakeEnvName = 'env';

const configuration = new CoreConfiguration(
fakeClientToken,
fakeEnvName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
* Copyright 2016-Present Datadog, Inc.
*/

import BigInt from 'big-integer';

import { InternalLog } from '../../../InternalLog';
import { SdkVerbosity } from '../../../SdkVerbosity';
import { getGlobalInstance } from '../../../utils/singletonUtils';
import type { FirstPartyHost } from '../../types';

import { firstPartyHostsRegexMapBuilder } from './distributedTracing/firstPartyHosts';
Expand All @@ -14,37 +17,50 @@ import { filterDevResource } from './requestProxy/XHRProxy/DatadogRumResource/in
import { XHRProxy } from './requestProxy/XHRProxy/XHRProxy';
import type { RequestProxy } from './requestProxy/interfaces/RequestProxy';

export const MAX_TRACE_ID = BigInt.one.shiftLeft(64).minus(BigInt.one);
const RUM_RESOURCE_TRACKING_MODULE =
'com.datadog.reactnative.rum.resource_tracking';

/**
* Provides RUM auto-instrumentation feature to track resources (fetch, XHR, axios) as RUM events.
*/
export class DdRumResourceTracking {
private static isTracking = false;
private static requestProxy: RequestProxy | null;
class RumResourceTracking {
private _isTracking = false;
private _requestProxy: RequestProxy | null = null;
private _maxSampledTraceId: BigInt.BigInteger | null = null;

get isTracking(): boolean {
return this._isTracking;
}

get maxSampledTraceId(): BigInt.BigInteger {
return this._maxSampledTraceId ?? BigInt(0);
}

/**
* Starts tracking resources and sends a RUM Resource event every time a network request is detected.
*/
static startTracking({
startTracking({
tracingSamplingRate,
firstPartyHosts
}: {
tracingSamplingRate: number;
firstPartyHosts: FirstPartyHost[];
}): void {
// extra safety to avoid proxying the XHR class twice
if (DdRumResourceTracking.isTracking) {
if (this._isTracking) {
InternalLog.log(
'Datadog SDK is already tracking XHR resources',
SdkVerbosity.WARN
);
return;
}

this.requestProxy = new XHRProxy({
this._requestProxy = new XHRProxy({
xhrType: XMLHttpRequest,
resourceReporter: new ResourceReporter([filterDevResource])
});
this.requestProxy.onTrackingStart({
this._requestProxy.onTrackingStart({
tracingSamplingRate,
firstPartyHostsRegexMap: firstPartyHostsRegexMapBuilder(
firstPartyHosts
Expand All @@ -55,16 +71,30 @@ export class DdRumResourceTracking {
'Datadog SDK is tracking XHR resources',
SdkVerbosity.INFO
);
DdRumResourceTracking.isTracking = true;

this._isTracking = true;
this._maxSampledTraceId = RumResourceTracking.getMaxTraceId(
tracingSamplingRate
);
}

static stopTracking(): void {
if (DdRumResourceTracking.isTracking) {
DdRumResourceTracking.isTracking = false;
if (this.requestProxy) {
this.requestProxy.onTrackingStop();
stopTracking(): void {
if (this._isTracking) {
this._isTracking = false;
if (this._requestProxy) {
this._requestProxy.onTrackingStop();
}
this.requestProxy = null;
this._requestProxy = null;
this._maxSampledTraceId = null;
}
}

private static getMaxTraceId(sampleRate: number): BigInt.BigInteger {
return BigInt(MAX_TRACE_ID.toJSNumber() * (sampleRate / 100.0));
}
}

export const DdRumResourceTracking = getGlobalInstance(
RUM_RESOURCE_TRACKING_MODULE,
() => new RumResourceTracking()
);
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,25 @@ export class TracingIdentifier {
return new TracingIdentifier(TracingIdType.span) as SpanId;
}

/**
* Crate a Tracing Identifier from a given BigInt ID and type.
* @param id the BigInt ID.
* @param type the type of tracing identifier.
* @returns the generated {@link TraceId} or {@link SpanId}.
*/
public static fromBigInt(
id: BigInteger,
type: TracingIdType
): TraceId | SpanId {
return new TracingIdentifier(type, id) as TraceId | SpanId;
}

/**
* Private constructor to initialize the {@link TracingIdentifier} based on the given
* {@link TracingIdType}.
*/
private constructor(type: TracingIdType) {
this.id = this.generateUUID(type);
private constructor(type: TracingIdType, id?: BigInt.BigInteger) {
this.id = id ?? this.generateUUID(type);
this.type = type;
}

Expand Down
Loading