Skip to content

Commit c9b3d10

Browse files
committed
fix(sessions): Correctly compute session duration
Closes #3615 * Update the @sentry/browser client to not send durations when capturing sessions. * Leverage timestampInSeconds from @sentry/utils instead of Date.now() to make sure that durations are correctly reported to Sentry.
1 parent 60f2ce9 commit c9b3d10

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

packages/browser/src/client.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BaseClient, Scope, SDK_VERSION } from '@sentry/core';
1+
import { BaseClient, Scope, SDK_VERSION, Session } from '@sentry/core';
22
import { Event, EventHint } from '@sentry/types';
33
import { getGlobalObject, logger } from '@sentry/utils';
44

@@ -57,6 +57,15 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
5757
});
5858
}
5959

60+
/**
61+
* @inheritDoc
62+
*/
63+
public captureSession(session: Session): void {
64+
// Make sure session duration is not sent
65+
session.duration = undefined;
66+
super.captureSession(session);
67+
}
68+
6069
/**
6170
* @inheritDoc
6271
*/

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export {
1313
setUser,
1414
withScope,
1515
} from '@sentry/minimal';
16-
export { addGlobalEventProcessor, getCurrentHub, getHubFromCarrier, Hub, makeMain, Scope } from '@sentry/hub';
16+
export { addGlobalEventProcessor, getCurrentHub, getHubFromCarrier, Hub, makeMain, Scope, Session } from '@sentry/hub';
1717
export { API } from './api';
1818
export { BaseClient } from './baseclient';
1919
export { BackendClass, BaseBackend } from './basebackend';

packages/hub/src/session.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
SessionStatus,
99
Transport,
1010
} from '@sentry/types';
11-
import { dropUndefinedKeys, logger, uuid4 } from '@sentry/utils';
11+
import { dropUndefinedKeys, logger, timestampInSeconds, uuid4 } from '@sentry/utils';
1212

1313
import { getCurrentHub } from './hub';
1414

@@ -21,9 +21,9 @@ export class Session implements SessionInterface {
2121
public release?: string;
2222
public sid: string = uuid4();
2323
public did?: string;
24-
public timestamp: number = Date.now();
25-
public started: number = Date.now();
26-
public duration: number = 0;
24+
public timestamp: number = timestampInSeconds;
25+
public started: number = timestampInSeconds;
26+
public duration?: number = 0;
2727
public status: SessionStatus = SessionStatus.Ok;
2828
public environment?: string;
2929
public ipAddress?: string;
@@ -48,7 +48,7 @@ export class Session implements SessionInterface {
4848
}
4949
}
5050

51-
this.timestamp = context.timestamp || Date.now();
51+
this.timestamp = context.timestamp || timestampInSeconds;
5252

5353
if (context.sid) {
5454
// Good enough uuid validation. — Kamil
@@ -63,7 +63,7 @@ export class Session implements SessionInterface {
6363
if (typeof context.started === 'number') {
6464
this.started = context.started;
6565
}
66-
if (typeof context.duration === 'number') {
66+
if (typeof context.duration === 'number' || typeof context.duration === undefined) {
6767
this.duration = context.duration;
6868
} else {
6969
this.duration = this.timestamp - this.started;
@@ -104,9 +104,9 @@ export class Session implements SessionInterface {
104104
init: boolean;
105105
sid: string;
106106
did?: string;
107-
timestamp: string;
108-
started: string;
109-
duration: number;
107+
timestamp: number;
108+
started: number;
109+
duration?: number;
110110
status: SessionStatus;
111111
errors: number;
112112
attrs?: {
@@ -119,8 +119,8 @@ export class Session implements SessionInterface {
119119
return dropUndefinedKeys({
120120
sid: `${this.sid}`,
121121
init: this.init,
122-
started: new Date(this.started).toISOString(),
123-
timestamp: new Date(this.timestamp).toISOString(),
122+
started: this.started,
123+
timestamp: this.timestamp,
124124
status: this.status,
125125
errors: this.errors,
126126
did: typeof this.did === 'number' || typeof this.did === 'string' ? `${this.did}` : undefined,

packages/types/src/session.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export interface Session extends SessionContext {
1515
init: boolean;
1616
sid: string;
1717
did?: string;
18-
timestamp: string;
19-
started: string;
20-
duration: number;
18+
timestamp: number;
19+
started: number;
20+
duration?: number;
2121
status: SessionStatus;
2222
errors: number;
2323
attrs?: {

0 commit comments

Comments
 (0)