Skip to content

Commit a0c0afd

Browse files
committed
ref(replay): Sessions don't need to be sampled when fetched
1 parent 9b19dd4 commit a0c0afd

File tree

4 files changed

+13
-35
lines changed

4 files changed

+13
-35
lines changed

Diff for: packages/replay/src/session/fetchSession.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { REPLAY_SESSION_KEY, WINDOW } from '../constants';
2-
import { SampleRates } from '../types';
3-
import { makeSession, sampleSession, Session } from './Session';
2+
import { makeSession, Session } from './Session';
43

54
/**
65
* Fetches a session from storage
76
*/
8-
export function fetchSession({ sessionSampleRate, errorSampleRate }: SampleRates): Session | null {
7+
export function fetchSession(): Session | null {
98
const hasSessionStorage = 'sessionStorage' in WINDOW;
109

1110
if (!hasSessionStorage) {
@@ -20,10 +19,9 @@ export function fetchSession({ sessionSampleRate, errorSampleRate }: SampleRates
2019
return null;
2120
}
2221

23-
const sessionObj = JSON.parse(sessionStringFromStorage) as Partial<Session>;
24-
const sampled = sampleSession(sessionObj.sampled, sessionSampleRate, errorSampleRate);
22+
const sessionObj = JSON.parse(sessionStringFromStorage) as Session;
2523

26-
return makeSession({ ...sessionObj, sampled });
24+
return makeSession(sessionObj);
2725
} catch {
2826
return null;
2927
}

Diff for: packages/replay/src/session/getSession.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function getSession({
2929
errorSampleRate,
3030
}: GetSessionParams): { type: 'new' | 'saved'; session: Session } {
3131
// If session exists and is passed, use it instead of always hitting session storage
32-
const session = currentSession || (stickySession && fetchSession({ sessionSampleRate, errorSampleRate }));
32+
const session = currentSession || (stickySession && fetchSession());
3333

3434
if (session) {
3535
// If there is a session, check if it is valid (e.g. "last activity" time

Diff for: packages/replay/test/unit/session/fetchSession.test.ts

+5-25
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,13 @@ afterEach(() => {
1515
WINDOW.sessionStorage.clear();
1616
});
1717

18-
const SAMPLE_RATES = {
19-
sessionSampleRate: 1.0,
20-
errorSampleRate: 0.0,
21-
};
22-
2318
it('fetches a valid and sampled session', function () {
2419
WINDOW.sessionStorage.setItem(
2520
REPLAY_SESSION_KEY,
2621
'{"id":"fd09adfc4117477abc8de643e5a5798a","sampled": "session","started":1648827162630,"lastActivity":1648827162658}',
2722
);
2823

29-
expect(fetchSession(SAMPLE_RATES)).toEqual({
24+
expect(fetchSession()).toEqual({
3025
id: 'fd09adfc4117477abc8de643e5a5798a',
3126
lastActivity: 1648827162658,
3227
segmentId: 0,
@@ -41,7 +36,7 @@ it('fetches an unsampled session', function () {
4136
'{"id":"fd09adfc4117477abc8de643e5a5798a","sampled": false,"started":1648827162630,"lastActivity":1648827162658}',
4237
);
4338

44-
expect(fetchSession(SAMPLE_RATES)).toEqual({
39+
expect(fetchSession()).toEqual({
4540
id: 'fd09adfc4117477abc8de643e5a5798a',
4641
lastActivity: 1648827162658,
4742
segmentId: 0,
@@ -50,29 +45,14 @@ it('fetches an unsampled session', function () {
5045
});
5146
});
5247

53-
it('auto-fixes a session without sampled', function () {
54-
WINDOW.sessionStorage.setItem(
55-
REPLAY_SESSION_KEY,
56-
'{"id":"fd09adfc4117477abc8de643e5a5798a","started":1648827162630,"lastActivity":1648827162658}',
57-
);
58-
59-
expect(fetchSession(SAMPLE_RATES)).toEqual({
60-
id: 'fd09adfc4117477abc8de643e5a5798a',
61-
lastActivity: 1648827162658,
62-
segmentId: 0,
63-
sampled: 'session',
64-
started: 1648827162630,
65-
});
66-
});
67-
6848
it('fetches a session that does not exist', function () {
69-
expect(fetchSession(SAMPLE_RATES)).toBe(null);
49+
expect(fetchSession()).toBe(null);
7050
});
7151

7252
it('fetches an invalid session', function () {
7353
WINDOW.sessionStorage.setItem(REPLAY_SESSION_KEY, '{"id":"fd09adfc4117477abc8de643e5a5798a",');
7454

75-
expect(fetchSession(SAMPLE_RATES)).toBe(null);
55+
expect(fetchSession()).toBe(null);
7656
});
7757

7858
it('safely attempts to fetch session when Session Storage is disabled', function () {
@@ -85,5 +65,5 @@ it('safely attempts to fetch session when Session Storage is disabled', function
8565
},
8666
});
8767

88-
expect(fetchSession(SAMPLE_RATES)).toEqual(null);
68+
expect(fetchSession()).toEqual(null);
8969
});

Diff for: packages/replay/test/unit/session/getSession.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ it('creates a non-sticky session when one does not exist', function () {
5858
});
5959

6060
// Should not have anything in storage
61-
expect(FetchSession.fetchSession(SAMPLE_RATES)).toBe(null);
61+
expect(FetchSession.fetchSession()).toBe(null);
6262
});
6363

6464
it('creates a non-sticky session, regardless of session existing in sessionStorage', function () {
@@ -98,7 +98,7 @@ it('creates a non-sticky session, when one is expired', function () {
9898
});
9999

100100
it('creates a sticky session when one does not exist', function () {
101-
expect(FetchSession.fetchSession(SAMPLE_RATES)).toBe(null);
101+
expect(FetchSession.fetchSession()).toBe(null);
102102

103103
const { session } = getSession({
104104
expiry: 900000,
@@ -119,7 +119,7 @@ it('creates a sticky session when one does not exist', function () {
119119
});
120120

121121
// Should not have anything in storage
122-
expect(FetchSession.fetchSession(SAMPLE_RATES)).toEqual({
122+
expect(FetchSession.fetchSession()).toEqual({
123123
id: 'test_session_id',
124124
segmentId: 0,
125125
lastActivity: expect.any(Number),

0 commit comments

Comments
 (0)