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

✨ [Browser SDK][RUM-291] Allow logs when cookies are disabled #255

Merged
merged 14 commits into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from 13 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
13 changes: 11 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
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 {
BuildEnv,
Datacenter,
Environment,
makeStub,
makeGlobal,
commonInit,
checkCookiesAuthorized,
checkIsNotLocalFile,
} from './init'
export { InternalMonitoring, MonitoringMessage, monitored, monitor, addMonitoringMessage } from './internalMonitoring'
export { Observable } from './observable'
export { RequestType, RequestDetails, startRequestCollection, RequestObservable } from './requestCollection'
Expand All @@ -12,6 +21,6 @@ export {
} from './sessionManagement'
export { HttpRequest, Batch } from './transport'
export * from './utils'
export { getCookie, setCookie, COOKIE_ACCESS_DELAY } from './cookie'
export { areCookiesAuthorized, getCookie, setCookie, COOKIE_ACCESS_DELAY } from './cookie'

export * from './specHelper'
6 changes: 5 additions & 1 deletion packages/core/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ export function commonInit(userConfiguration: UserConfiguration, buildEnv: Build
}
}

export function isValidBrowsingContext() {
export function checkCookiesAuthorized() {
if (!areCookiesAuthorized()) {
console.error('Cookies are not authorized, we will not send any data.')
return false
}
return true
}

export function checkIsNotLocalFile() {
if (isLocalFile()) {
console.error('Execution is not allowed in the current context.')
return false
Expand Down
26 changes: 16 additions & 10 deletions packages/logs/src/loggerSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,30 @@ export enum LoggerSessionType {
TRACKED = '1',
}

export function startLoggerSession(configuration: Configuration): LoggerSession {
export function startLoggerSession(configuration: Configuration, areCookieAuthorized: boolean): LoggerSession {
if (!areCookieAuthorized) {
const isTracked = computeSessionType(configuration) === LoggerSessionType.TRACKED
return {
getId: () => undefined,
isTracked: () => isTracked,
}
}
mquentin marked this conversation as resolved.
Show resolved Hide resolved
const session = startSessionManagement(LOGGER_COOKIE_NAME, (rawType) => computeSessionState(configuration, rawType))

return {
getId: session.getId,
isTracked: () => session.getType() === LoggerSessionType.TRACKED,
}
}

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
function computeSessionType(configuration: Configuration): string {
if (!performDraw(configuration.sampleRate)) {
return LoggerSessionType.NOT_TRACKED
}
return LoggerSessionType.TRACKED
}

function computeSessionState(configuration: Configuration, rawSessionType?: string) {
const sessionType = hasValidLoggerSession(rawSessionType) ? rawSessionType : computeSessionType(configuration)
return {
isTracked: sessionType === LoggerSessionType.TRACKED,
type: sessionType,
Expand Down
8 changes: 4 additions & 4 deletions packages/logs/src/logs.entry.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
areCookiesAuthorized,
checkIsNotLocalFile,
commonInit,
Context,
ContextValue,
isPercentage,
isValidBrowsingContext,
makeGlobal,
makeStub,
monitor,
UserConfiguration,
} from '@datadog/browser-core'
import lodashAssign from 'lodash.assign'

import { buildEnv } from './buildEnv'
import { HandlerType, Logger, LoggerConfiguration, startLogger, StatusType } from './logger'
import { startLoggerSession } from './loggerSession'
Expand Down Expand Up @@ -77,7 +77,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 (!checkIsNotLocalFile() || !canInitLogs(userConfiguration)) {
return
}

Expand All @@ -91,7 +91,7 @@ datadogLogs.init = monitor((userConfiguration: LogsUserConfiguration) => {
isCollectingError,
}
const { errorObservable, configuration, internalMonitoring } = commonInit(logsUserConfiguration, buildEnv)
const session = startLoggerSession(configuration)
const session = startLoggerSession(configuration, areCookiesAuthorized())
const globalApi = startLogger(errorObservable, configuration, session, internalMonitoring)
lodashAssign(datadogLogs, globalApi)
isAlreadyInitialized = true
Expand Down
17 changes: 12 additions & 5 deletions packages/logs/test/loggerSession.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('logger session', () => {
it('when tracked should store session type and id', () => {
tracked = true

startLoggerSession(configuration as Configuration)
startLoggerSession(configuration as Configuration, true)

expect(getCookie(LOGGER_COOKIE_NAME)).toEqual(LoggerSessionType.TRACKED)
expect(getCookie(SESSION_COOKIE_NAME)).toMatch(/^[a-f0-9-]+$/)
Expand All @@ -39,7 +39,7 @@ describe('logger session', () => {
it('when not tracked should store session type', () => {
tracked = false

startLoggerSession(configuration as Configuration)
startLoggerSession(configuration as Configuration, true)

expect(getCookie(LOGGER_COOKIE_NAME)).toEqual(LoggerSessionType.NOT_TRACKED)
expect(getCookie(SESSION_COOKIE_NAME)).toBeUndefined()
Expand All @@ -49,7 +49,7 @@ describe('logger session', () => {
setCookie(LOGGER_COOKIE_NAME, LoggerSessionType.TRACKED, DURATION)
setCookie(SESSION_COOKIE_NAME, 'abcdef', DURATION)

startLoggerSession(configuration as Configuration)
startLoggerSession(configuration as Configuration, true)

expect(getCookie(LOGGER_COOKIE_NAME)).toEqual(LoggerSessionType.TRACKED)
expect(getCookie(SESSION_COOKIE_NAME)).toEqual('abcdef')
Expand All @@ -58,13 +58,13 @@ describe('logger session', () => {
it('when not tracked should keep existing session type', () => {
setCookie(LOGGER_COOKIE_NAME, LoggerSessionType.NOT_TRACKED, DURATION)

startLoggerSession(configuration as Configuration)
startLoggerSession(configuration as Configuration, true)

expect(getCookie(LOGGER_COOKIE_NAME)).toEqual(LoggerSessionType.NOT_TRACKED)
})

it('should renew on activity after expiration', () => {
startLoggerSession(configuration as Configuration)
startLoggerSession(configuration as Configuration, true)

setCookie(LOGGER_COOKIE_NAME, '', DURATION)
setCookie(SESSION_COOKIE_NAME, '', DURATION)
Expand All @@ -78,4 +78,11 @@ describe('logger session', () => {
expect(getCookie(LOGGER_COOKIE_NAME)).toEqual(LoggerSessionType.TRACKED)
expect(getCookie(SESSION_COOKIE_NAME)).toMatch(/^[a-f0-9-]+$/)
})

it('when no cookies available, isTracked is computed at each call and getId is undefined', () => {
const session = startLoggerSession(configuration as Configuration, false)

expect(session.getId()).toBeUndefined()
expect(session.isTracked()).toMatch(/true|false/)
})
})
5 changes: 3 additions & 2 deletions packages/rum/src/rum.entry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
checkCookiesAuthorized,
checkIsNotLocalFile,
commonInit,
Context,
ContextValue,
isPercentage,
isValidBrowsingContext,
makeGlobal,
makeStub,
monitor,
Expand Down Expand Up @@ -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 (!checkCookiesAuthorized() || !checkIsNotLocalFile() || !canInitRum(userConfiguration)) {
return
}
if (userConfiguration.publicApiKey) {
Expand Down