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

✨[init] add extra checks #191

Merged
merged 2 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { DEFAULT_CONFIGURATION, Configuration, UserConfiguration } from './configuration'
export { ErrorMessage, ErrorContext, HttpContext, ErrorOrigin, ErrorObservable } from './errorCollection'
export { BuildEnv, Datacenter, Environment, makeStub, makeGlobal, commonInit } from './init'
export { BuildEnv, Datacenter, Environment, makeStub, makeGlobal, commonInit, isValidBrowsingContext } from './init'
export { MonitoringMessage, monitored, monitor, addMonitoringMessage } from './internalMonitoring'
export { Observable } from './observable'
export { RequestType, RequestDetails, startRequestCollection, RequestObservable } from './requestCollection'
Expand All @@ -12,6 +12,6 @@ export {
} from './sessionManagement'
export { HttpRequest, Batch } from './transport'
export * from './utils'
export { areCookiesAuthorized, getCookie, setCookie, COOKIE_ACCESS_DELAY } from './cookie'
export { getCookie, setCookie, COOKIE_ACCESS_DELAY } from './cookie'

export * from './specHelper'
23 changes: 23 additions & 0 deletions packages/core/src/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildConfiguration, UserConfiguration } from './configuration'
import { areCookiesAuthorized } from './cookie'
import { startErrorCollection } from './errorCollection'
import { setDebugMode, startInternalMonitoring } from './internalMonitoring'

Expand Down Expand Up @@ -29,6 +30,7 @@ export interface BuildEnv {
env: Environment
version: string
}

export function commonInit(userConfiguration: UserConfiguration, buildEnv: BuildEnv) {
const configuration = buildConfiguration(userConfiguration, buildEnv)
startInternalMonitoring(configuration)
Expand All @@ -39,3 +41,24 @@ export function commonInit(userConfiguration: UserConfiguration, buildEnv: Build
errorObservable,
}
}

export function isValidBrowsingContext() {
if (!areCookiesAuthorized()) {
console.error('Cookies are not authorized, we will not send any data.')
return false
}
if (isDocPrerendered() || isLocalFile()) {
console.error('Execution is not allowed in the current context.')
return false
}
return true
}

function isLocalFile() {
return window.location.protocol === 'file:'
}

function isDocPrerendered() {
// https://www.w3.org/TR/resource-hints/#dfn-prerender
return document.visibilityState === 'prerender'
}
34 changes: 19 additions & 15 deletions packages/logs/src/logs.entry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
areCookiesAuthorized,
commonInit,
Context,
ContextValue,
isPercentage,
isValidBrowsingContext,
makeGlobal,
makeStub,
monitor,
Expand Down Expand Up @@ -77,26 +77,14 @@ export type LogsGlobal = typeof STUBBED_LOGS
export const datadogLogs = makeGlobal(STUBBED_LOGS)
let isAlreadyInitialized = false
datadogLogs.init = monitor((userConfiguration: LogsUserConfiguration) => {
if (isAlreadyInitialized) {
console.error('DD_LOGS is already initialized.')
return
}
if (!areCookiesAuthorized()) {
console.error('Cookies are not authorized, we will not send any data.')
return
}
if (!userConfiguration || (!userConfiguration.publicApiKey && !userConfiguration.clientToken)) {
console.error('Client Token is not configured, we will not send any data.')
if (!isValidBrowsingContext() || !canInitLogs(userConfiguration)) {
return
}

if (userConfiguration.publicApiKey) {
userConfiguration.clientToken = userConfiguration.publicApiKey
console.warn('Public API Key is deprecated. Please use Client Token instead.')
}
if (userConfiguration.sampleRate !== undefined && !isPercentage(userConfiguration.sampleRate)) {
console.error('Sample Rate should be a number between 0 and 100')
return
}
const isCollectingError = userConfiguration.forwardErrorsToLogs !== false
const logsUserConfiguration = {
...userConfiguration,
Expand All @@ -109,6 +97,22 @@ datadogLogs.init = monitor((userConfiguration: LogsUserConfiguration) => {
isAlreadyInitialized = true
})

function canInitLogs(userConfiguration: LogsUserConfiguration) {
if (isAlreadyInitialized) {
console.error('DD_LOGS is already initialized.')
return false
}
if (!userConfiguration || (!userConfiguration.publicApiKey && !userConfiguration.clientToken)) {
console.error('Client Token is not configured, we will not send any data.')
return false
}
if (userConfiguration.sampleRate !== undefined && !isPercentage(userConfiguration.sampleRate)) {
console.error('Sample Rate should be a number between 0 and 100')
return false
}
return true
}

declare global {
interface Window {
DD_LOGS?: LogsGlobal
Expand Down
49 changes: 26 additions & 23 deletions packages/rum/src/rum.entry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
areCookiesAuthorized,
commonInit,
Context,
ContextValue,
isPercentage,
isValidBrowsingContext,
makeGlobal,
makeStub,
monitor,
Expand Down Expand Up @@ -54,33 +54,12 @@ export type RumGlobal = typeof STUBBED_RUM
export const datadogRum = makeGlobal(STUBBED_RUM)
let isAlreadyInitialized = false
datadogRum.init = monitor((userConfiguration: RumUserConfiguration) => {
if (isAlreadyInitialized) {
console.error('DD_RUM is already initialized.')
return
}
if (!areCookiesAuthorized()) {
console.error('Cookies are not authorized, we will not send any data.')
return
}
if (!userConfiguration || (!userConfiguration.clientToken && !userConfiguration.publicApiKey)) {
console.error('Client Token is not configured, we will not send any data.')
if (!isValidBrowsingContext() || !canInitRum(userConfiguration)) {
return
}
if (userConfiguration.publicApiKey) {
userConfiguration.clientToken = userConfiguration.publicApiKey
}
if (!userConfiguration.applicationId) {
console.error('Application ID is not configured, no RUM data will be collected.')
return
}
if (userConfiguration.sampleRate !== undefined && !isPercentage(userConfiguration.sampleRate)) {
console.error('Sample Rate should be a number between 0 and 100')
return
}
if (userConfiguration.resourceSampleRate !== undefined && !isPercentage(userConfiguration.resourceSampleRate)) {
console.error('Resource Sample Rate should be a number between 0 and 100')
return
}
const rumUserConfiguration = { ...userConfiguration, isCollectingError: true }
const lifeCycle = new LifeCycle()

Expand All @@ -99,6 +78,30 @@ datadogRum.init = monitor((userConfiguration: RumUserConfiguration) => {
isAlreadyInitialized = true
})

function canInitRum(userConfiguration: RumUserConfiguration) {
if (isAlreadyInitialized) {
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
}
return true
}

declare global {
interface Window {
DD_RUM?: RumGlobal
Expand Down