@opentelemetry/core
Version:
OpenTelemetry Core provides constants and utilities shared by all OpenTelemetry SDK packages.
1 lines • 2.69 kB
Source Map (JSON)
{"version":3,"file":"anchored-clock.cjs","names":[],"sources":["../../src/common/anchored-clock.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nexport interface Clock {\n /**\n * Return the current time in milliseconds from some epoch such as the Unix epoch or process start\n */\n now(): number;\n}\n\n/**\n * A utility for returning wall times anchored to a given point in time. Wall time measurements will\n * not be taken from the system, but instead are computed by adding a monotonic clock time\n * to the anchor point.\n *\n * This is needed because the system time can change and result in unexpected situations like\n * spans ending before they are started. Creating an anchored clock for each local root span\n * ensures that span timings and durations are accurate while preventing span times from drifting\n * too far from the system clock.\n *\n * Only creating an anchored clock once per local trace ensures span times are correct relative\n * to each other. For example, a child span will never have a start time before its parent even\n * if the system clock is corrected during the local trace.\n *\n * Heavily inspired by the OTel Java anchored clock\n * https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/AnchoredClock.java\n */\nexport class AnchoredClock implements Clock {\n private _monotonicClock: Clock;\n private _epochMillis: number;\n private _performanceMillis: number;\n\n /**\n * Create a new AnchoredClock anchored to the current time returned by systemClock.\n *\n * @param systemClock should be a clock that returns the number of milliseconds since January 1 1970 such as Date\n * @param monotonicClock should be a clock that counts milliseconds monotonically such as window.performance or perf_hooks.performance\n */\n public constructor(systemClock: Clock, monotonicClock: Clock) {\n this._monotonicClock = monotonicClock;\n this._epochMillis = systemClock.now();\n this._performanceMillis = monotonicClock.now();\n }\n\n /**\n * Returns the current time by adding the number of milliseconds since the\n * AnchoredClock was created to the creation epoch time\n */\n public now(): number {\n const delta = this._monotonicClock.now() - this._performanceMillis;\n return this._epochMillis + delta;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA6BA,IAAa,gBAAb,MAA4C;CAC1C;CACA;CACA;;;;;;;CAQA,YAAmB,aAAoB,gBAAuB;EAC5D,KAAK,kBAAkB;EACvB,KAAK,eAAe,YAAY,IAAI;EACpC,KAAK,qBAAqB,eAAe,IAAI;CAC/C;;;;;CAMA,MAAqB;EACnB,MAAM,QAAQ,KAAK,gBAAgB,IAAI,IAAI,KAAK;EAChD,OAAO,KAAK,eAAe;CAC7B;AACF"}