rollbar
Version:
Effortlessly track and debug errors in your JavaScript applications with Rollbar. This package includes advanced error tracking features and an intuitive interface to help you identify and fix issues more quickly.
105 lines (97 loc) • 2.9 kB
JavaScript
/**
* @module hrtime
*
* @description Methods for handling OpenTelemetry hrtime.
*/
/**
* Convert a duration in milliseconds to an OpenTelemetry hrtime tuple.
*
* @param {number} millis - The duration in milliseconds.
* @returns {[number, number]} An array where the first element is seconds
* and the second is nanoseconds.
*/
function fromMillis(millis) {
return [Math.trunc(millis / 1000), Math.round((millis % 1000) * 1e6)];
}
/**
* Convert an OpenTelemetry hrtime tuple back to a duration in milliseconds.
*
* @param {[number, number]} hrtime - The hrtime tuple [seconds, nanoseconds].
* @returns {number} The total duration in milliseconds.
*/
function toMillis(hrtime) {
return hrtime[0] * 1e3 + Math.round(hrtime[1] / 1e6);
}
/**
* Convert an OpenTelemetry hrtime tuple back to a duration in nanoseconds.
*
* @param {[number, number]} hrtime - The hrtime tuple [seconds, nanoseconds].
* @returns {number} The total duration in nanoseconds.
*/
function toNanos(hrtime) {
return hrtime[0] * 1e9 + hrtime[1];
}
/**
* Adds two OpenTelemetry hrtime tuples.
*
* @param {[number, number]} a - The first hrtime tuple [s, ns].
* @param {[number, number]} b - The second hrtime tuple [s, ns].
* @returns {[number, number]} Summed hrtime tuple, normalized.
*
*/
function add(a, b) {
return [a[0] + b[0] + Math.trunc((a[1] + b[1]) / 1e9), (a[1] + b[1]) % 1e9];
}
/**
* Get the current high-resolution time as an OpenTelemetry hrtime tuple.
*
* @param {boolean} usePerformance=false - If true, uses the Performance API (timeOrigin + now()).
*
* @returns {[number, number]} The current hrtime tuple [s, ns].
*/
function now(usePerformance = false) {
if (usePerformance) {
return add(
fromMillis(performance.timeOrigin),
fromMillis(performance.now()),
);
}
return fromMillis(Date.now());
}
/**
* Check if a value is a valid OpenTelemetry hrtime tuple.
*
* An hrtime tuple is an Array of exactly two numbers:
* [seconds, nanoseconds]
*
* @param {*} value – anything to test
* @returns {boolean} true if `value` is a [number, number] array of length 2
*
* @example
* isHrTime([ 1, 500 ]); // true
* isHrTime([ 0, 1e9 ]); // true
* isHrTime([ '1', 500 ]); // false
* isHrTime({ 0: 1, 1: 500 }); // false
*/
function isHrTime(value) {
return (
Array.isArray(value) &&
value.length === 2 &&
typeof value[0] === 'number' &&
typeof value[1] === 'number'
);
}
/**
* Methods for handling hrtime. OpenTelemetry uses the [seconds, nanoseconds]
* format for hrtime in the `ReadableSpan` interface.
*
* @example
* import hrtime from '@tracing/hrtime.js';
*
* hrtime.fromMillis(1000);
* hrtime.toMillis([0, 1000]);
* hrtime.add([0, 0], [0, 1000]);
* hrtime.now();
* hrtime.isHrTime([0, 1000]);
*/
export default { fromMillis, toMillis, toNanos, add, now, isHrTime };