Skip to content

Commit

Permalink
:✨⚗ [RUMF-1047] implement a RUM synthetics entry point (#1188)
Browse files Browse the repository at this point in the history
* ✨ create a new entry point for synthetics

* 👷📝 validate and document existing build modes

This commit has nothing much to do with the PR, but was introduced in a
previous PR and still relevant.

* 📝 document the test to add some context, and fix a unit test title

* 👌 move RUM entry points

* 👌 move Logs entry point

* 👌 move RUM-slim entry point

* add tsconfig "rootDir"

Fix the rum-slim package changing its directory structure because it has
no other module than entries/main:
https://github.com/Microsoft/TypeScript/wiki/FAQ#why-does---outdir-moves-output-after-adding-a-new-file

While this option is not required for other packages, it is still a good
failsafe to make sure the directory structure changes when moving
modules around.
  • Loading branch information
BenoitZugmeyer authored and bcaudan committed Dec 7, 2021
1 parent d701278 commit f1b4755
Show file tree
Hide file tree
Showing 38 changed files with 215 additions and 108 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ module.exports = {
},
},
{
files: ['packages/{rum,logs,rum-slim}/src/index.ts', 'packages/rum/src/internal.ts'],
files: ['packages/{rum,logs,rum-slim}/src/entries/*.ts'],
rules: {
'local-rules/disallow-enum-exports': 'error',
},
Expand Down
9 changes: 3 additions & 6 deletions eslint-local-rules/disallowSideEffects.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ const packagesRoot = path.resolve(__dirname, '..', 'packages')

// Those modules are known to have side effects when evaluated
const pathsWithSideEffect = new Set([
`${packagesRoot}/logs/src/boot/logs.entry.ts`,
`${packagesRoot}/logs/src/index.ts`,
`${packagesRoot}/rum/src/boot/rum.entry.ts`,
`${packagesRoot}/rum/src/index.ts`,
`${packagesRoot}/rum-slim/src/boot/rumSlim.entry.ts`,
`${packagesRoot}/rum-slim/src/index.ts`,
`${packagesRoot}/logs/src/entries/main.ts`,
`${packagesRoot}/rum/src/entries/main.ts`,
`${packagesRoot}/rum-slim/src/entries/main.ts`,
])

// Those packages are known to have no side effects when evaluated
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RumEvent } from '../../../../rum/src'
import { RumEvent } from '../../../../rum-core/src/rumEvent.types'
import { BuildEnv, BuildMode } from '../../boot/init'
import { display } from '../../tools/display'
import { buildConfiguration } from './configuration'
Expand Down
1 change: 1 addition & 0 deletions packages/core/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"baseUrl": ".",
"declaration": true,
"module": "commonjs",
"rootDir": "./src/",
"outDir": "./cjs/"
},
"include": ["./src/**/*.ts"],
Expand Down
1 change: 1 addition & 0 deletions packages/core/tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"baseUrl": ".",
"declaration": true,
"module": "es6",
"rootDir": "./src/",
"outDir": "./esm/"
},
"include": ["./src/**/*.ts"],
Expand Down
6 changes: 3 additions & 3 deletions packages/logs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "@datadog/browser-logs",
"version": "3.8.0",
"license": "Apache-2.0",
"main": "cjs/index.js",
"module": "esm/index.js",
"types": "cjs/index.d.ts",
"main": "cjs/entries/main.js",
"module": "esm/entries/main.js",
"types": "cjs/entries/main.d.ts",
"scripts": {
"build": "run-p build:cjs build:esm build:bundle",
"build:bundle": "rm -rf bundle && webpack --mode=production",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import {
updateExperimentalFeatures,
resetExperimentalFeatures,
} from '@datadog/browser-core'
import { LogsInitConfiguration } from '..'
import { Clock, deleteEventBridgeStub, initEventBridgeStub, mockClock } from '../../../core/test/specHelper'

import { HandlerType, LogsMessage, StatusType } from '../domain/logger'
import { HybridInitConfiguration, LogsPublicApi, makeLogsPublicApi, StartLogs } from './logs.entry'
import { HybridInitConfiguration, LogsPublicApi, makeLogsPublicApi, StartLogs } from './logsPublicApi'
import { LogsInitConfiguration } from './startLogs'

const DEFAULT_INIT_CONFIGURATION = { clientToken: 'xxx' }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {
combine,
Context,
createContextManager,
defineGlobal,
getGlobalObject,
isPercentage,
makePublicApi,
monitor,
Expand All @@ -26,13 +24,6 @@ export type HybridInitConfiguration = Omit<LogsInitConfiguration, 'clientToken'>

export type LogsPublicApi = ReturnType<typeof makeLogsPublicApi>

export const datadogLogs = makeLogsPublicApi(startLogs)

interface BrowserWindow extends Window {
DD_LOGS?: LogsPublicApi
}
defineGlobal(getGlobalObject<BrowserWindow>(), 'DD_LOGS', datadogLogs)

export type StartLogs = typeof startLogs

export function makeLogsPublicApi(startLogsImpl: StartLogs) {
Expand Down
15 changes: 15 additions & 0 deletions packages/logs/src/entries/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineGlobal, getGlobalObject } from '@datadog/browser-core'
import { LogsPublicApi, makeLogsPublicApi } from '../boot/logsPublicApi'
import { startLogs } from '../boot/startLogs'

export { Logger, LogsMessage, StatusType, HandlerType } from '../domain/logger'
export { LoggerConfiguration, LogsPublicApi as LogsGlobal } from '../boot/logsPublicApi'
export { LogsInitConfiguration } from '../boot/startLogs'
export { LogsEvent } from '../logsEvent.types'

export const datadogLogs = makeLogsPublicApi(startLogs)

interface BrowserWindow extends Window {
DD_LOGS?: LogsPublicApi
}
defineGlobal(getGlobalObject<BrowserWindow>(), 'DD_LOGS', datadogLogs)
4 changes: 0 additions & 4 deletions packages/logs/src/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/logs/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"baseUrl": ".",
"declaration": true,
"module": "commonjs",
"rootDir": "./src/",
"outDir": "./cjs/"
},
"include": ["./src/**/*.ts"],
Expand Down
1 change: 1 addition & 0 deletions packages/logs/tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"baseUrl": ".",
"declaration": true,
"module": "es6",
"rootDir": "./src/",
"outDir": "./esm/"
},
"include": ["./src/**/*.ts"],
Expand Down
2 changes: 1 addition & 1 deletion packages/logs/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const webpackBase = require('../../webpack.base')
module.exports = (_env, argv) =>
webpackBase({
mode: argv.mode,
entry: path.resolve(__dirname, 'src/boot/logs.entry.ts'),
entry: path.resolve(__dirname, 'src/entries/main.ts'),
filename: 'datadog-logs.js',
})
28 changes: 22 additions & 6 deletions packages/rum-core/src/boot/rumPublicApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,38 @@ describe('rum public api', () => {
})

