Skip to content

Commit 5d0e024

Browse files
committed
ref: change getSession param errorSampleRate to allowBuffering
1 parent 8446202 commit 5d0e024

File tree

8 files changed

+50
-43
lines changed

8 files changed

+50
-43
lines changed

packages/replay/src/replay.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export class ReplayContainer implements ReplayContainerInterface {
230230
currentSession: this.session,
231231
// This is intentional: create a new session-based replay when calling `start()`
232232
sessionSampleRate: 1,
233-
errorSampleRate: 0,
233+
allowBuffering: false,
234234
});
235235

236236
session.previousSessionId = previousSessionId;
@@ -255,8 +255,7 @@ export class ReplayContainer implements ReplayContainerInterface {
255255
stickySession: Boolean(this._options.stickySession),
256256
currentSession: this.session,
257257
sessionSampleRate: 0,
258-
// This is intentional: create a new buffer-based replay when calling `startBuffering()`
259-
errorSampleRate: 1,
258+
allowBuffering: true,
260259
});
261260

262261
session.previousSessionId = previousSessionId;
@@ -596,7 +595,7 @@ export class ReplayContainer implements ReplayContainerInterface {
596595
stickySession: Boolean(this._options.stickySession),
597596
currentSession: this.session,
598597
sessionSampleRate: this._options.sessionSampleRate,
599-
errorSampleRate: this._options.errorSampleRate,
598+
allowBuffering: this._options.errorSampleRate > 0,
600599
});
601600

602601
// If session was newly created (i.e. was not loaded from storage), then

packages/replay/src/session/Session.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { uuid4 } from '@sentry/utils';
22

33
import type { Sampled, Session } from '../types';
4-
import { isSampled } from '../util/isSampled';
54

65
/**
76
* Get a session with defaults & applied sampling.
@@ -24,10 +23,3 @@ export function makeSession(session: Partial<Session> & { sampled: Sampled }): S
2423
shouldRefresh: true,
2524
};
2625
}
27-
28-
/**
29-
* Get the sampled status for a session based on sample rates & current sampled status.
30-
*/
31-
export function getSessionSampleType(sessionSampleRate: number, errorSampleRate: number): Sampled {
32-
return isSampled(sessionSampleRate) ? 'session' : errorSampleRate > 0 ? 'buffer' : false;
33-
}

packages/replay/src/session/createSession.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import { logger } from '@sentry/utils';
22

3-
import type { Session, SessionOptions } from '../types';
3+
import type { Sampled,Session, SessionOptions } from '../types';
4+
import { isSampled } from '../util/isSampled';
45
import { saveSession } from './saveSession';
5-
import { getSessionSampleType, makeSession } from './Session';
6+
import { makeSession } from './Session';
7+
8+
/**
9+
* Get the sampled status for a session based on sample rates & current sampled status.
10+
*/
11+
export function getSessionSampleType(sessionSampleRate: number, allowBuffering: boolean): Sampled {
12+
return isSampled(sessionSampleRate) ? 'session' : allowBuffering ? 'buffer' : false;
13+
}
614

