-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb5793b
commit ab0a69e
Showing
4 changed files
with
140 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/logs/src/domain/logsCollection/reportCollection.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { ErrorSource, resetExperimentalFeatures, updateExperimentalFeatures } from '@datadog/browser-core' | ||
import { stubReportingObserver } from '../../../../core/test/stubReportApis' | ||
import { validateAndBuildLogsConfiguration } from '../configuration' | ||
import { StatusType } from '../logger' | ||
import { createSender } from '../sender' | ||
import { startReportCollection } from './reportCollection' | ||
|
||
describe('reports', () => { | ||
const initConfiguration = { clientToken: 'xxx', service: 'service' } | ||
let sendLogSpy: jasmine.Spy | ||
let reportingObserverStub: ReturnType<typeof stubReportingObserver> | ||
|
||
beforeEach(() => { | ||
sendLogSpy = jasmine.createSpy('sendLogSpy') | ||
reportingObserverStub = stubReportingObserver() | ||
}) | ||
|
||
afterEach(() => { | ||
reportingObserverStub.reset() | ||
}) | ||
|
||
it('should send reports when ff forward-reports is enabled', () => { | ||
updateExperimentalFeatures(['forward-reports']) | ||
const { stop } = startReportCollection( | ||
validateAndBuildLogsConfiguration({ ...initConfiguration, forwardReports: ['intervention'] })!, | ||
createSender(sendLogSpy) | ||
) | ||
|
||
reportingObserverStub.raiseReport('intervention') | ||
expect(sendLogSpy).toHaveBeenCalledOnceWith({ | ||
error: { | ||
kind: 'NavigatorVibrate', | ||
origin: ErrorSource.REPORT, | ||
stack: jasmine.any(String), | ||
}, | ||
message: 'intervention: foo bar', | ||
status: StatusType.error, | ||
}) | ||
|
||
resetExperimentalFeatures() | ||
stop() | ||
}) | ||
|
||
it('should not send reports when ff forward-reports is disabled', () => { | ||
const { stop } = startReportCollection( | ||
validateAndBuildLogsConfiguration({ ...initConfiguration, forwardReports: ['intervention'] })!, | ||
createSender(sendLogSpy) | ||
) | ||
reportingObserverStub.raiseReport('intervention') | ||
|
||
expect(sendLogSpy).not.toHaveBeenCalled() | ||
stop() | ||
}) | ||
|
||
it('should not send reports when forwardReports init option not specified', () => { | ||
const { stop } = startReportCollection( | ||
validateAndBuildLogsConfiguration({ ...initConfiguration })!, | ||
createSender(sendLogSpy) | ||
) | ||
reportingObserverStub.raiseReport('intervention') | ||
|
||
expect(sendLogSpy).not.toHaveBeenCalled() | ||
stop() | ||
}) | ||
|
||
it('should add the source file information to the message for non error reports', () => { | ||
updateExperimentalFeatures(['forward-reports']) | ||
const { stop } = startReportCollection( | ||
validateAndBuildLogsConfiguration({ ...initConfiguration, forwardReports: ['deprecation'] })!, | ||
createSender(sendLogSpy) | ||
) | ||
|
||
reportingObserverStub.raiseReport('deprecation') | ||
|
||
expect(sendLogSpy).toHaveBeenCalledOnceWith({ | ||
message: 'deprecation: foo bar Found in http://foo.bar/index.js:20:10', | ||
status: StatusType.warn, | ||
}) | ||
|
||
resetExperimentalFeatures() | ||
stop() | ||
}) | ||
}) |
49 changes: 49 additions & 0 deletions
49
packages/logs/src/domain/logsCollection/reportCollection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { Context, ClocksState, RawReport } from '@datadog/browser-core' | ||
import { ErrorSource, RawReportType, getFileFromStackTraceString, initReportObservable } from '@datadog/browser-core' | ||
import type { LogsEvent } from '../../logsEvent.types' | ||
import type { LogsConfiguration } from '../configuration' | ||
import { StatusType } from '../logger' | ||
import type { Sender } from '../sender' | ||
|
||
export interface ProvidedError { | ||
startClocks: ClocksState | ||
error: unknown | ||
context?: Context | ||
handlingStack: string | ||
} | ||
|
||
const LogStatusForReport = { | ||
[RawReportType.cspViolation]: StatusType.error, | ||
[RawReportType.intervention]: StatusType.error, | ||
[RawReportType.deprecation]: StatusType.warn, | ||
} | ||
|
||
export function startReportCollection(configuration: LogsConfiguration, sender: Sender) { | ||
const reportObservable = initReportObservable(configuration.forwardReports) | ||
const reportSubscription = reportObservable.subscribe(logReport) | ||
|
||
function logReport(report: RawReport) { | ||
let message = report.message | ||
let messageContext: Partial<LogsEvent> | undefined | ||
const logStatus = LogStatusForReport[report.type] | ||
if (logStatus === StatusType.error) { | ||
messageContext = { | ||
error: { | ||
kind: report.subtype, | ||
origin: ErrorSource.REPORT, | ||
stack: report.stack, | ||
}, | ||
} | ||
} else if (report.stack) { | ||
message += ` Found in ${getFileFromStackTraceString(report.stack)!}` | ||
} | ||
|
||
sender.sendToHttp(message, messageContext, logStatus) | ||
} | ||
|
||
return { | ||
stop: () => { | ||
reportSubscription.unsubscribe() | ||
}, | ||
} | ||
} |