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

[RUM] User action collection adjustements #346

Merged
merged 3 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 10 additions & 9 deletions packages/rum/src/rum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
} from './resourceUtils'
import { RumGlobal } from './rum.entry'
import { RumSession } from './rumSession'
import { getUserActionId } from './userActionCollection'
import { getUserActionReference, UserActionReference } from './userActionCollection'
import { trackView, viewContext, ViewMeasures } from './viewTracker'

export interface PerformancePaintTiming extends PerformanceEntry {
Expand Down Expand Up @@ -106,17 +106,18 @@ export interface RumResourceEvent {
kind: ResourceKind
}
traceId?: number
userActionId?: string
userAction?: UserActionReference
}

export interface RumErrorEvent {
date: number
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then, could you also make this change:

function trackErrors(lifeCycle: LifeCycle, addRumEvent: (event: RumErrorEvent) => void) {

http?: HttpContext
error: ErrorContext
evt: {
category: RumEventCategory.ERROR
}
message: string
userActionId?: string
userAction?: UserActionReference
}

export interface RumViewEvent {
Expand All @@ -138,7 +139,7 @@ export interface RumLongTaskEvent {
evt: {
category: RumEventCategory.LONG_TASK
}
userActionId?: string
userAction?: UserActionReference
}

export interface RumUserActionEvent {
Expand Down Expand Up @@ -264,7 +265,7 @@ function trackErrors(lifeCycle: LifeCycle, addRumEvent: (event: RumEvent) => voi
evt: {
category: RumEventCategory.ERROR,
},
userActionId: getUserActionId(startTime),
userAction: getUserActionReference(startTime),
...context,
})
})
Expand Down Expand Up @@ -292,7 +293,7 @@ function trackAutoUserAction(lifeCycle: LifeCycle, addRumEvent: (event: RumUserA
if (userAction.type !== UserActionType.CUSTOM) {
addRumEvent({
date: getTimestamp(userAction.startTime),
duration: userAction.duration,
duration: msToNs(userAction.duration),
evt: {
category: RumEventCategory.USER_ACTION,
name: userAction.name,
Expand Down Expand Up @@ -341,7 +342,7 @@ export function trackRequests(
kind,
},
traceId: request.traceId,
userActionId: getUserActionId(startTime),
userAction: getUserActionReference(startTime),
})
lifeCycle.notify(LifeCycleEventType.RESOURCE_ADDED_TO_BATCH)
})
Expand Down Expand Up @@ -395,7 +396,7 @@ export function handleResourceEntry(
resource: {
kind: resourceKind,
},
userActionId: getUserActionId(entry.startTime),
userAction: getUserActionReference(entry.startTime),
})
lifeCycle.notify(LifeCycleEventType.RESOURCE_ADDED_TO_BATCH)
}
Expand All @@ -408,6 +409,6 @@ export function handleLongTaskEntry(entry: PerformanceLongTaskTiming, addRumEven
category: RumEventCategory.LONG_TASK,
},
startTime: entry.startTime,
userActionId: getUserActionId(entry.startTime),
userAction: getUserActionReference(entry.startTime),
})
}
7 changes: 5 additions & 2 deletions packages/rum/src/userActionCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ export function startUserActionCollection(lifeCycle: LifeCycle) {

let currentUserAction: { id: string; startTime: number } | undefined

export function getUserActionId(time: number): string | undefined {
export interface UserActionReference {
id: string
}
export function getUserActionReference(time: number): UserActionReference | undefined {
if (currentUserAction && time >= currentUserAction.startTime) {
return currentUserAction.id
return { id: currentUserAction.id }
}
return undefined
}
Expand Down
22 changes: 11 additions & 11 deletions packages/rum/test/userActionCollection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LifeCycle, LifeCycleEventType } from '../src/lifeCycle'
import { UserActionType } from '../src/rum'
import {
$$tests,
getUserActionId,
getUserActionReference,
PageActivityEvent,
startUserActionCollection,
USER_ACTION_MAX_DURATION,
Expand Down Expand Up @@ -165,42 +165,42 @@ describe('newUserAction', () => {
})
})

describe('getUserActionId', () => {
describe('getUserActionReference', () => {
const clock = mockClock()

beforeEach(() => {
resetUserAction()
})

it('returns the current user action id', (done) => {
expect(getUserActionId(Date.now())).toBeUndefined()
it('returns the current user action reference', (done) => {
expect(getUserActionReference(Date.now())).toBeUndefined()
const activityObservable = new Observable<PageActivityEvent>()
newUserAction(activityObservable, (userAction) => {
expect(userAction!.id).toBe(userActionId)
expect(getUserActionId(Date.now())).toBeUndefined()
expect(userAction!.id).toBe(userActionReference.id)
expect(getUserActionReference(Date.now())).toBeUndefined()
done()
})

const userActionId = getUserActionId(Date.now())!
const userActionReference = getUserActionReference(Date.now())!

expect(userActionId).toBeDefined()
expect(userActionReference).toBeDefined()

clock.tick(80)
activityObservable.notify({ isBusy: false })

expect(getUserActionId(Date.now())).toBeDefined()
expect(getUserActionReference(Date.now())).toBeDefined()

clock.expire()
})

it('do not return the user action id for events occuring before the start of the user action', (done) => {
it('do not return the user action reference for events occuring before the start of the user action', (done) => {
const activityObservable = new Observable<PageActivityEvent>()
const time = Date.now()
clock.tick(50)
newUserAction(activityObservable, done)

clock.tick(50)
expect(getUserActionId(time)).toBeUndefined()
expect(getUserActionReference(time)).toBeUndefined()

clock.expire()
})
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/scenario/agents.scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,6 @@ describe('user action collection', () => {
expect(userActionEvents[0].duration).toBeGreaterThan(0)

expect(resourceEvents.length).toBe(1)
expect(resourceEvents[0].user_action_id).toBe(userActionEvents[0].user_action.id)
expect(resourceEvents[0].user_action!.id).toBe(userActionEvents[0].user_action.id!)
})
})
4 changes: 3 additions & 1 deletion test/e2e/scenario/serverTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export interface ServerRumResourceEvent extends ServerRumEvent {
kind: 'fetch' | 'xhr' | 'document'
}
duration: number
user_action_id?: string
user_action?: {
id: string
}
}

export function isRumResourceEvent(event: ServerRumEvent): event is ServerRumResourceEvent {
Expand Down