Skip to content

Commit

Permalink
ref: Create crossPlatformPerformance layer with better typings and fa…
Browse files Browse the repository at this point in the history
…llback for edge-cases
  • Loading branch information
kamilogorek committed Feb 27, 2020
1 parent 13d8398 commit 6e1f442
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

## Unreleased

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

## 5.12.5

Expand Down
20 changes: 12 additions & 8 deletions packages/apm/src/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ import {
uuid4,
} from '@sentry/utils';

const global = getGlobalObject<Window>();

const performanceNow = (() => {
const crossPlatformPerformance: Pick<Performance, 'now'> = (() => {
if (isNodeEnv()) {
const { performance } = dynamicRequire(module, 'perf_hooks');
return performance.now;
const { performance } = dynamicRequire(module, 'perf_hooks') as { performance: Performance };
return performance;
}
return global.performance.now.bind(global.performance);
return (
getGlobalObject<Window>().performance || {
now(): number {
return Date.now();
},
}
);
})();

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

/**
* Finish timestamp of the span.
Expand Down Expand Up @@ -291,7 +295,7 @@ export class Span implements SpanInterface, SpanContext {
return undefined;
}

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

if (this.spanRecorder === undefined) {
Expand Down
2 changes: 0 additions & 2 deletions packages/node/test/transports/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ describe('HTTPTransport', () => {
const now = Date.now();
const mock = jest
.spyOn(Date, 'now')
// Initialize _disabledUntil attribute
.mockReturnValueOnce(now)
// Check for first event
.mockReturnValueOnce(now)
// Setting disabledUntil
Expand Down
2 changes: 0 additions & 2 deletions packages/node/test/transports/https.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ describe('HTTPSTransport', () => {
const now = Date.now();
const mock = jest
.spyOn(Date, 'now')
// Initialize _disabledUntil attribute
.mockReturnValueOnce(now)
// Check for first event
.mockReturnValueOnce(now)
// Setting disabledUntil
Expand Down

0 comments on commit 6e1f442

Please sign in to comment.