-
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
aa2bd37
commit bb5793b
Showing
4 changed files
with
144 additions
and
108 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
91 changes: 91 additions & 0 deletions
91
packages/logs/src/domain/logsCollection/consoleCollection.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,91 @@ | ||
import { ErrorSource, resetExperimentalFeatures, updateExperimentalFeatures, display } from '@datadog/browser-core' | ||
import { validateAndBuildLogsConfiguration } from '../configuration' | ||
import { HandlerType, StatusType } from '../logger' | ||
import { createSender } from '../sender' | ||
import { startConsoleCollection } from './consoleCollection' | ||
|
||
describe('error collection', () => { | ||
const initConfiguration = { clientToken: 'xxx', service: 'service' } | ||
let sendLogSpy: jasmine.Spy | ||
let consoleLogSpy: jasmine.Spy | ||
|
||
beforeEach(() => { | ||
sendLogSpy = jasmine.createSpy('sendLogSpy') | ||
consoleLogSpy = spyOn(console, 'log').and.callFake(() => true) | ||
spyOn(console, 'error').and.callFake(() => true) | ||
}) | ||
|
||
it('should send console logs when ff forward-logs is enabled', () => { | ||
updateExperimentalFeatures(['forward-logs']) | ||
const { stop } = startConsoleCollection( | ||
validateAndBuildLogsConfiguration({ ...initConfiguration, forwardConsoleLogs: ['log'] })!, | ||
createSender(sendLogSpy) | ||
) | ||
|
||
/* eslint-disable-next-line no-console */ | ||
console.log('foo', 'bar') | ||
|
||
expect(sendLogSpy).toHaveBeenCalledWith({ | ||
message: 'console log: foo bar', | ||
status: StatusType.info, | ||
}) | ||
|
||
expect(consoleLogSpy).toHaveBeenCalled() | ||
|
||
resetExperimentalFeatures() | ||
stop() | ||
}) | ||
|
||
it('should not send console logs when ff forward-logs is disabled', () => { | ||
const { stop } = startConsoleCollection( | ||
validateAndBuildLogsConfiguration({ ...initConfiguration, forwardConsoleLogs: ['log'] })!, | ||
createSender(sendLogSpy) | ||
) | ||
|
||
/* eslint-disable-next-line no-console */ | ||
console.log('foo', 'bar') | ||
|
||
expect(sendLogSpy).not.toHaveBeenCalled() | ||
expect(consoleLogSpy).toHaveBeenCalled() | ||
|
||
stop() | ||
}) | ||
|
||
it('console error should have an error object defined', () => { | ||
const { stop } = startConsoleCollection( | ||
validateAndBuildLogsConfiguration({ ...initConfiguration, forwardErrorsToLogs: true })!, | ||
createSender(sendLogSpy) | ||
) | ||
|
||
/* eslint-disable-next-line no-console */ | ||
console.error('foo', 'bar') | ||
|
||
expect(sendLogSpy.calls.mostRecent().args[0].error).toEqual({ | ||
origin: ErrorSource.CONSOLE, | ||
stack: undefined, | ||
}) | ||
|
||
stop() | ||
}) | ||
|
||
it('should not print the log twice when console handler is enabled', () => { | ||
updateExperimentalFeatures(['forward-logs']) | ||
|
||
const sender = createSender(sendLogSpy) | ||
const displaySpy = spyOn(display, 'log') | ||
const { stop } = startConsoleCollection( | ||
validateAndBuildLogsConfiguration({ ...initConfiguration, forwardConsoleLogs: ['log'] })!, | ||
sender | ||
) | ||
|
||
sender.setHandler([HandlerType.console]) | ||
/* eslint-disable-next-line no-console */ | ||
console.log('foo', 'bar') | ||
|
||
expect(consoleLogSpy).toHaveBeenCalledTimes(1) | ||
expect(displaySpy).not.toHaveBeenCalled() | ||
|
||
resetExperimentalFeatures() | ||
stop() | ||
}) | ||
}) |
44 changes: 44 additions & 0 deletions
44
packages/logs/src/domain/logsCollection/consoleCollection.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,44 @@ | ||
import type { Context, ClocksState, ConsoleLog } from '@datadog/browser-core' | ||
import { ConsoleApiName, ErrorSource, initConsoleObservable } 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 LogStatusForApi = { | ||
[ConsoleApiName.log]: StatusType.info, | ||
[ConsoleApiName.debug]: StatusType.debug, | ||
[ConsoleApiName.info]: StatusType.info, | ||
[ConsoleApiName.warn]: StatusType.warn, | ||
[ConsoleApiName.error]: StatusType.error, | ||
} | ||
export function startConsoleCollection(configuration: LogsConfiguration, sender: Sender) { | ||
const consoleObservable = initConsoleObservable(configuration.forwardConsoleLogs) | ||
const consoleSubscription = consoleObservable.subscribe(reportConsoleLog) | ||
|
||
function reportConsoleLog(log: ConsoleLog) { | ||
let messageContext: Partial<LogsEvent> | undefined | ||
if (log.api === ConsoleApiName.error) { | ||
messageContext = { | ||
error: { | ||
origin: ErrorSource.CONSOLE, | ||
stack: log.stack, | ||
}, | ||
} | ||
} | ||
sender.sendToHttp(log.message, messageContext, LogStatusForApi[log.api]) | ||
} | ||
|
||
return { | ||
stop: () => { | ||
consoleSubscription.unsubscribe() | ||
}, | ||
} | ||
} |