Skip to content

Commit

Permalink
✨ [RUMF-1264] scrub customer frames from telemetry errors (#1546)
Browse files Browse the repository at this point in the history
* ✨ [RUMF-1264] scrub customer frames from telemetry errors

* 👌 simplify scrub
  • Loading branch information
bcaudan authored May 18, 2022
1 parent e57b91f commit bcc5684
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { StackTrace } from '@datadog/browser-core'
import type { Context } from '../../tools/context'
import { display } from '../../tools/display'
import type { Configuration } from '../configuration'
Expand All @@ -17,6 +18,7 @@ import {
startInternalMonitoring,
callMonitored,
setDebugMode,
scrubCustomerFrames,
} from './internalMonitoring'
import type { TelemetryEvent } from './telemetryEvent.types'

Expand Down Expand Up @@ -360,3 +362,21 @@ describe('internal monitoring', () => {
})
})
})

describe('scrubCustomerFrames', () => {
it('should remove stack trace frames that are related to customer files', () => {
;[
{ scrub: false, url: 'https://www.datadoghq-browser-agent.com/datadog-rum-v4.js' },
{ scrub: false, url: 'https://www.datad0g-browser-agent.com/datadog-rum-v5.js' },
{ scrub: false, url: 'http://localhost/index.html' },
{ scrub: false, url: undefined },
{ scrub: false, url: '<anonymous>' },
{ scrub: true, url: 'https://foo.bar/path?qux=qix' },
].forEach(({ url, scrub }) => {
const candidate: Partial<StackTrace> = {
stack: [{ url }],
}
expect(scrubCustomerFrames(candidate as StackTrace).stack.length).toBe(scrub ? 0 : 1, `for url: ${url!}`)
})
})
})
19 changes: 17 additions & 2 deletions packages/core/src/domain/internalMonitoring/internalMonitoring.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { Context } from '../../tools/context'
import { display } from '../../tools/display'
import { toStackTraceString } from '../../tools/error'
import { assign, combine, jsonStringify, performDraw, includes } from '../../tools/utils'
import { assign, combine, jsonStringify, performDraw, includes, startsWith } from '../../tools/utils'
import type { Configuration } from '../configuration'
import type { StackTrace } from '../tracekit'
import { computeStackTrace } from '../tracekit'
import { Observable } from '../../tools/observable'
import { timeStampNow } from '../../tools/timeUtils'
Expand All @@ -17,6 +18,13 @@ const enum StatusType {
error = 'error',
}

const ALLOWED_FRAME_URLS = [
'https://www.datadoghq-browser-agent.com',
'https://www.datad0g-browser-agent.com',
'http://localhost',
'<anonymous>',
]

export interface InternalMonitoring {
setExternalContextProvider: (provider: () => Context) => void
monitoringMessageObservable: Observable<MonitoringMessage>
Expand Down Expand Up @@ -220,7 +228,7 @@ function formatError(e: unknown) {
return {
error: {
kind: stackTrace.name,
stack: toStackTraceString(stackTrace),
stack: toStackTraceString(scrubCustomerFrames(stackTrace)),
},
message: stackTrace.message!,
}
Expand All @@ -233,6 +241,13 @@ function formatError(e: unknown) {
}
}

export function scrubCustomerFrames(stackTrace: StackTrace) {
stackTrace.stack = stackTrace.stack.filter(
(frame) => !frame.url || ALLOWED_FRAME_URLS.some((allowedFrameUrl) => startsWith(frame.url!, allowedFrameUrl))
)
return stackTrace
}

export function setDebugMode(debugMode: boolean) {
monitoringConfiguration.debugMode = debugMode
}
Expand Down

0 comments on commit bcc5684

Please sign in to comment.