Skip to content

Commit

Permalink
Revert "Change view logic to emit LifeCycle events (#366)"
Browse files Browse the repository at this point in the history
This reverts commit 692e9c5.
  • Loading branch information
BenoitZugmeyer committed Apr 22, 2020
1 parent 99dcd70 commit 4cbf78b
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 353 deletions.
17 changes: 13 additions & 4 deletions packages/core/src/transport.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import lodashMerge from 'lodash.merge'

import { monitor } from './internalMonitoring'
import { Context, DOM_EVENT, jsonStringify, noop, objectValues } from './utils'
import { Context, DOM_EVENT, jsonStringify, objectValues } from './utils'

/**
* Use POST request without content type to:
Expand Down Expand Up @@ -33,6 +33,7 @@ function addBatchTime(url: string) {
}

export class Batch<T> {
private beforeFlushOnUnloadHandlers: Array<() => void> = []
private pushOnlyBuffer: string[] = []
private upsertBuffer: { [key: string]: string } = {}
private bufferBytesSize = 0
Expand All @@ -44,8 +45,7 @@ export class Batch<T> {
private bytesLimit: number,
private maxMessageSize: number,
private flushTimeout: number,
private contextProvider: () => Context,
private beforeUnloadCallback: () => void = noop
private contextProvider: () => Context
) {
this.flushOnVisibilityHidden()
this.flushPeriodically()
Expand All @@ -59,6 +59,10 @@ export class Batch<T> {
this.addOrUpdate(message, key)
}

beforeFlushOnUnload(handler: () => void) {
this.beforeFlushOnUnloadHandlers.push(handler)
}

flush() {
if (this.bufferMessageCount !== 0) {
const messages = [...this.pushOnlyBuffer, ...objectValues(this.upsertBuffer)]
Expand Down Expand Up @@ -156,7 +160,12 @@ export class Batch<T> {
* register first to be sure to be called before flush on beforeunload
* caveat: unload can still be canceled by another listener
*/
window.addEventListener(DOM_EVENT.BEFORE_UNLOAD, monitor(this.beforeUnloadCallback))
window.addEventListener(
DOM_EVENT.BEFORE_UNLOAD,
monitor(() => {
this.beforeFlushOnUnloadHandlers.forEach((handler) => handler())
})
)

