Skip to content

Commit

Permalink
Collect error in Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Feb 11, 2022
1 parent 3351acc commit f960da4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
15 changes: 13 additions & 2 deletions packages/logs/src/boot/startLogs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Context, RawError, RelativeTime, TimeStamp } from '@datadog/browser-core'
import type { Context, RawError, RelativeTime, Report, TimeStamp } from '@datadog/browser-core'
import {
ErrorSource,
noop,
Expand Down Expand Up @@ -61,6 +61,8 @@ describe('logs', () => {
let sessionIsTracked: boolean
let server: sinon.SinonFakeServer
let errorObservable: Observable<RawError>
let reportObservable: Observable<Report>

const sessionManager: LogsSessionManager = {
findTrackedSession: () => (sessionIsTracked ? { id: SESSION_ID } : undefined),
}
Expand All @@ -69,12 +71,21 @@ describe('logs', () => {
configuration: configurationOverrides,
}: { errorLogger?: Logger; configuration?: Partial<LogsConfiguration> } = {}) => {
const configuration = { ...baseConfiguration, ...configurationOverrides }
return doStartLogs(configuration, errorObservable, internalMonitoring, sessionManager, errorLogger)
return doStartLogs(
configuration,
errorObservable,
reportObservable,
internalMonitoring,
sessionManager,
errorLogger
)
}

beforeEach(() => {
sessionIsTracked = true
errorObservable = new Observable<RawError>()
reportObservable = new Observable<Report>()

server = sinon.fakeServer.create()
})

Expand Down
39 changes: 34 additions & 5 deletions packages/logs/src/boot/startLogs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Context, InternalMonitoring, RawError, RelativeTime } from '@datadog/browser-core'
import type { Context, InternalMonitoring, RawError, RelativeTime, Report } from '@datadog/browser-core'
import {
areCookiesAuthorized,
combine,
Expand All @@ -10,6 +10,9 @@ import {
getEventBridge,
getRelativeTime,
startInternalMonitoring,
ReportType,
ErrorSource,
initReportObservable,
} from '@datadog/browser-core'
import { trackNetworkError } from '../domain/trackNetworkError'
import type { Logger, LogsMessage } from '../domain/logger'
Expand All @@ -18,6 +21,13 @@ import type { LogsSessionManager } from '../domain/logsSessionManager'
import { startLogsSessionManager, startLogsSessionManagerStub } from '../domain/logsSessionManager'
import { startLoggerBatch } from '../transport/startLoggerBatch'
import type { LogsConfiguration } from '../domain/configuration'
import type { LogsEvent } from '../logsEvent.types'

const LogStatusForReport = {
[ReportType.csp_violation]: StatusType.error,
[ReportType.intervention]: StatusType.error,
[ReportType.deprecation]: StatusType.warn,
}

export function startLogs(configuration: LogsConfiguration, errorLogger: Logger) {
const internalMonitoring = startInternalMonitoring(configuration)
Expand All @@ -30,17 +40,20 @@ export function startLogs(configuration: LogsConfiguration, errorLogger: Logger)
trackNetworkError(configuration, errorObservable)
}

const reportObservable = initReportObservable(['intervention', 'deprecation', 'csp_violation'])

const session =
areCookiesAuthorized(configuration.cookieOptions) && !canUseEventBridge()
? startLogsSessionManager(configuration)
: startLogsSessionManagerStub(configuration)

return doStartLogs(configuration, errorObservable, internalMonitoring, session, errorLogger)
return doStartLogs(configuration, errorObservable, reportObservable, internalMonitoring, session, errorLogger)
}

export function doStartLogs(
configuration: LogsConfiguration,
errorObservable: Observable<RawError>,
reportObservable: Observable<Report>,
internalMonitoring: InternalMonitoring,
sessionManager: LogsSessionManager,
errorLogger: Logger
Expand All @@ -51,7 +64,7 @@ export function doStartLogs(
})
)

const assemble = buildAssemble(sessionManager, configuration, reportError)
const assemble = buildAssemble(sessionManager, configuration, logError)

let onLogEventCollected: (message: Context) => void
if (canUseEventBridge()) {
Expand All @@ -62,7 +75,7 @@ export function doStartLogs(
onLogEventCollected = (message) => batch.add(message)
}

function reportError(error: RawError) {
function logError(error: RawError) {
errorLogger.error(
error.message,
combine(
Expand All @@ -86,7 +99,23 @@ export function doStartLogs(
)
)
}
errorObservable.subscribe(reportError)

function logReport(report: Report) {
let messageContext: Partial<LogsEvent> | undefined
const logStatus = LogStatusForReport[report.type]
if (logStatus === StatusType.error) {
messageContext = {
error: {
origin: ErrorSource.REPORT,
stack: report.stack,
},
}
}
errorLogger.log(report.message, messageContext, logStatus)
}

reportObservable.subscribe(logReport)
errorObservable.subscribe(logError)

return (message: LogsMessage, currentContext: Context) => {
const contextualizedMessage = assemble(message, currentContext)
Expand Down
2 changes: 1 addition & 1 deletion packages/logs/src/logsEvent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface LogsEvent {
/**
* Origin of the error
*/
origin: 'network' | 'source' | 'console' | 'logger' | 'agent' | 'custom'
origin: 'network' | 'source' | 'console' | 'logger' | 'agent' | 'report' | 'custom'
/**
* Stacktrace of the error
*/
Expand Down

0 comments on commit f960da4

Please sign in to comment.