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-605] associate event to parent context by start date (behind flag) #445

Merged
merged 5 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export {
} from './requestCollection'
export {
startSessionManagement,
SESSION_TIME_OUT_DELAY,
// Exposed for tests
SESSION_COOKIE_NAME,
stopSessionManagement,
Expand Down
122 changes: 107 additions & 15 deletions packages/rum/src/parentContexts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Context } from '@datadog/browser-core'
import { Context, monitor, ONE_MINUTE, SESSION_TIME_OUT_DELAY } from '@datadog/browser-core'
import { LifeCycle, LifeCycleEventType } from './lifeCycle'
import { RumSession } from './rumSession'
import { AutoUserAction } from './userActionCollection'

export const VIEW_CONTEXT_TIME_OUT_DELAY = SESSION_TIME_OUT_DELAY
export const ACTION_CONTEXT_TIME_OUT_DELAY = 5 * ONE_MINUTE // arbitrary
export const CLEAR_OLD_CONTEXTS_INTERVAL = ONE_MINUTE

export interface ViewContext extends Context {
sessionId: string | undefined
Expand All @@ -16,45 +21,132 @@ export interface ActionContext extends Context {
}
}

interface InternalContext {
interface CurrentContext {
id: string
startTime: number
}

interface PreviousContext<T> {
startTime: number
endTime: number
context: T
}

export interface ParentContexts {
findAction: (startTime?: number) => ActionContext | undefined
findView: (startTime?: number) => ViewContext | undefined
stop: () => void
}

export function startParentContexts(location: Location, lifeCycle: LifeCycle, session: RumSession): ParentContexts {
let currentView: InternalContext | undefined
let currentAction: InternalContext | undefined
export function startParentContexts(
location: Location,
lifeCycle: LifeCycle,
session: RumSession,
withContextHistory: boolean
): ParentContexts {
let currentView: CurrentContext | undefined
let currentAction: CurrentContext | undefined
let currentSessionId: string | undefined

lifeCycle.subscribe(LifeCycleEventType.VIEW_CREATED, (internalContext) => {
currentView = internalContext
let previousViews: Array<PreviousContext<ViewContext>> = []
let previousActions: Array<PreviousContext<ActionContext>> = []

lifeCycle.subscribe(LifeCycleEventType.VIEW_CREATED, (currentContext) => {
if (currentView && withContextHistory) {
previousViews.unshift({
context: buildCurrentViewContext(),
endTime: currentContext.startTime,
startTime: currentView.startTime,
})
}
currentView = currentContext
currentSessionId = session.getId()
})

lifeCycle.subscribe(LifeCycleEventType.AUTO_ACTION_CREATED, (internalContext) => {
currentAction = internalContext
lifeCycle.subscribe(LifeCycleEventType.AUTO_ACTION_CREATED, (currentContext) => {
currentAction = currentContext
})

lifeCycle.subscribe(LifeCycleEventType.AUTO_ACTION_COMPLETED, () => {
lifeCycle.subscribe(LifeCycleEventType.AUTO_ACTION_COMPLETED, (userAction: AutoUserAction) => {
if (currentAction && withContextHistory) {
previousActions.unshift({
context: buildCurrentActionContext(),
endTime: currentAction.startTime + userAction.duration,
startTime: currentAction.startTime,
})
}
currentAction = undefined
})

lifeCycle.subscribe(LifeCycleEventType.AUTO_ACTION_DISCARDED, () => {
currentAction = undefined
})

lifeCycle.subscribe(LifeCycleEventType.SESSION_RENEWED, () => {
previousViews = []
previousActions = []
currentView = undefined
currentAction = undefined
})

const clearOldContextsInterval = window.setInterval(
monitor(() => {
clearOldContexts(previousViews, VIEW_CONTEXT_TIME_OUT_DELAY)
clearOldContexts(previousActions, ACTION_CONTEXT_TIME_OUT_DELAY)
}),
CLEAR_OLD_CONTEXTS_INTERVAL
)

function clearOldContexts(previousContexts: Array<PreviousContext<unknown>>, timeOutDelay: number) {
const oldTimeThreshold = performance.now() - timeOutDelay
while (previousContexts.length > 0 && previousContexts[previousContexts.length - 1].startTime < oldTimeThreshold) {
previousContexts.pop()
}
}

function buildCurrentViewContext() {
return { sessionId: currentSessionId, view: { id: currentView!.id, url: location.href } }
}

function buildCurrentActionContext() {
return { userAction: { id: currentAction!.id } }
}

function findContext<T>(
buildContext: () => T,
previousContexts: Array<PreviousContext<T>>,
currentContext?: CurrentContext,
startTime?: number
) {
if (!startTime) {
return currentContext ? buildContext() : undefined
}
if (currentContext && startTime >= currentContext.startTime) {
return buildContext()
}
if (!withContextHistory) {
mquentin marked this conversation as resolved.
Show resolved Hide resolved
return undefined
}
for (const previousContext of previousContexts) {
if (startTime > previousContext.endTime) {
break
}
if (startTime >= previousContext.startTime) {
return previousContext.context
}
}
return undefined
}

return {
findAction: (startTime) => {
if (!currentAction || (startTime !== undefined && startTime < currentAction.startTime)) {
return undefined
}
return { userAction: { id: currentAction.id } }
return findContext(buildCurrentActionContext, previousActions, currentAction, startTime)
},
findView: (startTime) => {
return findContext(buildCurrentViewContext, previousViews, currentView, startTime)
},
stop: () => {
window.clearInterval(clearOldContextsInterval)
},
findView: () => currentView && { sessionId: currentSessionId, view: { id: currentView.id, url: location.href } },
}
}
2 changes: 1 addition & 1 deletion packages/rum/src/rum.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const STUBBED_RUM = {
addUserAction(name: string, context: Context) {
makeStub('addUserAction')
},
getInternalContext(): InternalContext | undefined {
getInternalContext(startTime?: number): InternalContext | undefined {
makeStub('getInternalContext')
return undefined
},
Expand Down
7 changes: 6 additions & 1 deletion packages/rum/src/rum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ export function startRum(
): Omit<RumGlobal, 'init'> {
let globalContext: Context = {}

const parentContexts = startParentContexts(window.location, lifeCycle, session)
const parentContexts = startParentContexts(
window.location,
lifeCycle,
session,
configuration.isEnabled('context-history')
)

internalMonitoring.setExternalContextProvider(() => {
return deepMerge(
Expand Down
2 changes: 0 additions & 2 deletions packages/rum/src/userActionCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ export interface AutoUserAction {
measures: UserActionMeasures
}

export type UserAction = CustomUserAction | AutoUserAction

export function startUserActionCollection(lifeCycle: LifeCycle) {
const userAction = startUserActionManagement(lifeCycle)

Expand Down
Loading