Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [RUMF-1518] implement a new API to stop the RUM session #2064

Merged
merged 16 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/domain/session/oldCookiesMigration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CookieOptions } from '../../browser/cookie'
import { getCookie } from '../../browser/cookie'
import type { SessionState } from './sessionStore'
import { SESSION_COOKIE_NAME, persistSession } from './sessionCookieStore'
import { SESSION_COOKIE_NAME, persistSessionCookie } from './sessionCookieStore'

export const OLD_SESSION_COOKIE_NAME = '_dd'
export const OLD_RUM_COOKIE_NAME = '_dd_r'
Expand Down Expand Up @@ -31,6 +31,6 @@ export function tryOldCookiesMigration(options: CookieOptions) {
if (oldRumType && /^[012]$/.test(oldRumType)) {
session[RUM_SESSION_KEY] = oldRumType
}
persistSession(session, options)
persistSessionCookie(session, options)
}
}
36 changes: 18 additions & 18 deletions packages/core/src/domain/session/sessionCookieStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { isChromium } from '../../tools/browserDetection'
import {
SESSION_COOKIE_NAME,
toSessionString,
retrieveSession,
persistSession,
retrieveSessionCookie,
persistSessionCookie,
MAX_NUMBER_OF_LOCK_RETRIES,
LOCK_RETRY_DELAY,
withCookieLockAccess,
Expand Down Expand Up @@ -33,37 +33,37 @@ describe('session cookie store', () => {
})

it('should persist session when process return a value', () => {
persistSession(initialSession, COOKIE_OPTIONS)
persistSessionCookie(initialSession, COOKIE_OPTIONS)
processSpy.and.returnValue({ ...otherSession })

withCookieLockAccess({ options: COOKIE_OPTIONS, process: processSpy, after: afterSpy })

expect(processSpy).toHaveBeenCalledWith(initialSession)
const expectedSession = { ...otherSession, expire: jasmine.any(String) }
expect(retrieveSession()).toEqual(expectedSession)
expect(retrieveSessionCookie()).toEqual(expectedSession)
expect(afterSpy).toHaveBeenCalledWith(expectedSession)
})

it('should clear session when process return an empty value', () => {
persistSession(initialSession, COOKIE_OPTIONS)
persistSessionCookie(initialSession, COOKIE_OPTIONS)
processSpy.and.returnValue({})

withCookieLockAccess({ options: COOKIE_OPTIONS, process: processSpy, after: afterSpy })

expect(processSpy).toHaveBeenCalledWith(initialSession)
const expectedSession = {}
expect(retrieveSession()).toEqual(expectedSession)
expect(retrieveSessionCookie()).toEqual(expectedSession)
expect(afterSpy).toHaveBeenCalledWith(expectedSession)
})

it('should not persist session when process return undefined', () => {
persistSession(initialSession, COOKIE_OPTIONS)
persistSessionCookie(initialSession, COOKIE_OPTIONS)
processSpy.and.returnValue(undefined)

withCookieLockAccess({ options: COOKIE_OPTIONS, process: processSpy, after: afterSpy })

expect(processSpy).toHaveBeenCalledWith(initialSession)
expect(retrieveSession()).toEqual(initialSession)
expect(retrieveSessionCookie()).toEqual(initialSession)
expect(afterSpy).toHaveBeenCalledWith(initialSession)
})
})
Expand All @@ -74,37 +74,37 @@ describe('session cookie store', () => {
})

it('should persist session when process return a value', () => {
persistSession(initialSession, COOKIE_OPTIONS)
persistSessionCookie(initialSession, COOKIE_OPTIONS)
processSpy.and.callFake((session) => ({ ...otherSession, lock: session.lock }))

withCookieLockAccess({ options: COOKIE_OPTIONS, process: processSpy, after: afterSpy })

expect(processSpy).toHaveBeenCalledWith({ ...initialSession, lock: jasmine.any(String) })
const expectedSession = { ...otherSession, expire: jasmine.any(String) }
expect(retrieveSession()).toEqual(expectedSession)
expect(retrieveSessionCookie()).toEqual(expectedSession)
expect(afterSpy).toHaveBeenCalledWith(expectedSession)
})

it('should clear session when process return an empty value', () => {
persistSession(initialSession, COOKIE_OPTIONS)
persistSessionCookie(initialSession, COOKIE_OPTIONS)
processSpy.and.returnValue({})

withCookieLockAccess({ options: COOKIE_OPTIONS, process: processSpy, after: afterSpy })

expect(processSpy).toHaveBeenCalledWith({ ...initialSession, lock: jasmine.any(String) })
const expectedSession = {}
expect(retrieveSession()).toEqual(expectedSession)
expect(retrieveSessionCookie()).toEqual(expectedSession)
expect(afterSpy).toHaveBeenCalledWith(expectedSession)
})

it('should not persist session when process return undefined', () => {
persistSession(initialSession, COOKIE_OPTIONS)
persistSessionCookie(initialSession, COOKIE_OPTIONS)
processSpy.and.returnValue(undefined)

withCookieLockAccess({ options: COOKIE_OPTIONS, process: processSpy, after: afterSpy })

expect(processSpy).toHaveBeenCalledWith({ ...initialSession, lock: jasmine.any(String) })
expect(retrieveSession()).toEqual(initialSession)
expect(retrieveSessionCookie()).toEqual(initialSession)
expect(afterSpy).toHaveBeenCalledWith(initialSession)
})

