Skip to content

Commit

Permalink
Create a "RUM" batch for telemetry events in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaudan committed Feb 23, 2022
1 parent 057dcde commit 8fd540d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
5 changes: 4 additions & 1 deletion packages/logs/src/boot/logsPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { LogsInitConfiguration } from '../domain/configuration'
import { validateAndBuildLogsConfiguration } from '../domain/configuration'
import type { HandlerType, LogsMessage, StatusType } from '../domain/logger'
import { Logger } from '../domain/logger'
import { startLogsInternalMonitoring } from '../domain/internalMonitoring'
import type { startLogs } from './startLogs'
import { buildEnv } from './buildEnv'

Expand Down Expand Up @@ -56,7 +57,9 @@ export function makeLogsPublicApi(startLogsImpl: StartLogs) {
return
}

sendLogStrategy = startLogsImpl(configuration, logger)
const internalMonitoring = startLogsInternalMonitoring(configuration)

sendLogStrategy = startLogsImpl(configuration, logger, internalMonitoring)
getInitConfigurationStrategy = () => deepClone(initConfiguration)
beforeInitSendLog.drain()

Expand Down
6 changes: 3 additions & 3 deletions packages/logs/src/boot/startLogs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe('logs', () => {
const consoleLogSpy = spyOn(console, 'log').and.callFake(() => true)

updateExperimentalFeatures(['forward-logs'])
originalStartLogs({ ...baseConfiguration, forwardConsoleLogs: ['log'] }, logger)
originalStartLogs({ ...baseConfiguration, forwardConsoleLogs: ['log'] }, logger, internalMonitoringStub)

/* eslint-disable-next-line no-console */
console.log('foo', 'bar')
Expand All @@ -212,13 +212,13 @@ describe('logs', () => {
const sendSpy = spyOn(initEventBridgeStub(), 'send')

let configuration = { ...baseConfiguration, sampleRate: 0 }
let sendLog = originalStartLogs(configuration, new Logger(noop))
let sendLog = originalStartLogs(configuration, new Logger(noop), internalMonitoringStub)
sendLog(DEFAULT_MESSAGE, {})

expect(sendSpy).not.toHaveBeenCalled()

configuration = { ...baseConfiguration, sampleRate: 100 }
sendLog = originalStartLogs(configuration, new Logger(noop))
sendLog = originalStartLogs(configuration, new Logger(noop), internalMonitoringStub)
sendLog(DEFAULT_MESSAGE, {})

expect(sendSpy).toHaveBeenCalled()
Expand Down
19 changes: 15 additions & 4 deletions packages/logs/src/boot/startLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
canUseEventBridge,
getEventBridge,
getRelativeTime,
startInternalMonitoring,
initConsoleObservable,
ConsoleApiName,
ErrorSource,
Expand All @@ -30,9 +29,7 @@ const LogStatusForApi = {
[ConsoleApiName.error]: StatusType.error,
}

export function startLogs(configuration: LogsConfiguration, logger: Logger) {
const internalMonitoring = startInternalMonitoring(configuration)

export function startLogs(configuration: LogsConfiguration, logger: Logger, internalMonitoring: InternalMonitoring) {
const rawErrorObservable = new Observable<RawError>()

if (configuration.forwardErrorsToLogs) {
Expand Down Expand Up @@ -62,6 +59,20 @@ export function doStartLogs(
view: { name: null, url: null, referrer: null },
})
)
internalMonitoring.setTelemetryContextProvider(() => ({
application: {
id: getRUMInternalContext()?.application_id,
},
session: {
id: sessionManager.findTrackedSession()?.id,
},
view: {
id: (getRUMInternalContext()?.view as Context)?.id,
},
action: {
id: (getRUMInternalContext()?.user_action as Context)?.id,
},
}))

const assemble = buildAssemble(sessionManager, configuration, reportRawError)

Expand Down
16 changes: 16 additions & 0 deletions packages/logs/src/domain/internalMonitoring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Configuration, InternalMonitoring } from '@datadog/browser-core'
import { startInternalMonitoring, Batch, HttpRequest } from '@datadog/browser-core'

export function startLogsInternalMonitoring(configuration: Configuration): InternalMonitoring {
const internalMonitoring = startInternalMonitoring(configuration)
const monitoringBatch = new Batch(
new HttpRequest(configuration.rumEndpointBuilder, configuration.batchBytesLimit),
configuration.maxBatchSize,
configuration.batchBytesLimit,
configuration.maxMessageSize,
configuration.flushTimeout
)
// TODO handle replica
internalMonitoring.telemetryEventObservable.subscribe((event) => monitoringBatch.add(event))
return internalMonitoring
}

0 comments on commit 8fd540d

Please sign in to comment.