describe('init', () => {
let rumPublicApi: RumPublicApi
let startRumSpy: jasmine.Spy<StartRum>

beforeEach(() => {
startRumSpy = jasmine.createSpy()
rumPublicApi = makeRumPublicApi(startRumSpy, noopRecorderApi)
})

afterEach(() => {
cleanupSyntheticsWorkerValues()
})

it('does not call startRum if Synthetics will inject its own instance of RUM', () => {
mockSyntheticsWorkerValues({ injectsRum: true })
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)
expect(startRumSpy).not.toHaveBeenCalled()
describe('skipInitIfSyntheticsWillInjectRum option', () => {
it('when true, ignores init() call if Synthetics will inject its own instance of RUM', () => {
mockSyntheticsWorkerValues({ injectsRum: true })

const rumPublicApi = makeRumPublicApi(startRumSpy, noopRecorderApi, {
ignoreInitIfSyntheticsWillInjectRum: true,
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)

expect(startRumSpy).not.toHaveBeenCalled()
})

it('when false, does not ignore init() call even if Synthetics will inject its own instance of RUM', () => {
mockSyntheticsWorkerValues({ injectsRum: true })

const rumPublicApi = makeRumPublicApi(startRumSpy, noopRecorderApi, {
ignoreInitIfSyntheticsWillInjectRum: false,
})
rumPublicApi.init(DEFAULT_INIT_CONFIGURATION)

expect(startRumSpy).toHaveBeenCalled()
})
})
})

Expand Down
15 changes: 13 additions & 2 deletions packages/rum-core/src/boot/rumPublicApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ export interface RecorderApi {
isRecording: () => boolean
getReplayStats: (viewId: string) => ReplayStats | undefined
}
interface RumPublicApiOptions {
ignoreInitIfSyntheticsWillInjectRum?: boolean
}

export function makeRumPublicApi<C extends RumInitConfiguration>(startRumImpl: StartRum<C>, recorderApi: RecorderApi) {
export function makeRumPublicApi<C extends RumInitConfiguration>(
startRumImpl: StartRum<C>,
recorderApi: RecorderApi,
{ ignoreInitIfSyntheticsWillInjectRum = true }: RumPublicApiOptions = {}
) {
let isAlreadyInitialized = false

const globalContextManager = createContextManager()
Expand Down Expand Up @@ -98,7 +105,11 @@ export function makeRumPublicApi<C extends RumInitConfiguration>(startRumImpl: S
}

function initRum(initConfiguration: C) {
if (willSyntheticsInjectRum()) {
// If we are in a Synthetics test configured to automatically inject a RUM instance, we want to
// completely discard the customer application RUM instance by ignoring their init() call. But,
// we should not ignore the init() call from the Synthetics-injected RUM instance, so the
// internal `ignoreInitIfSyntheticsWillInjectRum` option is here to bypass this condition.
if (ignoreInitIfSyntheticsWillInjectRum && willSyntheticsInjectRum()) {
return
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Context, DOM_EVENT, ClocksState, Observable } from '@datadog/browser-core'
import { Clock, createNewEvent } from '../../../../../core/test/specHelper'
import { RumEvent } from '../../../../../rum/src'
import { setup, TestSetupBuilder } from '../../../../test/specHelper'
import { RumEventType, ActionType } from '../../../rawRumEvent.types'
import { RumEvent } from '../../../rumEvent.types'
import { LifeCycleEventType } from '../../lifeCycle'
import { PAGE_ACTIVITY_VALIDATION_DELAY } from '../../waitIdlePage'
import { AutoAction, AUTO_ACTION_MAX_DURATION, trackActions } from './trackActions'
Expand Down
2 changes: 1 addition & 1 deletion packages/rum-core/src/domain/trackEventCounts.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context, objectValues } from '@datadog/browser-core'
import { RumEvent } from '../../../rum/src'
import { RumEvent } from '../rumEvent.types'
import { RumEventType } from '../rawRumEvent.types'
import { LifeCycle, LifeCycleEventType } from './lifeCycle'
import { EventCounts, trackEventCounts } from './trackEventCounts'
Expand Down
1 change: 1 addition & 0 deletions packages/rum-core/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"baseUrl": ".",
"declaration": true,
"module": "commonjs",
"rootDir": "./src/",
"outDir": "./cjs/"
},
"include": ["./src/**/*.ts"],
Expand Down
1 change: 1 addition & 0 deletions packages/rum-core/tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"baseUrl": ".",
"declaration": true,
"module": "es6",
"rootDir": "./src/",
"outDir": "./esm/"
},
"include": ["./src/**/*.ts"],
Expand Down
6 changes: 3 additions & 3 deletions packages/rum-slim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "@datadog/browser-rum-slim",
"version": "3.8.0",
"license": "Apache-2.0",
"main": "cjs/index.js",
"module": "esm/index.js",
"types": "cjs/index.d.ts",
"main": "cjs/entries/main.js",
"module": "esm/entries/main.js",
"types": "cjs/entries/main.d.ts",
"scripts": {
"build": "run-p build:cjs build:esm build:bundle",
"build:bundle": "rm -rf bundle && webpack --mode=production",
Expand Down
15 changes: 0 additions & 15 deletions packages/rum-slim/src/boot/rumSlim.entry.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Keep the following in sync with packages/rum/src/index.ts
export { datadogRum } from './boot/rumSlim.entry'
// Keep the following in sync with packages/rum/src/entries/main.ts
import { defineGlobal, getGlobalObject, noop } from '@datadog/browser-core'
import { makeRumPublicApi, RumPublicApi, startRum } from '@datadog/browser-rum-core'

export {
CommonProperties,
RumPublicApi as RumGlobal,
Expand All @@ -22,3 +24,16 @@ export {
RumLongTaskEventDomainContext,
} from '@datadog/browser-rum-core'
export { DefaultPrivacyLevel } from '@datadog/browser-core'

export const datadogRum = makeRumPublicApi(startRum, {
start: noop,
stop: noop,
onRumStart: noop,
isRecording: () => false,
getReplayStats: () => undefined,
})

interface BrowserWindow extends Window {
DD_RUM?: RumPublicApi
}
defineGlobal(getGlobalObject<BrowserWindow>(), 'DD_RUM', datadogRum)
1 change: 1 addition & 0 deletions packages/rum-slim/tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"baseUrl": ".",
"declaration": true,
"module": "commonjs",
"rootDir": "./src/",
"outDir": "./cjs/"
},
"include": ["./src/**/*.ts"],
Expand Down
1 change: 1 addition & 0 deletions packages/rum-slim/tsconfig.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"baseUrl": ".",
"declaration": true,
"module": "es6",
"rootDir": "./src/",
"outDir": "./esm/"
},
"include": ["./src/**/*.ts"],
Expand Down
2 changes: 1 addition & 1 deletion packages/rum-slim/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const webpackBase = require('../../webpack.base')
module.exports = (_env, argv) =>
webpackBase({
mode: argv.mode,
entry: path.resolve(__dirname, 'src/boot/rumSlim.entry.ts'),
entry: path.resolve(__dirname, 'src/entries/main.ts'),
filename: 'datadog-rum-slim.js',
})
1 change: 1 addition & 0 deletions packages/rum/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
!/esm/**/*
!/src/**/*
!/internal/*
!/internal-synthetics/*
6 changes: 6 additions & 0 deletions packages/rum/internal-synthetics/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"main": "../cjs/entries/internalSynthetics.js",
"module": "../esm/entries/internalSynthetics.js",
"types": "../cjs/entries/internalSynthetics.d.ts"
}
6 changes: 3 additions & 3 deletions packages/rum/internal/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"main": "../cjs/internal.js",
"module": "../esm/internal.js",
"types": "../cjs/internal.d.ts"
"main": "../cjs/entries/internal.js",
"module": "../esm/entries/internal.js",
"types": "../cjs/entries/internal.d.ts"
}
6 changes: 3 additions & 3 deletions packages/rum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "@datadog/browser-rum",
"version": "3.8.0",
"license": "Apache-2.0",
"main": "cjs/index.js",
"module": "esm/index.js",
"types": "cjs/index.d.ts",
"main": "cjs/entries/main.js",
"module": "esm/entries/main.js",
"types": "cjs/entries/main.d.ts",
"scripts": {
"build": "run-p build:cjs build:esm build:bundle",
"build:bundle": "rm -rf bundle && webpack --mode=production",
Expand Down
13 changes: 0 additions & 13 deletions packages/rum/src/boot/rum.entry.ts

This file was deleted.

Loading

0 comments on commit f1b4755

Please sign in to comment.