715
/**
816
* Create a new session, which in its current implementation is a Sentry event
917
* that all replays will be saved to as attachments. Currently, we only expect
1018
* one of these Sentry events per "replay session".
1119
*/
12-
export function createSession({ sessionSampleRate, errorSampleRate, stickySession = false }: SessionOptions): Session {
13-
const sampled = getSessionSampleType(sessionSampleRate, errorSampleRate);
20+
export function createSession({ sessionSampleRate, allowBuffering, stickySession = false }: SessionOptions): Session {
21+
const sampled = getSessionSampleType(sessionSampleRate, allowBuffering);
1422
const session = makeSession({
1523
sampled,
1624
});

packages/replay/src/session/getSession.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function getSession({
2323
currentSession,
2424
stickySession,
2525
sessionSampleRate,
26-
errorSampleRate,
26+
allowBuffering,
2727
}: GetSessionParams): { type: 'new' | 'saved'; session: Session } {
2828
// If session exists and is passed, use it instead of always hitting session storage
2929
const session = currentSession || (stickySession && fetchSession());
@@ -50,7 +50,7 @@ export function getSession({
5050
const newSession = createSession({
5151
stickySession,
5252
sessionSampleRate,
53-
errorSampleRate,
53+
allowBuffering,
5454
});
5555

5656
return { type: 'new', session: newSession };

packages/replay/src/types.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,32 +182,39 @@ export interface WorkerResponse {
182182

183183
export type AddEventResult = void;
184184

185-
export interface SampleRates {
185+
interface SessionAndPluginOptions {
186186
/**
187187
* The sample rate for session-long replays. 1.0 will record all sessions and
188188
* 0 will record none.
189189
*/
190190
sessionSampleRate: number;
191191

192192
/**
193-
* The sample rate for sessions that has had an error occur. This is
194-
* independent of `sessionSampleRate`.
193+
* If false, will create a new session per pageload. Otherwise, saves session
194+
* to Session Storage.
195195
*/
196-
errorSampleRate: number;
196+
stickySession: boolean;
197197
}
198198

199199
/**
200200
* Session options that are configurable by the integration configuration
201201
*/
202-
export interface SessionOptions extends SampleRates {
202+
export interface SessionOptions extends SessionAndPluginOptions {
203203
/**
204-
* If false, will create a new session per pageload. Otherwise, saves session
205-
* to Session Storage.
204+
* Should buffer recordings to be saved later either by error sampling, or by
205+
* manually calling `flush()`. This is only a factor if not sampled for a
206+
* session-based replay.
206207
*/
207-
stickySession: boolean;
208+
allowBuffering: boolean;
208209
}
209210

210-
export interface ReplayPluginOptions extends SessionOptions {
211+
export interface ReplayPluginOptions extends SessionAndPluginOptions {
212+
/**
213+
* The sample rate for each error event. This is only a factor if not sampled
214+
* for a session-based replay.
215+
*/
216+
errorSampleRate: number;
217+
211218
/**
212219
* The amount of time to wait before sending a replay
213220
*/

packages/replay/test/unit/session/createSession.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('Unit | session | createSession', () => {
3434
const newSession = createSession({
3535
stickySession: false,
3636
sessionSampleRate: 1.0,
37-
errorSampleRate: 0,
37+
allowBuffering: false,
3838
});
3939
expect(captureEventMock).not.toHaveBeenCalled();
4040

@@ -49,7 +49,7 @@ describe('Unit | session | createSession', () => {
4949
const newSession = createSession({
5050
stickySession: true,
5151
sessionSampleRate: 1.0,
52-
errorSampleRate: 0,
52+
allowBuffering: false,
5353
});
5454
expect(captureEventMock).not.toHaveBeenCalled();
5555

packages/replay/test/unit/session/getSession.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ jest.mock('@sentry/utils', () => {
1212
};
1313
});
1414

15-
const SAMPLE_RATES = {
15+
const SAMPLE_OPTIONS = {
1616
sessionSampleRate: 1.0,
17-
errorSampleRate: 0,
17+
allowBuffering: false,
1818
};
1919

2020
function createMockSession(when: number = Date.now()) {
@@ -48,7 +48,7 @@ describe('Unit | session | getSession', () => {
4848
maxSessionLife: MAX_SESSION_LIFE,
4949
},
5050
stickySession: false,
51-
...SAMPLE_RATES,
51+
...SAMPLE_OPTIONS,
5252
});
5353

5454
expect(FetchSession.fetchSession).not.toHaveBeenCalled();
@@ -76,7 +76,7 @@ describe('Unit | session | getSession', () => {
7676
maxSessionLife: MAX_SESSION_LIFE,
7777
},
7878
stickySession: false,
79-
...SAMPLE_RATES,
79+
...SAMPLE_OPTIONS,
8080
});
8181

8282
expect(FetchSession.fetchSession).not.toHaveBeenCalled();
@@ -92,7 +92,7 @@ describe('Unit | session | getSession', () => {
9292
maxSessionLife: MAX_SESSION_LIFE,
9393
},
9494
stickySession: false,
95-
...SAMPLE_RATES,
95+
...SAMPLE_OPTIONS,
9696
currentSession: makeSession({
9797
id: 'old_session_id',
9898
lastActivity: Date.now() - 1001,
@@ -119,7 +119,7 @@ describe('Unit | session | getSession', () => {
119119
},
120120
stickySession: true,
121121
sessionSampleRate: 1.0,
122-
errorSampleRate: 0.0,
122+
allowBuffering: false,
123123
});
124124

125125
expect(FetchSession.fetchSession).toHaveBeenCalled();
@@ -156,7 +156,7 @@ describe('Unit | session | getSession', () => {
156156
},
157157
stickySession: true,
158158
sessionSampleRate: 1.0,
159-
errorSampleRate: 0.0,
159+
allowBuffering: false,
160160
});
161161

162162
expect(FetchSession.fetchSession).toHaveBeenCalled();
@@ -182,7 +182,7 @@ describe('Unit | session | getSession', () => {
182182
maxSessionLife: MAX_SESSION_LIFE,
183183
},
184184
stickySession: true,
185-
...SAMPLE_RATES,
185+
...SAMPLE_OPTIONS,
186186
});
187187

188188
expect(FetchSession.fetchSession).toHaveBeenCalled();
@@ -201,7 +201,7 @@ describe('Unit | session | getSession', () => {
201201
maxSessionLife: MAX_SESSION_LIFE,
202202
},
203203
stickySession: false,
204-
...SAMPLE_RATES,
204+
...SAMPLE_OPTIONS,
205205
currentSession: makeSession({
206206
id: 'test_session_uuid_2',
207207
lastActivity: +new Date() - 500,
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
import { getSessionSampleType, makeSession } from '../../../src/session/Session';
1+
import { getSessionSampleType} from '../../../src/session/createSession';
2+
import { makeSession } from '../../../src/session/Session';
23

34
describe('Unit | session | sessionSampling', () => {
45
it('does not sample', function () {
56
const newSession = makeSession({
6-
sampled: getSessionSampleType(0, 0),
7+
sampled: getSessionSampleType(0, false),
78
});
89

910
expect(newSession.sampled).toBe(false);
1011
});
1112

1213
it('samples using `sessionSampleRate`', function () {
1314
const newSession = makeSession({
14-
sampled: getSessionSampleType(1.0, 0),
15+
sampled: getSessionSampleType(1.0, false),
1516
});
1617

1718
expect(newSession.sampled).toBe('session');
1819
});
1920

2021
it('samples using `errorSampleRate`', function () {
2122
const newSession = makeSession({
22-
sampled: getSessionSampleType(0, 1),
23+
sampled: getSessionSampleType(0, true),
2324
});
2425

2526
expect(newSession.sampled).toBe('buffer');
@@ -32,4 +33,4 @@ describe('Unit | session | sessionSampling', () => {
3233

3334
expect(newSession.sampled).toBe('session');
3435
});
35-
});
36+
})

0 commit comments

Comments
 (0)