-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb89c9b
commit c03dad4
Showing
72 changed files
with
287 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
* | ||
!/cjs/**/* | ||
!/esm/**/* | ||
!/src/**/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
save-exact true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# `rum-core` | ||
|
||
Datadog browser RUM core utilities. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.