Skip to content

Commit

Permalink
🚚 create browser-rum-core
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Jan 6, 2021
1 parent eb89c9b commit c03dad4
Show file tree
Hide file tree
Showing 72 changed files with 287 additions and 199 deletions.
4 changes: 4 additions & 0 deletions packages/rum-core/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*
!/cjs/**/*
!/esm/**/*
!/src/**/*
2 changes: 2 additions & 0 deletions packages/rum-core/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
save-exact true

3 changes: 3 additions & 0 deletions packages/rum-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# `rum-core`

Datadog browser RUM core utilities.
26 changes: 26 additions & 0 deletions packages/rum-core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@datadog/browser-rum-core",
"version": "2.1.2",
"license": "Apache-2.0",
"main": "cjs/index.js",
"module": "esm/index.js",
"types": "cjs/index.d.ts",
"scripts": {
"build": "run-p build:cjs build:esm",
"build:cjs": "rm -rf cjs && tsc -p tsconfig.cjs.json && yarn replace-build-env cjs",
"build:esm": "rm -rf esm && tsc -p tsconfig.esm.json && yarn replace-build-env esm",
"replace-build-env": "node ../../scripts/replace-build-env.js"
},
"dependencies": {
"@datadog/browser-core": "2.1.2",
"tslib": "^1.10.0"
},
"devDependencies": {
"ajv": "6.12.6"
},
"repository": {
"type": "git",
"url": "https://github.com/DataDog/browser-sdk.git",
"directory": "packages/rum-core"
}
}
File renamed without changes.
189 changes: 189 additions & 0 deletions packages/rum-core/src/boot/rum.entry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import {
BoundedBuffer,
buildCookieOptions,
checkCookiesAuthorized,
checkIsNotLocalFile,
Context,
createContextManager,
deepClone,
ErrorSource,
isPercentage,
makeGlobal,
monitor,
UserConfiguration,
} from '@datadog/browser-core'
import { ActionType, CustomAction } from '../domain/rumEventsCollection/action/trackActions'
import { ProvidedError, ProvidedSource } from '../domain/rumEventsCollection/error/errorCollection'
import { CommonContext, User } from '../rawRumEvent.types'
import { RumEvent } from '../rumEvent.types'
import { startRum } from './rum'

export interface RumUserConfiguration extends UserConfiguration {
applicationId: string
beforeSend?: (event: RumEvent) => void
}

export type RumGlobal = ReturnType<typeof makeRumGlobal>

export type StartRum = typeof startRum

export function makeRumGlobal(startRumImpl: StartRum) {
let isAlreadyInitialized = false

const globalContextManager = createContextManager()
let user: User = {}

let getInternalContextStrategy: ReturnType<StartRum>['getInternalContext'] = () => {
return undefined
}

const beforeInitAddAction = new BoundedBuffer<[CustomAction, CommonContext]>()
let addActionStrategy: ReturnType<StartRum>['addAction'] = (action) => {
beforeInitAddAction.add([action, clonedCommonContext()])
}

const beforeInitAddError = new BoundedBuffer<[ProvidedError, CommonContext]>()
let addErrorStrategy: ReturnType<StartRum>['addError'] = (providedError) => {
beforeInitAddError.add([providedError, clonedCommonContext()])
}

function clonedCommonContext(): CommonContext {
return deepClone({
context: globalContextManager.get(),
user: user as Context,
})
}

const rumGlobal = makeGlobal({
init: monitor((userConfiguration: RumUserConfiguration) => {
if (
!checkCookiesAuthorized(buildCookieOptions(userConfiguration)) ||
!checkIsNotLocalFile() ||
!canInitRum(userConfiguration)
) {
return
}
if (userConfiguration.publicApiKey) {
userConfiguration.clientToken = userConfiguration.publicApiKey
}

;({
addAction: addActionStrategy,
addError: addErrorStrategy,
getInternalContext: getInternalContextStrategy,
} = startRumImpl(userConfiguration, () => ({
user,
context: globalContextManager.get(),
})))
beforeInitAddAction.drain(([action, commonContext]) => addActionStrategy(action, commonContext))
beforeInitAddError.drain(([error, commonContext]) => addErrorStrategy(error, commonContext))

isAlreadyInitialized = true
}),

addRumGlobalContext: monitor(globalContextManager.add),

removeRumGlobalContext: monitor(globalContextManager.remove),

getRumGlobalContext: monitor(globalContextManager.get),
setRumGlobalContext: monitor(globalContextManager.set),

getInternalContext: monitor((startTime?: number) => {
return getInternalContextStrategy(startTime)
}),

addAction: monitor((name: string, context?: object) => {
addActionStrategy({
name,
context: deepClone(context as Context),
startTime: performance.now(),
type: ActionType.CUSTOM,
})
}),

/**
* @deprecated
* @see addAction
*/
addUserAction: (name: string, context?: object) => {
rumGlobal.addAction(name, context as Context)
},

addError: monitor((error: unknown, context?: object, 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 as Context),
source: checkedSource,
startTime: performance.now(),
})
}),

