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

♻️ Simplify RUM assembly #1588

Merged
merged 4 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 14 additions & 15 deletions packages/rum-core/src/domain/assembly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export function startRumAssembly(
const session = sessionManager.findTrackedSession(rawRumEvent.type !== RumEventType.VIEW ? startTime : undefined)
if (session && viewContext && urlContext) {
const commonContext = savedCommonContext || getCommonContext()
const actionId = actionContexts.findActionId(startTime)

const rumContext: RumContext = {
_dd: {
format_version: 2,
Expand All @@ -113,30 +115,27 @@ export function startRumAssembly(
id: configuration.applicationId,
},
date: timeStampNow(),
service: configuration.service,
service: isExperimentalFeatureEnabled('sub-apps')
? viewContext.service || configuration.service
: configuration.service,
version: isExperimentalFeatureEnabled('sub-apps') ? viewContext.version || configuration.version : undefined,
source: 'browser',
session: {
id: session.id,
type: syntheticsContext ? SessionType.SYNTHETICS : ciTestContext ? SessionType.CI_TEST : SessionType.USER,
},
view: {
id: viewContext.view.id,
name: viewContext.view.name,
url: urlContext.view.url,
referrer: urlContext.view.referrer,
Copy link
Member

Choose a reason for hiding this comment

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

🥜 nitpick: ‏maybe remove the view extra-level from those contexts

},
action: needToAssembleWithAction(rawRumEvent) && actionId ? { id: actionId } : undefined,
synthetics: syntheticsContext,
ci_test: ciTestContext,
}
const actionId = actionContexts.findActionId(startTime)

if (!isExperimentalFeatureEnabled('sub-apps')) {
delete viewContext.service
delete viewContext.version
} else {
rumContext.version = configuration.version
}

const serverRumEvent = (
needToAssembleWithAction(rawRumEvent) && actionId
? combine(rumContext, urlContext, viewContext, { action: { id: actionId } }, rawRumEvent)
: combine(rumContext, urlContext, viewContext, rawRumEvent)
) as RumEvent & Context

const serverRumEvent = combine(rumContext, rawRumEvent) as RumEvent & Context
serverRumEvent.context = combine(commonContext.context, customerContext)

if (!('has_replay' in serverRumEvent.session)) {
Expand Down
17 changes: 13 additions & 4 deletions packages/rum-core/src/rawRumEvent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export type RawRumEvent =
| RawRumLongTaskEvent
| RawRumActionEvent

export interface RumContext {
export interface RumContext extends Context {
Copy link
Member

Choose a reason for hiding this comment

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

💬 suggestion: ‏ we need to extend Context here just to work around the Index signature for type 'string' is missing in type ... error, but RumContext is never actually used with an index, except when using combine.

Maybe we could remove the extends here, and cast the object as RumContext & Context where RumContext is used with combine. WDYT?

I know this is a bit confusing, and there is a long standing TS issue related to that.

date: TimeStamp
application: {
id: string
Expand All @@ -194,6 +194,15 @@ export interface RumContext {
type: string
has_replay?: boolean
}
view: {
id: string
referrer?: string
url: string
name?: string
}
action?: {
id: string | string[]
}
synthetics?: {
test_id: string
result_id: string
Expand All @@ -211,7 +220,7 @@ export interface RumContext {
}
}

export interface ViewContext extends Context {
export interface ViewContext {
service?: string
version?: string
view: {
Expand All @@ -220,13 +229,13 @@ export interface ViewContext extends Context {
}
}

export interface ActionContext extends Context {
export interface ActionContext {
action: {
id: string | string[]
}
}

export interface UrlContext extends Context {
export interface UrlContext {
view: {
url: string
referrer: string
Expand Down
4 changes: 2 additions & 2 deletions packages/rum-core/test/specHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { ViewEvent, ViewOptions } from '../src/domain/rumEventsCollection/v
import { trackViews } from '../src/domain/rumEventsCollection/view/trackViews'
import type { RumSessionManager } from '../src/domain/rumSessionManager'
import { RumSessionPlan } from '../src/domain/rumSessionManager'
import type { RawRumEvent, RumContext, ViewContext, UrlContext } from '../src/rawRumEvent.types'
import type { RawRumEvent, RumContext } from '../src/rawRumEvent.types'
import type { LocationChange } from '../src/browser/locationChangeObservable'
import type { UrlContexts } from '../src/domain/urlContexts'
import type { BrowserWindow } from '../src/domain/syntheticsContext'
Expand Down Expand Up @@ -193,7 +193,7 @@ export function setup(): TestSetupBuilder {

function validateRumEventFormat(rawRumEvent: RawRumEvent) {
const fakeId = '00000000-aaaa-0000-aaaa-000000000000'
const fakeContext: RumContext & ViewContext & UrlContext = {
const fakeContext: RumContext = {
_dd: {
format_version: 2,
drift: 0,
Expand Down