Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨[RUMF-1314] Expose a DD_LOGS.getInternalContext #1626

Merged
merged 7 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/logs/src/boot/logsPublicApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { makeLogsPublicApi } from './logsPublicApi'
const DEFAULT_INIT_CONFIGURATION = { clientToken: 'xxx' }
const INVALID_INIT_CONFIGURATION = {} as LogsInitConfiguration

const mockSessionId = 'some-session-id'
const getInternalContext = () => ({ session_id: mockSessionId })

describe('logs entry', () => {
let handleLogSpy: jasmine.Spy<
(
Expand All @@ -30,7 +33,7 @@ describe('logs entry', () => {

beforeEach(() => {
handleLogSpy = jasmine.createSpy()
startLogs = jasmine.createSpy().and.callFake(() => ({ handleLog: handleLogSpy }))
startLogs = jasmine.createSpy().and.callFake(() => ({ handleLog: handleLogSpy, getInternalContext }))
})

it('should define the public API with init', () => {
Expand Down Expand Up @@ -305,5 +308,22 @@ describe('logs entry', () => {
expect(LOGS.getLogger('bar')).toBeUndefined()
})
})

describe('internal context', () => {
let LOGS: LogsPublicApi

beforeEach(() => {
LOGS = makeLogsPublicApi(startLogs)
})

it('should return undefined if not initalized', () => {
expect(LOGS.getInternalContext()).toBeUndefined()
})

it('should get the internal context', () => {
LOGS.init(DEFAULT_INIT_CONFIGURATION)
expect(LOGS.getInternalContext()?.session_id).toEqual(mockSessionId)
})
})
})
})
12 changes: 11 additions & 1 deletion packages/logs/src/boot/logsPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ export type StartLogs = typeof startLogs

type StartLogsResult = ReturnType<typeof startLogs>

type StartTime = number | undefined

export function makeLogsPublicApi(startLogsImpl: StartLogs) {
let isAlreadyInitialized = false

const globalContextManager = createContextManager()
const customLoggers: { [name: string]: Logger | undefined } = {}
let getInternalContextStrategy: StartLogsResult['getInternalContext'] = () => undefined

const beforeInitLoggerLog = new BoundedBuffer()

Expand Down Expand Up @@ -76,7 +79,12 @@ export function makeLogsPublicApi(startLogsImpl: StartLogs) {
return
}

;({ handleLog: handleLogStrategy } = startLogsImpl(configuration, getCommonContext, mainLogger))
;({ handleLog: handleLogStrategy, getInternalContext: getInternalContextStrategy } = startLogsImpl(
configuration,
getCommonContext,
mainLogger
))

getInitConfigurationStrategy = () => deepClone(initConfiguration)
beforeInitLoggerLog.drain()

Expand Down Expand Up @@ -105,6 +113,8 @@ export function makeLogsPublicApi(startLogsImpl: StartLogs) {
getLogger: monitor((name: string) => customLoggers[name]),

getInitConfiguration: monitor(() => getInitConfigurationStrategy()),

getInternalContext: monitor((startTime?: StartTime) => getInternalContextStrategy(startTime)),
liywjl marked this conversation as resolved.
Show resolved Hide resolved
})

function overrideInitConfigurationForBridge<C extends InitConfiguration>(initConfiguration: C): C {
Expand Down
4 changes: 4 additions & 0 deletions packages/logs/src/boot/startLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type { CommonContext } from '../rawLogsEvent.types'
import { startLogsBatch } from '../transport/startLogsBatch'
import { startLogsBridge } from '../transport/startLogsBridge'
import type { Logger } from '../domain/logger'
import { startInternalContext } from '../domain/internalContext'

export function startLogs(configuration: LogsConfiguration, getCommonContext: () => CommonContext, mainLogger: Logger) {
const lifeCycle = new LifeCycle()
Expand Down Expand Up @@ -59,8 +60,11 @@ export function startLogs(configuration: LogsConfiguration, getCommonContext: ()
startLogsBridge(lifeCycle)
}

const internalContext = startInternalContext(session)

return {
handleLog,
getInternalContext: internalContext.get,
}
}

Expand Down
22 changes: 22 additions & 0 deletions packages/logs/src/domain/internalContext.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { startInternalContext } from './internalContext'

describe('internal context', () => {
it('should return undefined if sessionManager cannot find session', () => {
const sessionManagerMock = {
findTrackedSession: () => undefined,
}
expect(startInternalContext(sessionManagerMock).get()).toEqual(undefined)
})

it('should return internal context corresponding to startTime', () => {
const sessionIdMock = '123'
const sessionManagerMock = {
findTrackedSession: () => ({
id: sessionIdMock,
}),
}
expect(startInternalContext(sessionManagerMock).get()).toEqual({
session_id: sessionIdMock,
})
})
})
19 changes: 19 additions & 0 deletions packages/logs/src/domain/internalContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { RelativeTime } from '@datadog/browser-core'
import type { LogsSessionManager } from './logsSessionManager'

export interface InternalContext {
session_id: string | undefined
}

export function startInternalContext(sessionManager: LogsSessionManager) {
return {
get: (startTime?: number): InternalContext | undefined => {
const trackedSession = sessionManager.findTrackedSession(startTime as RelativeTime)
if (trackedSession) {
return {
session_id: trackedSession.id,
}
}
},
}
}