@datadog/mobile-react-native
Version:
A client-side React Native module to interact with Datadog
60 lines (57 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getTracingAttributes = exports.generateTracingAttributesWithSampling = void 0;
var _bigInteger = _interopRequireDefault(require("big-integer"));
var _TracingIdentifier = require("./TracingIdentifier");
var _firstPartyHosts = require("./firstPartyHosts");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
* This product includes software developed at Datadog (https://www.datadoghq.com/).
* Copyright 2016-Present Datadog, Inc.
*/
const knuthFactor = (0, _bigInteger.default)('1111111111111111111');
const twoPow64 = (0, _bigInteger.default)('10000000000000000', 16); // 2n ** 64n
const DISCARDED_TRACE_ATTRIBUTES = {
samplingPriorityHeader: '0',
tracingStrategy: 'DISCARD'
};
const getTracingAttributes = ({
hostname,
firstPartyHostsRegexMap,
tracingSamplingRate,
rumSessionId
}) => {
if (hostname === null) {
return DISCARDED_TRACE_ATTRIBUTES;
}
const propagatorsForHost = (0, _firstPartyHosts.getPropagatorsForHost)(hostname, firstPartyHostsRegexMap);
if (propagatorsForHost) {
return generateTracingAttributesWithSampling(tracingSamplingRate, propagatorsForHost, rumSessionId);
}
return DISCARDED_TRACE_ATTRIBUTES;
};
exports.getTracingAttributes = getTracingAttributes;
const generateTracingAttributesWithSampling = (tracingSamplingRate, propagatorTypes, rumSessionId) => {
if (!propagatorTypes || propagatorTypes.length === 0) {
return DISCARDED_TRACE_ATTRIBUTES;
}
const traceId = _TracingIdentifier.TracingIdentifier.createTraceId();
const hash = Number(traceId.id.multiply(knuthFactor).remainder(twoPow64));
const threshold = tracingSamplingRate / 100 * Number(twoPow64);
const isSampled = hash <= threshold;
const tracingAttributes = {
traceId,
spanId: _TracingIdentifier.TracingIdentifier.createSpanId(),
samplingPriorityHeader: isSampled ? '1' : '0',
tracingStrategy: 'KEEP',
rulePsr: tracingSamplingRate / 100,
propagatorTypes,
rumSessionId
};
return tracingAttributes;
};
exports.generateTracingAttributesWithSampling = generateTracingAttributesWithSampling;
//# sourceMappingURL=distributedTracing.js.map