1 | ;
|
2 | /*
|
3 | * Copyright The OpenTelemetry Authors
|
4 | *
|
5 | * Licensed under the Apache License, Version 2.0 (the "License");
|
6 | * you may not use this file except in compliance with the License.
|
7 | * You may obtain a copy of the License at
|
8 | *
|
9 | * https://www.apache.org/licenses/LICENSE-2.0
|
10 | *
|
11 | * Unless required by applicable law or agreed to in writing, software
|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 | * See the License for the specific language governing permissions and
|
15 | * limitations under the License.
|
16 | */
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.AnchoredClock = void 0;
|
19 | /**
|
20 | * A utility for returning wall times anchored to a given point in time. Wall time measurements will
|
21 | * not be taken from the system, but instead are computed by adding a monotonic clock time
|
22 | * to the anchor point.
|
23 | *
|
24 | * This is needed because the system time can change and result in unexpected situations like
|
25 | * spans ending before they are started. Creating an anchored clock for each local root span
|
26 | * ensures that span timings and durations are accurate while preventing span times from drifting
|
27 | * too far from the system clock.
|
28 | *
|
29 | * Only creating an anchored clock once per local trace ensures span times are correct relative
|
30 | * to each other. For example, a child span will never have a start time before its parent even
|
31 | * if the system clock is corrected during the local trace.
|
32 | *
|
33 | * Heavily inspired by the OTel Java anchored clock
|
34 | * https://github.com/open-telemetry/opentelemetry-java/blob/main/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/AnchoredClock.java
|
35 | */
|
36 | class AnchoredClock {
|
37 | /**
|
38 | * Create a new AnchoredClock anchored to the current time returned by systemClock.
|
39 | *
|
40 | * @param systemClock should be a clock that returns the number of milliseconds since January 1 1970 such as Date
|
41 | * @param monotonicClock should be a clock that counts milliseconds monotonically such as window.performance or perf_hooks.performance
|
42 | */
|
43 | constructor(systemClock, monotonicClock) {
|
44 | this._monotonicClock = monotonicClock;
|
45 | this._epochMillis = systemClock.now();
|
46 | this._performanceMillis = monotonicClock.now();
|
47 | }
|
48 | /**
|
49 | * Returns the current time by adding the number of milliseconds since the
|
50 | * AnchoredClock was created to the creation epoch time
|
51 | */
|
52 | now() {
|
53 | const delta = this._monotonicClock.now() - this._performanceMillis;
|
54 | return this._epochMillis + delta;
|
55 | }
|
56 | }
|
57 | exports.AnchoredClock = AnchoredClock;
|
58 | //# sourceMappingURL=anchored-clock.js.map |
\ | No newline at end of file |