From 1124ff5b5ac83a9aca29a8644d545fb75f06e307 Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Fri, 20 Nov 2020 11:06:10 +0100 Subject: [PATCH 01/12] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20switch=20parent=20co?= =?UTF-8?q?ntext=20to=20v2=20+=20missing=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rum/src/domain/parentContexts.spec.ts | 236 ++++++++++++++++++ packages/rum/src/domain/parentContexts.ts | 34 +-- 2 files changed, 253 insertions(+), 17 deletions(-) diff --git a/packages/rum/src/domain/parentContexts.spec.ts b/packages/rum/src/domain/parentContexts.spec.ts index ce5a1a72b5..f36ae27fea 100644 --- a/packages/rum/src/domain/parentContexts.spec.ts +++ b/packages/rum/src/domain/parentContexts.spec.ts @@ -250,3 +250,239 @@ describe('parentContexts', () => { }) }) }) + +describe('parentContexts v2', () => { + const FAKE_ID = 'fake' + const startTime = 10 + + function buildViewCreatedEvent(partialViewCreatedEvent: Partial = {}): ViewCreatedEvent { + return { location, startTime, id: FAKE_ID, referrer: 'http://foo.com', ...partialViewCreatedEvent } + } + + let sessionId: string + let setupBuilder: TestSetupBuilder + let parentContexts: ParentContexts + + beforeEach(() => { + sessionId = 'fake-session' + setupBuilder = setup() + .withFakeLocation('http://fake-url.com') + .withSession({ + getId: () => sessionId, + isTracked: () => true, + isTrackedWithResource: () => true, + }) + .beforeBuild(({ lifeCycle, session }) => { + parentContexts = startParentContexts(lifeCycle, session) + return parentContexts + }) + }) + + afterEach(() => { + setupBuilder.cleanup() + }) + + describe('findView', () => { + it('should return undefined when there is no current view and no startTime', () => { + setupBuilder.build() + + expect(parentContexts.findViewV2()).toBeUndefined() + }) + + it('should return the current view context when there is no start time', () => { + const { lifeCycle } = setupBuilder.build() + + lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent()) + + expect(parentContexts.findViewV2()).toBeDefined() + expect(parentContexts.findViewV2()!.view.id).toEqual(FAKE_ID) + }) + + it('should return the view context corresponding to startTime', () => { + const { lifeCycle } = setupBuilder.build() + + lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 10, id: 'view 1' })) + lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 20, id: 'view 2' })) + lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 30, id: 'view 3' })) + + expect(parentContexts.findViewV2(15)!.view.id).toEqual('view 1') + expect(parentContexts.findViewV2(20)!.view.id).toEqual('view 2') + expect(parentContexts.findViewV2(40)!.view.id).toEqual('view 3') + }) + + it('should return undefined when no view context corresponding to startTime', () => { + const { lifeCycle } = setupBuilder.build() + + lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 10, id: 'view 1' })) + lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 20, id: 'view 2' })) + + expect(parentContexts.findViewV2(5)).not.toBeDefined() + }) + + it('should replace the current view context on VIEW_CREATED', () => { + const { lifeCycle } = setupBuilder.build() + + lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent()) + const newViewId = 'fake 2' + lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ id: newViewId })) + + expect(parentContexts.findViewV2()!.view.id).toEqual(newViewId) + }) + + it('should return the current url with the current view', () => { + const { lifeCycle, fakeLocation } = setupBuilder.build() + + lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ location: fakeLocation as Location })) + expect(parentContexts.findViewV2()!.view.url).toBe('http://fake-url.com/') + + history.pushState({}, '', '/foo') + + expect(parentContexts.findViewV2()!.view.url).toBe('http://fake-url.com/foo') + }) + + it('should update session id only on VIEW_CREATED', () => { + const { lifeCycle } = setupBuilder.build() + + lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent()) + expect(parentContexts.findViewV2()!.session.id).toBe('fake-session') + + sessionId = 'other-session' + expect(parentContexts.findViewV2()!.session.id).toBe('fake-session') + + lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ id: 'fake 2' })) + expect(parentContexts.findViewV2()!.session.id).toBe('other-session') + }) + }) + + describe('findAction', () => { + it('should return undefined when there is no current action and no startTime', () => { + setupBuilder.build() + + expect(parentContexts.findActionV2()).toBeUndefined() + }) + + it('should return the current action context when no startTime', () => { + const { lifeCycle } = setupBuilder.build() + + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime, id: FAKE_ID }) + + expect(parentContexts.findActionV2()).toBeDefined() + expect(parentContexts.findActionV2()!.action.id).toBe(FAKE_ID) + }) + + it('should return the action context corresponding to startTime', () => { + const { lifeCycle } = setupBuilder.build() + + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 10, id: 'action 1' }) + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, stubActionWithDuration(10)) + + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 30, id: 'action 2' }) + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, stubActionWithDuration(10)) + + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 50, id: 'action 3' }) + + expect(parentContexts.findActionV2(15)!.action.id).toBe('action 1') + expect(parentContexts.findActionV2(20)!.action.id).toBe('action 1') + expect(parentContexts.findActionV2(30)!.action.id).toBe('action 2') + expect(parentContexts.findActionV2(55)!.action.id).toBe('action 3') + }) + + it('should return undefined if no action context corresponding to startTime', () => { + const { lifeCycle } = setupBuilder.build() + + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 10, id: 'action 1' }) + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_DISCARDED) + + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 20, id: 'action 2' }) + + expect(parentContexts.findActionV2(10)).toBeUndefined() + }) + + it('should clear the current action on ACTION_DISCARDED', () => { + const { lifeCycle } = setupBuilder.build() + + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime, id: FAKE_ID }) + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_DISCARDED) + + expect(parentContexts.findActionV2()).toBeUndefined() + }) + + it('should clear the current action on ACTION_COMPLETED', () => { + const { lifeCycle } = setupBuilder.build() + + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime, id: FAKE_ID }) + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, stubActionWithDuration(10)) + + expect(parentContexts.findActionV2()).toBeUndefined() + }) + }) + + describe('history contexts', () => { + it('should be cleared on SESSION_RENEWED', () => { + const { lifeCycle } = setupBuilder.build() + + lifeCycle.notify( + LifeCycleEventType.VIEW_CREATED, + buildViewCreatedEvent({ + id: 'view 1', + startTime: 10, + }) + ) + lifeCycle.notify( + LifeCycleEventType.VIEW_CREATED, + buildViewCreatedEvent({ + id: 'view 2', + startTime: 20, + }) + ) + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 10, id: 'action 1' }) + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, stubActionWithDuration(10)) + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 20, id: 'action 2' }) + + expect(parentContexts.findViewV2(15)).toBeDefined() + expect(parentContexts.findActionV2(15)).toBeDefined() + expect(parentContexts.findViewV2(25)).toBeDefined() + expect(parentContexts.findActionV2(25)).toBeDefined() + + lifeCycle.notify(LifeCycleEventType.SESSION_RENEWED) + + expect(parentContexts.findViewV2(15)).toBeUndefined() + expect(parentContexts.findActionV2(15)).toBeUndefined() + expect(parentContexts.findViewV2(25)).toBeUndefined() + expect(parentContexts.findActionV2(25)).toBeUndefined() + }) + + it('should be cleared when too old', () => { + const { lifeCycle, clock } = setupBuilder.withFakeClock().build() + + const originalTime = performance.now() + const targetTime = originalTime + 5 + + lifeCycle.notify( + LifeCycleEventType.VIEW_CREATED, + buildViewCreatedEvent({ + id: 'view 1', + startTime: originalTime, + }) + ) + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: originalTime, id: 'action 1' }) + lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, stubActionWithDuration(10)) + lifeCycle.notify( + LifeCycleEventType.VIEW_CREATED, + buildViewCreatedEvent({ startTime: originalTime + 10, id: 'view 2' }) + ) + + clock.tick(10) + expect(parentContexts.findViewV2(targetTime)).toBeDefined() + expect(parentContexts.findActionV2(targetTime)).toBeDefined() + + clock.tick(ACTION_CONTEXT_TIME_OUT_DELAY + CLEAR_OLD_CONTEXTS_INTERVAL) + expect(parentContexts.findViewV2(targetTime)).toBeDefined() + expect(parentContexts.findActionV2(targetTime)).toBeUndefined() + + clock.tick(VIEW_CONTEXT_TIME_OUT_DELAY + CLEAR_OLD_CONTEXTS_INTERVAL) + expect(parentContexts.findViewV2(targetTime)).toBeUndefined() + expect(parentContexts.findActionV2(targetTime)).toBeUndefined() + }) + }) +}) diff --git a/packages/rum/src/domain/parentContexts.ts b/packages/rum/src/domain/parentContexts.ts index 2570851717..dc27af46d8 100644 --- a/packages/rum/src/domain/parentContexts.ts +++ b/packages/rum/src/domain/parentContexts.ts @@ -29,8 +29,8 @@ export function startParentContexts(lifeCycle: LifeCycle, session: RumSession): let currentAction: AutoActionCreatedEvent | undefined let currentSessionId: string | undefined - let previousViews: Array> = [] - let previousActions: Array> = [] + let previousViews: Array> = [] + let previousActions: Array> = [] lifeCycle.subscribe(LifeCycleEventType.VIEW_CREATED, (currentContext) => { if (currentView) { @@ -95,7 +95,9 @@ export function startParentContexts(lifeCycle: LifeCycle, session: RumSession): function buildCurrentViewContext() { return { - sessionId: currentSessionId, + session: { + id: currentSessionId, + }, view: { id: currentView!.id, referrer: currentView!.referrer, @@ -105,7 +107,7 @@ export function startParentContexts(lifeCycle: LifeCycle, session: RumSession): } function buildCurrentActionContext() { - return { userAction: { id: currentAction!.id } } + return { action: { id: currentAction!.id } } } function findContext( @@ -133,34 +135,32 @@ export function startParentContexts(lifeCycle: LifeCycle, session: RumSession): const parentContexts: ParentContexts = { findAction: (startTime) => { - return findContext(buildCurrentActionContext, previousActions, currentAction, startTime) - }, - findActionV2: (startTime) => { - const actionContext = parentContexts.findAction(startTime) + const actionContext = parentContexts.findActionV2(startTime) if (!actionContext) { return } return { - action: { - id: actionContext.userAction.id, + userAction: { + id: actionContext.action.id, }, } }, - findView: (startTime) => { - return findContext(buildCurrentViewContext, previousViews, currentView, startTime) + findActionV2: (startTime) => { + return findContext(buildCurrentActionContext, previousActions, currentAction, startTime) }, - findViewV2: (startTime) => { - const viewContext = parentContexts.findView(startTime) + findView: (startTime) => { + const viewContext = parentContexts.findViewV2(startTime) if (!viewContext) { return } return { - session: { - id: viewContext.sessionId, - }, + sessionId: viewContext.session.id, view: viewContext.view, } }, + findViewV2: (startTime) => { + return findContext(buildCurrentViewContext, previousViews, currentView, startTime) + }, stop: () => { window.clearInterval(clearOldContextsInterval) }, From eddebd7b48540071316dc90943c19f316af789f2 Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Fri, 20 Nov 2020 11:21:51 +0100 Subject: [PATCH 02/12] =?UTF-8?q?=F0=9F=94=A5=20remove=20v1=20specific=20c?= =?UTF-8?q?ode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/rum/src/boot/rum.entry.ts | 5 +- packages/rum/src/boot/rum.spec.ts | 55 ---- packages/rum/src/boot/rum.ts | 6 +- packages/rum/src/domain/assembly.spec.ts | 225 ----------------- packages/rum/src/domain/assembly.ts | 72 ------ .../rum/src/domain/internalContext.spec.ts | 78 +----- packages/rum/src/domain/internalContext.ts | 40 +-- packages/rum/src/domain/lifeCycle.ts | 28 +-- .../rum/src/domain/parentContexts.spec.ts | 236 ------------------ packages/rum/src/domain/parentContexts.ts | 27 +- .../action/actionCollection.spec.ts | 75 ------ .../action/actionCollection.ts | 53 +--- .../action/trackActions.spec.ts | 28 --- .../error/errorCollection.spec.ts | 117 +-------- .../error/errorCollection.ts | 57 +---- .../longTask/longTaskCollection.spec.ts | 50 ---- .../longTask/longTaskCollection.ts | 33 +-- .../resource/resourceCollection.spec.ts | 224 ----------------- .../resource/resourceCollection.ts | 76 +----- .../view/trackViews.spec.ts | 158 ------------ .../view/viewCollection.spec.ts | 75 ------ .../view/viewCollection.ts | 38 +-- .../rum/src/domain/trackEventCounts.spec.ts | 68 ----- packages/rum/src/domain/trackEventCounts.ts | 23 -- packages/rum/src/index.ts | 12 +- packages/rum/src/transport/batch.ts | 12 - packages/rum/src/types.ts | 141 ----------- packages/rum/src/typesV2.ts | 13 + packages/rum/test/specHelper.ts | 15 -- test/e2e/lib/framework/createTest.ts | 2 +- 30 files changed, 73 insertions(+), 1969 deletions(-) delete mode 100644 packages/rum/src/domain/assembly.spec.ts delete mode 100644 packages/rum/src/domain/assembly.ts delete mode 100644 packages/rum/src/types.ts diff --git a/packages/rum/src/boot/rum.entry.ts b/packages/rum/src/boot/rum.entry.ts index 0e78897654..4480877900 100644 --- a/packages/rum/src/boot/rum.entry.ts +++ b/packages/rum/src/boot/rum.entry.ts @@ -96,8 +96,11 @@ export function makeRumGlobal(startRumImpl: StartRum) { }) }), + /** + * @deprecated + * @see addAction + */ addUserAction: (name: string, context?: Context) => { - // TODO deprecate in v2 rumGlobal.addAction(name, context) }, diff --git a/packages/rum/src/boot/rum.spec.ts b/packages/rum/src/boot/rum.spec.ts index a67969fb9f..4ac758a66c 100644 --- a/packages/rum/src/boot/rum.spec.ts +++ b/packages/rum/src/boot/rum.spec.ts @@ -26,9 +26,6 @@ interface ServerRumEvent { function collectServerEvents(lifeCycle: LifeCycle) { const serverRumEvents: ServerRumEvent[] = [] - lifeCycle.subscribe(LifeCycleEventType.RUM_EVENT_COLLECTED, ({ serverRumEvent }) => { - serverRumEvents.push(serverRumEvent as any) - }) lifeCycle.subscribe(LifeCycleEventType.RUM_EVENT_V2_COLLECTED, ({ serverRumEvent }) => { serverRumEvents.push(serverRumEvent as any) }) @@ -54,34 +51,6 @@ describe('rum session', () => { setupBuilder.cleanup() }) - it('when the session is renewed, a new view event should be sent (v1)', () => { - let sessionId = '42' - const { lifeCycle } = setupBuilder - .withSession({ - getId: () => sessionId, - isTracked: () => true, - isTrackedWithResource: () => true, - }) - .withConfiguration({ - isEnabled: () => false, - }) - .build() - - expect(serverRumEvents.length).toEqual(1) - expect(serverRumEvents[0].evt.category).toEqual('view') - expect(serverRumEvents[0].session_id).toEqual('42') - - sessionId = '43' - lifeCycle.notify(LifeCycleEventType.SESSION_RENEWED) - - expect(serverRumEvents.length).toEqual(2) - - // New view event - expect(serverRumEvents[1].evt.category).toEqual('view') - expect(serverRumEvents[1].session_id).toEqual('43') - expect(serverRumEvents[1].view.id).not.toEqual(serverRumEvents[0].view.id) - }) - it('when the session is renewed, a new view event should be sent', () => { let sessionId = '42' const { lifeCycle } = setupBuilder @@ -135,30 +104,6 @@ describe('rum session keep alive', () => { setupBuilder.cleanup() }) - it('should send a view update regularly (v1)', () => { - const { clock } = setupBuilder - .withConfiguration({ - isEnabled: () => false, - }) - .build() - - // clear initial events - clock.tick(SESSION_KEEP_ALIVE_INTERVAL * 0.9) - serverRumEvents.length = 0 - - clock.tick(SESSION_KEEP_ALIVE_INTERVAL * 0.1) - - // view update - expect(serverRumEvents.length).toEqual(1) - expect(serverRumEvents[0].evt.category).toEqual('view') - - clock.tick(SESSION_KEEP_ALIVE_INTERVAL) - - // view update - expect(serverRumEvents.length).toEqual(2) - expect(serverRumEvents[1].evt.category).toEqual('view') - }) - it('should send a view update regularly', () => { const { clock } = setupBuilder.build() diff --git a/packages/rum/src/boot/rum.ts b/packages/rum/src/boot/rum.ts index f7bf63fafd..d3ee7450db 100644 --- a/packages/rum/src/boot/rum.ts +++ b/packages/rum/src/boot/rum.ts @@ -1,7 +1,6 @@ import { combine, commonInit, Configuration, Context } from '@datadog/browser-core' import { startDOMMutationCollection } from '../browser/domMutationCollection' import { startPerformanceCollection } from '../browser/performanceCollection' -import { startRumAssembly } from '../domain/assembly' import { startRumAssemblyV2 } from '../domain/assemblyV2' import { startInternalContext } from '../domain/internalContext' import { LifeCycle } from '../domain/lifeCycle' @@ -29,7 +28,7 @@ export function startRum(userConfiguration: RumUserConfiguration, getGlobalConte { application_id: userConfiguration.applicationId, }, - parentContexts.findView(), + parentContexts.findViewV2(), getGlobalContext() ) }) @@ -47,7 +46,7 @@ export function startRum(userConfiguration: RumUserConfiguration, getGlobalConte startPerformanceCollection(lifeCycle, configuration) startDOMMutationCollection(lifeCycle) - const internalContext = startInternalContext(userConfiguration.applicationId, session, parentContexts, configuration) + const internalContext = startInternalContext(userConfiguration.applicationId, session, parentContexts) return { addAction, @@ -66,7 +65,6 @@ export function startRumEventCollection( ) { const parentContexts = startParentContexts(lifeCycle, session) const batch = startRumBatch(configuration, lifeCycle) - startRumAssembly(applicationId, configuration, lifeCycle, session, parentContexts, getGlobalContext) startRumAssemblyV2(applicationId, configuration, lifeCycle, session, parentContexts, getGlobalContext) startLongTaskCollection(lifeCycle, configuration) startResourceCollection(lifeCycle, configuration, session) diff --git a/packages/rum/src/domain/assembly.spec.ts b/packages/rum/src/domain/assembly.spec.ts deleted file mode 100644 index c91c029713..0000000000 --- a/packages/rum/src/domain/assembly.spec.ts +++ /dev/null @@ -1,225 +0,0 @@ -import { Context } from '@datadog/browser-core' -import { setup, TestSetupBuilder } from '../../test/specHelper' -import { RumEventCategory } from '../index' -import { RawRumEvent } from '../types' -import { startRumAssembly } from './assembly' -import { LifeCycle, LifeCycleEventType } from './lifeCycle' - -interface ServerRumEvents { - application_id: string - user_action: { - id: string - } - date: number - evt: { - category: string - } - session_id: string - view: { - id: string - referrer: string - url: string - } - network?: { - bytes_written: number - } -} - -describe('rum assembly', () => { - let setupBuilder: TestSetupBuilder - let lifeCycle: LifeCycle - let globalContext: Context - let serverRumEvents: ServerRumEvents[] - let isTracked: boolean - let viewSessionId: string | undefined - - function generateRawRumEvent( - category: RumEventCategory, - properties?: Partial, - savedGlobalContext?: Context, - customerContext?: Context - ) { - const viewEvent = { evt: { category }, ...properties } - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { - customerContext, - savedGlobalContext, - rawRumEvent: viewEvent as RawRumEvent, - startTime: 0, - }) - } - - beforeEach(() => { - isTracked = true - viewSessionId = '1234' - setupBuilder = setup() - .withSession({ - getId: () => '1234', - isTracked: () => isTracked, - isTrackedWithResource: () => true, - }) - .withParentContexts({ - findAction: () => ({ - userAction: { - id: '7890', - }, - }), - findView: () => ({ - sessionId: viewSessionId, - view: { - id: 'abcde', - referrer: 'url', - url: 'url', - }, - }), - }) - .beforeBuild(({ applicationId, configuration, lifeCycle: localLifeCycle, session, parentContexts }) => { - startRumAssembly(applicationId, configuration, localLifeCycle, session, parentContexts, () => globalContext) - }) - ;({ lifeCycle } = setupBuilder.build()) - - serverRumEvents = [] - lifeCycle.subscribe(LifeCycleEventType.RUM_EVENT_COLLECTED, ({ serverRumEvent }) => - serverRumEvents.push((serverRumEvent as unknown) as ServerRumEvents) - ) - }) - - afterEach(() => { - setupBuilder.cleanup() - }) - - describe('events', () => { - it('should have snake cased attributes', () => { - generateRawRumEvent(RumEventCategory.RESOURCE, { network: { bytesWritten: 2 } }) - - expect(serverRumEvents[0].network!.bytes_written).toBe(2) - }) - }) - - describe('rum context', () => { - it('should be merged with event attributes', () => { - generateRawRumEvent(RumEventCategory.VIEW) - - expect(serverRumEvents[0].view.id).toBeDefined() - expect(serverRumEvents[0].date).toBeDefined() - }) - - it('should be snake cased', () => { - generateRawRumEvent(RumEventCategory.VIEW) - - expect(serverRumEvents[0].application_id).toBe('appId') - }) - - it('should be overwritten by event attributes', () => { - generateRawRumEvent(RumEventCategory.VIEW, { date: 10 }) - - expect(serverRumEvents[0].date).toBe(10) - }) - }) - - describe('rum global context', () => { - it('should be merged with event attributes', () => { - globalContext = { bar: 'foo' } - generateRawRumEvent(RumEventCategory.VIEW) - - expect((serverRumEvents[0] as any).bar).toEqual('foo') - }) - - it('should ignore subsequent context mutation', () => { - globalContext = { bar: 'foo' } - generateRawRumEvent(RumEventCategory.VIEW) - delete globalContext.bar - generateRawRumEvent(RumEventCategory.VIEW) - - expect((serverRumEvents[0] as any).bar).toEqual('foo') - expect((serverRumEvents[1] as any).bar).toBeUndefined() - }) - - it('should not be automatically snake cased', () => { - globalContext = { fooBar: 'foo' } - generateRawRumEvent(RumEventCategory.VIEW) - - expect(((serverRumEvents[0] as any) as any).fooBar).toEqual('foo') - }) - - it('should ignore the current global context when a saved global context is provided', () => { - globalContext = { replacedContext: 'b', addedContext: 'x' } - - generateRawRumEvent(RumEventCategory.VIEW, undefined, { replacedContext: 'a' }) - - expect((serverRumEvents[0] as any).replacedContext).toEqual('a') - expect((serverRumEvents[0] as any).addedContext).toEqual(undefined) - }) - }) - - describe('customer context', () => { - it('should be merged with event attributes', () => { - generateRawRumEvent(RumEventCategory.VIEW, undefined, undefined, { foo: 'bar' }) - - expect((serverRumEvents[0] as any).foo).toEqual('bar') - }) - - it('should not be automatically snake cased', () => { - generateRawRumEvent(RumEventCategory.VIEW, undefined, undefined, { fooBar: 'foo' }) - - expect(((serverRumEvents[0] as any) as any).fooBar).toEqual('foo') - }) - }) - - describe('action context', () => { - it('should be added on some event categories', () => { - ;[RumEventCategory.RESOURCE, RumEventCategory.LONG_TASK, RumEventCategory.ERROR].forEach((category) => { - generateRawRumEvent(category) - expect(serverRumEvents[0].user_action.id).toBeDefined() - serverRumEvents = [] - }) - ;[RumEventCategory.VIEW, RumEventCategory.USER_ACTION].forEach((category) => { - generateRawRumEvent(category) - expect(serverRumEvents[0].user_action).not.toBeDefined() - serverRumEvents = [] - }) - }) - }) - - describe('view context', () => { - it('should be merged with event attributes', () => { - generateRawRumEvent(RumEventCategory.USER_ACTION) - - expect(serverRumEvents[0].view).toEqual({ - id: 'abcde', - referrer: 'url', - url: 'url', - }) - expect(serverRumEvents[0].session_id).toEqual('1234') - }) - }) - - describe('session', () => { - it('when tracked, it should generate event', () => { - isTracked = true - - generateRawRumEvent(RumEventCategory.VIEW) - expect(serverRumEvents.length).toBe(1) - }) - - it('when not tracked, it should not generate event', () => { - isTracked = false - - generateRawRumEvent(RumEventCategory.VIEW) - expect(serverRumEvents.length).toBe(0) - }) - - it('when view context has session id, it should generate event', () => { - viewSessionId = '1234' - - generateRawRumEvent(RumEventCategory.VIEW) - expect(serverRumEvents.length).toBe(1) - }) - - it('when view context has no session id, it should not generate event', () => { - viewSessionId = undefined - - generateRawRumEvent(RumEventCategory.VIEW) - expect(serverRumEvents.length).toBe(0) - }) - }) -}) diff --git a/packages/rum/src/domain/assembly.ts b/packages/rum/src/domain/assembly.ts deleted file mode 100644 index 7db30f7c75..0000000000 --- a/packages/rum/src/domain/assembly.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { combine, Configuration, Context, withSnakeCaseKeys } from '@datadog/browser-core' -import { RawRumEvent, RumContext, RumErrorEvent, RumEventCategory, RumLongTaskEvent, RumResourceEvent } from '../types' -import { LifeCycle, LifeCycleEventType } from './lifeCycle' -import { ParentContexts } from './parentContexts' -import { RumSession } from './rumSession' - -interface BrowserWindow extends Window { - _DATADOG_SYNTHETICS_BROWSER?: unknown -} - -enum SessionType { - SYNTHETICS = 'synthetics', - USER = 'user', -} - -export function startRumAssembly( - applicationId: string, - configuration: Configuration, - lifeCycle: LifeCycle, - session: RumSession, - parentContexts: ParentContexts, - getGlobalContext: () => Context -) { - lifeCycle.subscribe( - LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - ({ - startTime, - rawRumEvent, - savedGlobalContext, - customerContext, - }: { - startTime: number - rawRumEvent: RawRumEvent - savedGlobalContext?: Context - customerContext?: Context - }) => { - const viewContext = parentContexts.findView(startTime) - if (session.isTracked() && viewContext && viewContext.sessionId) { - const actionContext = parentContexts.findAction(startTime) - const rumContext: RumContext = { - applicationId, - date: new Date().getTime(), - service: configuration.service, - session: { - // must be computed on each event because synthetics instrumentation can be done after sdk execution - // cf https://github.com/puppeteer/puppeteer/issues/3667 - type: getSessionType(), - }, - } - const rumEvent = needToAssembleWithAction(rawRumEvent) - ? combine(rumContext, viewContext, actionContext, rawRumEvent) - : combine(rumContext, viewContext, rawRumEvent) - const serverRumEvent = combine( - savedGlobalContext || getGlobalContext(), - customerContext, - withSnakeCaseKeys(rumEvent) - ) - lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, { rumEvent, serverRumEvent }) - } - } - ) -} - -function needToAssembleWithAction(event: RawRumEvent): event is RumErrorEvent | RumResourceEvent | RumLongTaskEvent { - return ( - [RumEventCategory.ERROR, RumEventCategory.RESOURCE, RumEventCategory.LONG_TASK].indexOf(event.evt.category) !== -1 - ) -} - -function getSessionType() { - return (window as BrowserWindow)._DATADOG_SYNTHETICS_BROWSER === undefined ? SessionType.USER : SessionType.SYNTHETICS -} diff --git a/packages/rum/src/domain/internalContext.spec.ts b/packages/rum/src/domain/internalContext.spec.ts index 7580d4f667..2fab44f807 100644 --- a/packages/rum/src/domain/internalContext.spec.ts +++ b/packages/rum/src/domain/internalContext.spec.ts @@ -2,80 +2,6 @@ import { setup, TestSetupBuilder } from '../../test/specHelper' import { startInternalContext } from './internalContext' import { ParentContexts } from './parentContexts' -describe('internal context', () => { - let setupBuilder: TestSetupBuilder - let parentContextsStub: Partial - let internalContext: ReturnType - - beforeEach(() => { - parentContextsStub = { - findAction: jasmine.createSpy('findAction').and.returnValue({ - userAction: { - id: '7890', - }, - }), - findView: jasmine.createSpy('findView').and.returnValue({ - sessionId: '1234', - view: { - id: 'abcde', - referrer: 'referrer', - url: 'url', - }, - }), - } - setupBuilder = setup() - .withParentContexts(parentContextsStub) - .withConfiguration({ - isEnabled: () => false, - }) - .beforeBuild(({ applicationId, session, parentContexts, configuration }) => { - internalContext = startInternalContext(applicationId, session, parentContexts, configuration) - }) - }) - - afterEach(() => { - setupBuilder.cleanup() - }) - - it('should return current internal context', () => { - setupBuilder.build() - - expect(internalContext.get()).toEqual({ - application_id: 'appId', - session_id: '1234', - user_action: { - id: '7890', - }, - view: { - id: 'abcde', - referrer: 'referrer', - url: 'url', - }, - }) - }) - - it("should return undefined if the session isn't tracked", () => { - setupBuilder - .withSession({ - getId: () => '1234', - isTracked: () => false, - isTrackedWithResource: () => false, - }) - .build() - - expect(internalContext.get()).toEqual(undefined) - }) - - it('should return internal context corresponding to startTime', () => { - setupBuilder.build() - - internalContext.get(123) - - expect(parentContextsStub.findView).toHaveBeenCalledWith(123) - expect(parentContextsStub.findAction).toHaveBeenCalledWith(123) - }) -}) - describe('internal context v2', () => { let setupBuilder: TestSetupBuilder let parentContextsStub: Partial @@ -101,8 +27,8 @@ describe('internal context v2', () => { } setupBuilder = setup() .withParentContexts(parentContextsStub) - .beforeBuild(({ applicationId, session, parentContexts, configuration }) => { - internalContext = startInternalContext(applicationId, session, parentContexts, configuration) + .beforeBuild(({ applicationId, session, parentContexts }) => { + internalContext = startInternalContext(applicationId, session, parentContexts) }) }) diff --git a/packages/rum/src/domain/internalContext.ts b/packages/rum/src/domain/internalContext.ts index b62cb723a6..6644632b67 100644 --- a/packages/rum/src/domain/internalContext.ts +++ b/packages/rum/src/domain/internalContext.ts @@ -1,5 +1,5 @@ -import { combine, Configuration, withSnakeCaseKeys } from '@datadog/browser-core' -import { InternalContext } from '../types' +import { combine, withSnakeCaseKeys } from '@datadog/browser-core' +import { InternalContext } from '../typesV2' import { ParentContexts } from './parentContexts' import { RumSession } from './rumSession' @@ -7,33 +7,19 @@ import { RumSession } from './rumSession' * Internal context keep returning v1 format * to not break compatibility with logs data format */ -export function startInternalContext( - applicationId: string, - session: RumSession, - parentContexts: ParentContexts, - configuration: Configuration -) { +export function startInternalContext(applicationId: string, session: RumSession, parentContexts: ParentContexts) { return { get: (startTime?: number) => { - if (configuration.isEnabled('v2_format')) { - const viewContext = parentContexts.findViewV2(startTime) - if (session.isTracked() && viewContext && viewContext.session.id) { - const actionContext = parentContexts.findActionV2(startTime) - return (withSnakeCaseKeys( - combine( - { applicationId }, - { sessionId: viewContext.session.id, view: viewContext.view }, - actionContext ? { userAction: { id: actionContext.action.id } } : undefined - ) - ) as unknown) as InternalContext - } - } else { - const viewContext = parentContexts.findView(startTime) - if (session.isTracked() && viewContext && viewContext.sessionId) { - return (withSnakeCaseKeys( - combine({ applicationId }, viewContext, parentContexts.findAction(startTime)) - ) as unknown) as InternalContext - } + const viewContext = parentContexts.findViewV2(startTime) + if (session.isTracked() && viewContext && viewContext.session.id) { + const actionContext = parentContexts.findActionV2(startTime) + return (withSnakeCaseKeys( + combine( + { applicationId }, + { sessionId: viewContext.session.id, view: viewContext.view }, + actionContext ? { userAction: { id: actionContext.action.id } } : undefined + ) + ) as unknown) as InternalContext } }, } diff --git a/packages/rum/src/domain/lifeCycle.ts b/packages/rum/src/domain/lifeCycle.ts index d188254a96..1c3f966415 100644 --- a/packages/rum/src/domain/lifeCycle.ts +++ b/packages/rum/src/domain/lifeCycle.ts @@ -1,9 +1,8 @@ import { Context } from '@datadog/browser-core' import { RumPerformanceEntry } from '../browser/performanceCollection' -import { RawRumEvent, RumEvent } from '../types' import { RawRumEventV2, RumEventV2 } from '../typesV2' import { RequestCompleteEvent, RequestStartEvent } from './requestCollection' -import { AutoAction, AutoActionCreatedEvent, CustomAction } from './rumEventsCollection/action/trackActions' +import { AutoAction, AutoActionCreatedEvent } from './rumEventsCollection/action/trackActions' import { View, ViewCreatedEvent } from './rumEventsCollection/view/trackViews' export enum LifeCycleEventType { @@ -18,9 +17,7 @@ export enum LifeCycleEventType { SESSION_RENEWED, DOM_MUTATED, BEFORE_UNLOAD, - RAW_RUM_EVENT_COLLECTED, RAW_RUM_EVENT_V2_COLLECTED, - RUM_EVENT_COLLECTED, RUM_EVENT_V2_COLLECTED, } @@ -45,15 +42,6 @@ export class LifeCycle { | LifeCycleEventType.BEFORE_UNLOAD | LifeCycleEventType.AUTO_ACTION_DISCARDED ): void - notify( - eventType: LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - data: { - startTime: number - rawRumEvent: RawRumEvent - savedGlobalContext?: Context - customerContext?: Context - } - ): void notify( eventType: LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, data: { @@ -63,7 +51,6 @@ export class LifeCycle { customerContext?: Context } ): void - notify(eventType: LifeCycleEventType.RUM_EVENT_COLLECTED, data: { rumEvent: RumEvent; serverRumEvent: Context }): void notify( eventType: LifeCycleEventType.RUM_EVENT_V2_COLLECTED, data: { rumEvent: RumEventV2; serverRumEvent: Context } @@ -99,15 +86,6 @@ export class LifeCycle { | LifeCycleEventType.AUTO_ACTION_DISCARDED, callback: () => void ): Subscription - subscribe( - eventType: LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - callback: (data: { - startTime: number - rawRumEvent: RawRumEvent - savedGlobalContext?: Context - customerContext?: Context - }) => void - ): Subscription subscribe( eventType: LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, callback: (data: { @@ -117,10 +95,6 @@ export class LifeCycle { customerContext?: Context }) => void ): Subscription - subscribe( - eventType: LifeCycleEventType.RUM_EVENT_COLLECTED, - callback: (data: { rumEvent: RumEvent; serverRumEvent: Context }) => void - ): Subscription subscribe( eventType: LifeCycleEventType.RUM_EVENT_V2_COLLECTED, callback: (data: { rumEvent: RumEventV2; serverRumEvent: Context }) => void diff --git a/packages/rum/src/domain/parentContexts.spec.ts b/packages/rum/src/domain/parentContexts.spec.ts index f36ae27fea..c9a0bcdbe1 100644 --- a/packages/rum/src/domain/parentContexts.spec.ts +++ b/packages/rum/src/domain/parentContexts.spec.ts @@ -15,242 +15,6 @@ function stubActionWithDuration(duration: number): AutoAction { return action as AutoAction } -describe('parentContexts', () => { - const FAKE_ID = 'fake' - const startTime = 10 - - function buildViewCreatedEvent(partialViewCreatedEvent: Partial = {}): ViewCreatedEvent { - return { location, startTime, id: FAKE_ID, referrer: 'http://foo.com', ...partialViewCreatedEvent } - } - - let sessionId: string - let setupBuilder: TestSetupBuilder - let parentContexts: ParentContexts - - beforeEach(() => { - sessionId = 'fake-session' - setupBuilder = setup() - .withFakeLocation('http://fake-url.com') - .withSession({ - getId: () => sessionId, - isTracked: () => true, - isTrackedWithResource: () => true, - }) - .beforeBuild(({ lifeCycle, session }) => { - parentContexts = startParentContexts(lifeCycle, session) - return parentContexts - }) - }) - - afterEach(() => { - setupBuilder.cleanup() - }) - - describe('findView', () => { - it('should return undefined when there is no current view and no startTime', () => { - setupBuilder.build() - - expect(parentContexts.findView()).toBeUndefined() - }) - - it('should return the current view context when there is no start time', () => { - const { lifeCycle } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent()) - - expect(parentContexts.findView()).toBeDefined() - expect(parentContexts.findView()!.view.id).toEqual(FAKE_ID) - }) - - it('should return the view context corresponding to startTime', () => { - const { lifeCycle } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 10, id: 'view 1' })) - lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 20, id: 'view 2' })) - lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 30, id: 'view 3' })) - - expect(parentContexts.findView(15)!.view.id).toEqual('view 1') - expect(parentContexts.findView(20)!.view.id).toEqual('view 2') - expect(parentContexts.findView(40)!.view.id).toEqual('view 3') - }) - - it('should return undefined when no view context corresponding to startTime', () => { - const { lifeCycle } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 10, id: 'view 1' })) - lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 20, id: 'view 2' })) - - expect(parentContexts.findView(5)).not.toBeDefined() - }) - - it('should replace the current view context on VIEW_CREATED', () => { - const { lifeCycle } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent()) - const newViewId = 'fake 2' - lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ id: newViewId })) - - expect(parentContexts.findView()!.view.id).toEqual(newViewId) - }) - - it('should return the current url with the current view', () => { - const { lifeCycle, fakeLocation } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ location: fakeLocation as Location })) - expect(parentContexts.findView()!.view.url).toBe('http://fake-url.com/') - - history.pushState({}, '', '/foo') - - expect(parentContexts.findView()!.view.url).toBe('http://fake-url.com/foo') - }) - - it('should update session id only on VIEW_CREATED', () => { - const { lifeCycle } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent()) - expect(parentContexts.findView()!.sessionId).toBe('fake-session') - - sessionId = 'other-session' - expect(parentContexts.findView()!.sessionId).toBe('fake-session') - - lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ id: 'fake 2' })) - expect(parentContexts.findView()!.sessionId).toBe('other-session') - }) - }) - - describe('findAction', () => { - it('should return undefined when there is no current action and no startTime', () => { - setupBuilder.build() - - expect(parentContexts.findAction()).toBeUndefined() - }) - - it('should return the current action context when no startTime', () => { - const { lifeCycle } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime, id: FAKE_ID }) - - expect(parentContexts.findAction()).toBeDefined() - expect(parentContexts.findAction()!.userAction.id).toBe(FAKE_ID) - }) - - it('should return the action context corresponding to startTime', () => { - const { lifeCycle } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 10, id: 'action 1' }) - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, stubActionWithDuration(10)) - - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 30, id: 'action 2' }) - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, stubActionWithDuration(10)) - - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 50, id: 'action 3' }) - - expect(parentContexts.findAction(15)!.userAction.id).toBe('action 1') - expect(parentContexts.findAction(20)!.userAction.id).toBe('action 1') - expect(parentContexts.findAction(30)!.userAction.id).toBe('action 2') - expect(parentContexts.findAction(55)!.userAction.id).toBe('action 3') - }) - - it('should return undefined if no action context corresponding to startTime', () => { - const { lifeCycle } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 10, id: 'action 1' }) - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_DISCARDED) - - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 20, id: 'action 2' }) - - expect(parentContexts.findAction(10)).toBeUndefined() - }) - - it('should clear the current action on ACTION_DISCARDED', () => { - const { lifeCycle } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime, id: FAKE_ID }) - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_DISCARDED) - - expect(parentContexts.findAction()).toBeUndefined() - }) - - it('should clear the current action on ACTION_COMPLETED', () => { - const { lifeCycle } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime, id: FAKE_ID }) - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, stubActionWithDuration(10)) - - expect(parentContexts.findAction()).toBeUndefined() - }) - }) - - describe('history contexts', () => { - it('should be cleared on SESSION_RENEWED', () => { - const { lifeCycle } = setupBuilder.build() - - lifeCycle.notify( - LifeCycleEventType.VIEW_CREATED, - buildViewCreatedEvent({ - id: 'view 1', - startTime: 10, - }) - ) - lifeCycle.notify( - LifeCycleEventType.VIEW_CREATED, - buildViewCreatedEvent({ - id: 'view 2', - startTime: 20, - }) - ) - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 10, id: 'action 1' }) - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, stubActionWithDuration(10)) - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 20, id: 'action 2' }) - - expect(parentContexts.findView(15)).toBeDefined() - expect(parentContexts.findAction(15)).toBeDefined() - expect(parentContexts.findView(25)).toBeDefined() - expect(parentContexts.findAction(25)).toBeDefined() - - lifeCycle.notify(LifeCycleEventType.SESSION_RENEWED) - - expect(parentContexts.findView(15)).toBeUndefined() - expect(parentContexts.findAction(15)).toBeUndefined() - expect(parentContexts.findView(25)).toBeUndefined() - expect(parentContexts.findAction(25)).toBeUndefined() - }) - - it('should be cleared when too old', () => { - const { lifeCycle, clock } = setupBuilder.withFakeClock().build() - - const originalTime = performance.now() - const targetTime = originalTime + 5 - - lifeCycle.notify( - LifeCycleEventType.VIEW_CREATED, - buildViewCreatedEvent({ - id: 'view 1', - startTime: originalTime, - }) - ) - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: originalTime, id: 'action 1' }) - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, stubActionWithDuration(10)) - lifeCycle.notify( - LifeCycleEventType.VIEW_CREATED, - buildViewCreatedEvent({ startTime: originalTime + 10, id: 'view 2' }) - ) - - clock.tick(10) - expect(parentContexts.findView(targetTime)).toBeDefined() - expect(parentContexts.findAction(targetTime)).toBeDefined() - - clock.tick(ACTION_CONTEXT_TIME_OUT_DELAY + CLEAR_OLD_CONTEXTS_INTERVAL) - expect(parentContexts.findView(targetTime)).toBeDefined() - expect(parentContexts.findAction(targetTime)).toBeUndefined() - - clock.tick(VIEW_CONTEXT_TIME_OUT_DELAY + CLEAR_OLD_CONTEXTS_INTERVAL) - expect(parentContexts.findView(targetTime)).toBeUndefined() - expect(parentContexts.findAction(targetTime)).toBeUndefined() - }) - }) -}) - describe('parentContexts v2', () => { const FAKE_ID = 'fake' const startTime = 10 diff --git a/packages/rum/src/domain/parentContexts.ts b/packages/rum/src/domain/parentContexts.ts index dc27af46d8..48211d0bd1 100644 --- a/packages/rum/src/domain/parentContexts.ts +++ b/packages/rum/src/domain/parentContexts.ts @@ -1,5 +1,4 @@ import { monitor, ONE_MINUTE, SESSION_TIME_OUT_DELAY } from '@datadog/browser-core' -import { ActionContext, ViewContext } from '../types' import { ActionContextV2, ViewContextV2 } from '../typesV2' import { LifeCycle, LifeCycleEventType } from './lifeCycle' import { AutoAction, AutoActionCreatedEvent } from './rumEventsCollection/action/trackActions' @@ -17,9 +16,7 @@ interface PreviousContext { } export interface ParentContexts { - findAction: (startTime?: number) => ActionContext | undefined findActionV2: (startTime?: number) => ActionContextV2 | undefined - findView: (startTime?: number) => ViewContext | undefined findViewV2: (startTime?: number) => ViewContextV2 | undefined stop: () => void } @@ -133,31 +130,10 @@ export function startParentContexts(lifeCycle: LifeCycle, session: RumSession): return undefined } - const parentContexts: ParentContexts = { - findAction: (startTime) => { - const actionContext = parentContexts.findActionV2(startTime) - if (!actionContext) { - return - } - return { - userAction: { - id: actionContext.action.id, - }, - } - }, + return { findActionV2: (startTime) => { return findContext(buildCurrentActionContext, previousActions, currentAction, startTime) }, - findView: (startTime) => { - const viewContext = parentContexts.findViewV2(startTime) - if (!viewContext) { - return - } - return { - sessionId: viewContext.session.id, - view: viewContext.view, - } - }, findViewV2: (startTime) => { return findContext(buildCurrentViewContext, previousViews, currentView, startTime) }, @@ -165,5 +141,4 @@ export function startParentContexts(lifeCycle: LifeCycle, session: RumSession): window.clearInterval(clearOldContextsInterval) }, } - return parentContexts } diff --git a/packages/rum/src/domain/rumEventsCollection/action/actionCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/action/actionCollection.spec.ts index 07aaa4d25f..6fb64b181b 100644 --- a/packages/rum/src/domain/rumEventsCollection/action/actionCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/action/actionCollection.spec.ts @@ -1,84 +1,9 @@ import { setup, TestSetupBuilder } from '../../../../test/specHelper' -import { RumEventCategory } from '../../../types' import { RumEventType } from '../../../typesV2' import { LifeCycleEventType } from '../../lifeCycle' import { startActionCollection } from './actionCollection' import { ActionType } from './trackActions' -describe('actionCollection', () => { - let setupBuilder: TestSetupBuilder - let addAction: ReturnType['addAction'] - - beforeEach(() => { - setupBuilder = setup() - .withConfiguration({ - isEnabled: () => false, - }) - .beforeBuild(({ lifeCycle, configuration }) => { - ;({ addAction } = startActionCollection(lifeCycle, configuration)) - }) - }) - - afterEach(() => { - setupBuilder.cleanup() - }) - it('should create action from auto action', () => { - const { lifeCycle, rawRumEvents } = setupBuilder.build() - lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, { - counts: { - errorCount: 10, - longTaskCount: 10, - resourceCount: 10, - }, - duration: 100, - id: 'xxx', - name: 'foo', - startTime: 1234, - type: ActionType.CLICK, - }) - - expect(rawRumEvents[0].startTime).toBe(1234) - expect(rawRumEvents[0].rawRumEvent).toEqual({ - date: jasmine.any(Number), - duration: 100 * 1e6, - evt: { - category: RumEventCategory.USER_ACTION, - name: 'foo', - }, - userAction: { - id: 'xxx', - measures: { - errorCount: 10, - longTaskCount: 10, - resourceCount: 10, - }, - type: ActionType.CLICK, - }, - }) - }) - - it('should create action from custom action', () => { - const { rawRumEvents } = setupBuilder.build() - addAction({ - name: 'foo', - startTime: 1234, - type: ActionType.CUSTOM, - }) - - expect(rawRumEvents[0].startTime).toBe(1234) - expect(rawRumEvents[0].rawRumEvent).toEqual({ - date: jasmine.any(Number), - evt: { - category: RumEventCategory.USER_ACTION, - name: 'foo', - }, - userAction: { - type: ActionType.CUSTOM, - }, - }) - }) -}) - describe('actionCollection v2', () => { let setupBuilder: TestSetupBuilder let addAction: ReturnType['addAction'] diff --git a/packages/rum/src/domain/rumEventsCollection/action/actionCollection.ts b/packages/rum/src/domain/rumEventsCollection/action/actionCollection.ts index 129347f244..a83a9ebc26 100644 --- a/packages/rum/src/domain/rumEventsCollection/action/actionCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/action/actionCollection.ts @@ -1,15 +1,12 @@ import { combine, Configuration, Context, getTimestamp, msToNs } from '@datadog/browser-core' -import { RumEventCategory, RumUserActionEvent } from '../../../types' import { RumActionEventV2, RumEventType } from '../../../typesV2' import { LifeCycle, LifeCycleEventType } from '../../lifeCycle' import { ActionType, AutoAction, CustomAction, trackActions } from './trackActions' export function startActionCollection(lifeCycle: LifeCycle, configuration: Configuration) { - lifeCycle.subscribe(LifeCycleEventType.AUTO_ACTION_COMPLETED, (action) => { - configuration.isEnabled('v2_format') - ? lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processActionV2(action)) - : lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processAction(action)) - }) + lifeCycle.subscribe(LifeCycleEventType.AUTO_ACTION_COMPLETED, (action) => + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processActionV2(action)) + ) if (configuration.trackInteractions) { trackActions(lifeCycle) @@ -17,50 +14,14 @@ export function startActionCollection(lifeCycle: LifeCycle, configuration: Confi return { addAction(action: CustomAction, savedGlobalContext?: Context) { - configuration.isEnabled('v2_format') - ? lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { - savedGlobalContext, - ...processActionV2(action), - }) - : lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { - savedGlobalContext, - ...processAction(action), - }) + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + savedGlobalContext, + ...processActionV2(action), + }) }, } } -function processAction(action: AutoAction | CustomAction) { - const autoActionProperties = isAutoAction(action) - ? { - duration: msToNs(action.duration), - userAction: { - id: action.id, - measures: action.counts, - }, - } - : undefined - const customerContext = !isAutoAction(action) ? action.context : undefined - const actionEvent: RumUserActionEvent = combine( - { - date: getTimestamp(action.startTime), - evt: { - category: RumEventCategory.USER_ACTION as const, - name: action.name, - }, - userAction: { - type: action.type, - }, - }, - autoActionProperties - ) - return { - customerContext, - rawRumEvent: actionEvent, - startTime: action.startTime, - } -} - function processActionV2(action: AutoAction | CustomAction) { const autoActionProperties = isAutoAction(action) ? { diff --git a/packages/rum/src/domain/rumEventsCollection/action/trackActions.spec.ts b/packages/rum/src/domain/rumEventsCollection/action/trackActions.spec.ts index 8d5b793d31..c19cd13820 100644 --- a/packages/rum/src/domain/rumEventsCollection/action/trackActions.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/action/trackActions.spec.ts @@ -1,7 +1,6 @@ import { DOM_EVENT } from '@datadog/browser-core' import { createRawRumEvent } from '../../../../test/fixtures' import { setup, TestSetupBuilder } from '../../../../test/specHelper' -import { RumErrorEvent, RumEventCategory } from '../../../types' import { RumEventType } from '../../../typesV2' import { LifeCycle, LifeCycleEventType } from '../../lifeCycle' import { PAGE_ACTIVITY_MAX_DURATION, PAGE_ACTIVITY_VALIDATION_DELAY } from '../../trackPageActivities' @@ -175,33 +174,6 @@ describe('newAction', () => { expect(events[0].name).toBe('test-1') }) - it('counts errors occurring during the action', () => { - const { lifeCycle, clock } = setupBuilder.build() - const collectedRawRumEvent = { - rawRumEvent: ({ evt: { category: RumEventCategory.ERROR } } as unknown) as RumErrorEvent, - startTime: 0, - } - lifeCycle.subscribe(LifeCycleEventType.AUTO_ACTION_COMPLETED, pushEvent) - - newClick('test-1') - - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, collectedRawRumEvent) - clock.tick(BEFORE_PAGE_ACTIVITY_VALIDATION_DELAY) - lifeCycle.notify(LifeCycleEventType.DOM_MUTATED) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, collectedRawRumEvent) - - clock.tick(EXPIRE_DELAY) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, collectedRawRumEvent) - - expect(events.length).toBe(1) - const action = events[0] as AutoAction - expect(action.counts).toEqual({ - errorCount: 2, - longTaskCount: 0, - resourceCount: 0, - }) - }) - it('counts errors occurring during the action v2', () => { const { lifeCycle, clock } = setupBuilder.build() const collectedRawRumEvent = { diff --git a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts index 8d4d43223e..38e02d830b 100644 --- a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts @@ -1,124 +1,9 @@ import { Observable, RawError } from '@datadog/browser-core' import { setup, TestSetupBuilder } from '../../../../test/specHelper' -import { ErrorSource, RumEventCategory } from '../../../index' +import { ErrorSource } from '../../../index' import { RumEventType } from '../../../typesV2' import { doStartErrorCollection } from './errorCollection' -describe('error collection', () => { - let setupBuilder: TestSetupBuilder - const errorObservable = new Observable() - let addError: ReturnType['addError'] - - beforeEach(() => { - setupBuilder = setup() - .withConfiguration({ - isEnabled: () => false, - }) - .beforeBuild(({ lifeCycle, configuration }) => { - ;({ addError } = doStartErrorCollection(lifeCycle, configuration, errorObservable)) - }) - }) - - afterEach(() => { - setupBuilder.cleanup() - }) - - describe('provided', () => { - it('notifies a raw rum error event', () => { - const { rawRumEvents } = setupBuilder.build() - addError({ - error: new Error('foo'), - source: ErrorSource.CUSTOM, - startTime: 12, - }) - - expect(rawRumEvents.length).toBe(1) - expect(rawRumEvents[0]).toEqual({ - customerContext: undefined, - rawRumEvent: { - date: jasmine.any(Number), - error: { - kind: 'Error', - origin: ErrorSource.CUSTOM, - stack: jasmine.stringMatching('Error: foo'), - }, - evt: { - category: RumEventCategory.ERROR, - }, - message: 'foo', - }, - savedGlobalContext: undefined, - startTime: 12, - }) - }) - - it('should save the specified customer context', () => { - const { rawRumEvents } = setupBuilder.build() - addError({ - context: { foo: 'bar' }, - error: new Error('foo'), - source: ErrorSource.CUSTOM, - startTime: 12, - }) - expect(rawRumEvents[0].customerContext).toEqual({ - foo: 'bar', - }) - }) - - it('should save the global context', () => { - const { rawRumEvents } = setupBuilder.build() - addError( - { - error: new Error('foo'), - source: ErrorSource.CUSTOM, - startTime: 12, - }, - { foo: 'bar' } - ) - expect(rawRumEvents[0].savedGlobalContext).toEqual({ - foo: 'bar', - }) - }) - }) - - describe('auto', () => { - it('should create error event from collected error', () => { - const { rawRumEvents } = setupBuilder.build() - errorObservable.notify({ - message: 'hello', - resource: { - method: 'GET', - statusCode: 500, - url: 'url', - }, - source: ErrorSource.NETWORK, - stack: 'bar', - startTime: 1234, - type: 'foo', - }) - - expect(rawRumEvents[0].startTime).toBe(1234) - expect(rawRumEvents[0].rawRumEvent).toEqual({ - date: jasmine.any(Number), - error: { - kind: 'foo', - origin: ErrorSource.NETWORK, - stack: 'bar', - }, - evt: { - category: RumEventCategory.ERROR, - }, - http: { - method: 'GET', - status_code: 500, - url: 'url', - }, - message: 'hello', - }) - }) - }) -}) - describe('error collection v2', () => { let setupBuilder: TestSetupBuilder const errorObservable = new Observable() diff --git a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts index c78b5ebca9..5686ddf87c 100644 --- a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts @@ -1,5 +1,4 @@ import { - combine, computeStackTrace, Configuration, Context, @@ -10,7 +9,6 @@ import { RawError, startAutomaticErrorCollection, } from '@datadog/browser-core' -import { RumErrorEvent, RumEventCategory } from '../../../types' import { RumErrorEventV2, RumEventType } from '../../../typesV2' import { LifeCycle, LifeCycleEventType } from '../../lifeCycle' @@ -30,27 +28,18 @@ export function doStartErrorCollection( configuration: Configuration, observable: Observable ) { - observable.subscribe((error) => { - configuration.isEnabled('v2_format') - ? lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processErrorV2(error)) - : lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processError(error)) - }) + observable.subscribe((error) => + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processErrorV2(error)) + ) return { addError({ error, startTime, context: customerContext, source }: ProvidedError, savedGlobalContext?: Context) { const rawError = computeRawError(error, startTime, source) - - configuration.isEnabled('v2_format') - ? lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { - customerContext, - savedGlobalContext, - ...processErrorV2(rawError), - }) - : lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { - customerContext, - savedGlobalContext, - ...processError(rawError), - }) + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + customerContext, + savedGlobalContext, + ...processErrorV2(rawError), + }) }, } } @@ -60,36 +49,6 @@ function computeRawError(error: unknown, startTime: number, source: ErrorSource) return { startTime, source, ...formatUnknownError(stackTrace, error, 'Provided') } } -function processError(error: RawError) { - const rawRumEvent: RumErrorEvent = combine( - { - date: getTimestamp(error.startTime), - error: { - kind: error.type, - origin: error.source, - stack: error.stack, - }, - evt: { - category: RumEventCategory.ERROR as const, - }, - message: error.message, - }, - error.resource - ? { - http: { - method: error.resource.method, - status_code: error.resource.statusCode, - url: error.resource.url, - }, - } - : undefined - ) - return { - rawRumEvent, - startTime: error.startTime, - } -} - function processErrorV2(error: RawError) { const rawRumEvent: RumErrorEventV2 = { date: getTimestamp(error.startTime), diff --git a/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.spec.ts index a45a381afe..7178db5707 100644 --- a/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.spec.ts @@ -1,59 +1,9 @@ import { setup, TestSetupBuilder } from '../../../../test/specHelper' import { RumPerformanceEntry } from '../../../browser/performanceCollection' -import { RumEventCategory } from '../../../index' import { RumEventType } from '../../../typesV2' import { LifeCycleEventType } from '../../lifeCycle' import { startLongTaskCollection } from './longTaskCollection' -describe('long task collection', () => { - let setupBuilder: TestSetupBuilder - - beforeEach(() => { - setupBuilder = setup() - .withConfiguration({ - isEnabled: () => false, - }) - .beforeBuild(({ lifeCycle, configuration }) => { - startLongTaskCollection(lifeCycle, configuration) - }) - }) - - afterEach(() => { - setupBuilder.cleanup() - }) - - it('should only listen to long task performance entry', () => { - const { lifeCycle, rawRumEvents } = setupBuilder.build() - ;[ - { duration: 100, entryType: 'longtask', startTime: 1234 }, - { duration: 100, entryType: 'navigation', startTime: 1234 }, - { duration: 100, entryType: 'resource', startTime: 1234 }, - { duration: 100, entryType: 'paint', startTime: 1234 }, - ].forEach((entry) => { - lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, entry as RumPerformanceEntry) - }) - expect(rawRumEvents.length).toBe(1) - }) - - it('should create raw rum event from performance entry', () => { - const { lifeCycle, rawRumEvents } = setupBuilder.build() - lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, { - duration: 100, - entryType: 'longtask', - startTime: 1234, - }) - - expect(rawRumEvents[0].startTime).toBe(1234) - expect(rawRumEvents[0].rawRumEvent).toEqual({ - date: jasmine.any(Number), - duration: 100 * 1e6, - evt: { - category: RumEventCategory.LONG_TASK, - }, - }) - }) -}) - describe('long task collection v2', () => { let setupBuilder: TestSetupBuilder diff --git a/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.ts b/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.ts index 402c279566..1d93551311 100644 --- a/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.ts @@ -1,5 +1,4 @@ import { Configuration, getTimestamp, msToNs } from '@datadog/browser-core' -import { RumEventCategory, RumLongTaskEvent } from '../../../types' import { RumEventType, RumLongTaskEventV2 } from '../../../typesV2' import { LifeCycle, LifeCycleEventType } from '../../lifeCycle' @@ -8,30 +7,16 @@ export function startLongTaskCollection(lifeCycle: LifeCycle, configuration: Con if (entry.entryType !== 'longtask') { return } - if (configuration.isEnabled('v2_format')) { - const rawRumEvent: RumLongTaskEventV2 = { - date: getTimestamp(entry.startTime), - longTask: { - duration: msToNs(entry.duration), - }, - type: RumEventType.LONG_TASK, - } - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { - rawRumEvent, - startTime: entry.startTime, - }) - } else { - const rawRumEvent: RumLongTaskEvent = { - date: getTimestamp(entry.startTime), + const rawRumEvent: RumLongTaskEventV2 = { + date: getTimestamp(entry.startTime), + longTask: { duration: msToNs(entry.duration), - evt: { - category: RumEventCategory.LONG_TASK, - }, - } - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { - rawRumEvent, - startTime: entry.startTime, - }) + }, + type: RumEventType.LONG_TASK, } + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + rawRumEvent, + startTime: entry.startTime, + }) }) } diff --git a/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.spec.ts index f18ac54850..433485847c 100644 --- a/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.spec.ts @@ -1,236 +1,12 @@ import { RequestType, ResourceType } from '@datadog/browser-core' import { createResourceEntry } from '../../../../test/fixtures' import { setup, TestSetupBuilder } from '../../../../test/specHelper' -import { RumEventCategory, RumResourceEvent } from '../../../types' import { RumEventType, RumResourceEventV2 } from '../../../typesV2' import { LifeCycleEventType } from '../../lifeCycle' import { RequestCompleteEvent } from '../../requestCollection' -import { RumSession } from '../../rumSession' import { TraceIdentifier } from '../../tracing/tracer' import { startResourceCollection } from './resourceCollection' -describe('resourceCollection', () => { - let setupBuilder: TestSetupBuilder - - describe('when resource tracking is enabled', () => { - beforeEach(() => { - setupBuilder = setup() - .withSession({ - getId: () => '1234', - isTracked: () => true, - isTrackedWithResource: () => true, - }) - .withConfiguration({ - isEnabled: () => false, - }) - .beforeBuild(({ lifeCycle, configuration, session }) => { - startResourceCollection(lifeCycle, configuration, session) - }) - }) - - afterEach(() => { - setupBuilder.cleanup() - }) - - it('should create resource from performance entry', () => { - const { lifeCycle, rawRumEvents } = setupBuilder.build() - lifeCycle.notify( - LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, - createResourceEntry({ - duration: 100, - name: 'https://resource.com/valid', - startTime: 1234, - }) - ) - - expect(rawRumEvents[0].startTime).toBe(1234) - expect(rawRumEvents[0].rawRumEvent).toEqual({ - date: (jasmine.any(Number) as unknown) as number, - duration: 100 * 1e6, - evt: { - category: RumEventCategory.RESOURCE, - }, - http: { - performance: undefined, - url: 'https://resource.com/valid', - }, - network: { - bytesWritten: undefined, - }, - resource: { - kind: ResourceType.OTHER, - }, - }) - }) - - it('should create resource from completed request', () => { - const { lifeCycle, rawRumEvents } = setupBuilder.build() - lifeCycle.notify( - LifeCycleEventType.REQUEST_COMPLETED, - createCompletedRequest({ - duration: 100, - method: 'GET', - startTime: 1234, - status: 200, - type: RequestType.XHR, - url: 'https://resource.com/valid', - }) - ) - - expect(rawRumEvents[0].startTime).toBe(1234) - expect(rawRumEvents[0].rawRumEvent).toEqual({ - date: (jasmine.any(Number) as unknown) as number, - duration: 100 * 1e6, - evt: { - category: RumEventCategory.RESOURCE, - }, - http: { - method: 'GET', - statusCode: 200, - url: 'https://resource.com/valid', - }, - resource: { - kind: ResourceType.XHR, - }, - }) - }) - }) - - describe('when resource tracking is disabled', () => { - beforeEach(() => { - setupBuilder = setup() - .withSession({ - getId: () => '1234', - isTracked: () => true, - isTrackedWithResource: () => false, - }) - .withConfiguration({ - isEnabled: () => false, - }) - .beforeBuild(({ lifeCycle, configuration, session }) => { - startResourceCollection(lifeCycle, configuration, session) - }) - }) - - afterEach(() => { - setupBuilder.cleanup() - }) - - it('should not create resource from performance entry', () => { - const { lifeCycle, rawRumEvents } = setupBuilder.build() - lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, createResourceEntry()) - - expect(rawRumEvents.length).toBe(0) - }) - - it('should not create resource from completed request', () => { - const { lifeCycle, rawRumEvents } = setupBuilder.build() - lifeCycle.notify(LifeCycleEventType.REQUEST_COMPLETED, createCompletedRequest()) - - expect(rawRumEvents.length).toBe(0) - }) - }) - - describe('when resource tracking change', () => { - let isTrackedWithResource = true - - beforeEach(() => { - setupBuilder = setup() - .withSession({ - getId: () => '1234', - isTracked: () => true, - isTrackedWithResource: () => isTrackedWithResource, - }) - .withConfiguration({ - isEnabled: () => false, - }) - .beforeBuild(({ lifeCycle, configuration, session }) => { - startResourceCollection(lifeCycle, configuration, session) - }) - }) - - afterEach(() => { - setupBuilder.cleanup() - }) - - it('should enable/disable resource creation from performance entry', () => { - const { lifeCycle, rawRumEvents } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, createResourceEntry()) - expect(rawRumEvents.length).toBe(1) - - isTrackedWithResource = false - lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, createResourceEntry()) - expect(rawRumEvents.length).toBe(1) - - isTrackedWithResource = true - lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, createResourceEntry()) - expect(rawRumEvents.length).toBe(2) - }) - - it('should enable/disable resource creation from completed request', () => { - const { lifeCycle, rawRumEvents } = setupBuilder.build() - - lifeCycle.notify(LifeCycleEventType.REQUEST_COMPLETED, createCompletedRequest()) - expect(rawRumEvents.length).toBe(1) - - isTrackedWithResource = false - lifeCycle.notify(LifeCycleEventType.REQUEST_COMPLETED, createCompletedRequest()) - expect(rawRumEvents.length).toBe(1) - - isTrackedWithResource = true - lifeCycle.notify(LifeCycleEventType.REQUEST_COMPLETED, createCompletedRequest()) - expect(rawRumEvents.length).toBe(2) - }) - }) - - describe('tracing info', () => { - beforeEach(() => { - setupBuilder = setup() - .withConfiguration({ - isEnabled: () => false, - }) - .beforeBuild(({ lifeCycle, configuration, session }) => { - startResourceCollection(lifeCycle, configuration, session) - }) - }) - - afterEach(() => { - setupBuilder.cleanup() - }) - - it('should be processed from traced initial document', () => { - const { lifeCycle, rawRumEvents } = setupBuilder.build() - lifeCycle.notify( - LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, - createResourceEntry({ - traceId: 'xxx', - }) - ) - - const traceInfo = (rawRumEvents[0].rawRumEvent as RumResourceEvent)._dd! - expect(traceInfo).toBeDefined() - expect(traceInfo.traceId).toBe('xxx') - }) - - it('should be processed from completed request', () => { - const { lifeCycle, rawRumEvents } = setupBuilder.build() - lifeCycle.notify( - LifeCycleEventType.REQUEST_COMPLETED, - createCompletedRequest({ - spanId: new TraceIdentifier(), - traceId: new TraceIdentifier(), - }) - ) - - const traceInfo = (rawRumEvents[0].rawRumEvent as RumResourceEvent)._dd! - expect(traceInfo).toBeDefined() - expect(traceInfo.traceId).toBeDefined() - expect(traceInfo.spanId).toBeDefined() - }) - }) -}) - describe('resourceCollection V2', () => { let setupBuilder: TestSetupBuilder diff --git a/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.ts b/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.ts index 08fb90cb37..2c3df21d1f 100644 --- a/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.ts @@ -8,7 +8,6 @@ import { ResourceType, } from '@datadog/browser-core' import { RumPerformanceResourceTiming } from '../../../browser/performanceCollection' -import { RumEventCategory, RumResourceEvent } from '../../../types' import { RumEventType, RumResourceEventV2 } from '../../../typesV2' import { LifeCycle, LifeCycleEventType } from '../../lifeCycle' import { RequestCompleteEvent } from '../../requestCollection' @@ -25,52 +24,17 @@ import { export function startResourceCollection(lifeCycle: LifeCycle, configuration: Configuration, session: RumSession) { lifeCycle.subscribe(LifeCycleEventType.REQUEST_COMPLETED, (request: RequestCompleteEvent) => { if (session.isTrackedWithResource()) { - configuration.isEnabled('v2_format') - ? lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processRequestV2(request)) - : lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processRequest(request)) + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processRequestV2(request)) } }) lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, (entry) => { if (session.isTrackedWithResource() && entry.entryType === 'resource' && !isRequestKind(entry)) { - configuration.isEnabled('v2_format') - ? lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processResourceEntryV2(entry)) - : lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processResourceEntry(entry)) + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processResourceEntryV2(entry)) } }) } -function processRequest(request: RequestCompleteEvent) { - const kind = request.type === RequestType.XHR ? ResourceType.XHR : ResourceType.FETCH - - const matchingTiming = matchRequestTiming(request) - const startTime = matchingTiming ? matchingTiming.startTime : request.startTime - const correspondingTimingOverrides = matchingTiming ? computePerformanceEntryMetrics(matchingTiming) : undefined - - const tracingInfo = computeRequestTracingInfo(request) - - const resourceEvent: RumResourceEvent = combine( - { - date: getTimestamp(startTime), - duration: msToNs(request.duration), - evt: { - category: RumEventCategory.RESOURCE as const, - }, - http: { - method: request.method, - statusCode: request.status, - url: request.url, - }, - resource: { - kind, - }, - }, - tracingInfo, - correspondingTimingOverrides - ) - return { startTime, rawRumEvent: resourceEvent } -} - function processRequestV2(request: RequestCompleteEvent) { const type = request.type === RequestType.XHR ? ResourceType.XHR : ResourceType.FETCH @@ -98,30 +62,6 @@ function processRequestV2(request: RequestCompleteEvent) { return { startTime, rawRumEvent: resourceEvent as RumResourceEventV2 } } -function processResourceEntry(entry: RumPerformanceResourceTiming) { - const resourceKind = computeResourceKind(entry) - const entryMetrics = computePerformanceEntryMetrics(entry) - const tracingInfo = computeEntryTracingInfo(entry) - - const resourceEvent: RumResourceEvent = combine( - { - date: getTimestamp(entry.startTime), - evt: { - category: RumEventCategory.RESOURCE as const, - }, - http: { - url: entry.name, - }, - resource: { - kind: resourceKind, - }, - }, - tracingInfo, - entryMetrics - ) - return { startTime: entry.startTime, rawRumEvent: resourceEvent } -} - function processResourceEntryV2(entry: RumPerformanceResourceTiming) { const type = computeResourceKind(entry) const entryMetrics = computePerformanceEntryMetricsV2(entry) @@ -142,18 +82,6 @@ function processResourceEntryV2(entry: RumPerformanceResourceTiming) { return { startTime: entry.startTime, rawRumEvent: resourceEvent as RumResourceEventV2 } } -function computePerformanceEntryMetrics(timing: RumPerformanceResourceTiming) { - return { - duration: computePerformanceResourceDuration(timing), - http: { - performance: computePerformanceResourceDetails(timing), - }, - network: { - bytesWritten: computeSize(timing), - }, - } -} - function computePerformanceEntryMetricsV2(timing: RumPerformanceResourceTiming) { return { resource: { diff --git a/packages/rum/src/domain/rumEventsCollection/view/trackViews.spec.ts b/packages/rum/src/domain/rumEventsCollection/view/trackViews.spec.ts index 540496d435..8ffbbd4b31 100644 --- a/packages/rum/src/domain/rumEventsCollection/view/trackViews.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/view/trackViews.spec.ts @@ -5,7 +5,6 @@ import { RumPerformanceNavigationTiming, RumPerformancePaintTiming, } from '../../../browser/performanceCollection' -import { RawRumEvent, RumEventCategory } from '../../../types' import { RumEventType } from '../../../typesV2' import { LifeCycleEventType } from '../../lifeCycle' import { @@ -579,163 +578,6 @@ describe('rum view measures', () => { }) }) - describe('event counts', () => { - function createFakeCollectedRawRumEvent(category: RumEventCategory) { - return { - rawRumEvent: ({ evt: { category } } as unknown) as RawRumEvent, - startTime: 0, - } - } - - it('should track error count', () => { - const { lifeCycle } = setupBuilder.build() - expect(getHandledCount()).toEqual(1) - expect(getViewEvent(0).eventCounts.errorCount).toEqual(0) - - lifeCycle.notify( - LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - createFakeCollectedRawRumEvent(RumEventCategory.ERROR) - ) - lifeCycle.notify( - LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - createFakeCollectedRawRumEvent(RumEventCategory.ERROR) - ) - history.pushState({}, '', '/bar') - - expect(getHandledCount()).toEqual(3) - expect(getViewEvent(1).eventCounts.errorCount).toEqual(2) - expect(getViewEvent(2).eventCounts.errorCount).toEqual(0) - }) - - it('should track long task count', () => { - const { lifeCycle } = setupBuilder.build() - expect(getHandledCount()).toEqual(1) - expect(getViewEvent(0).eventCounts.longTaskCount).toEqual(0) - - lifeCycle.notify( - LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - createFakeCollectedRawRumEvent(RumEventCategory.LONG_TASK) - ) - history.pushState({}, '', '/bar') - - expect(getHandledCount()).toEqual(3) - expect(getViewEvent(1).eventCounts.longTaskCount).toEqual(1) - expect(getViewEvent(2).eventCounts.longTaskCount).toEqual(0) - }) - - it('should track resource count', () => { - const { lifeCycle } = setupBuilder.build() - expect(getHandledCount()).toEqual(1) - expect(getViewEvent(0).eventCounts.resourceCount).toEqual(0) - - lifeCycle.notify( - LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - createFakeCollectedRawRumEvent(RumEventCategory.RESOURCE) - ) - history.pushState({}, '', '/bar') - - expect(getHandledCount()).toEqual(3) - expect(getViewEvent(1).eventCounts.resourceCount).toEqual(1) - expect(getViewEvent(2).eventCounts.resourceCount).toEqual(0) - }) - - it('should track action count', () => { - const { lifeCycle } = setupBuilder.build() - expect(getHandledCount()).toEqual(1) - expect(getViewEvent(0).eventCounts.userActionCount).toEqual(0) - - lifeCycle.notify( - LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - createFakeCollectedRawRumEvent(RumEventCategory.USER_ACTION) - ) - history.pushState({}, '', '/bar') - - expect(getHandledCount()).toEqual(3) - expect(getViewEvent(1).eventCounts.userActionCount).toEqual(1) - expect(getViewEvent(2).eventCounts.userActionCount).toEqual(0) - }) - - it('should reset event count when the view changes', () => { - const { lifeCycle } = setupBuilder.build() - expect(getHandledCount()).toEqual(1) - expect(getViewEvent(0).eventCounts.resourceCount).toEqual(0) - - lifeCycle.notify( - LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - createFakeCollectedRawRumEvent(RumEventCategory.RESOURCE) - ) - history.pushState({}, '', '/bar') - - expect(getHandledCount()).toEqual(3) - expect(getViewEvent(1).eventCounts.resourceCount).toEqual(1) - expect(getViewEvent(2).eventCounts.resourceCount).toEqual(0) - - lifeCycle.notify( - LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - createFakeCollectedRawRumEvent(RumEventCategory.RESOURCE) - ) - lifeCycle.notify( - LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - createFakeCollectedRawRumEvent(RumEventCategory.RESOURCE) - ) - history.pushState({}, '', '/baz') - - expect(getHandledCount()).toEqual(5) - expect(getViewEvent(3).eventCounts.resourceCount).toEqual(2) - expect(getViewEvent(4).eventCounts.resourceCount).toEqual(0) - }) - - it('should update eventCounts when a resource event is collected (throttled)', () => { - const { lifeCycle, clock } = setupBuilder.withFakeClock().build() - expect(getHandledCount()).toEqual(1) - expect(getViewEvent(0).eventCounts).toEqual({ - errorCount: 0, - longTaskCount: 0, - resourceCount: 0, - userActionCount: 0, - }) - - lifeCycle.notify( - LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - createFakeCollectedRawRumEvent(RumEventCategory.RESOURCE) - ) - - expect(getHandledCount()).toEqual(1) - - clock.tick(THROTTLE_VIEW_UPDATE_PERIOD) - - expect(getHandledCount()).toEqual(2) - expect(getViewEvent(1).eventCounts).toEqual({ - errorCount: 0, - longTaskCount: 0, - resourceCount: 1, - userActionCount: 0, - }) - }) - - it('should not update eventCounts after ending a view', () => { - const { lifeCycle, clock } = setupBuilder.withFakeClock().build() - expect(getHandledCount()).toEqual(1) - - lifeCycle.notify( - LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, - createFakeCollectedRawRumEvent(RumEventCategory.RESOURCE) - ) - - expect(getHandledCount()).toEqual(1) - - history.pushState({}, '', '/bar') - - expect(getHandledCount()).toEqual(3) - expect(getViewEvent(1).id).toEqual(getViewEvent(0).id) - expect(getViewEvent(2).id).not.toEqual(getViewEvent(0).id) - - clock.tick(THROTTLE_VIEW_UPDATE_PERIOD) - - expect(getHandledCount()).toEqual(3) - }) - }) - describe('event counts V2', () => { it('should track error count', () => { const { lifeCycle } = setupBuilder.build() diff --git a/packages/rum/src/domain/rumEventsCollection/view/viewCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/view/viewCollection.spec.ts index 53c149322b..2addffffdd 100644 --- a/packages/rum/src/domain/rumEventsCollection/view/viewCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/view/viewCollection.spec.ts @@ -1,84 +1,9 @@ import { setup, TestSetupBuilder } from '../../../../test/specHelper' -import { RumEventCategory } from '../../../types' import { RumEventType } from '../../../typesV2' import { LifeCycleEventType } from '../../lifeCycle' import { View, ViewLoadingType } from './trackViews' import { startViewCollection } from './viewCollection' -describe('viewCollection', () => { - let setupBuilder: TestSetupBuilder - - beforeEach(() => { - setupBuilder = setup() - .withConfiguration({ - isEnabled: () => false, - }) - .beforeBuild(({ lifeCycle, configuration }) => { - startViewCollection(lifeCycle, configuration, location) - }) - }) - - afterEach(() => { - setupBuilder.cleanup() - }) - - it('should create view from view update', () => { - const { lifeCycle, rawRumEvents } = setupBuilder.build() - const view = { - documentVersion: 3, - duration: 100, - eventCounts: { - errorCount: 10, - longTaskCount: 10, - resourceCount: 10, - userActionCount: 10, - }, - id: 'xxx', - loadingTime: 20, - loadingType: ViewLoadingType.INITIAL_LOAD, - location: {}, - referrer: '', - startTime: 1234, - timings: { - domComplete: 10, - domContentLoaded: 10, - domInteractive: 10, - firstContentfulPaint: 10, - largestContentfulPaint: 10, - loadEventEnd: 10, - }, - } - lifeCycle.notify(LifeCycleEventType.VIEW_UPDATED, view as View) - - expect(rawRumEvents[rawRumEvents.length - 1].startTime).toBe(1234) - expect(rawRumEvents[rawRumEvents.length - 1].rawRumEvent).toEqual({ - date: jasmine.any(Number), - duration: 100 * 1e6, - evt: { - category: RumEventCategory.VIEW, - }, - rum: { - documentVersion: 3, - }, - view: { - loadingTime: 20 * 1e6, - loadingType: ViewLoadingType.INITIAL_LOAD, - measures: { - domComplete: 10 * 1e6, - domContentLoaded: 10 * 1e6, - domInteractive: 10 * 1e6, - errorCount: 10, - firstContentfulPaint: 10 * 1e6, - loadEventEnd: 10 * 1e6, - longTaskCount: 10, - resourceCount: 10, - userActionCount: 10, - }, - }, - }) - }) -}) - describe('viewCollection V2', () => { let setupBuilder: TestSetupBuilder diff --git a/packages/rum/src/domain/rumEventsCollection/view/viewCollection.ts b/packages/rum/src/domain/rumEventsCollection/view/viewCollection.ts index 484e8e3167..4155af1f39 100644 --- a/packages/rum/src/domain/rumEventsCollection/view/viewCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/view/viewCollection.ts @@ -1,48 +1,16 @@ import { Configuration, getTimestamp, msToNs } from '@datadog/browser-core' -import { RumEventCategory, RumViewEvent } from '../../../types' import { RumEventType, RumViewEventV2 } from '../../../typesV2' import { LifeCycle, LifeCycleEventType } from '../../lifeCycle' import { trackViews, View } from './trackViews' export function startViewCollection(lifeCycle: LifeCycle, configuration: Configuration, location: Location) { - lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, (view) => { - configuration.isEnabled('v2_format') - ? lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processViewUpdateV2(view)) - : lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processViewUpdate(view)) - }) + lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, (view) => + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processViewUpdateV2(view)) + ) return trackViews(location, lifeCycle) } -function processViewUpdate(view: View) { - const viewEvent: RumViewEvent = { - date: getTimestamp(view.startTime), - duration: msToNs(view.duration), - evt: { - category: RumEventCategory.VIEW, - }, - rum: { - documentVersion: view.documentVersion, - }, - view: { - loadingTime: msToNs(view.loadingTime), - loadingType: view.loadingType, - measures: { - ...view.eventCounts, - domComplete: msToNs(view.timings.domComplete), - domContentLoaded: msToNs(view.timings.domContentLoaded), - domInteractive: msToNs(view.timings.domInteractive), - firstContentfulPaint: msToNs(view.timings.firstContentfulPaint), - loadEventEnd: msToNs(view.timings.loadEventEnd), - }, - }, - } - return { - rawRumEvent: viewEvent, - startTime: view.startTime, - } -} - function processViewUpdateV2(view: View) { const viewEvent: RumViewEventV2 = { _dd: { diff --git a/packages/rum/src/domain/trackEventCounts.spec.ts b/packages/rum/src/domain/trackEventCounts.spec.ts index 6b7829b06e..89f789c31e 100644 --- a/packages/rum/src/domain/trackEventCounts.spec.ts +++ b/packages/rum/src/domain/trackEventCounts.spec.ts @@ -1,76 +1,8 @@ import { objectValues } from '@datadog/browser-core' -import { RawRumEvent, RumEventCategory } from '../types' import { RawRumEventV2, RumEventType } from '../typesV2' import { LifeCycle, LifeCycleEventType } from './lifeCycle' import { EventCounts, trackEventCounts } from './trackEventCounts' -describe('trackEventCounts', () => { - let lifeCycle: LifeCycle - - beforeEach(() => { - lifeCycle = new LifeCycle() - }) - - function notifyCollectedRawRumEvent(category: RumEventCategory) { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { - rawRumEvent: ({ evt: { category } } as unknown) as RawRumEvent, - startTime: 0, - }) - } - - it('tracks errors', () => { - const { eventCounts } = trackEventCounts(lifeCycle) - notifyCollectedRawRumEvent(RumEventCategory.ERROR) - expect(eventCounts.errorCount).toBe(1) - }) - - it('tracks long tasks', () => { - const { eventCounts } = trackEventCounts(lifeCycle) - notifyCollectedRawRumEvent(RumEventCategory.LONG_TASK) - expect(eventCounts.longTaskCount).toBe(1) - }) - - it("doesn't track views", () => { - const { eventCounts } = trackEventCounts(lifeCycle) - notifyCollectedRawRumEvent(RumEventCategory.VIEW) - expect(objectValues(eventCounts).every((value) => value === 0)).toBe(true) - }) - - it('tracks actions', () => { - const { eventCounts } = trackEventCounts(lifeCycle) - notifyCollectedRawRumEvent(RumEventCategory.USER_ACTION) - expect(eventCounts.userActionCount).toBe(1) - }) - - it('tracks resources', () => { - const { eventCounts } = trackEventCounts(lifeCycle) - notifyCollectedRawRumEvent(RumEventCategory.RESOURCE) - expect(eventCounts.resourceCount).toBe(1) - }) - - it('stops tracking when stop is called', () => { - const { eventCounts, stop } = trackEventCounts(lifeCycle) - notifyCollectedRawRumEvent(RumEventCategory.RESOURCE) - expect(eventCounts.resourceCount).toBe(1) - stop() - notifyCollectedRawRumEvent(RumEventCategory.RESOURCE) - expect(eventCounts.resourceCount).toBe(1) - }) - - it('invokes a potential callback when a count is increased', () => { - const spy = jasmine.createSpy<(eventCounts: EventCounts) => void>() - trackEventCounts(lifeCycle, spy) - - notifyCollectedRawRumEvent(RumEventCategory.RESOURCE) - expect(spy).toHaveBeenCalledTimes(1) - expect(spy.calls.mostRecent().args[0].resourceCount).toBe(1) - - notifyCollectedRawRumEvent(RumEventCategory.RESOURCE) - expect(spy).toHaveBeenCalledTimes(2) - expect(spy.calls.mostRecent().args[0].resourceCount).toBe(2) - }) -}) - describe('trackEventCounts v2', () => { let lifeCycle: LifeCycle diff --git a/packages/rum/src/domain/trackEventCounts.ts b/packages/rum/src/domain/trackEventCounts.ts index 68b0d8abe1..54557532f1 100644 --- a/packages/rum/src/domain/trackEventCounts.ts +++ b/packages/rum/src/domain/trackEventCounts.ts @@ -1,5 +1,4 @@ import { noop } from '@datadog/browser-core' -import { RumEventCategory } from '../types' import { RumEventType } from '../typesV2' import { LifeCycle, LifeCycleEventType } from './lifeCycle' @@ -18,27 +17,6 @@ export function trackEventCounts(lifeCycle: LifeCycle, callback: (eventCounts: E userActionCount: 0, } - const subscription = lifeCycle.subscribe(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, ({ rawRumEvent }): void => { - switch (rawRumEvent.evt.category) { - case RumEventCategory.ERROR: - eventCounts.errorCount += 1 - callback(eventCounts) - break - case RumEventCategory.USER_ACTION: - eventCounts.userActionCount += 1 - callback(eventCounts) - break - case RumEventCategory.LONG_TASK: - eventCounts.longTaskCount += 1 - callback(eventCounts) - break - case RumEventCategory.RESOURCE: - eventCounts.resourceCount += 1 - callback(eventCounts) - break - } - }) - const subscriptionV2 = lifeCycle.subscribe(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, ({ rawRumEvent }): void => { switch (rawRumEvent.type) { case RumEventType.ERROR: @@ -62,7 +40,6 @@ export function trackEventCounts(lifeCycle: LifeCycle, callback: (eventCounts: E return { stop() { - subscription.unsubscribe() subscriptionV2.unsubscribe() }, eventCounts, diff --git a/packages/rum/src/index.ts b/packages/rum/src/index.ts index de9fbb0fed..bb9f48eafa 100644 --- a/packages/rum/src/index.ts +++ b/packages/rum/src/index.ts @@ -2,9 +2,9 @@ export { Datacenter, ErrorSource } from '@datadog/browser-core' export { RumUserConfiguration, RumGlobal, datadogRum } from './boot/rum.entry' export { InternalContext, - RumEvent, - RumEventCategory, - RumUserActionEvent, - RumViewEvent, - RumResourceEvent, -} from './types' + RumEventV2, + RumEventType, + RumActionEventV2, + RumViewEventV2, + RumResourceEventV2, +} from './typesV2' diff --git a/packages/rum/src/transport/batch.ts b/packages/rum/src/transport/batch.ts index 56eb6d69b8..99f7e01286 100644 --- a/packages/rum/src/transport/batch.ts +++ b/packages/rum/src/transport/batch.ts @@ -1,22 +1,10 @@ import { Batch, combine, Configuration, Context, HttpRequest } from '@datadog/browser-core' import { LifeCycle, LifeCycleEventType } from '../domain/lifeCycle' -import { RumEvent, RumEventCategory } from '../types' import { RumEventType, RumEventV2 } from '../typesV2' export function startRumBatch(configuration: Configuration, lifeCycle: LifeCycle) { const batch = makeRumBatch(configuration, lifeCycle) - lifeCycle.subscribe( - LifeCycleEventType.RUM_EVENT_COLLECTED, - ({ rumEvent, serverRumEvent }: { rumEvent: RumEvent; serverRumEvent: Context }) => { - if (rumEvent.evt.category === RumEventCategory.VIEW) { - batch.upsert(serverRumEvent, rumEvent.view.id) - } else { - batch.add(serverRumEvent) - } - } - ) - lifeCycle.subscribe( LifeCycleEventType.RUM_EVENT_V2_COLLECTED, ({ rumEvent, serverRumEvent }: { rumEvent: RumEventV2; serverRumEvent: Context }) => { diff --git a/packages/rum/src/types.ts b/packages/rum/src/types.ts deleted file mode 100644 index 7ce90cb456..0000000000 --- a/packages/rum/src/types.ts +++ /dev/null @@ -1,141 +0,0 @@ -import { Context, ErrorSource, ResourceType } from '@datadog/browser-core' -import { ActionCounts, ActionType } from './domain/rumEventsCollection/action/trackActions' -import { PerformanceResourceDetails } from './domain/rumEventsCollection/resource/resourceUtils' -import { Timings } from './domain/rumEventsCollection/view/trackTimings' -import { ViewLoadingType } from './domain/rumEventsCollection/view/trackViews' -import { EventCounts } from './domain/trackEventCounts' - -export enum RumEventCategory { - USER_ACTION = 'user_action', - ERROR = 'error', - LONG_TASK = 'long_task', - VIEW = 'view', - RESOURCE = 'resource', -} - -export interface RumResourceEvent { - date: number - duration: number - evt: { - category: RumEventCategory.RESOURCE - } - http: { - performance?: PerformanceResourceDetails - method?: string - statusCode?: number - url: string - } - network?: { - bytesWritten?: number - } - resource: { - kind: ResourceType - id?: string // only for traced requests - } - _dd?: { - traceId: string - spanId?: string // not available for initial document tracing - } -} - -export interface RumErrorEvent { - date: number - http?: { - url: string - status_code: number - method: string - } - error: { - kind?: string - stack?: string - origin: ErrorSource - } - evt: { - category: RumEventCategory.ERROR - } - message: string -} - -export interface RumViewEvent { - date: number - duration: number - evt: { - category: RumEventCategory.VIEW - } - rum: { - documentVersion: number - } - view: { - loadingTime?: number - loadingType: ViewLoadingType - measures: EventCounts & Timings - } -} - -export interface RumLongTaskEvent { - date: number - duration: number - evt: { - category: RumEventCategory.LONG_TASK - } -} - -export interface RumUserActionEvent { - date?: number - duration?: number - evt: { - category: RumEventCategory.USER_ACTION - name: string - } - userAction: { - id?: string - type: ActionType - measures?: ActionCounts - } -} - -export type RawRumEvent = RumErrorEvent | RumResourceEvent | RumViewEvent | RumLongTaskEvent | RumUserActionEvent - -export interface RumContext { - applicationId: string - date: number - service?: string - session: { - type: string - } -} - -export interface ViewContext extends Context { - sessionId: string | undefined - view: { - id: string - url: string - referrer: string - } -} - -export interface ActionContext extends Context { - userAction: { - id: string - } -} - -export type RumEvent = - | RumErrorEvent & ActionContext & ViewContext & RumContext - | RumResourceEvent & ActionContext & ViewContext & RumContext - | RumViewEvent & ViewContext & RumContext - | RumLongTaskEvent & ActionContext & ViewContext & RumContext - | RumUserActionEvent & ViewContext & RumContext - -export interface InternalContext { - application_id: string - session_id: string | undefined - view?: { - id: string - url: string - referrer: string - } - user_action?: { - id: string - } -} diff --git a/packages/rum/src/typesV2.ts b/packages/rum/src/typesV2.ts index 1cb711db45..731d2cb02e 100644 --- a/packages/rum/src/typesV2.ts +++ b/packages/rum/src/typesV2.ts @@ -147,3 +147,16 @@ export type RumEventV2 = | RumViewEventV2 & ViewContextV2 & RumContextV2 | RumLongTaskEventV2 & ActionContextV2 & ViewContextV2 & RumContextV2 | RumActionEventV2 & ViewContextV2 & RumContextV2 + +export interface InternalContext { + application_id: string + session_id: string | undefined + view?: { + id: string + url: string + referrer: string + } + user_action?: { + id: string + } +} diff --git a/packages/rum/test/specHelper.ts b/packages/rum/test/specHelper.ts index 1eb5508656..5dc5b09081 100644 --- a/packages/rum/test/specHelper.ts +++ b/packages/rum/test/specHelper.ts @@ -12,7 +12,6 @@ import { import { LifeCycle, LifeCycleEventType } from '../src/domain/lifeCycle' import { ParentContexts } from '../src/domain/parentContexts' import { RumSession } from '../src/domain/rumSession' -import { RawRumEvent } from '../src/types' import { RawRumEventV2, RumContextV2, ViewContextV2 } from '../src/typesV2' import { validateFormat } from './formatValidation' @@ -43,12 +42,6 @@ export interface TestIO { clock: jasmine.Clock fakeLocation: Partial session: RumSession - rawRumEvents: Array<{ - startTime: number - rawRumEvent: RawRumEvent - savedGlobalContext?: Context - customerContext?: Context - }> rawRumEventsV2: Array<{ startTime: number rawRumEvent: RawRumEventV2 @@ -67,12 +60,6 @@ export function setup(): TestSetupBuilder { const cleanupTasks: Array<() => void> = [] let cleanupClock = noop const beforeBuildTasks: BeforeBuildCallback[] = [] - const rawRumEvents: Array<{ - startTime: number - rawRumEvent: RawRumEvent - savedGlobalContext?: Context - customerContext?: Context - }> = [] const rawRumEventsV2: Array<{ startTime: number rawRumEvent: RawRumEventV2 @@ -91,7 +78,6 @@ export function setup(): TestSetupBuilder { const FAKE_APP_ID = 'appId' // ensure that events generated before build are collected - lifeCycle.subscribe(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, (data) => rawRumEvents.push(data)) const rawRumEventsV2Collected = lifeCycle.subscribe(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, (data) => { rawRumEventsV2.push(data) validateRumEventFormat(data.rawRumEvent) @@ -160,7 +146,6 @@ export function setup(): TestSetupBuilder { clock, fakeLocation, lifeCycle, - rawRumEvents, rawRumEventsV2, session, } diff --git a/test/e2e/lib/framework/createTest.ts b/test/e2e/lib/framework/createTest.ts index 8cb49a7ef8..59083eca16 100644 --- a/test/e2e/lib/framework/createTest.ts +++ b/test/e2e/lib/framework/createTest.ts @@ -12,7 +12,7 @@ import { createMockServerApp } from './serverApps/mock' const DEFAULT_RUM_OPTIONS = { applicationId: 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee', clientToken: 'token', - enableExperimentalFeatures: ['v2_format'], + enableExperimentalFeatures: [], } const DEFAULT_LOGS_OPTIONS = { From 27b96c7c73243f83b46bde544803a2af55670dce Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Fri, 20 Nov 2020 11:31:09 +0100 Subject: [PATCH 03/12] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20remove=20v2=20refere?= =?UTF-8?q?nces?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/rum/src/boot/rum.spec.ts | 2 +- packages/rum/src/boot/rum.ts | 6 +- .../{assemblyV2.spec.ts => assembly.spec.ts} | 52 ++++++------- .../src/domain/{assemblyV2.ts => assembly.ts} | 27 +++---- .../rum/src/domain/internalContext.spec.ts | 10 +-- packages/rum/src/domain/internalContext.ts | 6 +- packages/rum/src/domain/lifeCycle.ts | 23 +++--- .../rum/src/domain/parentContexts.spec.ts | 76 +++++++++---------- packages/rum/src/domain/parentContexts.ts | 14 ++-- .../action/actionCollection.spec.ts | 16 ++-- .../action/actionCollection.ts | 12 +-- .../action/trackActions.spec.ts | 10 +-- .../error/errorCollection.spec.ts | 24 +++--- .../error/errorCollection.ts | 14 ++-- .../longTask/longTaskCollection.spec.ts | 14 ++-- .../longTask/longTaskCollection.ts | 6 +- .../resource/resourceCollection.spec.ts | 48 ++++++------ .../resource/resourceCollection.ts | 20 ++--- .../view/trackViews.spec.ts | 24 +++--- .../view/viewCollection.spec.ts | 10 +-- .../view/viewCollection.ts | 8 +- .../rum/src/domain/trackEventCounts.spec.ts | 8 +- packages/rum/src/domain/trackEventCounts.ts | 6 +- packages/rum/src/index.ts | 9 +-- packages/rum/src/transport/batch.ts | 6 +- packages/rum/src/{typesV2.ts => types.ts} | 40 +++++----- packages/rum/test/fixtures.ts | 4 +- packages/rum/test/specHelper.ts | 22 +++--- 28 files changed, 248 insertions(+), 269 deletions(-) rename packages/rum/src/domain/{assemblyV2.spec.ts => assembly.spec.ts} (79%) rename packages/rum/src/domain/{assemblyV2.ts => assembly.ts} (74%) rename packages/rum/src/{typesV2.ts => types.ts} (76%) diff --git a/packages/rum/src/boot/rum.spec.ts b/packages/rum/src/boot/rum.spec.ts index 4ac758a66c..509b0164b8 100644 --- a/packages/rum/src/boot/rum.spec.ts +++ b/packages/rum/src/boot/rum.spec.ts @@ -26,7 +26,7 @@ interface ServerRumEvent { function collectServerEvents(lifeCycle: LifeCycle) { const serverRumEvents: ServerRumEvent[] = [] - lifeCycle.subscribe(LifeCycleEventType.RUM_EVENT_V2_COLLECTED, ({ serverRumEvent }) => { + lifeCycle.subscribe(LifeCycleEventType.RUM_EVENT_COLLECTED, ({ serverRumEvent }) => { serverRumEvents.push(serverRumEvent as any) }) return serverRumEvents diff --git a/packages/rum/src/boot/rum.ts b/packages/rum/src/boot/rum.ts index d3ee7450db..ac3b5edbb5 100644 --- a/packages/rum/src/boot/rum.ts +++ b/packages/rum/src/boot/rum.ts @@ -1,7 +1,7 @@ import { combine, commonInit, Configuration, Context } from '@datadog/browser-core' import { startDOMMutationCollection } from '../browser/domMutationCollection' import { startPerformanceCollection } from '../browser/performanceCollection' -import { startRumAssemblyV2 } from '../domain/assemblyV2' +import { startRumAssembly } from '../domain/assembly' import { startInternalContext } from '../domain/internalContext' import { LifeCycle } from '../domain/lifeCycle' import { startParentContexts } from '../domain/parentContexts' @@ -28,7 +28,7 @@ export function startRum(userConfiguration: RumUserConfiguration, getGlobalConte { application_id: userConfiguration.applicationId, }, - parentContexts.findViewV2(), + parentContexts.findView(), getGlobalContext() ) }) @@ -65,7 +65,7 @@ export function startRumEventCollection( ) { const parentContexts = startParentContexts(lifeCycle, session) const batch = startRumBatch(configuration, lifeCycle) - startRumAssemblyV2(applicationId, configuration, lifeCycle, session, parentContexts, getGlobalContext) + startRumAssembly(applicationId, configuration, lifeCycle, session, parentContexts, getGlobalContext) startLongTaskCollection(lifeCycle, configuration) startResourceCollection(lifeCycle, configuration, session) startViewCollection(lifeCycle, configuration, location) diff --git a/packages/rum/src/domain/assemblyV2.spec.ts b/packages/rum/src/domain/assembly.spec.ts similarity index 79% rename from packages/rum/src/domain/assemblyV2.spec.ts rename to packages/rum/src/domain/assembly.spec.ts index 5ed320ee06..6ab0437818 100644 --- a/packages/rum/src/domain/assemblyV2.spec.ts +++ b/packages/rum/src/domain/assembly.spec.ts @@ -1,8 +1,8 @@ import { Context } from '@datadog/browser-core' import { createRawRumEvent } from '../../test/fixtures' import { setup, TestSetupBuilder } from '../../test/specHelper' -import { RumEventType } from '../typesV2' -import { startRumAssemblyV2 } from './assemblyV2' +import { RumEventType } from '../types' +import { startRumAssembly } from './assembly' import { LifeCycle, LifeCycleEventType } from './lifeCycle' interface ServerRumEvents { @@ -31,7 +31,7 @@ interface ServerRumEvents { } } -describe('rum assembly v2', () => { +describe('rum assembly', () => { let setupBuilder: TestSetupBuilder let lifeCycle: LifeCycle let globalContext: Context @@ -49,12 +49,12 @@ describe('rum assembly v2', () => { isTrackedWithResource: () => true, }) .withParentContexts({ - findActionV2: () => ({ + findAction: () => ({ action: { id: '7890', }, }), - findViewV2: () => ({ + findView: () => ({ session: { id: viewSessionId, }, @@ -66,12 +66,12 @@ describe('rum assembly v2', () => { }), }) .beforeBuild(({ applicationId, configuration, lifeCycle: localLifeCycle, session, parentContexts }) => { - startRumAssemblyV2(applicationId, configuration, localLifeCycle, session, parentContexts, () => globalContext) + startRumAssembly(applicationId, configuration, localLifeCycle, session, parentContexts, () => globalContext) }) ;({ lifeCycle } = setupBuilder.build()) serverRumEvents = [] - lifeCycle.subscribe(LifeCycleEventType.RUM_EVENT_V2_COLLECTED, ({ serverRumEvent }) => + lifeCycle.subscribe(LifeCycleEventType.RUM_EVENT_COLLECTED, ({ serverRumEvent }) => serverRumEvents.push((serverRumEvent as unknown) as ServerRumEvents) ) }) @@ -82,7 +82,7 @@ describe('rum assembly v2', () => { describe('events', () => { it('should have snake cased attributes', () => { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.LONG_TASK, { longTask: { duration: 2 } }), startTime: 0, }) @@ -93,7 +93,7 @@ describe('rum assembly v2', () => { describe('rum context', () => { it('should be merged with event attributes', () => { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW, undefined), startTime: 0, }) @@ -103,7 +103,7 @@ describe('rum assembly v2', () => { }) it('should be snake cased', () => { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW, undefined), startTime: 0, }) @@ -112,7 +112,7 @@ describe('rum assembly v2', () => { }) it('should be overwritten by event attributes', () => { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW, { date: 10 }), startTime: 0, }) @@ -124,7 +124,7 @@ describe('rum assembly v2', () => { describe('rum global context', () => { it('should be merged with event attributes', () => { globalContext = { bar: 'foo' } - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW), startTime: 0, }) @@ -134,12 +134,12 @@ describe('rum assembly v2', () => { it('should ignore subsequent context mutation', () => { globalContext = { bar: 'foo' } - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW), startTime: 0, }) delete globalContext.bar - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW), startTime: 0, }) @@ -150,7 +150,7 @@ describe('rum assembly v2', () => { it('should not be automatically snake cased', () => { globalContext = { fooBar: 'foo' } - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW), startTime: 0, }) @@ -161,7 +161,7 @@ describe('rum assembly v2', () => { it('should ignore the current global context when a saved global context is provided', () => { globalContext = { replacedContext: 'b', addedContext: 'x' } - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW), savedGlobalContext: { replacedContext: 'a' }, startTime: 0, @@ -174,7 +174,7 @@ describe('rum assembly v2', () => { describe('customer context', () => { it('should be merged with event attributes', () => { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { customerContext: { foo: 'bar' }, rawRumEvent: createRawRumEvent(RumEventType.VIEW), startTime: 0, @@ -184,7 +184,7 @@ describe('rum assembly v2', () => { }) it('should not be automatically snake cased', () => { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { customerContext: { fooBar: 'foo' }, rawRumEvent: createRawRumEvent(RumEventType.VIEW), startTime: 0, @@ -197,7 +197,7 @@ describe('rum assembly v2', () => { describe('action context', () => { it('should be added on some event categories', () => { ;[RumEventType.RESOURCE, RumEventType.LONG_TASK, RumEventType.ERROR].forEach((category) => { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(category), startTime: 0, }) @@ -205,14 +205,14 @@ describe('rum assembly v2', () => { serverRumEvents = [] }) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW), startTime: 0, }) expect(serverRumEvents[0].action).not.toBeDefined() serverRumEvents = [] - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.ACTION), startTime: 0, }) @@ -223,7 +223,7 @@ describe('rum assembly v2', () => { describe('view context', () => { it('should be merged with event attributes', () => { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.ACTION), startTime: 0, }) @@ -240,7 +240,7 @@ describe('rum assembly v2', () => { it('when tracked, it should generate event', () => { isTracked = true - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW), startTime: 0, }) @@ -250,7 +250,7 @@ describe('rum assembly v2', () => { it('when not tracked, it should not generate event', () => { isTracked = false - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW), startTime: 0, }) @@ -260,7 +260,7 @@ describe('rum assembly v2', () => { it('when view context has session id, it should generate event', () => { viewSessionId = '1234' - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW), startTime: 0, }) @@ -270,7 +270,7 @@ describe('rum assembly v2', () => { it('when view context has no session id, it should not generate event', () => { viewSessionId = undefined - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.VIEW), startTime: 0, }) diff --git a/packages/rum/src/domain/assemblyV2.ts b/packages/rum/src/domain/assembly.ts similarity index 74% rename from packages/rum/src/domain/assemblyV2.ts rename to packages/rum/src/domain/assembly.ts index 7e05d39da6..8533082d5c 100644 --- a/packages/rum/src/domain/assemblyV2.ts +++ b/packages/rum/src/domain/assembly.ts @@ -1,12 +1,5 @@ import { combine, Configuration, Context, withSnakeCaseKeys } from '@datadog/browser-core' -import { - RawRumEventV2, - RumContextV2, - RumErrorEventV2, - RumEventType, - RumLongTaskEventV2, - RumResourceEventV2, -} from '../typesV2' +import { RawRumEvent, RumContext, RumErrorEvent, RumEventType, RumLongTaskEvent, RumResourceEvent } from '../types' import { LifeCycle, LifeCycleEventType } from './lifeCycle' import { ParentContexts } from './parentContexts' import { RumSession } from './rumSession' @@ -20,7 +13,7 @@ enum SessionType { USER = 'user', } -export function startRumAssemblyV2( +export function startRumAssembly( applicationId: string, configuration: Configuration, lifeCycle: LifeCycle, @@ -29,7 +22,7 @@ export function startRumAssemblyV2( getGlobalContext: () => Context ) { lifeCycle.subscribe( - LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, + LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, ({ startTime, rawRumEvent, @@ -37,14 +30,14 @@ export function startRumAssemblyV2( customerContext, }: { startTime: number - rawRumEvent: RawRumEventV2 + rawRumEvent: RawRumEvent savedGlobalContext?: Context customerContext?: Context }) => { - const viewContext = parentContexts.findViewV2(startTime) + const viewContext = parentContexts.findView(startTime) if (session.isTracked() && viewContext && viewContext.session.id) { - const actionContext = parentContexts.findActionV2(startTime) - const rumContext: RumContextV2 = { + const actionContext = parentContexts.findAction(startTime) + const rumContext: RumContext = { _dd: { formatVersion: 2, }, @@ -64,15 +57,13 @@ export function startRumAssemblyV2( : combine(rumContext, viewContext, rawRumEvent) const serverRumEvent = withSnakeCaseKeys(rumEvent) serverRumEvent.context = combine(savedGlobalContext || getGlobalContext(), customerContext) - lifeCycle.notify(LifeCycleEventType.RUM_EVENT_V2_COLLECTED, { rumEvent, serverRumEvent }) + lifeCycle.notify(LifeCycleEventType.RUM_EVENT_COLLECTED, { rumEvent, serverRumEvent }) } } ) } -function needToAssembleWithAction( - event: RawRumEventV2 -): event is RumErrorEventV2 | RumResourceEventV2 | RumLongTaskEventV2 { +function needToAssembleWithAction(event: RawRumEvent): event is RumErrorEvent | RumResourceEvent | RumLongTaskEvent { return [RumEventType.ERROR, RumEventType.RESOURCE, RumEventType.LONG_TASK].indexOf(event.type) !== -1 } diff --git a/packages/rum/src/domain/internalContext.spec.ts b/packages/rum/src/domain/internalContext.spec.ts index 2fab44f807..c4f6008dd1 100644 --- a/packages/rum/src/domain/internalContext.spec.ts +++ b/packages/rum/src/domain/internalContext.spec.ts @@ -2,19 +2,19 @@ import { setup, TestSetupBuilder } from '../../test/specHelper' import { startInternalContext } from './internalContext' import { ParentContexts } from './parentContexts' -describe('internal context v2', () => { +describe('internal context', () => { let setupBuilder: TestSetupBuilder let parentContextsStub: Partial let internalContext: ReturnType beforeEach(() => { parentContextsStub = { - findActionV2: jasmine.createSpy('findAction').and.returnValue({ + findAction: jasmine.createSpy('findAction').and.returnValue({ action: { id: '7890', }, }), - findViewV2: jasmine.createSpy('findView').and.returnValue({ + findView: jasmine.createSpy('findView').and.returnValue({ session: { id: '1234', }, @@ -70,7 +70,7 @@ describe('internal context v2', () => { internalContext.get(123) - expect(parentContextsStub.findViewV2).toHaveBeenCalledWith(123) - expect(parentContextsStub.findActionV2).toHaveBeenCalledWith(123) + expect(parentContextsStub.findView).toHaveBeenCalledWith(123) + expect(parentContextsStub.findAction).toHaveBeenCalledWith(123) }) }) diff --git a/packages/rum/src/domain/internalContext.ts b/packages/rum/src/domain/internalContext.ts index 6644632b67..c93e8ed9f2 100644 --- a/packages/rum/src/domain/internalContext.ts +++ b/packages/rum/src/domain/internalContext.ts @@ -1,5 +1,5 @@ import { combine, withSnakeCaseKeys } from '@datadog/browser-core' -import { InternalContext } from '../typesV2' +import { InternalContext } from '../types' import { ParentContexts } from './parentContexts' import { RumSession } from './rumSession' @@ -10,9 +10,9 @@ import { RumSession } from './rumSession' export function startInternalContext(applicationId: string, session: RumSession, parentContexts: ParentContexts) { return { get: (startTime?: number) => { - const viewContext = parentContexts.findViewV2(startTime) + const viewContext = parentContexts.findView(startTime) if (session.isTracked() && viewContext && viewContext.session.id) { - const actionContext = parentContexts.findActionV2(startTime) + const actionContext = parentContexts.findAction(startTime) return (withSnakeCaseKeys( combine( { applicationId }, diff --git a/packages/rum/src/domain/lifeCycle.ts b/packages/rum/src/domain/lifeCycle.ts index 1c3f966415..470e6a1ffb 100644 --- a/packages/rum/src/domain/lifeCycle.ts +++ b/packages/rum/src/domain/lifeCycle.ts @@ -1,6 +1,6 @@ import { Context } from '@datadog/browser-core' import { RumPerformanceEntry } from '../browser/performanceCollection' -import { RawRumEventV2, RumEventV2 } from '../typesV2' +import { RawRumEvent, RumEvent } from '../types' import { RequestCompleteEvent, RequestStartEvent } from './requestCollection' import { AutoAction, AutoActionCreatedEvent } from './rumEventsCollection/action/trackActions' import { View, ViewCreatedEvent } from './rumEventsCollection/view/trackViews' @@ -17,8 +17,8 @@ export enum LifeCycleEventType { SESSION_RENEWED, DOM_MUTATED, BEFORE_UNLOAD, - RAW_RUM_EVENT_V2_COLLECTED, - RUM_EVENT_V2_COLLECTED, + RAW_RUM_EVENT_COLLECTED, + RUM_EVENT_COLLECTED, } export interface Subscription { @@ -43,18 +43,15 @@ export class LifeCycle { | LifeCycleEventType.AUTO_ACTION_DISCARDED ): void notify( - eventType: LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, + eventType: LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, data: { startTime: number - rawRumEvent: RawRumEventV2 + rawRumEvent: RawRumEvent savedGlobalContext?: Context customerContext?: Context } ): void - notify( - eventType: LifeCycleEventType.RUM_EVENT_V2_COLLECTED, - data: { rumEvent: RumEventV2; serverRumEvent: Context } - ): void + notify(eventType: LifeCycleEventType.RUM_EVENT_COLLECTED, data: { rumEvent: RumEvent; serverRumEvent: Context }): void notify(eventType: LifeCycleEventType, data?: any) { const eventCallbacks = this.callbacks[eventType] if (eventCallbacks) { @@ -87,17 +84,17 @@ export class LifeCycle { callback: () => void ): Subscription subscribe( - eventType: LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, + eventType: LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, callback: (data: { startTime: number - rawRumEvent: RawRumEventV2 + rawRumEvent: RawRumEvent savedGlobalContext?: Context customerContext?: Context }) => void ): Subscription subscribe( - eventType: LifeCycleEventType.RUM_EVENT_V2_COLLECTED, - callback: (data: { rumEvent: RumEventV2; serverRumEvent: Context }) => void + eventType: LifeCycleEventType.RUM_EVENT_COLLECTED, + callback: (data: { rumEvent: RumEvent; serverRumEvent: Context }) => void ): Subscription subscribe(eventType: LifeCycleEventType, callback: (data?: any) => void) { if (!this.callbacks[eventType]) { diff --git a/packages/rum/src/domain/parentContexts.spec.ts b/packages/rum/src/domain/parentContexts.spec.ts index c9a0bcdbe1..8831d5f7d2 100644 --- a/packages/rum/src/domain/parentContexts.spec.ts +++ b/packages/rum/src/domain/parentContexts.spec.ts @@ -15,7 +15,7 @@ function stubActionWithDuration(duration: number): AutoAction { return action as AutoAction } -describe('parentContexts v2', () => { +describe('parentContexts', () => { const FAKE_ID = 'fake' const startTime = 10 @@ -50,7 +50,7 @@ describe('parentContexts v2', () => { it('should return undefined when there is no current view and no startTime', () => { setupBuilder.build() - expect(parentContexts.findViewV2()).toBeUndefined() + expect(parentContexts.findView()).toBeUndefined() }) it('should return the current view context when there is no start time', () => { @@ -58,8 +58,8 @@ describe('parentContexts v2', () => { lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent()) - expect(parentContexts.findViewV2()).toBeDefined() - expect(parentContexts.findViewV2()!.view.id).toEqual(FAKE_ID) + expect(parentContexts.findView()).toBeDefined() + expect(parentContexts.findView()!.view.id).toEqual(FAKE_ID) }) it('should return the view context corresponding to startTime', () => { @@ -69,9 +69,9 @@ describe('parentContexts v2', () => { lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 20, id: 'view 2' })) lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 30, id: 'view 3' })) - expect(parentContexts.findViewV2(15)!.view.id).toEqual('view 1') - expect(parentContexts.findViewV2(20)!.view.id).toEqual('view 2') - expect(parentContexts.findViewV2(40)!.view.id).toEqual('view 3') + expect(parentContexts.findView(15)!.view.id).toEqual('view 1') + expect(parentContexts.findView(20)!.view.id).toEqual('view 2') + expect(parentContexts.findView(40)!.view.id).toEqual('view 3') }) it('should return undefined when no view context corresponding to startTime', () => { @@ -80,7 +80,7 @@ describe('parentContexts v2', () => { lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 10, id: 'view 1' })) lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ startTime: 20, id: 'view 2' })) - expect(parentContexts.findViewV2(5)).not.toBeDefined() + expect(parentContexts.findView(5)).not.toBeDefined() }) it('should replace the current view context on VIEW_CREATED', () => { @@ -90,31 +90,31 @@ describe('parentContexts v2', () => { const newViewId = 'fake 2' lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ id: newViewId })) - expect(parentContexts.findViewV2()!.view.id).toEqual(newViewId) + expect(parentContexts.findView()!.view.id).toEqual(newViewId) }) it('should return the current url with the current view', () => { const { lifeCycle, fakeLocation } = setupBuilder.build() lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ location: fakeLocation as Location })) - expect(parentContexts.findViewV2()!.view.url).toBe('http://fake-url.com/') + expect(parentContexts.findView()!.view.url).toBe('http://fake-url.com/') history.pushState({}, '', '/foo') - expect(parentContexts.findViewV2()!.view.url).toBe('http://fake-url.com/foo') + expect(parentContexts.findView()!.view.url).toBe('http://fake-url.com/foo') }) it('should update session id only on VIEW_CREATED', () => { const { lifeCycle } = setupBuilder.build() lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent()) - expect(parentContexts.findViewV2()!.session.id).toBe('fake-session') + expect(parentContexts.findView()!.session.id).toBe('fake-session') sessionId = 'other-session' - expect(parentContexts.findViewV2()!.session.id).toBe('fake-session') + expect(parentContexts.findView()!.session.id).toBe('fake-session') lifeCycle.notify(LifeCycleEventType.VIEW_CREATED, buildViewCreatedEvent({ id: 'fake 2' })) - expect(parentContexts.findViewV2()!.session.id).toBe('other-session') + expect(parentContexts.findView()!.session.id).toBe('other-session') }) }) @@ -122,7 +122,7 @@ describe('parentContexts v2', () => { it('should return undefined when there is no current action and no startTime', () => { setupBuilder.build() - expect(parentContexts.findActionV2()).toBeUndefined() + expect(parentContexts.findAction()).toBeUndefined() }) it('should return the current action context when no startTime', () => { @@ -130,8 +130,8 @@ describe('parentContexts v2', () => { lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime, id: FAKE_ID }) - expect(parentContexts.findActionV2()).toBeDefined() - expect(parentContexts.findActionV2()!.action.id).toBe(FAKE_ID) + expect(parentContexts.findAction()).toBeDefined() + expect(parentContexts.findAction()!.action.id).toBe(FAKE_ID) }) it('should return the action context corresponding to startTime', () => { @@ -145,10 +145,10 @@ describe('parentContexts v2', () => { lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 50, id: 'action 3' }) - expect(parentContexts.findActionV2(15)!.action.id).toBe('action 1') - expect(parentContexts.findActionV2(20)!.action.id).toBe('action 1') - expect(parentContexts.findActionV2(30)!.action.id).toBe('action 2') - expect(parentContexts.findActionV2(55)!.action.id).toBe('action 3') + expect(parentContexts.findAction(15)!.action.id).toBe('action 1') + expect(parentContexts.findAction(20)!.action.id).toBe('action 1') + expect(parentContexts.findAction(30)!.action.id).toBe('action 2') + expect(parentContexts.findAction(55)!.action.id).toBe('action 3') }) it('should return undefined if no action context corresponding to startTime', () => { @@ -159,7 +159,7 @@ describe('parentContexts v2', () => { lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 20, id: 'action 2' }) - expect(parentContexts.findActionV2(10)).toBeUndefined() + expect(parentContexts.findAction(10)).toBeUndefined() }) it('should clear the current action on ACTION_DISCARDED', () => { @@ -168,7 +168,7 @@ describe('parentContexts v2', () => { lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime, id: FAKE_ID }) lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_DISCARDED) - expect(parentContexts.findActionV2()).toBeUndefined() + expect(parentContexts.findAction()).toBeUndefined() }) it('should clear the current action on ACTION_COMPLETED', () => { @@ -177,7 +177,7 @@ describe('parentContexts v2', () => { lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime, id: FAKE_ID }) lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, stubActionWithDuration(10)) - expect(parentContexts.findActionV2()).toBeUndefined() + expect(parentContexts.findAction()).toBeUndefined() }) }) @@ -203,17 +203,17 @@ describe('parentContexts v2', () => { lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, stubActionWithDuration(10)) lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_CREATED, { startTime: 20, id: 'action 2' }) - expect(parentContexts.findViewV2(15)).toBeDefined() - expect(parentContexts.findActionV2(15)).toBeDefined() - expect(parentContexts.findViewV2(25)).toBeDefined() - expect(parentContexts.findActionV2(25)).toBeDefined() + expect(parentContexts.findView(15)).toBeDefined() + expect(parentContexts.findAction(15)).toBeDefined() + expect(parentContexts.findView(25)).toBeDefined() + expect(parentContexts.findAction(25)).toBeDefined() lifeCycle.notify(LifeCycleEventType.SESSION_RENEWED) - expect(parentContexts.findViewV2(15)).toBeUndefined() - expect(parentContexts.findActionV2(15)).toBeUndefined() - expect(parentContexts.findViewV2(25)).toBeUndefined() - expect(parentContexts.findActionV2(25)).toBeUndefined() + expect(parentContexts.findView(15)).toBeUndefined() + expect(parentContexts.findAction(15)).toBeUndefined() + expect(parentContexts.findView(25)).toBeUndefined() + expect(parentContexts.findAction(25)).toBeUndefined() }) it('should be cleared when too old', () => { @@ -237,16 +237,16 @@ describe('parentContexts v2', () => { ) clock.tick(10) - expect(parentContexts.findViewV2(targetTime)).toBeDefined() - expect(parentContexts.findActionV2(targetTime)).toBeDefined() + expect(parentContexts.findView(targetTime)).toBeDefined() + expect(parentContexts.findAction(targetTime)).toBeDefined() clock.tick(ACTION_CONTEXT_TIME_OUT_DELAY + CLEAR_OLD_CONTEXTS_INTERVAL) - expect(parentContexts.findViewV2(targetTime)).toBeDefined() - expect(parentContexts.findActionV2(targetTime)).toBeUndefined() + expect(parentContexts.findView(targetTime)).toBeDefined() + expect(parentContexts.findAction(targetTime)).toBeUndefined() clock.tick(VIEW_CONTEXT_TIME_OUT_DELAY + CLEAR_OLD_CONTEXTS_INTERVAL) - expect(parentContexts.findViewV2(targetTime)).toBeUndefined() - expect(parentContexts.findActionV2(targetTime)).toBeUndefined() + expect(parentContexts.findView(targetTime)).toBeUndefined() + expect(parentContexts.findAction(targetTime)).toBeUndefined() }) }) }) diff --git a/packages/rum/src/domain/parentContexts.ts b/packages/rum/src/domain/parentContexts.ts index 48211d0bd1..425058b503 100644 --- a/packages/rum/src/domain/parentContexts.ts +++ b/packages/rum/src/domain/parentContexts.ts @@ -1,5 +1,5 @@ import { monitor, ONE_MINUTE, SESSION_TIME_OUT_DELAY } from '@datadog/browser-core' -import { ActionContextV2, ViewContextV2 } from '../typesV2' +import { ActionContext, ViewContext } from '../types' import { LifeCycle, LifeCycleEventType } from './lifeCycle' import { AutoAction, AutoActionCreatedEvent } from './rumEventsCollection/action/trackActions' import { ViewCreatedEvent } from './rumEventsCollection/view/trackViews' @@ -16,8 +16,8 @@ interface PreviousContext { } export interface ParentContexts { - findActionV2: (startTime?: number) => ActionContextV2 | undefined - findViewV2: (startTime?: number) => ViewContextV2 | undefined + findAction: (startTime?: number) => ActionContext | undefined + findView: (startTime?: number) => ViewContext | undefined stop: () => void } @@ -26,8 +26,8 @@ export function startParentContexts(lifeCycle: LifeCycle, session: RumSession): let currentAction: AutoActionCreatedEvent | undefined let currentSessionId: string | undefined - let previousViews: Array> = [] - let previousActions: Array> = [] + let previousViews: Array> = [] + let previousActions: Array> = [] lifeCycle.subscribe(LifeCycleEventType.VIEW_CREATED, (currentContext) => { if (currentView) { @@ -131,10 +131,10 @@ export function startParentContexts(lifeCycle: LifeCycle, session: RumSession): } return { - findActionV2: (startTime) => { + findAction: (startTime) => { return findContext(buildCurrentActionContext, previousActions, currentAction, startTime) }, - findViewV2: (startTime) => { + findView: (startTime) => { return findContext(buildCurrentViewContext, previousViews, currentView, startTime) }, stop: () => { diff --git a/packages/rum/src/domain/rumEventsCollection/action/actionCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/action/actionCollection.spec.ts index 6fb64b181b..664423dd68 100644 --- a/packages/rum/src/domain/rumEventsCollection/action/actionCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/action/actionCollection.spec.ts @@ -1,10 +1,10 @@ import { setup, TestSetupBuilder } from '../../../../test/specHelper' -import { RumEventType } from '../../../typesV2' +import { RumEventType } from '../../../types' import { LifeCycleEventType } from '../../lifeCycle' import { startActionCollection } from './actionCollection' import { ActionType } from './trackActions' -describe('actionCollection v2', () => { +describe('actionCollection', () => { let setupBuilder: TestSetupBuilder let addAction: ReturnType['addAction'] @@ -22,7 +22,7 @@ describe('actionCollection v2', () => { setupBuilder.cleanup() }) it('should create action from auto action', () => { - const { lifeCycle, rawRumEventsV2 } = setupBuilder.build() + const { lifeCycle, rawRumEvents } = setupBuilder.build() lifeCycle.notify(LifeCycleEventType.AUTO_ACTION_COMPLETED, { counts: { errorCount: 10, @@ -36,8 +36,8 @@ describe('actionCollection v2', () => { type: ActionType.CLICK, }) - expect(rawRumEventsV2[0].startTime).toBe(1234) - expect(rawRumEventsV2[0].rawRumEvent).toEqual({ + expect(rawRumEvents[0].startTime).toBe(1234) + expect(rawRumEvents[0].rawRumEvent).toEqual({ action: { error: { count: 10, @@ -61,15 +61,15 @@ describe('actionCollection v2', () => { }) it('should create action from custom action', () => { - const { rawRumEventsV2 } = setupBuilder.build() + const { rawRumEvents } = setupBuilder.build() addAction({ name: 'foo', startTime: 1234, type: ActionType.CUSTOM, }) - expect(rawRumEventsV2[0].startTime).toBe(1234) - expect(rawRumEventsV2[0].rawRumEvent).toEqual({ + expect(rawRumEvents[0].startTime).toBe(1234) + expect(rawRumEvents[0].rawRumEvent).toEqual({ action: { target: { name: 'foo', diff --git a/packages/rum/src/domain/rumEventsCollection/action/actionCollection.ts b/packages/rum/src/domain/rumEventsCollection/action/actionCollection.ts index a83a9ebc26..19ef72dac6 100644 --- a/packages/rum/src/domain/rumEventsCollection/action/actionCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/action/actionCollection.ts @@ -1,11 +1,11 @@ import { combine, Configuration, Context, getTimestamp, msToNs } from '@datadog/browser-core' -import { RumActionEventV2, RumEventType } from '../../../typesV2' +import { RumActionEvent, RumEventType } from '../../../types' import { LifeCycle, LifeCycleEventType } from '../../lifeCycle' import { ActionType, AutoAction, CustomAction, trackActions } from './trackActions' export function startActionCollection(lifeCycle: LifeCycle, configuration: Configuration) { lifeCycle.subscribe(LifeCycleEventType.AUTO_ACTION_COMPLETED, (action) => - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processActionV2(action)) + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processAction(action)) ) if (configuration.trackInteractions) { @@ -14,15 +14,15 @@ export function startActionCollection(lifeCycle: LifeCycle, configuration: Confi return { addAction(action: CustomAction, savedGlobalContext?: Context) { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { savedGlobalContext, - ...processActionV2(action), + ...processAction(action), }) }, } } -function processActionV2(action: AutoAction | CustomAction) { +function processAction(action: AutoAction | CustomAction) { const autoActionProperties = isAutoAction(action) ? { action: { @@ -41,7 +41,7 @@ function processActionV2(action: AutoAction | CustomAction) { } : undefined const customerContext = !isAutoAction(action) ? action.context : undefined - const actionEvent: RumActionEventV2 = combine( + const actionEvent: RumActionEvent = combine( { action: { target: { diff --git a/packages/rum/src/domain/rumEventsCollection/action/trackActions.spec.ts b/packages/rum/src/domain/rumEventsCollection/action/trackActions.spec.ts index c19cd13820..96ae58a6dd 100644 --- a/packages/rum/src/domain/rumEventsCollection/action/trackActions.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/action/trackActions.spec.ts @@ -1,7 +1,7 @@ import { DOM_EVENT } from '@datadog/browser-core' import { createRawRumEvent } from '../../../../test/fixtures' import { setup, TestSetupBuilder } from '../../../../test/specHelper' -import { RumEventType } from '../../../typesV2' +import { RumEventType } from '../../../types' import { LifeCycle, LifeCycleEventType } from '../../lifeCycle' import { PAGE_ACTIVITY_MAX_DURATION, PAGE_ACTIVITY_VALIDATION_DELAY } from '../../trackPageActivities' import { ActionType, AutoAction, trackActions } from './trackActions' @@ -174,7 +174,7 @@ describe('newAction', () => { expect(events[0].name).toBe('test-1') }) - it('counts errors occurring during the action v2', () => { + it('counts errors occurring during the action', () => { const { lifeCycle, clock } = setupBuilder.build() const collectedRawRumEvent = { rawRumEvent: createRawRumEvent(RumEventType.ERROR), @@ -184,13 +184,13 @@ describe('newAction', () => { newClick('test-1') - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, collectedRawRumEvent) + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, collectedRawRumEvent) clock.tick(BEFORE_PAGE_ACTIVITY_VALIDATION_DELAY) lifeCycle.notify(LifeCycleEventType.DOM_MUTATED) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, collectedRawRumEvent) + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, collectedRawRumEvent) clock.tick(EXPIRE_DELAY) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, collectedRawRumEvent) + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, collectedRawRumEvent) expect(events.length).toBe(1) const action = events[0] as AutoAction diff --git a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts index 38e02d830b..ac1a0a2dc7 100644 --- a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts @@ -1,10 +1,10 @@ import { Observable, RawError } from '@datadog/browser-core' import { setup, TestSetupBuilder } from '../../../../test/specHelper' import { ErrorSource } from '../../../index' -import { RumEventType } from '../../../typesV2' +import { RumEventType } from '../../../types' import { doStartErrorCollection } from './errorCollection' -describe('error collection v2', () => { +describe('error collection', () => { let setupBuilder: TestSetupBuilder const errorObservable = new Observable() let addError: ReturnType['addError'] @@ -25,15 +25,15 @@ describe('error collection v2', () => { describe('provided', () => { it('notifies a raw rum error event', () => { - const { rawRumEventsV2 } = setupBuilder.build() + const { rawRumEvents } = setupBuilder.build() addError({ error: new Error('foo'), source: ErrorSource.CUSTOM, startTime: 12, }) - expect(rawRumEventsV2.length).toBe(1) - expect(rawRumEventsV2[0]).toEqual({ + expect(rawRumEvents.length).toBe(1) + expect(rawRumEvents[0]).toEqual({ customerContext: undefined, rawRumEvent: { date: jasmine.any(Number), @@ -52,20 +52,20 @@ describe('error collection v2', () => { }) it('should save the specified customer context', () => { - const { rawRumEventsV2 } = setupBuilder.build() + const { rawRumEvents } = setupBuilder.build() addError({ context: { foo: 'bar' }, error: new Error('foo'), source: ErrorSource.CUSTOM, startTime: 12, }) - expect(rawRumEventsV2[0].customerContext).toEqual({ + expect(rawRumEvents[0].customerContext).toEqual({ foo: 'bar', }) }) it('should save the global context', () => { - const { rawRumEventsV2 } = setupBuilder.build() + const { rawRumEvents } = setupBuilder.build() addError( { error: new Error('foo'), @@ -74,7 +74,7 @@ describe('error collection v2', () => { }, { foo: 'bar' } ) - expect(rawRumEventsV2[0].savedGlobalContext).toEqual({ + expect(rawRumEvents[0].savedGlobalContext).toEqual({ foo: 'bar', }) }) @@ -82,7 +82,7 @@ describe('error collection v2', () => { describe('auto', () => { it('should create error event from collected error', () => { - const { rawRumEventsV2 } = setupBuilder.build() + const { rawRumEvents } = setupBuilder.build() errorObservable.notify({ message: 'hello', resource: { @@ -96,8 +96,8 @@ describe('error collection v2', () => { type: 'foo', }) - expect(rawRumEventsV2[0].startTime).toBe(1234) - expect(rawRumEventsV2[0].rawRumEvent).toEqual({ + expect(rawRumEvents[0].startTime).toBe(1234) + expect(rawRumEvents[0].rawRumEvent).toEqual({ date: jasmine.any(Number), error: { message: 'hello', diff --git a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts index 5686ddf87c..48d8585b89 100644 --- a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts @@ -9,7 +9,7 @@ import { RawError, startAutomaticErrorCollection, } from '@datadog/browser-core' -import { RumErrorEventV2, RumEventType } from '../../../typesV2' +import { RumErrorEvent, RumEventType } from '../../../types' import { LifeCycle, LifeCycleEventType } from '../../lifeCycle' export interface ProvidedError { @@ -28,17 +28,15 @@ export function doStartErrorCollection( configuration: Configuration, observable: Observable ) { - observable.subscribe((error) => - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processErrorV2(error)) - ) + observable.subscribe((error) => lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processError(error))) return { addError({ error, startTime, context: customerContext, source }: ProvidedError, savedGlobalContext?: Context) { const rawError = computeRawError(error, startTime, source) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { customerContext, savedGlobalContext, - ...processErrorV2(rawError), + ...processError(rawError), }) }, } @@ -49,8 +47,8 @@ function computeRawError(error: unknown, startTime: number, source: ErrorSource) return { startTime, source, ...formatUnknownError(stackTrace, error, 'Provided') } } -function processErrorV2(error: RawError) { - const rawRumEvent: RumErrorEventV2 = { +function processError(error: RawError) { + const rawRumEvent: RumErrorEvent = { date: getTimestamp(error.startTime), error: { message: error.message, diff --git a/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.spec.ts index 7178db5707..0333b63640 100644 --- a/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.spec.ts @@ -1,10 +1,10 @@ import { setup, TestSetupBuilder } from '../../../../test/specHelper' import { RumPerformanceEntry } from '../../../browser/performanceCollection' -import { RumEventType } from '../../../typesV2' +import { RumEventType } from '../../../types' import { LifeCycleEventType } from '../../lifeCycle' import { startLongTaskCollection } from './longTaskCollection' -describe('long task collection v2', () => { +describe('long task collection', () => { let setupBuilder: TestSetupBuilder beforeEach(() => { @@ -22,7 +22,7 @@ describe('long task collection v2', () => { }) it('should only listen to long task performance entry', () => { - const { lifeCycle, rawRumEventsV2 } = setupBuilder.build() + const { lifeCycle, rawRumEvents } = setupBuilder.build() ;[ { duration: 100, entryType: 'longtask', startTime: 1234 }, { duration: 100, entryType: 'navigation', startTime: 1234 }, @@ -31,19 +31,19 @@ describe('long task collection v2', () => { ].forEach((entry) => { lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, entry as RumPerformanceEntry) }) - expect(rawRumEventsV2.length).toBe(1) + expect(rawRumEvents.length).toBe(1) }) it('should create raw rum event from performance entry', () => { - const { lifeCycle, rawRumEventsV2 } = setupBuilder.build() + const { lifeCycle, rawRumEvents } = setupBuilder.build() lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, { duration: 100, entryType: 'longtask', startTime: 1234, }) - expect(rawRumEventsV2[0].startTime).toBe(1234) - expect(rawRumEventsV2[0].rawRumEvent).toEqual({ + expect(rawRumEvents[0].startTime).toBe(1234) + expect(rawRumEvents[0].rawRumEvent).toEqual({ date: jasmine.any(Number), longTask: { duration: 100 * 1e6, diff --git a/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.ts b/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.ts index 1d93551311..b745026e18 100644 --- a/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/longTask/longTaskCollection.ts @@ -1,5 +1,5 @@ import { Configuration, getTimestamp, msToNs } from '@datadog/browser-core' -import { RumEventType, RumLongTaskEventV2 } from '../../../typesV2' +import { RumEventType, RumLongTaskEvent } from '../../../types' import { LifeCycle, LifeCycleEventType } from '../../lifeCycle' export function startLongTaskCollection(lifeCycle: LifeCycle, configuration: Configuration) { @@ -7,14 +7,14 @@ export function startLongTaskCollection(lifeCycle: LifeCycle, configuration: Con if (entry.entryType !== 'longtask') { return } - const rawRumEvent: RumLongTaskEventV2 = { + const rawRumEvent: RumLongTaskEvent = { date: getTimestamp(entry.startTime), longTask: { duration: msToNs(entry.duration), }, type: RumEventType.LONG_TASK, } - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent, startTime: entry.startTime, }) diff --git a/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.spec.ts index 433485847c..b900fa1d35 100644 --- a/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.spec.ts @@ -1,13 +1,13 @@ import { RequestType, ResourceType } from '@datadog/browser-core' import { createResourceEntry } from '../../../../test/fixtures' import { setup, TestSetupBuilder } from '../../../../test/specHelper' -import { RumEventType, RumResourceEventV2 } from '../../../typesV2' +import { RumEventType, RumResourceEvent } from '../../../types' import { LifeCycleEventType } from '../../lifeCycle' import { RequestCompleteEvent } from '../../requestCollection' import { TraceIdentifier } from '../../tracing/tracer' import { startResourceCollection } from './resourceCollection' -describe('resourceCollection V2', () => { +describe('resourceCollection', () => { let setupBuilder: TestSetupBuilder describe('when resource tracking is enabled', () => { @@ -31,7 +31,7 @@ describe('resourceCollection V2', () => { }) it('should create resource from performance entry', () => { - const { lifeCycle, rawRumEventsV2 } = setupBuilder.build() + const { lifeCycle, rawRumEvents } = setupBuilder.build() lifeCycle.notify( LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, createResourceEntry({ @@ -41,8 +41,8 @@ describe('resourceCollection V2', () => { }) ) - expect(rawRumEventsV2[0].startTime).toBe(1234) - expect(rawRumEventsV2[0].rawRumEvent).toEqual({ + expect(rawRumEvents[0].startTime).toBe(1234) + expect(rawRumEvents[0].rawRumEvent).toEqual({ date: (jasmine.any(Number) as unknown) as number, resource: { duration: 100 * 1e6, @@ -55,7 +55,7 @@ describe('resourceCollection V2', () => { }) it('should create resource from completed request', () => { - const { lifeCycle, rawRumEventsV2 } = setupBuilder.build() + const { lifeCycle, rawRumEvents } = setupBuilder.build() lifeCycle.notify( LifeCycleEventType.REQUEST_COMPLETED, createCompletedRequest({ @@ -68,8 +68,8 @@ describe('resourceCollection V2', () => { }) ) - expect(rawRumEventsV2[0].startTime).toBe(1234) - expect(rawRumEventsV2[0].rawRumEvent).toEqual({ + expect(rawRumEvents[0].startTime).toBe(1234) + expect(rawRumEvents[0].rawRumEvent).toEqual({ date: (jasmine.any(Number) as unknown) as number, resource: { duration: 100 * 1e6, @@ -104,17 +104,17 @@ describe('resourceCollection V2', () => { }) it('should not create resource from performance entry', () => { - const { lifeCycle, rawRumEventsV2 } = setupBuilder.build() + const { lifeCycle, rawRumEvents } = setupBuilder.build() lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, createResourceEntry()) - expect(rawRumEventsV2.length).toBe(0) + expect(rawRumEvents.length).toBe(0) }) it('should not create resource from completed request', () => { - const { lifeCycle, rawRumEventsV2 } = setupBuilder.build() + const { lifeCycle, rawRumEvents } = setupBuilder.build() lifeCycle.notify(LifeCycleEventType.REQUEST_COMPLETED, createCompletedRequest()) - expect(rawRumEventsV2.length).toBe(0) + expect(rawRumEvents.length).toBe(0) }) }) @@ -141,33 +141,33 @@ describe('resourceCollection V2', () => { }) it('should enable/disable resource creation from performance entry', () => { - const { lifeCycle, rawRumEventsV2 } = setupBuilder.build() + const { lifeCycle, rawRumEvents } = setupBuilder.build() lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, createResourceEntry()) - expect(rawRumEventsV2.length).toBe(1) + expect(rawRumEvents.length).toBe(1) isTrackedWithResource = false lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, createResourceEntry()) - expect(rawRumEventsV2.length).toBe(1) + expect(rawRumEvents.length).toBe(1) isTrackedWithResource = true lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, createResourceEntry()) - expect(rawRumEventsV2.length).toBe(2) + expect(rawRumEvents.length).toBe(2) }) it('should enable/disable resource creation from completed request', () => { - const { lifeCycle, rawRumEventsV2 } = setupBuilder.build() + const { lifeCycle, rawRumEvents } = setupBuilder.build() lifeCycle.notify(LifeCycleEventType.REQUEST_COMPLETED, createCompletedRequest()) - expect(rawRumEventsV2.length).toBe(1) + expect(rawRumEvents.length).toBe(1) isTrackedWithResource = false lifeCycle.notify(LifeCycleEventType.REQUEST_COMPLETED, createCompletedRequest()) - expect(rawRumEventsV2.length).toBe(1) + expect(rawRumEvents.length).toBe(1) isTrackedWithResource = true lifeCycle.notify(LifeCycleEventType.REQUEST_COMPLETED, createCompletedRequest()) - expect(rawRumEventsV2.length).toBe(2) + expect(rawRumEvents.length).toBe(2) }) }) @@ -187,7 +187,7 @@ describe('resourceCollection V2', () => { }) it('should be processed from traced initial document', () => { - const { lifeCycle, rawRumEventsV2 } = setupBuilder.build() + const { lifeCycle, rawRumEvents } = setupBuilder.build() lifeCycle.notify( LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, createResourceEntry({ @@ -195,13 +195,13 @@ describe('resourceCollection V2', () => { }) ) - const traceInfo = (rawRumEventsV2[0].rawRumEvent as RumResourceEventV2)._dd! + const traceInfo = (rawRumEvents[0].rawRumEvent as RumResourceEvent)._dd! expect(traceInfo).toBeDefined() expect(traceInfo.traceId).toBe('1234') }) it('should be processed from completed request', () => { - const { lifeCycle, rawRumEventsV2 } = setupBuilder.build() + const { lifeCycle, rawRumEvents } = setupBuilder.build() lifeCycle.notify( LifeCycleEventType.REQUEST_COMPLETED, createCompletedRequest({ @@ -210,7 +210,7 @@ describe('resourceCollection V2', () => { }) ) - const traceInfo = (rawRumEventsV2[0].rawRumEvent as RumResourceEventV2)._dd! + const traceInfo = (rawRumEvents[0].rawRumEvent as RumResourceEvent)._dd! expect(traceInfo).toBeDefined() expect(traceInfo.traceId).toBeDefined() expect(traceInfo.spanId).toBeDefined() diff --git a/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.ts b/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.ts index 2c3df21d1f..469d7e0cbf 100644 --- a/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/resource/resourceCollection.ts @@ -8,7 +8,7 @@ import { ResourceType, } from '@datadog/browser-core' import { RumPerformanceResourceTiming } from '../../../browser/performanceCollection' -import { RumEventType, RumResourceEventV2 } from '../../../typesV2' +import { RumEventType, RumResourceEvent } from '../../../types' import { LifeCycle, LifeCycleEventType } from '../../lifeCycle' import { RequestCompleteEvent } from '../../requestCollection' import { RumSession } from '../../rumSession' @@ -24,23 +24,23 @@ import { export function startResourceCollection(lifeCycle: LifeCycle, configuration: Configuration, session: RumSession) { lifeCycle.subscribe(LifeCycleEventType.REQUEST_COMPLETED, (request: RequestCompleteEvent) => { if (session.isTrackedWithResource()) { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processRequestV2(request)) + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processRequest(request)) } }) lifeCycle.subscribe(LifeCycleEventType.PERFORMANCE_ENTRY_COLLECTED, (entry) => { if (session.isTrackedWithResource() && entry.entryType === 'resource' && !isRequestKind(entry)) { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processResourceEntryV2(entry)) + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processResourceEntry(entry)) } }) } -function processRequestV2(request: RequestCompleteEvent) { +function processRequest(request: RequestCompleteEvent) { const type = request.type === RequestType.XHR ? ResourceType.XHR : ResourceType.FETCH const matchingTiming = matchRequestTiming(request) const startTime = matchingTiming ? matchingTiming.startTime : request.startTime - const correspondingTimingOverrides = matchingTiming ? computePerformanceEntryMetricsV2(matchingTiming) : undefined + const correspondingTimingOverrides = matchingTiming ? computePerformanceEntryMetrics(matchingTiming) : undefined const tracingInfo = computeRequestTracingInfo(request) @@ -59,12 +59,12 @@ function processRequestV2(request: RequestCompleteEvent) { tracingInfo, correspondingTimingOverrides ) - return { startTime, rawRumEvent: resourceEvent as RumResourceEventV2 } + return { startTime, rawRumEvent: resourceEvent as RumResourceEvent } } -function processResourceEntryV2(entry: RumPerformanceResourceTiming) { +function processResourceEntry(entry: RumPerformanceResourceTiming) { const type = computeResourceKind(entry) - const entryMetrics = computePerformanceEntryMetricsV2(entry) + const entryMetrics = computePerformanceEntryMetrics(entry) const tracingInfo = computeEntryTracingInfo(entry) const resourceEvent = combine( @@ -79,10 +79,10 @@ function processResourceEntryV2(entry: RumPerformanceResourceTiming) { tracingInfo, entryMetrics ) - return { startTime: entry.startTime, rawRumEvent: resourceEvent as RumResourceEventV2 } + return { startTime: entry.startTime, rawRumEvent: resourceEvent as RumResourceEvent } } -function computePerformanceEntryMetricsV2(timing: RumPerformanceResourceTiming) { +function computePerformanceEntryMetrics(timing: RumPerformanceResourceTiming) { return { resource: { duration: computePerformanceResourceDuration(timing), diff --git a/packages/rum/src/domain/rumEventsCollection/view/trackViews.spec.ts b/packages/rum/src/domain/rumEventsCollection/view/trackViews.spec.ts index 8ffbbd4b31..7d3e0bf641 100644 --- a/packages/rum/src/domain/rumEventsCollection/view/trackViews.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/view/trackViews.spec.ts @@ -5,7 +5,7 @@ import { RumPerformanceNavigationTiming, RumPerformancePaintTiming, } from '../../../browser/performanceCollection' -import { RumEventType } from '../../../typesV2' +import { RumEventType } from '../../../types' import { LifeCycleEventType } from '../../lifeCycle' import { PAGE_ACTIVITY_END_DELAY, @@ -578,17 +578,17 @@ describe('rum view measures', () => { }) }) - describe('event counts V2', () => { + describe('event counts', () => { it('should track error count', () => { const { lifeCycle } = setupBuilder.build() expect(getHandledCount()).toEqual(1) expect(getViewEvent(0).eventCounts.errorCount).toEqual(0) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.ERROR), startTime: 0, }) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.ERROR), startTime: 0, }) @@ -604,7 +604,7 @@ describe('rum view measures', () => { expect(getHandledCount()).toEqual(1) expect(getViewEvent(0).eventCounts.longTaskCount).toEqual(0) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.LONG_TASK), startTime: 0, }) @@ -620,7 +620,7 @@ describe('rum view measures', () => { expect(getHandledCount()).toEqual(1) expect(getViewEvent(0).eventCounts.resourceCount).toEqual(0) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.RESOURCE), startTime: 0, }) @@ -636,7 +636,7 @@ describe('rum view measures', () => { expect(getHandledCount()).toEqual(1) expect(getViewEvent(0).eventCounts.userActionCount).toEqual(0) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.ACTION), startTime: 0, }) @@ -652,7 +652,7 @@ describe('rum view measures', () => { expect(getHandledCount()).toEqual(1) expect(getViewEvent(0).eventCounts.resourceCount).toEqual(0) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.RESOURCE), startTime: 0, }) @@ -662,11 +662,11 @@ describe('rum view measures', () => { expect(getViewEvent(1).eventCounts.resourceCount).toEqual(1) expect(getViewEvent(2).eventCounts.resourceCount).toEqual(0) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.RESOURCE), startTime: 0, }) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.RESOURCE), startTime: 0, }) @@ -687,7 +687,7 @@ describe('rum view measures', () => { userActionCount: 0, }) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.RESOURCE), startTime: 0, }) @@ -709,7 +709,7 @@ describe('rum view measures', () => { const { lifeCycle, clock } = setupBuilder.withFakeClock().build() expect(getHandledCount()).toEqual(1) - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { rawRumEvent: createRawRumEvent(RumEventType.RESOURCE), startTime: 0, }) diff --git a/packages/rum/src/domain/rumEventsCollection/view/viewCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/view/viewCollection.spec.ts index 2addffffdd..0d628bf196 100644 --- a/packages/rum/src/domain/rumEventsCollection/view/viewCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/view/viewCollection.spec.ts @@ -1,10 +1,10 @@ import { setup, TestSetupBuilder } from '../../../../test/specHelper' -import { RumEventType } from '../../../typesV2' +import { RumEventType } from '../../../types' import { LifeCycleEventType } from '../../lifeCycle' import { View, ViewLoadingType } from './trackViews' import { startViewCollection } from './viewCollection' -describe('viewCollection V2', () => { +describe('viewCollection', () => { let setupBuilder: TestSetupBuilder beforeEach(() => { @@ -22,7 +22,7 @@ describe('viewCollection V2', () => { }) it('should create view from view update', () => { - const { lifeCycle, rawRumEventsV2 } = setupBuilder.build() + const { lifeCycle, rawRumEvents } = setupBuilder.build() const view = { documentVersion: 3, duration: 100, @@ -50,8 +50,8 @@ describe('viewCollection V2', () => { } lifeCycle.notify(LifeCycleEventType.VIEW_UPDATED, view as View) - expect(rawRumEventsV2[rawRumEventsV2.length - 1].startTime).toBe(1234) - expect(rawRumEventsV2[rawRumEventsV2.length - 1].rawRumEvent).toEqual({ + expect(rawRumEvents[rawRumEvents.length - 1].startTime).toBe(1234) + expect(rawRumEvents[rawRumEvents.length - 1].rawRumEvent).toEqual({ _dd: { documentVersion: 3, }, diff --git a/packages/rum/src/domain/rumEventsCollection/view/viewCollection.ts b/packages/rum/src/domain/rumEventsCollection/view/viewCollection.ts index 4155af1f39..c3baa1faab 100644 --- a/packages/rum/src/domain/rumEventsCollection/view/viewCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/view/viewCollection.ts @@ -1,18 +1,18 @@ import { Configuration, getTimestamp, msToNs } from '@datadog/browser-core' -import { RumEventType, RumViewEventV2 } from '../../../typesV2' +import { RumEventType, RumViewEvent } from '../../../types' import { LifeCycle, LifeCycleEventType } from '../../lifeCycle' import { trackViews, View } from './trackViews' export function startViewCollection(lifeCycle: LifeCycle, configuration: Configuration, location: Location) { lifeCycle.subscribe(LifeCycleEventType.VIEW_UPDATED, (view) => - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, processViewUpdateV2(view)) + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, processViewUpdate(view)) ) return trackViews(location, lifeCycle) } -function processViewUpdateV2(view: View) { - const viewEvent: RumViewEventV2 = { +function processViewUpdate(view: View) { + const viewEvent: RumViewEvent = { _dd: { documentVersion: view.documentVersion, }, diff --git a/packages/rum/src/domain/trackEventCounts.spec.ts b/packages/rum/src/domain/trackEventCounts.spec.ts index 89f789c31e..414d99096e 100644 --- a/packages/rum/src/domain/trackEventCounts.spec.ts +++ b/packages/rum/src/domain/trackEventCounts.spec.ts @@ -1,9 +1,9 @@ import { objectValues } from '@datadog/browser-core' -import { RawRumEventV2, RumEventType } from '../typesV2' +import { RawRumEvent, RumEventType } from '../types' import { LifeCycle, LifeCycleEventType } from './lifeCycle' import { EventCounts, trackEventCounts } from './trackEventCounts' -describe('trackEventCounts v2', () => { +describe('trackEventCounts', () => { let lifeCycle: LifeCycle beforeEach(() => { @@ -11,8 +11,8 @@ describe('trackEventCounts v2', () => { }) function notifyCollectedRawRumEvent(type: RumEventType) { - lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, { - rawRumEvent: ({ type } as unknown) as RawRumEventV2, + lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, { + rawRumEvent: ({ type } as unknown) as RawRumEvent, startTime: 0, }) } diff --git a/packages/rum/src/domain/trackEventCounts.ts b/packages/rum/src/domain/trackEventCounts.ts index 54557532f1..f0a9dff21c 100644 --- a/packages/rum/src/domain/trackEventCounts.ts +++ b/packages/rum/src/domain/trackEventCounts.ts @@ -1,5 +1,5 @@ import { noop } from '@datadog/browser-core' -import { RumEventType } from '../typesV2' +import { RumEventType } from '../types' import { LifeCycle, LifeCycleEventType } from './lifeCycle' export interface EventCounts { @@ -17,7 +17,7 @@ export function trackEventCounts(lifeCycle: LifeCycle, callback: (eventCounts: E userActionCount: 0, } - const subscriptionV2 = lifeCycle.subscribe(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, ({ rawRumEvent }): void => { + const subscription = lifeCycle.subscribe(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, ({ rawRumEvent }): void => { switch (rawRumEvent.type) { case RumEventType.ERROR: eventCounts.errorCount += 1 @@ -40,7 +40,7 @@ export function trackEventCounts(lifeCycle: LifeCycle, callback: (eventCounts: E return { stop() { - subscriptionV2.unsubscribe() + subscription.unsubscribe() }, eventCounts, } diff --git a/packages/rum/src/index.ts b/packages/rum/src/index.ts index bb9f48eafa..aa1a77d0ad 100644 --- a/packages/rum/src/index.ts +++ b/packages/rum/src/index.ts @@ -1,10 +1,3 @@ export { Datacenter, ErrorSource } from '@datadog/browser-core' export { RumUserConfiguration, RumGlobal, datadogRum } from './boot/rum.entry' -export { - InternalContext, - RumEventV2, - RumEventType, - RumActionEventV2, - RumViewEventV2, - RumResourceEventV2, -} from './typesV2' +export { InternalContext, RumEvent, RumEventType, RumActionEvent, RumViewEvent, RumResourceEvent } from './types' diff --git a/packages/rum/src/transport/batch.ts b/packages/rum/src/transport/batch.ts index 99f7e01286..c493b2f8d7 100644 --- a/packages/rum/src/transport/batch.ts +++ b/packages/rum/src/transport/batch.ts @@ -1,13 +1,13 @@ import { Batch, combine, Configuration, Context, HttpRequest } from '@datadog/browser-core' import { LifeCycle, LifeCycleEventType } from '../domain/lifeCycle' -import { RumEventType, RumEventV2 } from '../typesV2' +import { RumEvent, RumEventType } from '../types' export function startRumBatch(configuration: Configuration, lifeCycle: LifeCycle) { const batch = makeRumBatch(configuration, lifeCycle) lifeCycle.subscribe( - LifeCycleEventType.RUM_EVENT_V2_COLLECTED, - ({ rumEvent, serverRumEvent }: { rumEvent: RumEventV2; serverRumEvent: Context }) => { + LifeCycleEventType.RUM_EVENT_COLLECTED, + ({ rumEvent, serverRumEvent }: { rumEvent: RumEvent; serverRumEvent: Context }) => { if (rumEvent.type === RumEventType.VIEW) { batch.upsert(serverRumEvent, rumEvent.view.id) } else { diff --git a/packages/rum/src/typesV2.ts b/packages/rum/src/types.ts similarity index 76% rename from packages/rum/src/typesV2.ts rename to packages/rum/src/types.ts index 731d2cb02e..c10df5e699 100644 --- a/packages/rum/src/typesV2.ts +++ b/packages/rum/src/types.ts @@ -11,7 +11,7 @@ export enum RumEventType { RESOURCE = 'resource', } -export interface RumResourceEventV2 { +export interface RumResourceEvent { date: number type: RumEventType.RESOURCE resource: { @@ -35,7 +35,7 @@ export interface RumResourceEventV2 { } } -export interface RumErrorEventV2 { +export interface RumErrorEvent { date: number type: RumEventType.ERROR error: { @@ -51,7 +51,7 @@ export interface RumErrorEventV2 { } } -export interface RumViewEventV2 { +export interface RumViewEvent { date: number type: RumEventType.VIEW view: { @@ -79,7 +79,7 @@ interface Count { count: number } -export interface RumLongTaskEventV2 { +export interface RumLongTaskEvent { date: number type: RumEventType.LONG_TASK longTask: { @@ -87,7 +87,7 @@ export interface RumLongTaskEventV2 { } } -export interface RumActionEventV2 { +export interface RumActionEvent { date?: number type: RumEventType.ACTION action: { @@ -103,14 +103,14 @@ export interface RumActionEventV2 { } } -export type RawRumEventV2 = - | RumErrorEventV2 - | RumResourceEventV2 - | RumViewEventV2 - | RumLongTaskEventV2 - | RumActionEventV2 +export type RawRumEvent = + | RumErrorEvent + | RumResourceEvent + | RumViewEvent + | RumLongTaskEvent + | RumActionEvent -export interface RumContextV2 { +export interface RumContext { date: number application: { id: string @@ -124,7 +124,7 @@ export interface RumContextV2 { } } -export interface ViewContextV2 extends Context { +export interface ViewContext extends Context { session: { id: string | undefined } @@ -135,18 +135,18 @@ export interface ViewContextV2 extends Context { } } -export interface ActionContextV2 extends Context { +export interface ActionContext extends Context { action: { id: string } } -export type RumEventV2 = - | RumErrorEventV2 & ActionContextV2 & ViewContextV2 & RumContextV2 - | RumResourceEventV2 & ActionContextV2 & ViewContextV2 & RumContextV2 - | RumViewEventV2 & ViewContextV2 & RumContextV2 - | RumLongTaskEventV2 & ActionContextV2 & ViewContextV2 & RumContextV2 - | RumActionEventV2 & ViewContextV2 & RumContextV2 +export type RumEvent = + | RumErrorEvent & ActionContext & ViewContext & RumContext + | RumResourceEvent & ActionContext & ViewContext & RumContext + | RumViewEvent & ViewContext & RumContext + | RumLongTaskEvent & ActionContext & ViewContext & RumContext + | RumActionEvent & ViewContext & RumContext export interface InternalContext { application_id: string diff --git a/packages/rum/test/fixtures.ts b/packages/rum/test/fixtures.ts index 0fa7b23bdd..6b906b1376 100644 --- a/packages/rum/test/fixtures.ts +++ b/packages/rum/test/fixtures.ts @@ -2,9 +2,9 @@ import { combine, Context, ErrorSource, ResourceType } from '@datadog/browser-co import { RumPerformanceResourceTiming } from '../src/browser/performanceCollection' import { ActionType } from '../src/domain/rumEventsCollection/action/trackActions' import { ViewLoadingType } from '../src/domain/rumEventsCollection/view/trackViews' -import { RawRumEventV2, RumEventType } from '../src/typesV2' +import { RawRumEvent, RumEventType } from '../src/types' -export function createRawRumEvent(type: RumEventType, overrides?: Context): RawRumEventV2 { +export function createRawRumEvent(type: RumEventType, overrides?: Context): RawRumEvent { switch (type) { case RumEventType.ACTION: return combine( diff --git a/packages/rum/test/specHelper.ts b/packages/rum/test/specHelper.ts index 5dc5b09081..c233fa07e9 100644 --- a/packages/rum/test/specHelper.ts +++ b/packages/rum/test/specHelper.ts @@ -12,7 +12,7 @@ import { import { LifeCycle, LifeCycleEventType } from '../src/domain/lifeCycle' import { ParentContexts } from '../src/domain/parentContexts' import { RumSession } from '../src/domain/rumSession' -import { RawRumEventV2, RumContextV2, ViewContextV2 } from '../src/typesV2' +import { RawRumEvent, RumContext, ViewContext } from '../src/types' import { validateFormat } from './formatValidation' export interface TestSetupBuilder { @@ -42,9 +42,9 @@ export interface TestIO { clock: jasmine.Clock fakeLocation: Partial session: RumSession - rawRumEventsV2: Array<{ + rawRumEvents: Array<{ startTime: number - rawRumEvent: RawRumEventV2 + rawRumEvent: RawRumEvent savedGlobalContext?: Context customerContext?: Context }> @@ -60,9 +60,9 @@ export function setup(): TestSetupBuilder { const cleanupTasks: Array<() => void> = [] let cleanupClock = noop const beforeBuildTasks: BeforeBuildCallback[] = [] - const rawRumEventsV2: Array<{ + const rawRumEvents: Array<{ startTime: number - rawRumEvent: RawRumEventV2 + rawRumEvent: RawRumEvent savedGlobalContext?: Context customerContext?: Context }> = [] @@ -78,8 +78,8 @@ export function setup(): TestSetupBuilder { const FAKE_APP_ID = 'appId' // ensure that events generated before build are collected - const rawRumEventsV2Collected = lifeCycle.subscribe(LifeCycleEventType.RAW_RUM_EVENT_V2_COLLECTED, (data) => { - rawRumEventsV2.push(data) + const rawRumEventsCollected = lifeCycle.subscribe(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, (data) => { + rawRumEvents.push(data) validateRumEventFormat(data.rawRumEvent) }) @@ -146,7 +146,7 @@ export function setup(): TestSetupBuilder { clock, fakeLocation, lifeCycle, - rawRumEventsV2, + rawRumEvents, session, } }, @@ -154,7 +154,7 @@ export function setup(): TestSetupBuilder { cleanupTasks.forEach((task) => task()) // perform these steps at the end to generate correct events in cleanup and validate them cleanupClock() - rawRumEventsV2Collected.unsubscribe() + rawRumEventsCollected.unsubscribe() }, } return setupBuilder @@ -170,9 +170,9 @@ function buildLocation(url: string, base?: string) { } } -function validateRumEventFormat(rawRumEvent: RawRumEventV2) { +function validateRumEventFormat(rawRumEvent: RawRumEvent) { const fakeId = '00000000-aaaa-0000-aaaa-000000000000' - const fakeContext: RumContextV2 & ViewContextV2 = { + const fakeContext: RumContext & ViewContext = { _dd: { formatVersion: 2, }, From 0c2dbfb76e64b4f52d07163e0a58dde91b96ab4d Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Fri, 20 Nov 2020 18:21:56 +0100 Subject: [PATCH 04/12] =?UTF-8?q?=F0=9F=90=9B=20fix=20loadEvent=20v2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../view/trackTimings.spec.ts | 4 ++-- .../rumEventsCollection/view/trackTimings.ts | 4 ++-- .../view/trackViews.spec.ts | 6 +++--- .../rumEventsCollection/view/trackViews.ts | 18 +++++++++--------- .../view/viewCollection.spec.ts | 4 ++-- .../rumEventsCollection/view/viewCollection.ts | 2 +- packages/rum/src/types.ts | 9 ++------- test/e2e/lib/types/serverEvents.ts | 2 +- test/e2e/scenario/rum/views.scenario.ts | 2 +- 9 files changed, 23 insertions(+), 28 deletions(-) diff --git a/packages/rum/src/domain/rumEventsCollection/view/trackTimings.spec.ts b/packages/rum/src/domain/rumEventsCollection/view/trackTimings.spec.ts index 3f9b3faaf0..365cf7e9b6 100644 --- a/packages/rum/src/domain/rumEventsCollection/view/trackTimings.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/view/trackTimings.spec.ts @@ -71,7 +71,7 @@ describe('trackTimings', () => { domInteractive: 234, firstContentfulPaint: 123, firstInputDelay: 100, - loadEventEnd: 567, + loadEvent: 567, }) }) }) @@ -101,7 +101,7 @@ describe('trackNavigationTimings', () => { domComplete: 456, domContentLoaded: 345, domInteractive: 234, - loadEventEnd: 567, + loadEvent: 567, }) }) }) diff --git a/packages/rum/src/domain/rumEventsCollection/view/trackTimings.ts b/packages/rum/src/domain/rumEventsCollection/view/trackTimings.ts index 832a941314..eae2f2f8e3 100644 --- a/packages/rum/src/domain/rumEventsCollection/view/trackTimings.ts +++ b/packages/rum/src/domain/rumEventsCollection/view/trackTimings.ts @@ -7,7 +7,7 @@ export interface Timings { domInteractive?: number domContentLoaded?: number domComplete?: number - loadEventEnd?: number + loadEvent?: number largestContentfulPaint?: number firstInputDelay?: number } @@ -51,7 +51,7 @@ export function trackNavigationTimings(lifeCycle: LifeCycle, callback: (newTimin domComplete: entry.domComplete, domContentLoaded: entry.domContentLoadedEventEnd, domInteractive: entry.domInteractive, - loadEventEnd: entry.loadEventEnd, + loadEvent: entry.loadEventEnd, }) } }) diff --git a/packages/rum/src/domain/rumEventsCollection/view/trackViews.spec.ts b/packages/rum/src/domain/rumEventsCollection/view/trackViews.spec.ts index 7d3e0bf641..a3db5ce13a 100644 --- a/packages/rum/src/domain/rumEventsCollection/view/trackViews.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/view/trackViews.spec.ts @@ -488,7 +488,7 @@ describe('rum view measures', () => { domComplete: 456, domContentLoaded: 345, domInteractive: 234, - loadEventEnd: 567, + loadEvent: 567, }) }) @@ -511,7 +511,7 @@ describe('rum view measures', () => { domInteractive: 234, firstContentfulPaint: 123, largestContentfulPaint: 789, - loadEventEnd: 567, + loadEvent: 567, }) expect(getViewEvent(2).timings).toEqual({}) }) @@ -563,7 +563,7 @@ describe('rum view measures', () => { domInteractive: 234, firstContentfulPaint: 123, largestContentfulPaint: 789, - loadEventEnd: 567, + loadEvent: 567, }) }) diff --git a/packages/rum/src/domain/rumEventsCollection/view/trackViews.ts b/packages/rum/src/domain/rumEventsCollection/view/trackViews.ts index 7d8cb5e864..256db3810c 100644 --- a/packages/rum/src/domain/rumEventsCollection/view/trackViews.ts +++ b/packages/rum/src/domain/rumEventsCollection/view/trackViews.ts @@ -125,7 +125,7 @@ function newView( scheduleViewUpdate() }) - const { setActivityLoadingTime, setLoadEventEnd } = trackLoadingTime(loadingType, (newLoadingTime) => { + const { setActivityLoadingTime, setLoadEvent } = trackLoadingTime(loadingType, (newLoadingTime) => { loadingTime = newLoadingTime scheduleViewUpdate() }) @@ -171,8 +171,8 @@ function newView( }, updateTimings(newTimings: Timings) { timings = newTimings - if (newTimings.loadEventEnd !== undefined) { - setLoadEventEnd(newTimings.loadEventEnd) + if (newTimings.loadEvent !== undefined) { + setLoadEvent(newTimings.loadEvent) } }, updateLocation(newLocation: Location) { @@ -208,21 +208,21 @@ function trackHash(onHashChange: () => void) { } function trackLoadingTime(loadType: ViewLoadingType, callback: (loadingTime: number) => void) { - let isWaitingForLoadEventEnd = loadType === ViewLoadingType.INITIAL_LOAD + let isWaitingForLoadEvent = loadType === ViewLoadingType.INITIAL_LOAD let isWaitingForActivityLoadingTime = true const loadingTimeCandidates: number[] = [] function invokeCallbackIfAllCandidatesAreReceived() { - if (!isWaitingForActivityLoadingTime && !isWaitingForLoadEventEnd && loadingTimeCandidates.length > 0) { + if (!isWaitingForActivityLoadingTime && !isWaitingForLoadEvent && loadingTimeCandidates.length > 0) { callback(Math.max(...loadingTimeCandidates)) } } return { - setLoadEventEnd(loadEventEnd: number) { - if (isWaitingForLoadEventEnd) { - isWaitingForLoadEventEnd = false - loadingTimeCandidates.push(loadEventEnd) + setLoadEvent(loadEvent: number) { + if (isWaitingForLoadEvent) { + isWaitingForLoadEvent = false + loadingTimeCandidates.push(loadEvent) invokeCallbackIfAllCandidatesAreReceived() } }, diff --git a/packages/rum/src/domain/rumEventsCollection/view/viewCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/view/viewCollection.spec.ts index 0d628bf196..aab0dd07bb 100644 --- a/packages/rum/src/domain/rumEventsCollection/view/viewCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/view/viewCollection.spec.ts @@ -45,7 +45,7 @@ describe('viewCollection', () => { firstContentfulPaint: 10, firstInputDelay: 12, largestContentfulPaint: 10, - loadEventEnd: 10, + loadEvent: 10, }, } lifeCycle.notify(LifeCycleEventType.VIEW_UPDATED, view as View) @@ -70,7 +70,7 @@ describe('viewCollection', () => { firstContentfulPaint: 10 * 1e6, firstInputDelay: 12 * 1e6, largestContentfulPaint: 10 * 1e6, - loadEventEnd: 10 * 1e6, + loadEvent: 10 * 1e6, loadingTime: 20 * 1e6, loadingType: ViewLoadingType.INITIAL_LOAD, longTask: { diff --git a/packages/rum/src/domain/rumEventsCollection/view/viewCollection.ts b/packages/rum/src/domain/rumEventsCollection/view/viewCollection.ts index c3baa1faab..77606e6388 100644 --- a/packages/rum/src/domain/rumEventsCollection/view/viewCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/view/viewCollection.ts @@ -31,7 +31,7 @@ function processViewUpdate(view: View) { firstContentfulPaint: msToNs(view.timings.firstContentfulPaint), firstInputDelay: msToNs(view.timings.firstInputDelay), largestContentfulPaint: msToNs(view.timings.largestContentfulPaint), - loadEventEnd: msToNs(view.timings.loadEventEnd), + loadEvent: msToNs(view.timings.loadEvent), loadingTime: msToNs(view.loadingTime), loadingType: view.loadingType, longTask: { diff --git a/packages/rum/src/types.ts b/packages/rum/src/types.ts index c10df5e699..ea24fe1efa 100644 --- a/packages/rum/src/types.ts +++ b/packages/rum/src/types.ts @@ -62,7 +62,7 @@ export interface RumViewEvent { domInteractive?: number domContentLoaded?: number domComplete?: number - loadEventEnd?: number + loadEvent?: number loadingTime?: number timeSpent: number error: Count @@ -103,12 +103,7 @@ export interface RumActionEvent { } } -export type RawRumEvent = - | RumErrorEvent - | RumResourceEvent - | RumViewEvent - | RumLongTaskEvent - | RumActionEvent +export type RawRumEvent = RumErrorEvent | RumResourceEvent | RumViewEvent | RumLongTaskEvent | RumActionEvent export interface RumContext { date: number diff --git a/test/e2e/lib/types/serverEvents.ts b/test/e2e/lib/types/serverEvents.ts index ff1073c1bf..d99c4c5200 100644 --- a/test/e2e/lib/types/serverEvents.ts +++ b/test/e2e/lib/types/serverEvents.ts @@ -100,7 +100,7 @@ export interface ServerRumViewEvent extends ServerRumEvent { dom_complete: number dom_content_loaded: number dom_interactive: number - load_event_end: number + load_event: number first_input_delay?: number } } diff --git a/test/e2e/scenario/rum/views.scenario.ts b/test/e2e/scenario/rum/views.scenario.ts index bdc682ba61..b347b9818c 100644 --- a/test/e2e/scenario/rum/views.scenario.ts +++ b/test/e2e/scenario/rum/views.scenario.ts @@ -13,7 +13,7 @@ describe('rum views', () => { expect(viewEvent.view.dom_complete).toBeGreaterThan(0) expect(viewEvent.view.dom_content_loaded).toBeGreaterThan(0) expect(viewEvent.view.dom_interactive).toBeGreaterThan(0) - expect(viewEvent.view.load_event_end).toBeGreaterThan(0) + expect(viewEvent.view.load_event).toBeGreaterThan(0) }) // When run via WebDriver, Safari 12 and 13 (at least) have an issue with `event.timeStamp`, From 25fefaeaf77b0690412d75a3def32ac63b6e4bab Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Mon, 23 Nov 2020 15:38:49 +0100 Subject: [PATCH 05/12] =?UTF-8?q?=F0=9F=92=A5=20remove=20exported=20RUM=20?= =?UTF-8?q?event=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/rum/src/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/rum/src/index.ts b/packages/rum/src/index.ts index aa1a77d0ad..335f4b0d30 100644 --- a/packages/rum/src/index.ts +++ b/packages/rum/src/index.ts @@ -1,3 +1,2 @@ export { Datacenter, ErrorSource } from '@datadog/browser-core' export { RumUserConfiguration, RumGlobal, datadogRum } from './boot/rum.entry' -export { InternalContext, RumEvent, RumEventType, RumActionEvent, RumViewEvent, RumResourceEvent } from './types' From cdf9af09501c2a0372e9cb231030fb0f0552328c Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Mon, 23 Nov 2020 17:20:42 +0100 Subject: [PATCH 06/12] use v2 format for replica application overwrite --- packages/rum/src/transport/batch.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/rum/src/transport/batch.ts b/packages/rum/src/transport/batch.ts index 1ad098bc0d..2695627357 100644 --- a/packages/rum/src/transport/batch.ts +++ b/packages/rum/src/transport/batch.ts @@ -52,10 +52,7 @@ function makeRumBatch(configuration: Configuration, lifeCycle: LifeCycle): RumBa } function withReplicaApplicationId(message: Context) { - const applicationIdOverwrite = configuration.isEnabled('v2_format') - ? { application: { id: replica!.applicationId } } - : { application_id: replica!.applicationId } - return combine(message, applicationIdOverwrite) + return combine(message, { application: { id: replica!.applicationId } }) } let stopped = false From 32ddb6df43307e7819aff2825cdf8d0544c00690 Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Fri, 20 Nov 2020 12:15:23 +0100 Subject: [PATCH 07/12] =?UTF-8?q?=F0=9F=92=A5=20prefer=20constant=20union?= =?UTF-8?q?=20over=20enum=20in=20APIs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit reasoning: make APIs easier to use with TypeScript by using directly constants instead of importing our enums --- packages/core/src/domain/configuration.ts | 2 +- packages/logs/src/boot/logs.entry.ts | 8 +++----- packages/logs/src/domain/logger.ts | 14 +++++++------- packages/logs/src/index.ts | 5 ++--- packages/rum/src/boot/rum.entry.ts | 8 ++------ .../error/errorCollection.spec.ts | 3 +-- packages/rum/src/index.ts | 1 - 7 files changed, 16 insertions(+), 25 deletions(-) diff --git a/packages/core/src/domain/configuration.ts b/packages/core/src/domain/configuration.ts index 605cf91a06..dc9be4bdbf 100644 --- a/packages/core/src/domain/configuration.ts +++ b/packages/core/src/domain/configuration.ts @@ -44,7 +44,7 @@ export interface UserConfiguration { allowedTracingOrigins?: Array sampleRate?: number resourceSampleRate?: number - datacenter?: Datacenter // deprecated + datacenter?: 'us' | 'eu' // deprecated site?: string enableExperimentalFeatures?: string[] silentMultipleInit?: boolean diff --git a/packages/logs/src/boot/logs.entry.ts b/packages/logs/src/boot/logs.entry.ts index 08965f79c2..a4f895e5a2 100644 --- a/packages/logs/src/boot/logs.entry.ts +++ b/packages/logs/src/boot/logs.entry.ts @@ -19,13 +19,11 @@ export interface LogsUserConfiguration extends UserConfiguration { } export interface LoggerConfiguration { - level?: StatusType - handler?: HandlerType + level?: 'debug' | 'info' | 'warn' | 'error' + handler?: 'http' | 'console' | 'silent' context?: Context } -export type Status = keyof typeof StatusType - export type LogsGlobal = ReturnType export const datadogLogs = makeLogsGlobal(startLogs) @@ -77,7 +75,7 @@ export function makeLogsGlobal(startLogsImpl: StartLogs) { removeLoggerGlobalContext: monitor(globalContextManager.remove), createLogger: monitor((name: string, conf: LoggerConfiguration = {}) => { - customLoggers[name] = new Logger(sendLog, conf.handler, conf.level, { + customLoggers[name] = new Logger(sendLog, conf.handler as HandlerType, conf.level as StatusType, { ...conf.context, logger: { name }, }) diff --git a/packages/logs/src/domain/logger.ts b/packages/logs/src/domain/logger.ts index 7f462e848b..8d68039b1a 100644 --- a/packages/logs/src/domain/logger.ts +++ b/packages/logs/src/domain/logger.ts @@ -7,7 +7,7 @@ export enum StatusType { error = 'error', } -export const STATUS_PRIORITIES: { [key in StatusType]: number } = { +const STATUS_PRIORITIES: { [key in StatusType]: number } = { [StatusType.debug]: 0, [StatusType.info]: 1, [StatusType.warn]: 2, @@ -41,13 +41,13 @@ export class Logger { } @monitored - log(message: string, messageContext?: Context, status = StatusType.info) { + log(message: string, messageContext?: Context, status: 'debug' | 'info' | 'warn' | 'error' = StatusType.info) { if (STATUS_PRIORITIES[status] >= STATUS_PRIORITIES[this.level]) { switch (this.handlerType) { case HandlerType.http: this.sendLog({ message, - status, + status: status as StatusType, ...combine(this.contextManager.get(), messageContext), }) break @@ -93,11 +93,11 @@ export class Logger { this.contextManager.remove(key) } - setHandler(handler: HandlerType) { - this.handlerType = handler + setHandler(handler: 'http' | 'console' | 'silent') { + this.handlerType = handler as HandlerType } - setLevel(level: StatusType) { - this.level = level + setLevel(level: 'debug' | 'info' | 'warn' | 'error') { + this.level = level as StatusType } } diff --git a/packages/logs/src/index.ts b/packages/logs/src/index.ts index cbb07d9a0c..e4977bba23 100644 --- a/packages/logs/src/index.ts +++ b/packages/logs/src/index.ts @@ -1,3 +1,2 @@ -export { Datacenter } from '@datadog/browser-core' -export { StatusType, HandlerType, Logger, LogsMessage } from './domain/logger' -export { LogsUserConfiguration, Status, LoggerConfiguration, LogsGlobal, datadogLogs } from './boot/logs.entry' +export { Logger, LogsMessage } from './domain/logger' +export { LogsUserConfiguration, LoggerConfiguration, LogsGlobal, datadogLogs } from './boot/logs.entry' diff --git a/packages/rum/src/boot/rum.entry.ts b/packages/rum/src/boot/rum.entry.ts index 4480877900..26a3f1654a 100644 --- a/packages/rum/src/boot/rum.entry.ts +++ b/packages/rum/src/boot/rum.entry.ts @@ -105,14 +105,10 @@ export function makeRumGlobal(startRumImpl: StartRum) { }, addError: monitor( - ( - error: unknown, - context?: Context, - source: ErrorSource.CUSTOM | ErrorSource.NETWORK | ErrorSource.SOURCE = ErrorSource.CUSTOM - ) => { + (error: unknown, context?: Context, source: 'custom' | 'network' | 'source' = ErrorSource.CUSTOM) => { let checkedSource if (source === ErrorSource.CUSTOM || source === ErrorSource.NETWORK || source === ErrorSource.SOURCE) { - checkedSource = source + checkedSource = source as ErrorSource } else { console.error(`DD_RUM.addError: Invalid source '${source}'`) checkedSource = ErrorSource.CUSTOM diff --git a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts index ac1a0a2dc7..b15c51317f 100644 --- a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts +++ b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.spec.ts @@ -1,6 +1,5 @@ -import { Observable, RawError } from '@datadog/browser-core' +import { ErrorSource, Observable, RawError } from '@datadog/browser-core' import { setup, TestSetupBuilder } from '../../../../test/specHelper' -import { ErrorSource } from '../../../index' import { RumEventType } from '../../../types' import { doStartErrorCollection } from './errorCollection' diff --git a/packages/rum/src/index.ts b/packages/rum/src/index.ts index 335f4b0d30..4cb9f1d7ac 100644 --- a/packages/rum/src/index.ts +++ b/packages/rum/src/index.ts @@ -1,2 +1 @@ -export { Datacenter, ErrorSource } from '@datadog/browser-core' export { RumUserConfiguration, RumGlobal, datadogRum } from './boot/rum.entry' From 25da4807acfb7346b9b074216e5a765dd6fb31ec Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Tue, 24 Nov 2020 12:00:36 +0100 Subject: [PATCH 08/12] =?UTF-8?q?=F0=9F=91=8C=20propagate=20constant=20uni?= =?UTF-8?q?on=20for=20logs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/logs/src/boot/logs.entry.ts | 2 +- packages/logs/src/domain/logger.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/logs/src/boot/logs.entry.ts b/packages/logs/src/boot/logs.entry.ts index a4f895e5a2..c29b01e1d4 100644 --- a/packages/logs/src/boot/logs.entry.ts +++ b/packages/logs/src/boot/logs.entry.ts @@ -75,7 +75,7 @@ export function makeLogsGlobal(startLogsImpl: StartLogs) { removeLoggerGlobalContext: monitor(globalContextManager.remove), createLogger: monitor((name: string, conf: LoggerConfiguration = {}) => { - customLoggers[name] = new Logger(sendLog, conf.handler as HandlerType, conf.level as StatusType, { + customLoggers[name] = new Logger(sendLog, conf.handler, conf.level, { ...conf.context, logger: { name }, }) diff --git a/packages/logs/src/domain/logger.ts b/packages/logs/src/domain/logger.ts index 8d68039b1a..7848453ec4 100644 --- a/packages/logs/src/domain/logger.ts +++ b/packages/logs/src/domain/logger.ts @@ -18,7 +18,7 @@ export const STATUSES = Object.keys(StatusType) export interface LogsMessage { message: string - status: StatusType + status: 'debug' | 'info' | 'warn' | 'error' [key: string]: ContextValue } @@ -33,8 +33,8 @@ export class Logger { constructor( private sendLog: (message: LogsMessage) => void, - private handlerType = HandlerType.http, - private level = StatusType.debug, + private handlerType: 'http' | 'console' | 'silent' = HandlerType.http, + private level: 'debug' | 'info' | 'warn' | 'error' = StatusType.debug, loggerContext: Context = {} ) { this.contextManager.set(loggerContext) @@ -47,7 +47,7 @@ export class Logger { case HandlerType.http: this.sendLog({ message, - status: status as StatusType, + status, ...combine(this.contextManager.get(), messageContext), }) break @@ -94,10 +94,10 @@ export class Logger { } setHandler(handler: 'http' | 'console' | 'silent') { - this.handlerType = handler as HandlerType + this.handlerType = handler } setLevel(level: 'debug' | 'info' | 'warn' | 'error') { - this.level = level as StatusType + this.level = level } } From 80774bb20306af414e7de698212c5b09b1e1569d Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Tue, 24 Nov 2020 12:05:54 +0100 Subject: [PATCH 09/12] =?UTF-8?q?=F0=9F=91=8C=20propagate=20constant=20uni?= =?UTF-8?q?on=20for=20rum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/tools/error.ts | 2 +- packages/rum/src/boot/rum.entry.ts | 4 ++-- .../src/domain/rumEventsCollection/error/errorCollection.ts | 5 ++--- packages/rum/src/types.ts | 4 ++-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/core/src/tools/error.ts b/packages/core/src/tools/error.ts index b0134a56e4..b8fbcdfc7b 100644 --- a/packages/core/src/tools/error.ts +++ b/packages/core/src/tools/error.ts @@ -6,7 +6,7 @@ export interface RawError { message: string type?: string stack?: string - source: ErrorSource + source: 'agent' | 'console' | 'custom' | 'network' | 'source' | 'logger' resource?: { url: string statusCode: number diff --git a/packages/rum/src/boot/rum.entry.ts b/packages/rum/src/boot/rum.entry.ts index 26a3f1654a..81f436407e 100644 --- a/packages/rum/src/boot/rum.entry.ts +++ b/packages/rum/src/boot/rum.entry.ts @@ -106,9 +106,9 @@ export function makeRumGlobal(startRumImpl: StartRum) { addError: monitor( (error: unknown, context?: Context, source: 'custom' | 'network' | 'source' = ErrorSource.CUSTOM) => { - let checkedSource + let checkedSource: 'custom' | 'network' | 'source' if (source === ErrorSource.CUSTOM || source === ErrorSource.NETWORK || source === ErrorSource.SOURCE) { - checkedSource = source as ErrorSource + checkedSource = source } else { console.error(`DD_RUM.addError: Invalid source '${source}'`) checkedSource = ErrorSource.CUSTOM diff --git a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts index 48d8585b89..2e0fec97f6 100644 --- a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts @@ -2,7 +2,6 @@ import { computeStackTrace, Configuration, Context, - ErrorSource, formatUnknownError, getTimestamp, Observable, @@ -16,7 +15,7 @@ export interface ProvidedError { startTime: number error: unknown context?: Context - source: ErrorSource + source: 'custom' | 'network' | 'source' } export function startErrorCollection(lifeCycle: LifeCycle, configuration: Configuration) { @@ -42,7 +41,7 @@ export function doStartErrorCollection( } } -function computeRawError(error: unknown, startTime: number, source: ErrorSource): RawError { +function computeRawError(error: unknown, startTime: number, source: 'custom' | 'network' | 'source'): RawError { const stackTrace = error instanceof Error ? computeStackTrace(error) : undefined return { startTime, source, ...formatUnknownError(stackTrace, error, 'Provided') } } diff --git a/packages/rum/src/types.ts b/packages/rum/src/types.ts index ea24fe1efa..5f16be8f01 100644 --- a/packages/rum/src/types.ts +++ b/packages/rum/src/types.ts @@ -1,4 +1,4 @@ -import { Context, ErrorSource, ResourceType } from '@datadog/browser-core' +import { Context, ResourceType } from '@datadog/browser-core' import { ActionType } from './domain/rumEventsCollection/action/trackActions' import { PerformanceResourceDetailsElement } from './domain/rumEventsCollection/resource/resourceUtils' import { ViewLoadingType } from './domain/rumEventsCollection/view/trackViews' @@ -46,7 +46,7 @@ export interface RumErrorEvent { } type?: string stack?: string - source: ErrorSource + source: 'agent' | 'console' | 'custom' | 'network' | 'source' | 'logger' message: string } } From 18ff9a690de4bb76761da48de7e6910e9d77021f Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Tue, 24 Nov 2020 13:59:19 +0100 Subject: [PATCH 10/12] =?UTF-8?q?=F0=9F=91=8C=20alias=20constant=20union?= =?UTF-8?q?=20for=20logs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/logs/src/boot/logs.entry.ts | 6 +++--- packages/logs/src/domain/logger.ts | 16 ++++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/logs/src/boot/logs.entry.ts b/packages/logs/src/boot/logs.entry.ts index c29b01e1d4..287e2843cf 100644 --- a/packages/logs/src/boot/logs.entry.ts +++ b/packages/logs/src/boot/logs.entry.ts @@ -11,7 +11,7 @@ import { monitor, UserConfiguration, } from '@datadog/browser-core' -import { HandlerType, Logger, LogsMessage, StatusType } from '../domain/logger' +import { Handler, Logger, LogsMessage, Status } from '../domain/logger' import { startLogs } from './logs' export interface LogsUserConfiguration extends UserConfiguration { @@ -19,8 +19,8 @@ export interface LogsUserConfiguration extends UserConfiguration { } export interface LoggerConfiguration { - level?: 'debug' | 'info' | 'warn' | 'error' - handler?: 'http' | 'console' | 'silent' + level?: Status + handler?: Handler context?: Context } diff --git a/packages/logs/src/domain/logger.ts b/packages/logs/src/domain/logger.ts index 7848453ec4..7c9842143e 100644 --- a/packages/logs/src/domain/logger.ts +++ b/packages/logs/src/domain/logger.ts @@ -14,11 +14,13 @@ const STATUS_PRIORITIES: { [key in StatusType]: number } = { [StatusType.error]: 3, } +export type Status = 'debug' | 'info' | 'warn' | 'error' + export const STATUSES = Object.keys(StatusType) export interface LogsMessage { message: string - status: 'debug' | 'info' | 'warn' | 'error' + status: Status [key: string]: ContextValue } @@ -28,20 +30,22 @@ export enum HandlerType { silent = 'silent', } +export type Handler = 'http' | 'console' | 'silent' + export class Logger { private contextManager = createContextManager() constructor( private sendLog: (message: LogsMessage) => void, - private handlerType: 'http' | 'console' | 'silent' = HandlerType.http, - private level: 'debug' | 'info' | 'warn' | 'error' = StatusType.debug, + private handlerType: Handler = HandlerType.http, + private level: Status = StatusType.debug, loggerContext: Context = {} ) { this.contextManager.set(loggerContext) } @monitored - log(message: string, messageContext?: Context, status: 'debug' | 'info' | 'warn' | 'error' = StatusType.info) { + log(message: string, messageContext?: Context, status: Status = StatusType.info) { if (STATUS_PRIORITIES[status] >= STATUS_PRIORITIES[this.level]) { switch (this.handlerType) { case HandlerType.http: @@ -93,11 +97,11 @@ export class Logger { this.contextManager.remove(key) } - setHandler(handler: 'http' | 'console' | 'silent') { + setHandler(handler: Handler) { this.handlerType = handler } - setLevel(level: 'debug' | 'info' | 'warn' | 'error') { + setLevel(level: Status) { this.level = level } } From 1980845c0759e98a280bb7e5036a611e31f20640 Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Tue, 24 Nov 2020 16:06:15 +0100 Subject: [PATCH 11/12] =?UTF-8?q?=F0=9F=91=8C=20alias=20constant=20union?= =?UTF-8?q?=20for=20rum?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/index.ts | 2 +- packages/core/src/tools/error.ts | 4 ++- packages/rum/src/boot/rum.entry.ts | 32 +++++++++---------- .../error/errorCollection.ts | 6 ++-- packages/rum/src/types.ts | 4 +-- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index f2d9ed3705..bb05dd33cc 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -35,7 +35,7 @@ export { export { HttpRequest, Batch } from './transport/transport' export * from './tools/urlPolyfill' export * from './tools/utils' -export { ErrorSource, formatUnknownError, RawError } from './tools/error' +export { ErrorSource, formatUnknownError, RawError, Source } from './tools/error' export { combine, Context, ContextArray, ContextValue, deepClone, withSnakeCaseKeys } from './tools/context' export { areCookiesAuthorized, getCookie, setCookie, COOKIE_ACCESS_DELAY } from './browser/cookie' export { startXhrProxy, XhrCompleteContext, XhrStartContext, XhrProxy, resetXhrProxy } from './browser/xhrProxy' diff --git a/packages/core/src/tools/error.ts b/packages/core/src/tools/error.ts index b8fbcdfc7b..f4aabc3740 100644 --- a/packages/core/src/tools/error.ts +++ b/packages/core/src/tools/error.ts @@ -6,7 +6,7 @@ export interface RawError { message: string type?: string stack?: string - source: 'agent' | 'console' | 'custom' | 'network' | 'source' | 'logger' + source: Source resource?: { url: string statusCode: number @@ -23,6 +23,8 @@ export enum ErrorSource { CUSTOM = 'custom', } +export type Source = 'agent' | 'console' | 'custom' | 'network' | 'source' | 'logger' + export function formatUnknownError(stackTrace: StackTrace | undefined, errorObject: any, nonErrorPrefix: string) { if (!stackTrace || (stackTrace.message === undefined && !(errorObject instanceof Error))) { return { diff --git a/packages/rum/src/boot/rum.entry.ts b/packages/rum/src/boot/rum.entry.ts index 81f436407e..59e6085b4f 100644 --- a/packages/rum/src/boot/rum.entry.ts +++ b/packages/rum/src/boot/rum.entry.ts @@ -15,7 +15,7 @@ import { UserConfiguration, } from '@datadog/browser-core' import { ActionType, CustomAction } from '../domain/rumEventsCollection/action/trackActions' -import { ProvidedError } from '../domain/rumEventsCollection/error/errorCollection' +import { ProvidedError, ProvidedSource } from '../domain/rumEventsCollection/error/errorCollection' import { startRum } from './rum' export interface RumUserConfiguration extends UserConfiguration { @@ -104,23 +104,21 @@ export function makeRumGlobal(startRumImpl: StartRum) { rumGlobal.addAction(name, context) }, - addError: monitor( - (error: unknown, context?: Context, source: 'custom' | 'network' | 'source' = ErrorSource.CUSTOM) => { - let checkedSource: 'custom' | 'network' | 'source' - if (source === ErrorSource.CUSTOM || source === ErrorSource.NETWORK || source === ErrorSource.SOURCE) { - checkedSource = source - } else { - console.error(`DD_RUM.addError: Invalid source '${source}'`) - checkedSource = ErrorSource.CUSTOM - } - addErrorStrategy({ - error, - context: deepClone(context), - source: checkedSource, - startTime: performance.now(), - }) + addError: monitor((error: unknown, context?: Context, source: ProvidedSource = ErrorSource.CUSTOM) => { + let checkedSource: ProvidedSource + if (source === ErrorSource.CUSTOM || source === ErrorSource.NETWORK || source === ErrorSource.SOURCE) { + checkedSource = source + } else { + console.error(`DD_RUM.addError: Invalid source '${source}'`) + checkedSource = ErrorSource.CUSTOM } - ), + addErrorStrategy({ + error, + context: deepClone(context), + source: checkedSource, + startTime: performance.now(), + }) + }), }) return rumGlobal diff --git a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts index 2e0fec97f6..2b4ad474c9 100644 --- a/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts +++ b/packages/rum/src/domain/rumEventsCollection/error/errorCollection.ts @@ -15,9 +15,11 @@ export interface ProvidedError { startTime: number error: unknown context?: Context - source: 'custom' | 'network' | 'source' + source: ProvidedSource } +export type ProvidedSource = 'custom' | 'network' | 'source' + export function startErrorCollection(lifeCycle: LifeCycle, configuration: Configuration) { return doStartErrorCollection(lifeCycle, configuration, startAutomaticErrorCollection(configuration)) } @@ -41,7 +43,7 @@ export function doStartErrorCollection( } } -function computeRawError(error: unknown, startTime: number, source: 'custom' | 'network' | 'source'): RawError { +function computeRawError(error: unknown, startTime: number, source: ProvidedSource): RawError { const stackTrace = error instanceof Error ? computeStackTrace(error) : undefined return { startTime, source, ...formatUnknownError(stackTrace, error, 'Provided') } } diff --git a/packages/rum/src/types.ts b/packages/rum/src/types.ts index 5f16be8f01..734c7a3d8e 100644 --- a/packages/rum/src/types.ts +++ b/packages/rum/src/types.ts @@ -1,4 +1,4 @@ -import { Context, ResourceType } from '@datadog/browser-core' +import { Context, ResourceType, Source } from '@datadog/browser-core' import { ActionType } from './domain/rumEventsCollection/action/trackActions' import { PerformanceResourceDetailsElement } from './domain/rumEventsCollection/resource/resourceUtils' import { ViewLoadingType } from './domain/rumEventsCollection/view/trackViews' @@ -46,7 +46,7 @@ export interface RumErrorEvent { } type?: string stack?: string - source: 'agent' | 'console' | 'custom' | 'network' | 'source' | 'logger' + source: Source message: string } } From ffee4afcac231979685bf856bd2ad02d246d1a78 Mon Sep 17 00:00:00 2001 From: Bastien Caudan Date: Wed, 25 Nov 2020 14:47:56 +0100 Subject: [PATCH 12/12] =?UTF-8?q?=F0=9F=91=8C=20replace=20exposed=20enum?= =?UTF-8?q?=20by=20variable=20+=20type=20alias?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/boot/init.ts | 10 +++--- packages/core/src/domain/configuration.ts | 2 +- packages/core/src/index.ts | 2 +- packages/core/src/tools/error.ts | 20 ++++++------ packages/logs/src/boot/logs.entry.ts | 6 ++-- packages/logs/src/domain/logger.ts | 40 +++++++++++------------ packages/rum/src/types.ts | 4 +-- 7 files changed, 43 insertions(+), 41 deletions(-) diff --git a/packages/core/src/boot/init.ts b/packages/core/src/boot/init.ts index bc70d2d2e7..ad7aa3205f 100644 --- a/packages/core/src/boot/init.ts +++ b/packages/core/src/boot/init.ts @@ -34,10 +34,12 @@ export function defineGlobal(global: Global, } } -export enum Datacenter { - US = 'us', - EU = 'eu', -} +export const Datacenter = { + EU: 'eu', + US: 'us', +} as const + +export type Datacenter = (typeof Datacenter)[keyof typeof Datacenter] export const INTAKE_SITE = { [Datacenter.EU]: 'datadoghq.eu', diff --git a/packages/core/src/domain/configuration.ts b/packages/core/src/domain/configuration.ts index dc9be4bdbf..605cf91a06 100644 --- a/packages/core/src/domain/configuration.ts +++ b/packages/core/src/domain/configuration.ts @@ -44,7 +44,7 @@ export interface UserConfiguration { allowedTracingOrigins?: Array sampleRate?: number resourceSampleRate?: number - datacenter?: 'us' | 'eu' // deprecated + datacenter?: Datacenter // deprecated site?: string enableExperimentalFeatures?: string[] silentMultipleInit?: boolean diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index bb05dd33cc..f2d9ed3705 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -35,7 +35,7 @@ export { export { HttpRequest, Batch } from './transport/transport' export * from './tools/urlPolyfill' export * from './tools/utils' -export { ErrorSource, formatUnknownError, RawError, Source } from './tools/error' +export { ErrorSource, formatUnknownError, RawError } from './tools/error' export { combine, Context, ContextArray, ContextValue, deepClone, withSnakeCaseKeys } from './tools/context' export { areCookiesAuthorized, getCookie, setCookie, COOKIE_ACCESS_DELAY } from './browser/cookie' export { startXhrProxy, XhrCompleteContext, XhrStartContext, XhrProxy, resetXhrProxy } from './browser/xhrProxy' diff --git a/packages/core/src/tools/error.ts b/packages/core/src/tools/error.ts index f4aabc3740..1b65b4bbc5 100644 --- a/packages/core/src/tools/error.ts +++ b/packages/core/src/tools/error.ts @@ -6,7 +6,7 @@ export interface RawError { message: string type?: string stack?: string - source: Source + source: ErrorSource resource?: { url: string statusCode: number @@ -14,16 +14,16 @@ export interface RawError { } } -export enum ErrorSource { - AGENT = 'agent', - CONSOLE = 'console', - NETWORK = 'network', - SOURCE = 'source', - LOGGER = 'logger', - CUSTOM = 'custom', -} +export const ErrorSource = { + AGENT: 'agent', + CONSOLE: 'console', + CUSTOM: 'custom', + LOGGER: 'logger', + NETWORK: 'network', + SOURCE: 'source', +} as const -export type Source = 'agent' | 'console' | 'custom' | 'network' | 'source' | 'logger' +export type ErrorSource = (typeof ErrorSource)[keyof typeof ErrorSource] export function formatUnknownError(stackTrace: StackTrace | undefined, errorObject: any, nonErrorPrefix: string) { if (!stackTrace || (stackTrace.message === undefined && !(errorObject instanceof Error))) { diff --git a/packages/logs/src/boot/logs.entry.ts b/packages/logs/src/boot/logs.entry.ts index 287e2843cf..76ce5d0694 100644 --- a/packages/logs/src/boot/logs.entry.ts +++ b/packages/logs/src/boot/logs.entry.ts @@ -11,7 +11,7 @@ import { monitor, UserConfiguration, } from '@datadog/browser-core' -import { Handler, Logger, LogsMessage, Status } from '../domain/logger' +import { HandlerType, Logger, LogsMessage, StatusType } from '../domain/logger' import { startLogs } from './logs' export interface LogsUserConfiguration extends UserConfiguration { @@ -19,8 +19,8 @@ export interface LogsUserConfiguration extends UserConfiguration { } export interface LoggerConfiguration { - level?: Status - handler?: Handler + level?: StatusType + handler?: HandlerType context?: Context } diff --git a/packages/logs/src/domain/logger.ts b/packages/logs/src/domain/logger.ts index 7c9842143e..a9984e77c3 100644 --- a/packages/logs/src/domain/logger.ts +++ b/packages/logs/src/domain/logger.ts @@ -1,11 +1,13 @@ import { combine, Context, ContextValue, createContextManager, ErrorSource, monitored } from '@datadog/browser-core' -export enum StatusType { - debug = 'debug', - info = 'info', - warn = 'warn', - error = 'error', -} +export const StatusType = { + debug: 'debug', + error: 'error', + info: 'info', + warn: 'warn', +} as const + +export type StatusType = (typeof StatusType)[keyof typeof StatusType] const STATUS_PRIORITIES: { [key in StatusType]: number } = { [StatusType.debug]: 0, @@ -14,38 +16,36 @@ const STATUS_PRIORITIES: { [key in StatusType]: number } = { [StatusType.error]: 3, } -export type Status = 'debug' | 'info' | 'warn' | 'error' - export const STATUSES = Object.keys(StatusType) export interface LogsMessage { message: string - status: Status + status: StatusType [key: string]: ContextValue } -export enum HandlerType { - http = 'http', - console = 'console', - silent = 'silent', -} +export const HandlerType = { + console: 'console', + http: 'http', + silent: 'silent', +} as const -export type Handler = 'http' | 'console' | 'silent' +export type HandlerType = (typeof HandlerType)[keyof typeof HandlerType] export class Logger { private contextManager = createContextManager() constructor( private sendLog: (message: LogsMessage) => void, - private handlerType: Handler = HandlerType.http, - private level: Status = StatusType.debug, + private handlerType: HandlerType = HandlerType.http, + private level: StatusType = StatusType.debug, loggerContext: Context = {} ) { this.contextManager.set(loggerContext) } @monitored - log(message: string, messageContext?: Context, status: Status = StatusType.info) { + log(message: string, messageContext?: Context, status: StatusType = StatusType.info) { if (STATUS_PRIORITIES[status] >= STATUS_PRIORITIES[this.level]) { switch (this.handlerType) { case HandlerType.http: @@ -97,11 +97,11 @@ export class Logger { this.contextManager.remove(key) } - setHandler(handler: Handler) { + setHandler(handler: HandlerType) { this.handlerType = handler } - setLevel(level: Status) { + setLevel(level: StatusType) { this.level = level } } diff --git a/packages/rum/src/types.ts b/packages/rum/src/types.ts index 734c7a3d8e..ea24fe1efa 100644 --- a/packages/rum/src/types.ts +++ b/packages/rum/src/types.ts @@ -1,4 +1,4 @@ -import { Context, ResourceType, Source } from '@datadog/browser-core' +import { Context, ErrorSource, ResourceType } from '@datadog/browser-core' import { ActionType } from './domain/rumEventsCollection/action/trackActions' import { PerformanceResourceDetailsElement } from './domain/rumEventsCollection/resource/resourceUtils' import { ViewLoadingType } from './domain/rumEventsCollection/view/trackViews' @@ -46,7 +46,7 @@ export interface RumErrorEvent { } type?: string stack?: string - source: Source + source: ErrorSource message: string } }