Skip to content

Commit

Permalink
add handling stack for addErrors and addAction
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-lebeau committed Apr 30, 2024
1 parent 184a20f commit 60046eb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/rum-core/src/boot/preStartRum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ describe('preStartRum', () => {
strategy.addError(error)
strategy.init(DEFAULT_INIT_CONFIGURATION)
expect(addErrorSpy).toHaveBeenCalledOnceWith(error, undefined)
// todo: test with handlingStack
})

it('startView', () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/rum-core/src/boot/rumPublicApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,26 @@ describe('rum public api', () => {
name: 'foo',
startClocks: jasmine.any(Object),
type: ActionType.CUSTOM,
handlingStack: jasmine.any(String),
},
{ context: {}, user: {}, hasReplay: undefined },
])
})

it('should generate a handling stack', () => {
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)

function triggerAction() {
rumPublicApi.addAction('foo', { bar: 'baz' })
}

triggerAction()

expect(addActionSpy).toHaveBeenCalledTimes(1)
const stacktrace = addActionSpy.calls.argsFor(0)[0].handlingStack
expect(stacktrace).toMatch(/^Error:\s+at triggerAction @/)
})

describe('save context when sending an action', () => {
it('saves the date', () => {
const { clock } = setupBuilder.withFakeClock().build()
Expand Down
21 changes: 13 additions & 8 deletions packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,19 @@ export function makeRumPublicApi(startRumImpl: StartRum, recorderApi: RecorderAp

getInitConfiguration: monitor(() => deepClone(strategy.initConfiguration)),

addAction: monitor((name: string, context?: object) => {
strategy.addAction({
name: sanitize(name)!,
context: sanitize(context) as Context,
startClocks: clocksNow(),
type: ActionType.CUSTOM,
})
}),
addAction: (name: string, context?: object) => {
const handlingStack = createHandlingStack(2)

callMonitored(() =>
strategy.addAction({
name: sanitize(name)!,
context: sanitize(context) as Context,
startClocks: clocksNow(),
type: ActionType.CUSTOM,
handlingStack,
})
)
},

addError: (error: unknown, context?: object) => {
const handlingStack = createHandlingStack(2)
Expand Down
24 changes: 22 additions & 2 deletions packages/rum-core/src/domain/action/actionCollection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import type { ClocksState, Context, Observable } from '@datadog/browser-core'
import { noop, assign, combine, toServerDuration, generateUUID } from '@datadog/browser-core'
import {
noop,
assign,
combine,
toServerDuration,
generateUUID,
isExperimentalFeatureEnabled,
ExperimentalFeature,
} from '@datadog/browser-core'

import type { RawRumActionEvent } from '../../rawRumEvent.types'
import { ActionType, RumEventType } from '../../rawRumEvent.types'
Expand All @@ -9,6 +17,7 @@ import type { RumConfiguration } from '../configuration'
import type { CommonContext } from '../contexts/commonContext'
import type { PageStateHistory } from '../contexts/pageStateHistory'
import { PageState } from '../contexts/pageStateHistory'
import type { RumActionEventDomainContext } from '../../domainContext.types'
import type { ActionContexts, ClickAction } from './trackClickActions'
import { trackClickActions } from './trackClickActions'

Expand All @@ -19,6 +28,7 @@ export interface CustomAction {
name: string
startClocks: ClocksState
context?: Context
handlingStack?: string
}

export type AutoAction = ClickAction
Expand Down Expand Up @@ -101,11 +111,21 @@ function processAction(
autoActionProperties
)

const domainContext: RumActionEventDomainContext = isAutoAction(action) ? { events: action.events } : {}

if (
!isAutoAction(action) &&
action.handlingStack &&
isExperimentalFeatureEnabled(ExperimentalFeature.MICRO_FRONTEND)
) {
domainContext.handlingStack = action.handlingStack
}

return {
customerContext,
rawRumEvent: actionEvent,
startTime: action.startClocks.relative,
domainContext: isAutoAction(action) ? { events: action.events } : {},
domainContext,
}
}

Expand Down
15 changes: 12 additions & 3 deletions packages/rum-core/src/domain/error/errorCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
Observable,
trackRuntimeError,
NonErrorPrefix,
isExperimentalFeatureEnabled,
ExperimentalFeature,
} from '@datadog/browser-core'
import type { RumConfiguration } from '../configuration'
import type { RawRumErrorEvent } from '../../rawRumEvent.types'
Expand All @@ -20,6 +22,7 @@ import type { FeatureFlagContexts } from '../contexts/featureFlagContext'
import type { CommonContext } from '../contexts/commonContext'
import type { PageStateHistory } from '../contexts/pageStateHistory'
import { PageState } from '../contexts/pageStateHistory'
import type { RumErrorEventDomainContext } from '../../domainContext.types'
import { trackConsoleError } from './trackConsoleError'
import { trackReportError } from './trackReportError'

Expand Down Expand Up @@ -119,11 +122,17 @@ function processError(
rawRumEvent.feature_flags = featureFlagContext
}

const domainContext: RumErrorEventDomainContext = {
error: error.originalError,
}

if (isExperimentalFeatureEnabled(ExperimentalFeature.MICRO_FRONTEND)) {
domainContext.handlingStack = error.handlingStack
}

return {
rawRumEvent,
startTime: error.startClocks.relative,
domainContext: {
error: error.originalError,
},
domainContext,
}
}
2 changes: 2 additions & 0 deletions packages/rum-core/src/domainContext.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface RumViewEventDomainContext {

export interface RumActionEventDomainContext {
events?: Event[]
handlingStack?: string
}

export interface RumFetchResourceEventDomainContext {
Expand All @@ -49,6 +50,7 @@ export interface RumOtherResourceEventDomainContext {

export interface RumErrorEventDomainContext {
error: unknown
handlingStack?: string
}

export interface RumLongTaskEventDomainContext {
Expand Down

0 comments on commit 60046eb

Please sign in to comment.