OpenTelemetry Core provides constants and utilities shared by all OpenTelemetry SDK packages.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
High-resolution timing utilities with nanosecond precision and monotonic clock support for accurate span timing and performance measurement.
A utility for returning wall times anchored to a given point in time, ensuring accurate span timings that don't drift from system clock corrections.
/**
* Clock interface for time sources
*/
interface Clock {
/**
* Return the current time in milliseconds from some epoch such as Unix epoch or process start
*/
now(): number;
}
/**
* A utility for returning wall times anchored to a given point in time.
* Wall time measurements are computed by adding a monotonic clock time
* to the anchor point, preventing span timing issues from system clock changes.
*/
class AnchoredClock implements Clock {
/**
* Create a new AnchoredClock anchored to the current time returned by systemClock
* @param systemClock - Clock that returns milliseconds since January 1 1970 (like Date)
* @param monotonicClock - Clock that counts milliseconds monotonically (like performance)
*/
constructor(systemClock: Clock, monotonicClock: Clock);
/**
* Returns the current time by adding the number of milliseconds since the
* AnchoredClock was created to the creation epoch time
* @returns Current anchored time in milliseconds
*/
now(): number;
}Usage Examples:
import { AnchoredClock } from "@opentelemetry/core";
// Create clocks for system time and performance measurement
const systemClock = { now: () => Date.now() };
const performanceClock = { now: () => performance.now() };
// Create anchored clock for consistent timing
const anchoredClock = new AnchoredClock(systemClock, performanceClock);
// Use for span timing
const startTime = anchoredClock.now();
// ... some operation ...
const endTime = anchoredClock.now();
const duration = endTime - startTime;
// Times remain consistent even if system clock is adjusted
console.log(`Operation took ${duration}ms`);Functions for working with high-resolution time ([seconds, nanoseconds] tuples) with nanosecond precision.
/**
* Get current HrTime based on performance timing
* @param performanceNow - Optional performance.now() value to use instead of current time
* @returns HrTime array [seconds, nanoseconds]
*/
function hrTime(performanceNow?: number): HrTime;
/**
* Convert milliseconds from epoch to HrTime
* @param epochMillis - Milliseconds since Unix epoch
* @returns HrTime array [seconds, nanoseconds]
*/
function millisToHrTime(epochMillis: number): HrTime;
/**
* Convert various time inputs to HrTime format
* @param time - Time input (HrTime, number, or Date)
* @returns HrTime array [seconds, nanoseconds]
*/
function timeInputToHrTime(time: TimeInput): HrTime;
/**
* Calculate duration between two HrTime values
* @param startTime - Start time as HrTime
* @param endTime - End time as HrTime
* @returns Duration as HrTime [seconds, nanoseconds]
*/
function hrTimeDuration(startTime: HrTime, endTime: HrTime): HrTime;
/**
* Add two HrTime values together
* @param time1 - First HrTime value
* @param time2 - Second HrTime value
* @returns Sum as HrTime [seconds, nanoseconds]
*/
function addHrTimes(time1: HrTime, time2: HrTime): HrTime;Usage Examples:
import {
hrTime,
hrTimeDuration,
millisToHrTime,
timeInputToHrTime,
addHrTimes
} from "@opentelemetry/core";
// Get current high-resolution time
const now = hrTime();
console.log(now); // [1609459200, 123456789] (seconds, nanoseconds)
// Convert from milliseconds
const epochTime = millisToHrTime(Date.now());
// Convert various time formats
const hrFromDate = timeInputToHrTime(new Date());
const hrFromNumber = timeInputToHrTime(performance.now());
const hrFromHrTime = timeInputToHrTime([1609459200, 123456789]);
// Calculate duration between operations
const startTime = hrTime();
// ... some async operation ...
const endTime = hrTime();
const duration = hrTimeDuration(startTime, endTime);
console.log(`Operation took ${duration[0]} seconds and ${duration[1]} nanoseconds`);
// Add times together
const totalTime = addHrTimes(duration, [0, 500000000]); // Add 500msFunctions to convert HrTime to various other time formats.
/**
* Convert HrTime to nanoseconds
* @param time - HrTime array [seconds, nanoseconds]
* @returns Total nanoseconds as number
*/
function hrTimeToNanoseconds(time: HrTime): number;
/**
* Convert HrTime to milliseconds
* @param time - HrTime array [seconds, nanoseconds]
* @returns Total milliseconds as number
*/
function hrTimeToMilliseconds(time: HrTime): number;
/**
* Convert HrTime to microseconds
* @param time - HrTime array [seconds, nanoseconds]
* @returns Total microseconds as number
*/
function hrTimeToMicroseconds(time: HrTime): number;
/**
* Convert HrTime to ISO timestamp string
* @param time - HrTime array [seconds, nanoseconds]
* @returns ISO timestamp string with nanosecond precision
*/
function hrTimeToTimeStamp(time: HrTime): string;
/**
* Get performance timeOrigin value (with fallback for older browsers)
* @returns Time origin in milliseconds
*/
function getTimeOrigin(): number;Usage Examples:
import {
hrTimeToNanoseconds,
hrTimeToMilliseconds,
hrTimeToMicroseconds,
hrTimeToTimeStamp,
hrTime
} from "@opentelemetry/core";
const time = hrTime();
// Convert to different units
const nanos = hrTimeToNanoseconds(time);
const millis = hrTimeToMilliseconds(time);
const micros = hrTimeToMicroseconds(time);
console.log(`${nanos} nanoseconds`);
console.log(`${millis} milliseconds`);
console.log(`${micros} microseconds`);
// Convert to ISO timestamp with nanosecond precision
const timestamp = hrTimeToTimeStamp(time);
console.log(timestamp); // "2021-01-01T00:00:00.123456789Z"
// Use for span attributes
const spanAttributes = {
'timing.start': hrTimeToNanoseconds(startTime),
'timing.end': hrTimeToNanoseconds(endTime),
'timing.timestamp': hrTimeToTimeStamp(time)
};Type guard functions to validate time input values.
/**
* Check if value is HrTime format
* @param value - Value to check
* @returns True if value is HrTime [seconds, nanoseconds]
*/
function isTimeInputHrTime(value: unknown): value is HrTime;
/**
* Check if value is a valid TimeInput type
* @param value - Value to check
* @returns True if value is HrTime, number, or Date
*/
function isTimeInput(value: unknown): value is HrTime | number | Date;Usage Examples:
import { isTimeInputHrTime, isTimeInput, timeInputToHrTime } from "@opentelemetry/core";
function processTimeValue(value: unknown) {
if (isTimeInput(value)) {
const hrTime = timeInputToHrTime(value);
console.log("Valid time input converted to HrTime:", hrTime);
if (isTimeInputHrTime(value)) {
console.log("Input was already in HrTime format");
}
} else {
console.error("Invalid time input");
}
}
// Test with different input types
processTimeValue([1609459200, 123456789]); // HrTime
processTimeValue(Date.now()); // number
processTimeValue(new Date()); // Date
processTimeValue("invalid"); // Will log errorInstall with Tessl CLI
npx tessl i tessl/npm-opentelemetry--core