/**
* Only event that guarantee to fire on mobile devices when the page transitions to background state
Expand Down
9 changes: 1 addition & 8 deletions packages/rum/src/lifeCycle.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { ErrorMessage, RequestCompleteEvent, RequestStartEvent } from '@datadog/browser-core'
import { UserAction } from './userActionCollection'
import { View } from './viewCollection'

export enum LifeCycleEventType {
ERROR_COLLECTED,
PERFORMANCE_ENTRY_COLLECTED,
USER_ACTION_COLLECTED,
VIEW_COLLECTED,
REQUEST_STARTED,
REQUEST_COMPLETED,
SESSION_RENEWED,
RESOURCE_ADDED_TO_BATCH,
DOM_MUTATED,
BEFORE_UNLOAD,
}

export interface Subscription {
Expand All @@ -27,13 +24,11 @@ export class LifeCycle {
notify(eventType: LifeCycleEventType.REQUEST_STARTED, data: RequestStartEvent): void
notify(eventType: LifeCycleEventType.REQUEST_COMPLETED, data: RequestCompleteEvent): void
notify(eventType: LifeCycleEventType.USER_ACTION_COLLECTED, data: UserAction): void
notify(eventType: LifeCycleEventType.VIEW_COLLECTED, data: View): void
notify(
eventType:
| LifeCycleEventType.SESSION_RENEWED
| LifeCycleEventType.RESOURCE_ADDED_TO_BATCH
| LifeCycleEventType.DOM_MUTATED
| LifeCycleEventType.BEFORE_UNLOAD
): void
notify(eventType: LifeCycleEventType, data?: any) {
const eventCallbacks = this.callbacks[eventType]
Expand All @@ -53,13 +48,11 @@ export class LifeCycle {
callback: (data: RequestCompleteEvent) => void
): Subscription
subscribe(eventType: LifeCycleEventType.USER_ACTION_COLLECTED, callback: (data: UserAction) => void): Subscription
subscribe(eventType: LifeCycleEventType.VIEW_COLLECTED, callback: (data: View) => void): Subscription
subscribe(
eventType:
| LifeCycleEventType.SESSION_RENEWED
| LifeCycleEventType.RESOURCE_ADDED_TO_BATCH
| LifeCycleEventType.DOM_MUTATED
| LifeCycleEventType.BEFORE_UNLOAD,
| LifeCycleEventType.DOM_MUTATED,
callback: () => void
): Subscription
subscribe(eventType: LifeCycleEventType, callback: (data?: any) => void) {
Expand Down
2 changes: 0 additions & 2 deletions packages/rum/src/rum.entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { startPerformanceCollection } from './performanceCollection'
import { startRum } from './rum'
import { startRumSession } from './rumSession'
import { startUserActionCollection, UserActionReference } from './userActionCollection'
import { startViewCollection } from './viewCollection'

export interface RumUserConfiguration extends UserConfiguration {
applicationId: string
Expand Down Expand Up @@ -73,7 +72,6 @@ datadogRum.init = monitor((userConfiguration: RumUserConfiguration) => {
const session = startRumSession(configuration, lifeCycle)
const globalApi = startRum(rumUserConfiguration.applicationId, lifeCycle, configuration, session, internalMonitoring)

startViewCollection(location, lifeCycle, session)
const [requestStartObservable, requestCompleteObservable] = startRequestCollection()
startPerformanceCollection(lifeCycle, session)
startDOMMutationCollection(lifeCycle)
Expand Down
35 changes: 6 additions & 29 deletions packages/rum/src/rum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import { InternalContext, RumGlobal } from './rum.entry'
import { RumSession } from './rumSession'
import { getUserActionReference, UserActionMeasures, UserActionReference, UserActionType } from './userActionCollection'
import { viewContext, ViewMeasures } from './viewCollection'
import { trackView, viewContext, ViewMeasures } from './viewTracker'

export interface PerformancePaintTiming extends PerformanceEntry {
entryType: 'paint'
Expand Down Expand Up @@ -182,11 +182,10 @@ export function startRum(
url: viewContext.location.href,
},
}),
() => globalContext,
() => lifeCycle.notify(LifeCycleEventType.BEFORE_UNLOAD)
() => globalContext
)

trackView(lifeCycle, batch.upsertRumEvent)
trackView(window.location, lifeCycle, session, batch.upsertRumEvent, batch.beforeFlushOnUnload)
trackErrors(lifeCycle, batch.addRumEvent)
trackRequests(configuration, lifeCycle, session, batch.addRumEvent)
trackPerformanceTiming(configuration, lifeCycle, batch.addRumEvent)
Expand Down Expand Up @@ -222,24 +221,23 @@ function startRumBatch(
configuration: Configuration,
session: RumSession,
rumContextProvider: () => Context,
globalContextProvider: () => Context,
beforeUnloadCallback: () => void
globalContextProvider: () => Context
) {
const batch = new Batch<Context>(
new HttpRequest(configuration.rumEndpoint, configuration.batchBytesLimit, true),
configuration.maxBatchSize,
configuration.batchBytesLimit,
configuration.maxMessageSize,
configuration.flushTimeout,
() => lodashMerge(withSnakeCaseKeys(rumContextProvider()), globalContextProvider()),
beforeUnloadCallback
() => lodashMerge(withSnakeCaseKeys(rumContextProvider()), globalContextProvider())
)
return {
addRumEvent: (event: RumEvent, context?: Context) => {
if (session.isTracked()) {
batch.add({ ...context, ...withSnakeCaseKeys((event as unknown) as Context) })
}
},
beforeFlushOnUnload: (handler: () => void) => batch.beforeFlushOnUnload(handler),
upsertRumEvent: (event: RumEvent, key: string) => {
if (session.isTracked()) {
batch.upsert(withSnakeCaseKeys((event as unknown) as Context), key)
Expand All @@ -248,27 +246,6 @@ function startRumBatch(
}
}

function trackView(lifeCycle: LifeCycle, upsertRumEvent: (event: RumViewEvent, key: string) => void) {
lifeCycle.subscribe(LifeCycleEventType.VIEW_COLLECTED, (view) => {
upsertRumEvent(
{
date: getTimestamp(view.startTime),
duration: msToNs(view.duration),
evt: {
category: RumEventCategory.VIEW,
},
rum: {
documentVersion: view.documentVersion,
},
view: {
measures: view.measures,
},
},
view.id
)
})
}

function trackErrors(lifeCycle: LifeCycle, addRumEvent: (event: RumErrorEvent) => void) {
lifeCycle.subscribe(LifeCycleEventType.ERROR_COLLECTED, ({ message, startTime, context }: ErrorMessage) => {
addRumEvent({
Expand Down
8 changes: 8 additions & 0 deletions packages/rum/src/trackEventCounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,13 @@ export function trackEventCounts(lifeCycle: LifeCycle, callback: (eventCounts: E
subscriptions.forEach((s) => s.unsubscribe())
},
eventCounts,
reset() {
const eventCountsMap = eventCounts as { [key: string]: number }
for (const key in eventCountsMap) {
if (Object.prototype.hasOwnProperty.call(eventCountsMap, key)) {
eventCountsMap[key] = 0
}
}
},
}
}
171 changes: 0 additions & 171 deletions packages/rum/src/viewCollection.ts

This file was deleted.

Loading

0 comments on commit 4cbf78b

Please sign in to comment.