Skip to content

Commit 6e1f442

Browse files
committed
ref: Create crossPlatformPerformance layer with better typings and fallback for edge-cases
1 parent 13d8398 commit 6e1f442

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
## Unreleased
44

5-
- none
5+
- [core] ref: Remove unused `sentry_timestamp` header (#2458)
6+
- [node] ref: Drop Node v6, add Node v12 to test matrix, move all scripts to Node v12 (#2455)
7+
- [apm] fix: Use monotonic clock to compute durations (#2441)
8+
- [utils] ref: Prevent instantiating unnecessary Date objects in `timestampWithMs` (#2442)
69

710
## 5.12.5
811

packages/apm/src/span.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ import {
1313
uuid4,
1414
} from '@sentry/utils';
1515

16-
const global = getGlobalObject<Window>();
17-
18-
const performanceNow = (() => {
16+
const crossPlatformPerformance: Pick<Performance, 'now'> = (() => {
1917
if (isNodeEnv()) {
20-
const { performance } = dynamicRequire(module, 'perf_hooks');
21-
return performance.now;
18+
const { performance } = dynamicRequire(module, 'perf_hooks') as { performance: Performance };
19+
return performance;
2220
}
23-
return global.performance.now.bind(global.performance);
21+
return (
22+
getGlobalObject<Window>().performance || {
23+
now(): number {
24+
return Date.now();
25+
},
26+
}
27+
);
2428
})();
2529

2630
// TODO: Should this be exported?
@@ -109,7 +113,7 @@ export class Span implements SpanInterface, SpanContext {
109113
* Works with Node.js v8.5.0 or higher.
110114
* https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_now
111115
*/
112-
private readonly _startTimestampMonotonic: number = performanceNow();
116+
private readonly _startTimestampMonotonic: number = crossPlatformPerformance.now();
113117

114118
/**
115119
* Finish timestamp of the span.
@@ -291,7 +295,7 @@ export class Span implements SpanInterface, SpanContext {
291295
return undefined;
292296
}
293297

294-
const durationSeconds = (performanceNow() - this._startTimestampMonotonic) / 1000;
298+
const durationSeconds = (crossPlatformPerformance.now() - this._startTimestampMonotonic) / 1000;
295299
this.timestamp = this.startTimestamp + durationSeconds;
296300

297301
if (this.spanRecorder === undefined) {

packages/node/test/transports/http.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ describe('HTTPTransport', () => {
9797
const now = Date.now();
9898
const mock = jest
9999
.spyOn(Date, 'now')
100-
// Initialize _disabledUntil attribute
101-
.mockReturnValueOnce(now)
102100
// Check for first event
103101
.mockReturnValueOnce(now)
104102
// Setting disabledUntil

packages/node/test/transports/https.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ describe('HTTPSTransport', () => {
103103
const now = Date.now();
104104
const mock = jest
105105
.spyOn(Date, 'now')
106-
// Initialize _disabledUntil attribute
107-
.mockReturnValueOnce(now)
108106
// Check for first event
109107
.mockReturnValueOnce(now)
110108
// Setting disabledUntil

0 commit comments

Comments
 (0)