From 1f35d65f76d10a1206bd7ec5bdbed458b25ca7a8 Mon Sep 17 00:00:00 2001 From: "maxime.quentin" Date: Thu, 6 Feb 2020 12:50:14 +0100 Subject: [PATCH] [RUM-291]-SDK: send browser logs even when cookies are disabled - set a different RUM and LOGS valid browsing context / do not pass a sessionId when the cookies are off --- packages/core/src/index.ts | 4 +++- packages/core/src/init.ts | 32 +++++++++++++++++++++--------- packages/logs/src/logger.ts | 10 ++++++---- packages/logs/src/loggerSession.ts | 16 +++++++++++---- packages/logs/src/logs.entry.ts | 3 ++- packages/rum/src/rum.entry.ts | 5 +++-- 6 files changed, 49 insertions(+), 21 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index f3060ca32a..e49f24b357 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,6 +1,8 @@ export { DEFAULT_CONFIGURATION, Configuration, UserConfiguration } from './configuration' export { ErrorMessage, ErrorContext, HttpContext, ErrorOrigin, ErrorObservable } from './errorCollection' -export { BuildEnv, Datacenter, Environment, makeStub, makeGlobal, commonInit, isValidBrowsingContext } from './init' +export { + BrowsingContext, 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' diff --git a/packages/core/src/init.ts b/packages/core/src/init.ts index 92f6667be6..b808510f6f 100644 --- a/packages/core/src/init.ts +++ b/packages/core/src/init.ts @@ -25,6 +25,11 @@ export function makeGlobal(stub: T): T { export type Datacenter = 'eu' | 'us' export type Environment = 'production' | 'staging' | 'e2e-test' +export enum BrowsingContext { + RUM = 'rum', + LOGS = 'logs' +} + export interface BuildEnv { datacenter: Datacenter env: Environment @@ -42,16 +47,25 @@ export function commonInit(userConfiguration: UserConfiguration, buildEnv: Build } } -export function isValidBrowsingContext() { - if (!areCookiesAuthorized()) { - console.error('Cookies are not authorized, we will not send any data.') - return false - } - if (isLocalFile()) { - console.error('Execution is not allowed in the current context.') - return false +export function isValidBrowsingContext(browsingContext: BrowsingContext) { + switch (browsingContext) { + case BrowsingContext.RUM: + if (!areCookiesAuthorized()) { + console.error('Cookies are not authorized, RUM will not send any data.') + return false + } + if (isLocalFile()) { + console.error('Execution is not allowed in the current context.') + return false + } + return true + case BrowsingContext.LOGS: + if (isLocalFile()) { + console.error('Execution is not allowed in the current context.') + return false + } + return true } - return true } function isLocalFile() { diff --git a/packages/logs/src/logger.ts b/packages/logs/src/logger.ts index 4cb23feda1..c8c6e02eaa 100644 --- a/packages/logs/src/logger.ts +++ b/packages/logs/src/logger.ts @@ -14,6 +14,7 @@ import lodashMerge from 'lodash.merge' import { LoggerSession } from './loggerSession' import { LogsGlobal } from './logs.entry' +import { areCookiesAuthorized } from '../../core/src/cookie' export enum StatusType { debug = 'debug', @@ -34,6 +35,7 @@ export const STATUSES = Object.keys(StatusType) export interface LogsMessage { message: string status: StatusType + [key: string]: ContextValue } @@ -63,15 +65,15 @@ export function startLogger(errorObservable: ErrorObservable, configuration: Con lodashMerge( { date: new Date().getTime(), - session_id: session.getId(), view: { referrer: document.referrer, url: window.location.href, }, }, + (areCookiesAuthorized()) ? { session_id: session.getId() } : {}, globalContext, - getRUMInternalContext() - ) as Context + getRUMInternalContext(), + ) as Context, ) const handlers = { [HandlerType.console]: (message: LogsMessage) => console.log(`${message.status}: ${message.message}`), @@ -119,7 +121,7 @@ export class Logger { private handlers: { [key in HandlerType]: (message: LogsMessage) => void }, handler = HandlerType.http, private level = StatusType.debug, - private loggerContext: Context = {} + private loggerContext: Context = {}, ) { this.handler = this.handlers[handler] } diff --git a/packages/logs/src/loggerSession.ts b/packages/logs/src/loggerSession.ts index d3b439ae4c..fc31256bf9 100644 --- a/packages/logs/src/loggerSession.ts +++ b/packages/logs/src/loggerSession.ts @@ -17,18 +17,26 @@ export function startLoggerSession(configuration: Configuration): LoggerSession return { getId: session.getId, - isTracked: () => session.getType() === LoggerSessionType.TRACKED, + isTracked: () => (session.getType()) ? session.getType() === LoggerSessionType.TRACKED : computeSessionType(configuration) === LoggerSessionType.TRACKED, } } +function computeSessionType(configuration: Configuration): string { + let sessionType + if (!performDraw(configuration.sampleRate)) { + sessionType = LoggerSessionType.NOT_TRACKED + } else { + sessionType = LoggerSessionType.TRACKED + } + return sessionType +} + function computeSessionState(configuration: Configuration, rawSessionType?: string) { let sessionType if (hasValidLoggerSession(rawSessionType)) { sessionType = rawSessionType - } else if (!performDraw(configuration.sampleRate)) { - sessionType = LoggerSessionType.NOT_TRACKED } else { - sessionType = LoggerSessionType.TRACKED + sessionType = computeSessionType(configuration) } return { isTracked: sessionType === LoggerSessionType.TRACKED, diff --git a/packages/logs/src/logs.entry.ts b/packages/logs/src/logs.entry.ts index 0dd9d0a064..8e2d0ec30c 100644 --- a/packages/logs/src/logs.entry.ts +++ b/packages/logs/src/logs.entry.ts @@ -1,4 +1,5 @@ import { + BrowsingContext, commonInit, Context, ContextValue, @@ -77,7 +78,7 @@ export type LogsGlobal = typeof STUBBED_LOGS export const datadogLogs = makeGlobal(STUBBED_LOGS) let isAlreadyInitialized = false datadogLogs.init = monitor((userConfiguration: LogsUserConfiguration) => { - if (!isValidBrowsingContext() || !canInitLogs(userConfiguration)) { + if (!isValidBrowsingContext(BrowsingContext.LOGS) || !canInitLogs(userConfiguration)) { return } diff --git a/packages/rum/src/rum.entry.ts b/packages/rum/src/rum.entry.ts index 6378462236..5aa9879dca 100644 --- a/packages/rum/src/rum.entry.ts +++ b/packages/rum/src/rum.entry.ts @@ -1,4 +1,5 @@ import { + BrowsingContext, commonInit, Context, ContextValue, @@ -54,7 +55,7 @@ export type RumGlobal = typeof STUBBED_RUM export const datadogRum = makeGlobal(STUBBED_RUM) let isAlreadyInitialized = false datadogRum.init = monitor((userConfiguration: RumUserConfiguration) => { - if (!isValidBrowsingContext() || !canInitRum(userConfiguration)) { + if (!isValidBrowsingContext(BrowsingContext.RUM) || !canInitRum(userConfiguration)) { return } if (userConfiguration.publicApiKey) { @@ -70,7 +71,7 @@ datadogRum.init = monitor((userConfiguration: RumUserConfiguration) => { errorObservable.subscribe((errorMessage) => lifeCycle.notify(LifeCycleEventType.ERROR_COLLECTED, errorMessage)) requestObservable.subscribe((requestDetails) => - lifeCycle.notify(LifeCycleEventType.REQUEST_COLLECTED, requestDetails) + lifeCycle.notify(LifeCycleEventType.REQUEST_COLLECTED, requestDetails), ) const globalApi = startRum(rumUserConfiguration.applicationId, lifeCycle, configuration, session)