|
2 | 2 |
|
3 | 3 | import { getCurrentHub, Hub } from '@sentry/hub';
|
4 | 4 | import { Span as SpanInterface, SpanContext, SpanStatus } from '@sentry/types';
|
5 |
| -import { dropUndefinedKeys, isInstanceOf, logger, timestampWithMs, uuid4 } from '@sentry/utils'; |
| 5 | +import { |
| 6 | + dropUndefinedKeys, |
| 7 | + dynamicRequire, |
| 8 | + getGlobalObject, |
| 9 | + isInstanceOf, |
| 10 | + isNodeEnv, |
| 11 | + logger, |
| 12 | + timestampWithMs, |
| 13 | + uuid4, |
| 14 | +} from '@sentry/utils'; |
| 15 | + |
| 16 | +const global = getGlobalObject<Window>(); |
| 17 | + |
| 18 | +const performanceNow = (() => { |
| 19 | + if (isNodeEnv()) { |
| 20 | + const { performance } = dynamicRequire(module, 'perf_hooks'); |
| 21 | + return performance.now; |
| 22 | + } |
| 23 | + return global.performance.now.bind(global.performance); |
| 24 | +})(); |
6 | 25 |
|
7 | 26 | // TODO: Should this be exported?
|
8 | 27 | export const TRACEPARENT_REGEXP = new RegExp(
|
@@ -81,6 +100,17 @@ export class Span implements SpanInterface, SpanContext {
|
81 | 100 | */
|
82 | 101 | public readonly startTimestamp: number = timestampWithMs();
|
83 | 102 |
|
| 103 | + /** |
| 104 | + * Internal start time tracked with a monotonic clock. |
| 105 | + * |
| 106 | + * Works with mostly any browser version released since 2012. |
| 107 | + * https://caniuse.com/#search=performance.now |
| 108 | + * |
| 109 | + * Works with Node.js v8.5.0 or higher. |
| 110 | + * https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_now |
| 111 | + */ |
| 112 | + private readonly _startTimestampMonotonic: number = performanceNow(); |
| 113 | + |
84 | 114 | /**
|
85 | 115 | * Finish timestamp of the span.
|
86 | 116 | */
|
@@ -261,7 +291,8 @@ export class Span implements SpanInterface, SpanContext {
|
261 | 291 | return undefined;
|
262 | 292 | }
|
263 | 293 |
|
264 |
| - this.timestamp = timestampWithMs(); |
| 294 | + const durationSeconds = (performanceNow() - this._startTimestampMonotonic) / 1000; |
| 295 | + this.timestamp = this.startTimestamp + durationSeconds; |
265 | 296 |
|
266 | 297 | if (this.spanRecorder === undefined) {
|
267 | 298 | return undefined;
|
|
0 commit comments