From c9898cbe20f414565f991ffbc25a631ea5f082b1 Mon Sep 17 00:00:00 2001 From: Eloy Toro Date: Wed, 15 Jan 2025 09:13:04 +0100 Subject: [PATCH] define createLogger API --- .../domain/telemetry/telemetryEvent.types.ts | 16 +++++ .../rum-core/src/boot/rumPublicApi.spec.ts | 29 ++++++++ packages/rum-core/src/boot/rumPublicApi.ts | 66 ++++++++++++++++++- 3 files changed, 109 insertions(+), 2 deletions(-) diff --git a/packages/core/src/domain/telemetry/telemetryEvent.types.ts b/packages/core/src/domain/telemetry/telemetryEvent.types.ts index 2ba79a50ea..5a8419fdb4 100644 --- a/packages/core/src/domain/telemetry/telemetryEvent.types.ts +++ b/packages/core/src/domain/telemetry/telemetryEvent.types.ts @@ -431,6 +431,8 @@ export type TelemetryCommonFeaturesUsage = | StartView | AddAction | AddError + | CreateReporter + | GetReporter | SetGlobalContext | SetUser | SetAccount @@ -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 diff --git a/packages/rum-core/src/boot/rumPublicApi.spec.ts b/packages/rum-core/src/boot/rumPublicApi.spec.ts index 32c3507da5..30123eb910 100644 --- a/packages/rum-core/src/boot/rumPublicApi.spec.ts +++ b/packages/rum-core/src/boot/rumPublicApi.spec.ts @@ -357,6 +357,35 @@ describe('rum public api', () => { }) }) + describe('createReporter', () => { + let addErrorSpy: jasmine.Spy['addError']> + let addActionSpy: jasmine.Spy['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['addAction']> let displaySpy: jasmine.Spy<() => void> diff --git a/packages/rum-core/src/boot/rumPublicApi.ts b/packages/rum-core/src/boot/rumPublicApi.ts index 145f81cadc..74e4e216f9 100644 --- a/packages/rum-core/src/boot/rumPublicApi.ts +++ b/packages/rum-core/src/boot/rumPublicApi.ts @@ -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.` @@ -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 @@ -383,6 +402,7 @@ export function makeRumPublicApi( return buildCommonContext(globalContextManager, userContextManager, recorderApi) } + const customReporters: { [name: string]: Reporter | undefined } = {} let strategy = createPreStartStrategy( options, getCommonContext, @@ -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 @@ -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' }) }) @@ -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)