Expand Down Expand Up @@ -162,7 +162,7 @@ describe('session cookie store', () => {
retryState: { ...initialSession, other: 'other' },
}),
})
persistSession(initialSession, COOKIE_OPTIONS)
persistSessionCookie(initialSession, COOKIE_OPTIONS)
processSpy.and.callFake((session) => ({ ...session, processed: 'processed' } as SessionState))

withCookieLockAccess({
Expand All @@ -184,7 +184,7 @@ describe('session cookie store', () => {
processed: 'processed',
expire: jasmine.any(String),
}
expect(retrieveSession()).toEqual(expectedSession)
expect(retrieveSessionCookie()).toEqual(expectedSession)
expect(afterSession).toEqual(expectedSession)
done()
},
Expand All @@ -195,7 +195,7 @@ describe('session cookie store', () => {
it('should abort after a max number of retry', () => {
const clock = mockClock()

persistSession(initialSession, COOKIE_OPTIONS)
persistSessionCookie(initialSession, COOKIE_OPTIONS)
cookie.setSpy.calls.reset()

cookie.getSpy.and.returnValue(buildSessionString({ ...initialSession, lock: 'locked' }))
Expand All @@ -216,7 +216,7 @@ describe('session cookie store', () => {
retryState: initialSession,
}),
})
persistSession(initialSession, COOKIE_OPTIONS)
persistSessionCookie(initialSession, COOKIE_OPTIONS)

withCookieLockAccess({
options: COOKIE_OPTIONS,
Expand Down
34 changes: 17 additions & 17 deletions packages/core/src/domain/session/sessionCookieStore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CookieOptions } from '../../browser/cookie'
import { getCookie, setCookie } from '../../browser/cookie'
import { deleteCookie, getCookie, setCookie } from '../../browser/cookie'
import { setTimeout } from '../../browser/timer'
import { isChromium } from '../../tools/browserDetection'
import { dateNow } from '../../tools/timeUtils'
Expand Down Expand Up @@ -38,7 +38,7 @@ export function withCookieLockAccess(operations: Operations, numberOfRetries = 0
return
}
let currentLock: string
let currentSession = retrieveSession()
let currentSession = retrieveSessionCookie()
if (isCookieLockEnabled()) {
// if someone has lock, retry later
if (currentSession.lock) {
Expand All @@ -48,9 +48,9 @@ export function withCookieLockAccess(operations: Operations, numberOfRetries = 0
// acquire lock
currentLock = utils.generateUUID()
currentSession.lock = currentLock
setSession(currentSession, operations.options)
setSessionCookie(currentSession, operations.options)
// if lock is not acquired, retry later
currentSession = retrieveSession()
currentSession = retrieveSessionCookie()
if (currentSession.lock !== currentLock) {
retryLater(operations, numberOfRetries)
return
Expand All @@ -59,27 +59,27 @@ export function withCookieLockAccess(operations: Operations, numberOfRetries = 0
let processedSession = operations.process(currentSession)
if (isCookieLockEnabled()) {
// if lock corrupted after process, retry later
currentSession = retrieveSession()
currentSession = retrieveSessionCookie()
if (currentSession.lock !== currentLock!) {
retryLater(operations, numberOfRetries)
return
}
}
if (processedSession) {
persistSession(processedSession, operations.options)
persistSessionCookie(processedSession, operations.options)
}
if (isCookieLockEnabled()) {
// correctly handle lock around expiration would require to handle this case properly at several levels
// since we don't have evidence of lock issues around expiration, let's just not do the corruption check for it
if (!(processedSession && isExpiredState(processedSession))) {
// if lock corrupted after persist, retry later
currentSession = retrieveSession()
currentSession = retrieveSessionCookie()
if (currentSession.lock !== currentLock!) {
retryLater(operations, numberOfRetries)
return
}
delete currentSession.lock
setSession(currentSession, operations.options)
setSessionCookie(currentSession, operations.options)
processedSession = currentSession
}
}
Expand Down Expand Up @@ -111,16 +111,16 @@ function next() {
}
}

export function persistSession(session: SessionState, options: CookieOptions) {
export function persistSessionCookie(session: SessionState, options: CookieOptions) {
if (isExpiredState(session)) {
clearSession(options)
deleteSessionCookie(options)
return
}
session.expire = String(dateNow() + SESSION_EXPIRATION_DELAY)
setSession(session, options)
setSessionCookie(session, options)
}

function setSession(session: SessionState, options: CookieOptions) {
function setSessionCookie(session: SessionState, options: CookieOptions) {
setCookie(SESSION_COOKIE_NAME, toSessionString(session), SESSION_EXPIRATION_DELAY, options)
}

Expand All @@ -131,7 +131,7 @@ export function toSessionString(session: SessionState) {
.join(SESSION_ENTRY_SEPARATOR)
}

export function retrieveSession(): SessionState {
export function retrieveSessionCookie(): SessionState {
const sessionString = getCookie(SESSION_COOKIE_NAME)
const session: SessionState = {}
if (isValidSessionString(sessionString)) {
Expand All @@ -146,6 +146,10 @@ export function retrieveSession(): SessionState {
return session
}

export function deleteSessionCookie(options: CookieOptions) {
deleteCookie(SESSION_COOKIE_NAME, options)
}

function isValidSessionString(sessionString: string | undefined): sessionString is string {
return (
sessionString !== undefined &&
Expand All @@ -156,7 +160,3 @@ function isValidSessionString(sessionString: string | undefined): sessionString
function isExpiredState(session: SessionState) {
return utils.isEmptyObject(session)
}

function clearSession(options: CookieOptions) {
setCookie(SESSION_COOKIE_NAME, '', 0, options)
}
Loading