setUser: monitor((newUser: User) => {
const sanitizedUser = sanitizeUser(newUser)
if (sanitizedUser) {
user = sanitizedUser
} else {
console.error('Unsupported user:', newUser)
}
}),
})
return rumGlobal

function sanitizeUser(newUser: unknown) {
if (typeof newUser !== 'object' || !newUser) {
return
}
const result = deepClone(newUser as Context)
if ('id' in result) {
result.id = String(result.id)
}
if ('name' in result) {
result.name = String(result.name)
}
if ('email' in result) {
result.email = String(result.email)
}
return result
}

function canInitRum(userConfiguration: RumUserConfiguration) {
if (isAlreadyInitialized) {
if (!userConfiguration.silentMultipleInit) {
console.error('DD_RUM is already initialized.')
}
return false
}
if (!userConfiguration || (!userConfiguration.clientToken && !userConfiguration.publicApiKey)) {
console.error('Client Token is not configured, we will not send any data.')
return false
}
if (!userConfiguration.applicationId) {
console.error('Application ID is not configured, no RUM data will be collected.')
return false
}
if (userConfiguration.sampleRate !== undefined && !isPercentage(userConfiguration.sampleRate)) {
console.error('Sample Rate should be a number between 0 and 100')
return false
}
if (userConfiguration.resourceSampleRate !== undefined && !isPercentage(userConfiguration.resourceSampleRate)) {
console.error('Resource Sample Rate should be a number between 0 and 100')
return false
}
if (
Array.isArray(userConfiguration.allowedTracingOrigins) &&
userConfiguration.allowedTracingOrigins.length !== 0 &&
userConfiguration.service === undefined
) {
console.error('Service need to be configured when tracing is enabled')
return false
}
return true
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions packages/rum-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export { RumUserConfiguration, RumGlobal, makeRumGlobal } from './boot/rum.entry'
export { ProvidedSource } from './domain/rumEventsCollection/error/errorCollection'
export {
RumEvent,
RumActionEvent,
CommonProperties,
RumErrorEvent,
RumViewEvent,
RumResourceEvent,
RumLongTaskEvent,
} from './rumEvent.types'
export { startRum } from './boot/rum'
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions packages/rum-core/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"declaration": true,
"module": "commonjs",
"outDir": "./cjs/"
},
"include": ["./src/**/*.ts"],
"exclude": ["./src/**/*.spec.ts"]
}
11 changes: 11 additions & 0 deletions packages/rum-core/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"declaration": true,
"module": "es6",
"outDir": "./esm/"
},
"include": ["./src/**/*.ts"],
"exclude": ["./src/**/*.spec.ts"]
}
12 changes: 12 additions & 0 deletions packages/rum-core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "./bundle/",
"module": "es6",
"paths": {
"@datadog/browser-core": ["../core/src"]
}
},
"include": ["src", "test"]
}
1 change: 1 addition & 0 deletions packages/rum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"dependencies": {
"@datadog/browser-core": "2.1.2",
"@datadog/browser-rum-core": "2.1.2",
"tslib": "^1.10.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit c03dad4

Please sign in to comment.