Skip to content

Commit

Permalink
only pass handling stack if FF is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-lebeau committed May 28, 2024
1 parent 169fa16 commit 0b4c23a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
41 changes: 28 additions & 13 deletions packages/logs/src/domain/console/consoleCollection.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { ErrorWithCause } from '@datadog/browser-core'
import { ErrorSource, noop, objectEntries } from '@datadog/browser-core'
import { ErrorSource, ExperimentalFeature, noop, objectEntries } from '@datadog/browser-core'
import { mockExperimentalFeatures } from 'packages/core/test'
import type { RawConsoleLogsEvent } from '../../rawLogsEvent.types'
import { validateAndBuildLogsConfiguration } from '../configuration'
import type { RawLogsEventCollectedData } from '../lifeCycle'
import { LifeCycle, LifeCycleEventType } from '../lifeCycle'
import { startConsoleCollection, LogStatusForApi } from './consoleCollection'

describe('console collection', () => {
fdescribe('console collection', () => {
const initConfiguration = { clientToken: 'xxx', service: 'service' }
let consoleSpies: { [key: string]: jasmine.Spy }
let stopConsoleCollection: () => void
Expand Down Expand Up @@ -43,17 +44,31 @@ describe('console collection', () => {
/* eslint-disable-next-line no-console */
console[api as keyof typeof LogStatusForApi]('foo', 'bar')

expect(rawLogsEvents[0]).toEqual({
rawLogsEvent: {
date: jasmine.any(Number),
message: 'foo bar',
status,
origin: ErrorSource.CONSOLE,
error: whatever(),
},
domainContext: {
handlingStack: jasmine.any(String),
},
expect(rawLogsEvents[0].rawLogsEvent).toEqual({
date: jasmine.any(Number),
message: 'foo bar',
status,
origin: ErrorSource.CONSOLE,
error: whatever(),
})

expect(consoleSpies[api]).toHaveBeenCalled()
})
})

objectEntries(LogStatusForApi).forEach(([api]) => {
it(`should add domainContext to logs from console.${api}`, () => {
mockExperimentalFeatures([ExperimentalFeature.MICRO_FRONTEND])
;({ stop: stopConsoleCollection } = startConsoleCollection(
validateAndBuildLogsConfiguration({ ...initConfiguration, forwardConsoleLogs: 'all' })!,
lifeCycle
))

/* eslint-disable-next-line no-console */
console[api as keyof typeof LogStatusForApi]('foo', 'bar')

expect(rawLogsEvents[0].domainContext).toEqual({
handlingStack: jasmine.any(String),
})

expect(consoleSpies[api]).toHaveBeenCalled()
Expand Down
25 changes: 19 additions & 6 deletions packages/logs/src/domain/console/consoleCollection.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import type { Context, ClocksState, ConsoleLog } from '@datadog/browser-core'
import { timeStampNow, ConsoleApiName, ErrorSource, initConsoleObservable } from '@datadog/browser-core'
import {
timeStampNow,
ConsoleApiName,
ErrorSource,
initConsoleObservable,
isExperimentalFeatureEnabled,
ExperimentalFeature,
} from '@datadog/browser-core'
import type { LogsConfiguration } from '../configuration'
import type { LifeCycle } from '../lifeCycle'
import type { LifeCycle, RawLogsEventCollectedData } from '../lifeCycle'
import { LifeCycleEventType } from '../lifeCycle'
import { StatusType } from '../logger'
import type { RawLogsEvent } from '../../rawLogsEvent.types'

export interface ProvidedError {
startClocks: ClocksState
Expand All @@ -21,7 +29,7 @@ export const LogStatusForApi = {
}
export function startConsoleCollection(configuration: LogsConfiguration, lifeCycle: LifeCycle) {
const consoleSubscription = initConsoleObservable(configuration.forwardConsoleLogs).subscribe((log: ConsoleLog) => {
lifeCycle.notify(LifeCycleEventType.RAW_LOG_COLLECTED, {
const collectedData: RawLogsEventCollectedData<RawLogsEvent> = {
rawLogsEvent: {
date: timeStampNow(),
message: log.message,
Expand All @@ -36,10 +44,15 @@ export function startConsoleCollection(configuration: LogsConfiguration, lifeCyc
: undefined,
status: LogStatusForApi[log.api],
},
domainContext: {
}

if (isExperimentalFeatureEnabled(ExperimentalFeature.MICRO_FRONTEND)) {
collectedData.domainContext = {
handlingStack: log.handlingStack,
},
})
}
}

lifeCycle.notify(LifeCycleEventType.RAW_LOG_COLLECTED, collectedData)
})

return {
Expand Down

0 comments on commit 0b4c23a

Please sign in to comment.