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

define createLogger API #3286

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions packages/core/src/domain/telemetry/telemetryEvent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ export type TelemetryCommonFeaturesUsage =
| StartView
| AddAction
| AddError
| CreateReporter
| GetReporter
| SetGlobalContext
| SetUser
| SetAccount
Expand Down Expand Up @@ -610,6 +612,20 @@ export interface AddError {
feature: 'add-error'
[k: string]: unknown
}
export interface CreateReporter {
/**
* createReporter API
*/
feature: 'create-reporter'
[k: string]: unknown
}
export interface GetReporter {
/**
* getReporter API
*/
feature: 'get-reporter'
[k: string]: unknown
}
export interface SetGlobalContext {
/**
* setGlobalContext, setGlobalContextProperty, addAttribute APIs
Expand Down
29 changes: 29 additions & 0 deletions packages/rum-core/src/boot/rumPublicApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,35 @@ describe('rum public api', () => {
})
})

describe('createReporter', () => {
let addErrorSpy: jasmine.Spy<ReturnType<StartRum>['addError']>
let addActionSpy: jasmine.Spy<ReturnType<StartRum>['addAction']>
let rumPublicApi: RumPublicApi

beforeEach(() => {
addErrorSpy = jasmine.createSpy()
addActionSpy = jasmine.createSpy()
rumPublicApi = makeRumPublicApi(
() => ({
...noopStartRum(),
addError: addErrorSpy,
addAction: addActionSpy,
}),
noopRecorderApi
)
})

it('sets the component as part of the context', () => {
const reporter = rumPublicApi.createReporter('test-component', { context: { team: 'datadog' } })
reporter.addError(new Error('Something went wrong'))
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
expect(addErrorSpy.calls.argsFor(0)[0].context).toEqual({
component: 'test-component',
team: 'datadog',
})
})
})

describe('setUser', () => {
let addActionSpy: jasmine.Spy<ReturnType<StartRum>['addAction']>
let displaySpy: jasmine.Spy<() => void>
Expand Down
66 changes: 64 additions & 2 deletions packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ export interface RumPublicApi extends PublicApi {
*/
addError: (error: unknown, context?: object) => void

/**
* Create an event reporter that will add the provided context to all events sent from it.
*/
createReporter: (component: string, conf?: ReporterConfiguration) => Reporter

/**
* Get a reporter from the list of global reporter
*/
getReporter: (component: string) => Reporter | undefined

/**
* Add a custom timing relative to the start of the current view,
* stored in `@view.custom_timings.<timing_name>`
Expand Down Expand Up @@ -347,6 +357,15 @@ export interface RumPublicApiOptions {

const RUM_STORAGE_KEY = 'rum'

export interface Reporter {
addAction: RumPublicApi['addAction']
addError: RumPublicApi['addError']
}

export interface ReporterConfiguration {
context?: object
}

export interface Strategy {
init: (initConfiguration: RumInitConfiguration, publicApi: RumPublicApi) => void
initConfiguration: RumInitConfiguration | undefined
Expand Down Expand Up @@ -383,6 +402,7 @@ export function makeRumPublicApi(
return buildCommonContext(globalContextManager, userContextManager, recorderApi)
}

const customReporters: { [name: string]: Reporter | undefined } = {}
let strategy = createPreStartStrategy(
options,
getCommonContext,
Expand Down Expand Up @@ -425,6 +445,38 @@ export function makeRumPublicApi(
}
)

const createReporter = (reporterContext: object | undefined): Reporter => {
const sanitizedReporterContext = sanitize(reporterContext) as Context
return {
addError: (error, context) => {
const handlingStack = createHandlingStack()
callMonitored(() => {
strategy.addError({
error, // Do not sanitize error here, it is needed unserialized by computeRawError()
handlingStack,
context: { ...sanitizedReporterContext, ...(sanitize(context) as Context) },
startClocks: clocksNow(),
})
addTelemetryUsage({ feature: 'add-error' })
})
},

addAction: (name, context) => {
const handlingStack = createHandlingStack()
callMonitored(() => {
strategy.addAction({
name: sanitize(name)!,
context: { ...sanitizedReporterContext, ...(sanitize(context) as Context) },
startClocks: clocksNow(),
type: ActionType.CUSTOM,
handlingStack: handlingStack ?? createHandlingStack(),
})
addTelemetryUsage({ feature: 'add-action' })
})
},
}
}

const startView: {
(name?: string): void
(options: ViewOptions): void
Expand Down Expand Up @@ -483,14 +535,13 @@ export function makeRumPublicApi(

addAction: (name, context) => {
const handlingStack = createHandlingStack()

callMonitored(() => {
strategy.addAction({
name: sanitize(name)!,
context: sanitize(context) as Context,
startClocks: clocksNow(),
type: ActionType.CUSTOM,
handlingStack,
handlingStack: handlingStack ?? createHandlingStack(),
})
addTelemetryUsage({ feature: 'add-action' })
})
Expand All @@ -509,6 +560,17 @@ export function makeRumPublicApi(
})
},

createReporter: (component, conf) => {
addTelemetryUsage({ feature: 'create-reporter' })
customReporters[component] = createReporter({ component, ...conf?.context })
return customReporters[component]
},

getReporter: (component) => {
addTelemetryUsage({ feature: 'get-reporter' })
return customReporters[component]
},

addTiming: monitor((name, time) => {
// TODO: next major decide to drop relative time support or update its behaviour
strategy.addTiming(sanitize(name)!, time as RelativeTime | TimeStamp | undefined)
Expand Down
Loading