Skip to content

Commit 042e20e

Browse files
committed
ref: Differentiate between browser/other sessions
Previously in c9b3d10, the duration was updated to be undefined when the browser SDK sent sessions to Sentry. This patch removes that logic and instead adds a browser flag to the Session class, which indicates if a duration should be included or not.
1 parent 4878707 commit 042e20e

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

packages/browser/src/sdk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ function startSessionTracking(): void {
199199
return;
200200
}
201201

202-
hub.startSession();
202+
hub.startSession({ browser: true });
203203
hub.captureSession();
204204

205205
// We want to create a session for every navigation as well
@@ -209,7 +209,7 @@ function startSessionTracking(): void {
209209
if (from === undefined || from === to) {
210210
return;
211211
}
212-
hub.startSession();
212+
hub.startSession({ browser: true });
213213
hub.captureSession();
214214
},
215215
type: 'history',

packages/hub/src/session.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ export class Session implements SessionInterface {
2121
public release?: string;
2222
public sid: string = uuid4();
2323
public did?: string;
24-
public timestamp: number = timestampInSeconds;
25-
public started: number = timestampInSeconds;
24+
public timestamp: number = timestampInSeconds();
25+
public started: number = timestampInSeconds();
2626
public duration?: number = 0;
2727
public status: SessionStatus = SessionStatus.Ok;
2828
public environment?: string;
2929
public ipAddress?: string;
3030
public init: boolean = true;
31+
public browser: boolean = false;
3132

3233
public constructor(context?: Omit<SessionContext, 'started' | 'status'>) {
3334
if (context) {
@@ -48,8 +49,11 @@ export class Session implements SessionInterface {
4849
}
4950
}
5051

51-
this.timestamp = context.timestamp || timestampInSeconds;
52+
this.timestamp = context.timestamp || timestampInSeconds();
5253

54+
if (context.browser) {
55+
this.browser = context.browser;
56+
}
5357
if (context.sid) {
5458
// Good enough uuid validation. — Kamil
5559
this.sid = context.sid.length === 32 ? context.sid : uuid4();
@@ -63,10 +67,18 @@ export class Session implements SessionInterface {
6367
if (typeof context.started === 'number') {
6468
this.started = context.started;
6569
}
66-
if (typeof context.duration === 'number' || typeof context.duration === undefined) {
67-
this.duration = context.duration;
70+
// The session duration for browser sessions does not track a meaningful
71+
// concept that can be used as a metric.
72+
// Automatically captured sessions are akin to page views, and thus we
73+
// discard their duration.
74+
if (this.browser) {
75+
this.duration = undefined;
6876
} else {
69-
this.duration = this.timestamp - this.started;
77+
if (typeof context.duration === 'number') {
78+
this.duration = context.duration;
79+
} else {
80+
this.duration = this.timestamp - this.started;
81+
}
7082
}
7183
if (context.release) {
7284
this.release = context.release;

packages/types/src/session.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export interface SessionContext {
5050
ipAddress?: string;
5151
errors?: number;
5252
user?: User | null;
53+
// Sessions are handled differently in browser environments
54+
browser?: boolean;
5355
}
5456

5557
/**

packages/utils/src/time.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const timestampSource: TimestampSource =
103103
/**
104104
* Returns a timestamp in seconds since the UNIX epoch using the Date API.
105105
*/
106-
export const dateTimestampInSeconds = dateTimestampSource.nowSeconds.bind(dateTimestampSource);
106+
export const dateTimestampInSeconds: () => number = dateTimestampSource.nowSeconds.bind(dateTimestampSource);
107107

108108
/**
109109
* Returns a timestamp in seconds since the UNIX epoch using either the Performance or Date APIs, depending on the
@@ -116,7 +116,7 @@ export const dateTimestampInSeconds = dateTimestampSource.nowSeconds.bind(dateTi
116116
* skew can grow to arbitrary amounts like days, weeks or months.
117117
* See https://github.com/getsentry/sentry-javascript/issues/2590.
118118
*/
119-
export const timestampInSeconds = timestampSource.nowSeconds.bind(timestampSource);
119+
export const timestampInSeconds: () => number = timestampSource.nowSeconds.bind(timestampSource);
120120

121121
// Re-exported with an old name for backwards-compatibility.
122122
export const timestampWithMs = timestampInSeconds;

0 commit comments

Comments
 (0)