Skip to content

Commit

Permalink
retrieve fingerprint from addError calls
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaudan committed Apr 26, 2023
1 parent 0e957c1 commit 7ed5427
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/core/src/domain/error/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function computeRawError({
: NO_ERROR_STACK_PRESENT_MESSAGE
const causes = isErrorInstance ? flattenErrorCauses(originalError as ErrorWithCause, source) : undefined
const type = stackTrace?.name
const fingerprint = isErrorWithFingerprint(originalError) ? String(originalError.dd_fingerprint) : undefined

return {
startClocks,
Expand All @@ -52,6 +53,7 @@ export function computeRawError({
message,
stack,
causes,
fingerprint,
}
}

Expand All @@ -65,6 +67,10 @@ function hasUsableStack(isErrorInstance: boolean, stackTrace?: StackTrace): stac
return stackTrace.stack.length > 0 && (stackTrace.stack.length > 1 || stackTrace.stack[0].url !== undefined)
}

function isErrorWithFingerprint(error: unknown): error is { dd_fingerprint: unknown } {
return error instanceof Error && 'dd_fingerprint' in error
}

export function toStackTraceString(stack: StackTrace) {
let result = formatErrorMessage(stack)
stack.stack.forEach((frame) => {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/domain/error/error.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface RawError {
handling?: ErrorHandling
handlingStack?: string
causes?: RawErrorCause[]
fingerprint?: string
}

export const ErrorSource = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('error collection', () => {
handling: ErrorHandling.HANDLED,
source_type: 'browser',
causes: undefined,
fingerprint: undefined,
},
type: RumEventType.ERROR,
view: {
Expand Down Expand Up @@ -118,6 +119,39 @@ describe('error collection', () => {
expect(error?.causes?.[1].source).toEqual(ErrorSource.CUSTOM)
})

it('should extract fingerprint from error', () => {
const { rawRumEvents } = setupBuilder.build()

interface DatadogError extends Error {
dd_fingerprint?: string
}
const error = new Error('foo')
;(error as DatadogError).dd_fingerprint = 'my-fingerprint'

addError({
error,
handlingStack: 'Error: handling foo',
startClocks: { relative: 1234 as RelativeTime, timeStamp: 123456789 as TimeStamp },
})

expect((rawRumEvents[0].rawRumEvent as RawRumErrorEvent).error.fingerprint).toEqual('my-fingerprint')
})

it('should sanitize error fingerprint', () => {
const { rawRumEvents } = setupBuilder.build()

const error = new Error('foo')
;(error as any).dd_fingerprint = 2

addError({
error,
handlingStack: 'Error: handling foo',
startClocks: { relative: 1234 as RelativeTime, timeStamp: 123456789 as TimeStamp },
})

expect((rawRumEvents[0].rawRumEvent as RawRumErrorEvent).error.fingerprint).toEqual('2')
})

it('should save the specified customer context', () => {
const { rawRumEvents } = setupBuilder.build()
addError({
Expand Down Expand Up @@ -218,6 +252,7 @@ describe('error collection', () => {
handling: undefined,
source_type: 'browser',
causes: undefined,
fingerprint: undefined,
},
view: {
in_foreground: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function processError(
handling: error.handling,
causes: error.causes,
source_type: 'browser',
fingerprint: error.fingerprint,
},
type: RumEventType.ERROR as const,
}
Expand Down
1 change: 1 addition & 0 deletions packages/rum-core/src/rawRumEvent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface RawRumErrorEvent {
type?: string
stack?: string
handling_stack?: string
fingerprint?: string
source: ErrorSource
message: string
handling?: ErrorHandling
Expand Down

0 comments on commit 7ed5427

Please sign in to comment.