Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

✨ [RUMF-1467] Collect user data telemetry #1941

Merged
merged 19 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/domain/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const enum TelemetryService {
export interface Telemetry {
setContextProvider: (provider: () => Context) => void
observable: Observable<TelemetryEvent & Context>
enabled: boolean
}

const TELEMETRY_EXCLUDED_SITES: string[] = [INTAKE_SITE_US1_FED]
Expand Down Expand Up @@ -90,6 +91,7 @@ export function startTelemetry(telemetryService: TelemetryService, configuration
contextProvider = provider
},
observable,
enabled: telemetryConfiguration.telemetryEnabled,
amortemousque marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export {
Payload,
createHttpRequest,
Batch,
BatchFlushEvent,
canUseEventBridge,
getEventBridge,
startBatchWithReplica,
Expand Down Expand Up @@ -81,7 +82,7 @@ export * from './browser/addEventListener'
export { initConsoleObservable, ConsoleLog } from './domain/console/consoleObservable'
export { BoundedBuffer } from './tools/boundedBuffer'
export { catchUserErrors } from './tools/catchUserErrors'
export { createContextManager } from './tools/contextManager'
export { createContextManager, contextBytesCounter, ContextManager } from './tools/contextManager'
export { limitModification } from './tools/limitModification'
export { ContextHistory, ContextHistoryEntry, CLEAR_OLD_CONTEXTS_INTERVAL } from './tools/contextHistory'
export { readBytesFromStream } from './tools/readBytesFromStream'
Expand Down
56 changes: 55 additions & 1 deletion packages/core/src/tools/contextManager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { createContextManager } from './contextManager'
import { contextBytesCounterStub } from '../../test/specHelper'
import { contextBytesCounter, createContextManager } from './contextManager'
import { computeBytesCount } from './utils'

describe('createContextManager', () => {
it('starts with an empty context', () => {
Expand Down Expand Up @@ -73,4 +75,56 @@ describe('createContextManager', () => {
manager.clearContext()
expect(manager.getContext()).toEqual({})
})

it('should invalidate the context bytes counter at each mutation', () => {
const bytesCountStub = contextBytesCounterStub()
const manager = createContextManager(bytesCountStub)
manager.add('foo', 'bar')
manager.remove('foo')
manager.set({ foo: 'bar' })
manager.removeContextProperty('foo')
manager.setContext({ foo: 'bar' })
manager.clearContext()

expect(bytesCountStub.invalidate).toHaveBeenCalledTimes(6)
})

it('should get the context bytes count', () => {
const bytesCountStub = contextBytesCounterStub()
const manager = createContextManager(bytesCountStub)
const contextBytesCount = manager.getBytesCount()

expect(contextBytesCount).toEqual(1)
})
})

describe('contextBytesCounter', () => {
let computeBytesCountSpy: jasmine.Spy
let counter: ReturnType<typeof contextBytesCounter>

beforeEach(() => {
computeBytesCountSpy = jasmine.createSpy('computeBytesCount', computeBytesCount).and.callThrough()
counter = contextBytesCounter(computeBytesCountSpy)
})

it('should compute the batch count when the cache is invalidate', () => {
const bytesCount1 = counter.compute({ a: 'b' })
counter.invalidate()
const bytesCount2 = counter.compute({ foo: 'bar' })

expect(computeBytesCountSpy).toHaveBeenCalledTimes(2)
expect(bytesCount1).not.toEqual(bytesCount2)
})

it('should use the cached bytes count when the cache is not invalidate', () => {
const bytesCount1 = counter.compute({ a: 'b' })
const bytesCount2 = counter.compute({ foo: 'bar' })
expect(computeBytesCountSpy).toHaveBeenCalledTimes(1)
expect(bytesCount1).toEqual(bytesCount2)
})

it('should return a bytes count at 0 when the object is empty', () => {
const bytesCount = counter.compute({})
expect(bytesCount).toEqual(0)
})
})
33 changes: 30 additions & 3 deletions packages/core/src/tools/contextManager.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,72 @@
import { deepClone } from './utils'

import { computeBytesCount, deepClone, isEmptyObject, jsonStringify } from './utils'
import type { Context, ContextValue } from './context'

export function createContextManager() {
export type ContextManager = ReturnType<typeof createContextManager>

export function createContextManager(bytesCounter = contextBytesCounter()) {
let context: Context = {}

return {
getBytesCount: () => bytesCounter.compute(context),
/** @deprecated use getContext instead */
get: () => context,

/** @deprecated use setContextProperty instead */
add: (key: string, value: any) => {
context[key] = value as ContextValue
bytesCounter.invalidate()
},

/** @deprecated renamed to removeContextProperty */
remove: (key: string) => {
delete context[key]
bytesCounter.invalidate()
},

/** @deprecated use setContext instead */
set: (newContext: object) => {
context = newContext as Context
bytesCounter.invalidate()
},

getContext: () => deepClone(context),

setContext: (newContext: Context) => {
context = deepClone(newContext)
bytesCounter.invalidate()
},

setContextProperty: (key: string, property: any) => {
context[key] = deepClone(property)
bytesCounter.invalidate()
},

removeContextProperty: (key: string) => {
delete context[key]
bytesCounter.invalidate()
},

clearContext: () => {
context = {}
bytesCounter.invalidate()
},
}
}

export type ContextBytesCounter = ReturnType<typeof contextBytesCounter>

export function contextBytesCounter(computeBytesCountImpl = computeBytesCount) {
let bytesCount: number | undefined

return {
compute: (context: Context) => {
if (bytesCount === undefined) {
bytesCount = !isEmptyObject(context) ? computeBytesCountImpl(jsonStringify(context)!) : 0
amortemousque marked this conversation as resolved.
Show resolved Hide resolved
}
return bytesCount
},
invalidate: () => {
bytesCount = undefined
amortemousque marked this conversation as resolved.
Show resolved Hide resolved
},
}
}
11 changes: 11 additions & 0 deletions packages/core/src/tools/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { display } from './display'
import {
arrayFrom,
combine,
computeBytesCount,
cssEscape,
deepClone,
elementMatches,
Expand Down Expand Up @@ -689,3 +690,13 @@ describe('matchList', () => {
expect(display.error).toHaveBeenCalled()
})
})

describe('computeBytesCount', () => {
it('should count the bytes of a message composed of 1 byte characters', () => {
expect(computeBytesCount('1234')).toEqual(4)
})

it('should count the bytes of a message composed of multiple bytes characters', () => {
expect(computeBytesCount('🪐')).toEqual(4)
})
})
16 changes: 16 additions & 0 deletions packages/core/src/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,3 +594,19 @@ export function cssEscape(str: string) {
return `\\${ch}`
})
}

// eslint-disable-next-line no-control-regex
const HAS_MULTI_BYTES_CHARACTERS = /[^\u0000-\u007F]/

export function computeBytesCount(candidate: string): number {
// Accurate bytes count computations can degrade performances when there is a lot of events to process
if (!HAS_MULTI_BYTES_CHARACTERS.test(candidate)) {
return candidate.length
}

if (window.TextEncoder !== undefined) {
return new TextEncoder().encode(candidate).length
}

return new Blob([candidate]).size
}
17 changes: 9 additions & 8 deletions packages/core/src/transport/batch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { PageExitEvent } from '../browser/pageExitObservable'
import { PageExitReason } from '../browser/pageExitObservable'
import { Observable } from '../tools/observable'
import { noop } from '../tools/utils'
import type { BatchFlushEvent } from './batch'
import { Batch } from './batch'
import type { HttpRequest } from './httpRequest'

Expand All @@ -15,6 +16,7 @@ describe('batch', () => {
let transport: HttpRequest
let sendSpy: jasmine.Spy<HttpRequest['send']>
let pageExitObservable: Observable<PageExitEvent>
let flushNotifySpy: jasmine.Spy<() => BatchFlushEvent>

beforeEach(() => {
transport = { send: noop } as unknown as HttpRequest
Expand All @@ -28,6 +30,7 @@ describe('batch', () => {
FLUSH_TIMEOUT,
pageExitObservable
)
flushNotifySpy = spyOn(batch.flushObservable, 'notify')
})

it('should add context to message', () => {
Expand All @@ -51,14 +54,6 @@ describe('batch', () => {
expect(transport.send).not.toHaveBeenCalled()
})

it('should count the bytes of a message composed of 1 byte characters', () => {
expect(batch.computeBytesCount('1234')).toEqual(4)
})

it('should count the bytes of a message composed of multiple bytes characters', () => {
expect(batch.computeBytesCount('🪐')).toEqual(4)
})

it('should flush when the message count limit is reached', () => {
batch.add({ message: '1' })
batch.add({ message: '2' })
Expand Down Expand Up @@ -189,4 +184,10 @@ describe('batch', () => {
batch.flush()
expect(sendSpy).toHaveBeenCalledTimes(2)
})

it('should notify when the batch is flushed', () => {
batch.add({})
batch.flush()
expect(flushNotifySpy).toHaveBeenCalledOnceWith({ bufferBytesCount: 2, bufferMessagesCount: 1 })
})
})
35 changes: 15 additions & 20 deletions packages/core/src/transport/batch.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { display } from '../tools/display'
import type { Context } from '../tools/context'
import { jsonStringify, objectValues } from '../tools/utils'
import { computeBytesCount, jsonStringify, objectValues } from '../tools/utils'
import { monitor } from '../tools/monitor'
import type { Observable } from '../tools/observable'
import { Observable } from '../tools/observable'
import type { PageExitEvent } from '../browser/pageExitObservable'
import type { HttpRequest } from './httpRequest'

// https://en.wikipedia.org/wiki/UTF-8
// eslint-disable-next-line no-control-regex
const HAS_MULTI_BYTES_CHARACTERS = /[^\u0000-\u007F]/
export interface BatchFlushEvent {
bufferBytesCount: number
bufferMessagesCount: number
}

export class Batch {
flushObservable = new Observable<BatchFlushEvent>()

private pushOnlyBuffer: string[] = []
private upsertBuffer: { [key: string]: string } = {}
private bufferBytesCount = 0
Expand Down Expand Up @@ -41,6 +44,11 @@ export class Batch {
const messages = this.pushOnlyBuffer.concat(objectValues(this.upsertBuffer))
const bytesCount = this.bufferBytesCount

this.flushObservable.notify({
bufferBytesCount: this.bufferBytesCount,
bufferMessagesCount: this.bufferMessagesCount,
})

this.pushOnlyBuffer = []
this.upsertBuffer = {}
this.bufferBytesCount = 0
Expand All @@ -50,19 +58,6 @@ export class Batch {
}
}

computeBytesCount(candidate: string) {
// Accurate bytes count computations can degrade performances when there is a lot of events to process
if (!HAS_MULTI_BYTES_CHARACTERS.test(candidate)) {
return candidate.length
}

if (window.TextEncoder !== undefined) {
return new TextEncoder().encode(candidate).length
}

return new Blob([candidate]).size
}

private addOrUpdate(message: Context, key?: string) {
const { processedMessage, messageBytesCount } = this.process(message)
if (messageBytesCount >= this.messageBytesLimit) {
Expand All @@ -86,7 +81,7 @@ export class Batch {

private process(message: Context) {
const processedMessage = jsonStringify(message)!
const messageBytesCount = this.computeBytesCount(processedMessage)
const messageBytesCount = computeBytesCount(processedMessage)
return { processedMessage, messageBytesCount }
}

Expand All @@ -107,7 +102,7 @@ export class Batch {
private remove(key: string) {
const removedMessage = this.upsertBuffer[key]
delete this.upsertBuffer[key]
const messageBytesCount = this.computeBytesCount(removedMessage)
const messageBytesCount = computeBytesCount(removedMessage)
this.bufferBytesCount -= messageBytesCount
this.bufferMessagesCount -= 1
if (this.bufferMessagesCount > 0) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/transport/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { HttpRequest, createHttpRequest, Payload, RetryInfo } from './httpRequest'
export { Batch } from './batch'
export { Batch, BatchFlushEvent } from './batch'
export { canUseEventBridge, getEventBridge, BrowserWindowWithEventBridge } from './eventBridge'
export { startBatchWithReplica } from './startBatchWithReplica'
8 changes: 8 additions & 0 deletions packages/core/test/specHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { EndpointBuilder } from '../src/domain/configuration'
import type { ContextBytesCounter } from '../src/tools/contextManager'
import { instrumentMethod } from '../src/tools/instrumentMethod'
import { resetNavigationStart } from '../src/tools/timeUtils'
import { buildUrl } from '../src/tools/urlPolyfill'
Expand Down Expand Up @@ -499,6 +500,13 @@ export function interceptRequests() {
}
}

export function contextBytesCounterStub(): ContextBytesCounter {
return {
compute: () => 1,
invalidate: jasmine.createSpy('invalidate'),
}
}

export function isAdoptedStyleSheetsSupported() {
return Boolean((document as any).adoptedStyleSheets)
}
6 changes: 3 additions & 3 deletions packages/rum-core/src/boot/rumPublicApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ describe('rum public api', () => {

rumPublicApi.init(MANUAL_CONFIGURATION)
expect(startRumSpy).toHaveBeenCalled()
expect(startRumSpy.calls.argsFor(0)[4]).toEqual({ name: 'foo' })
expect(startRumSpy.calls.argsFor(0)[6]).toEqual({ name: 'foo' })
expect(recorderApiOnRumStartSpy).toHaveBeenCalled()
expect(startViewSpy).not.toHaveBeenCalled()
})
Expand All @@ -777,7 +777,7 @@ describe('rum public api', () => {

rumPublicApi.startView('foo')
expect(startRumSpy).toHaveBeenCalled()
expect(startRumSpy.calls.argsFor(0)[4]).toEqual({ name: 'foo' })
expect(startRumSpy.calls.argsFor(0)[6]).toEqual({ name: 'foo' })
expect(recorderApiOnRumStartSpy).toHaveBeenCalled()
expect(startViewSpy).not.toHaveBeenCalled()
})
Expand All @@ -788,7 +788,7 @@ describe('rum public api', () => {
rumPublicApi.startView('bar')

expect(startRumSpy).toHaveBeenCalled()
expect(startRumSpy.calls.argsFor(0)[4]).toEqual({ name: 'foo' })
expect(startRumSpy.calls.argsFor(0)[6]).toEqual({ name: 'foo' })
expect(recorderApiOnRumStartSpy).toHaveBeenCalled()
expect(startViewSpy).toHaveBeenCalled()
expect(startViewSpy.calls.argsFor(0)[0]).toEqual({ name: 'bar' })
Expand Down