Skip to content

Commit 830c8f0

Browse files
committed
✨ add extra slim package with navigation tracking and session management
1 parent 66ce71d commit 830c8f0

22 files changed

+300
-4
lines changed

developer-extension/src/background/domain/syncRules.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ function buildRules(
7171
createRedirectRule(/^https:\/\/.*\/datadog-rum-slim(-[\w-]+)?\.js$/, {
7272
url: DEV_RUM_SLIM_URL,
7373
}),
74+
createRedirectRule(/^https:\/\/.*\/datadog-rum-extra-slim(-[\w-]+)?\.js$/, {
75+
url: DEV_RUM_SLIM_URL,
76+
}),
7477
createRedirectRule(/^https:\/\/.*\/datadog-logs(-[\w-]+)?\.js$/, { url: DEV_LOGS_URL }),
7578
createRedirectRule(/^https:\/\/.*\/chunks\/recorder(-[\w-]+)?-datadog-rum.js$/, {
7679
url: DEV_RUM_RECORDER_CHUNK_URL,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const DEV_LOGS_URL = 'http://localhost:8080/datadog-logs.js'
22
export const DEV_RUM_RECORDER_CHUNK_URL = 'http://localhost:8080/chunks/recorder-datadog-rum.js'
33
export const DEV_RUM_SLIM_URL = 'http://localhost:8080/datadog-rum-slim.js'
4+
export const DEV_RUM_EXTRA_SLIM_URL = 'http://localhost:8080/datadog-rum-extra-slim.js'
45
export const DEV_RUM_URL = 'http://localhost:8080/datadog-rum.js'

eslint.config.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,19 @@ export default tseslint.config(
341341
},
342342

343343
{
344-
files: ['packages/{rum,logs,rum-slim}/src/entries/*.ts'],
344+
files: ['packages/{rum,logs,rum-slim,rum-extra-slim}/src/entries/*.ts'],
345345
rules: {
346346
'local-rules/disallow-enum-exports': 'error',
347347
},
348348
},
349349

350+
{
351+
files: ['packages/rum-extra-slim/**/*.ts'],
352+
rules: {
353+
'local-rules/disallow-side-effects': 'off',
354+
},
355+
},
356+
350357
{
351358
// Files executed by nodejs
352359
files: [

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"postinstall": "scripts/cli init_submodule",
1212
"build": "lerna run build --stream",
1313
"build:bundle": "lerna run build:bundle --stream",
14+
"build:xs": "lerna run build --scope @datadog/browser-rum-extra-slim",
1415
"build:app": "lerna run pack --stream && cd test/apps/vanilla && rm -rf node_modules && yarn install --no-immutable && yarn build",
1516
"build:react-app": "lerna run pack --stream && cd test/apps/react && rm -rf node_modules && yarn install --no-immutable && yarn build",
1617
"build:apps": "yarn build:app && yarn build:react-app",

packages/rum-extra-slim/.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*
2+
!/bundle/datadog-rum-extra-slim.js
3+
!/cjs/**/*
4+
!/esm/**/*
5+
!/src/**/*
6+
/src/**/*.spec.ts
7+
/src/**/*.specHelper.ts

packages/rum-extra-slim/.yarnrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
save-exact true
2+

packages/rum-extra-slim/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# RUM Browser Monitoring - extra slim package
2+
3+
## Overview
4+
5+
> [!CAUTION]
6+
> Experimental package. Not recommended for production use.
7+
8+
## Setup
9+
10+
See the [RUM package](../rum/README.md) documentation.

packages/rum-extra-slim/package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "@datadog/browser-rum-extra-slim",
3+
"version": "6.6.3",
4+
"license": "Apache-2.0",
5+
"main": "cjs/entries/main.js",
6+
"module": "esm/entries/main.js",
7+
"types": "cjs/entries/main.d.ts",
8+
"private": true,
9+
"scripts": {
10+
"pack": "yarn pack",
11+
"build": "run-p build:cjs build:esm build:bundle",
12+
"build:bundle": "rm -rf bundle && SDK_SETUP=cdn webpack --mode=production",
13+
"build:cjs": "rm -rf cjs && tsc -p tsconfig.cjs.json",
14+
"build:esm": "rm -rf esm && tsc -p tsconfig.esm.json"
15+
},
16+
"dependencies": {
17+
"@datadog/browser-core": "6.6.3",
18+
"@datadog/browser-rum-core": "6.6.3"
19+
},
20+
"peerDependencies": {
21+
"@datadog/browser-logs": "6.6.3"
22+
},
23+
"peerDependenciesMeta": {
24+
"@datadog/browser-logs": {
25+
"optional": true
26+
}
27+
},
28+
"repository": {
29+
"type": "git",
30+
"url": "https://github.com/DataDog/browser-sdk.git",
31+
"directory": "packages/rum-extra-slim"
32+
},
33+
"volta": {
34+
"extends": "../../package.json"
35+
},
36+
"publishConfig": {
37+
"access": "public"
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { startSessionManager } from '../domain/sessionManager'
2+
import { trackNavigationTimings } from '../domain/collection/trackNavigationTimings'
3+
import { startTransportManager } from '../domain/transportManager'
4+
import { trackUrlChange } from '../domain/collection/trackUrls'
5+
6+
export function start() {
7+
const sessionManager = startSessionManager()
8+
const transportManager = startTransportManager(sessionManager)
9+
10+
function init() {
11+
trackUrlChange(transportManager)
12+
trackNavigationTimings(transportManager)
13+
}
14+
15+
return {
16+
init,
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { RumPerformanceEntryType } from '@datadog/browser-rum-core'
2+
import { createPerformanceObservable } from '@datadog/browser-rum-core/src/browser/performanceObservable'
3+
import type { TransportManager } from '../transportManager'
4+
import type { NavigationTimingsEvent } from '../event'
5+
6+
export function trackNavigationTimings(transportManager: TransportManager) {
7+
const observable = createPerformanceObservable({} as any, {
8+
type: RumPerformanceEntryType.NAVIGATION,
9+
buffered: true,
10+
})
11+
12+
const subscription = observable.subscribe((entries) => {
13+
for (const entry of entries) {
14+
const data: NavigationTimingsEvent = {
15+
type: 'navigation_timings',
16+
...entry.toJSON(),
17+
}
18+
19+
transportManager.send(data)
20+
}
21+
})
22+
23+
return () => subscription.unsubscribe()
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { Configuration } from '@datadog/browser-core'
2+
import { addEventListener, DOM_EVENT, instrumentMethod } from '@datadog/browser-core'
3+
import type { UrlEvent } from '../event'
4+
import type { TransportManager } from '../transportManager'
5+
6+
export function trackInitialUrl(transportManager: TransportManager) {
7+
const url = window.location.href
8+
const data: UrlEvent = {
9+
type: 'url',
10+
url,
11+
}
12+
13+
transportManager.send(data)
14+
}
15+
16+
export function trackUrlChange(transportManager: TransportManager) {
17+
function onUrlChange() {
18+
const url = window.location.href
19+
const data: UrlEvent = {
20+
type: 'url',
21+
url,
22+
}
23+
transportManager.send(data)
24+
}
25+
26+
addEventListener({} as Configuration, window, DOM_EVENT.POP_STATE, onUrlChange)
27+
addEventListener({} as Configuration, window, DOM_EVENT.HASH_CHANGE, onUrlChange)
28+
instrumentMethod(window.history, 'pushState', ({ onPostCall }) => onPostCall(onUrlChange))
29+
instrumentMethod(window.history, 'replaceState', ({ onPostCall }) => onPostCall(onUrlChange))
30+
31+
// track initial url
32+
onUrlChange()
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { RumPerformanceNavigationTiming } from '@datadog/browser-rum-core/src/browser/performanceObservable'
2+
3+
export type UrlEvent = {
4+
type: 'url'
5+
url: string
6+
}
7+
8+
export type NavigationTimingsEvent = {
9+
type: 'navigation_timings'
10+
} & Omit<RumPerformanceNavigationTiming, 'toJSON'>
11+
12+
export type Event = UrlEvent | NavigationTimingsEvent
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { generateUUID, setCookie, ONE_YEAR, getInitCookie } from '@datadog/browser-core'
2+
import { toSessionState } from '@datadog/browser-core/src/domain/session/sessionState'
3+
import { createCookieObservable } from '@datadog/browser-rum-core/src/browser/cookieObservable'
4+
5+
const COOKIE = '_dd_xs'
6+
7+
export type SessionManager = ReturnType<typeof startSessionManager>
8+
9+
export function startSessionManager() {
10+
const contextId = generateUUID()
11+
let clientId: string | undefined
12+
13+
const { cid } = toSessionState(getInitCookie(COOKIE))
14+
15+
if (!cid) {
16+
setNewClientId()
17+
} else {
18+
clientId = cid
19+
}
20+
21+
const cookieObservable = createCookieObservable(COOKIE)
22+
23+
function setNewClientId() {
24+
clientId = generateUUID()
25+
setCookie(COOKIE, `cid=${clientId}`, ONE_YEAR)
26+
}
27+
28+
const subscription = cookieObservable.subscribe((cookieValue) => {
29+
if (!cookieValue) {
30+
return setNewClientId()
31+
}
32+
33+
const { cid } = toSessionState(cookieValue)
34+
35+
if (!cid) {
36+
return setNewClientId()
37+
}
38+
39+
clientId = cid
40+
})
41+
42+
return {
43+
clientId,
44+
contextId,
45+
stop: () => subscription.unsubscribe(),
46+
}
47+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { SessionManager } from './sessionManager'
2+
import type { Event } from './event'
3+
4+
const BASE_URL = '/'
5+
6+
export type TransportManager = ReturnType<typeof startTransportManager>
7+
8+
export function startTransportManager(sessionManager: SessionManager) {
9+
const { clientId, contextId } = sessionManager
10+
11+
function send(data: Event) {
12+
console.log(`[${data.type}]`, data) // eslint-disable-line no-console
13+
14+
fetch(`${BASE_URL}`, {
15+
method: 'POST',
16+
headers: {
17+
'Content-Type': 'application/json',
18+
},
19+
body: JSON.stringify({
20+
clientId,
21+
contextId,
22+
data,
23+
}),
24+
}).catch((e) => {
25+
console.error('[RUM_XS] Failed to send', e) // eslint-disable-line no-console
26+
})
27+
}
28+
29+
return {
30+
send,
31+
}
32+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* eslint-disable local-rules/disallow-side-effects */
2+
3+
import { defineGlobal, getGlobalObject, makePublicApi } from '@datadog/browser-core'
4+
import { start } from '../boot/start'
5+
6+
export const datadogRum = makePublicApi(start())
7+
8+
interface BrowserWindow extends Window {
9+
DD_RUM_XS?: any
10+
}
11+
12+
defineGlobal(getGlobalObject<BrowserWindow>(), 'DD_RUM_XS', datadogRum)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"baseUrl": ".",
5+
"declaration": true,
6+
"module": "commonjs",
7+
"rootDir": "./src/",
8+
"outDir": "./cjs/"
9+
},
10+
"include": ["./src/**/*.ts"],
11+
"exclude": ["./src/**/*.spec.ts", "./src/**/*.specHelper.ts"]
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"baseUrl": ".",
5+
"declaration": true,
6+
"rootDir": "./src/",
7+
"outDir": "./esm/"
8+
},
9+
"include": ["./src/**/*.ts"],
10+
"exclude": ["./src/**/*.spec.ts", "./src/**/*.specHelper.ts"]
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const path = require('path')
2+
3+
const webpackBase = require('../../webpack.base')
4+
5+
module.exports = (_env, argv) =>
6+
webpackBase({
7+
mode: argv.mode,
8+
entry: path.resolve(__dirname, 'src/entries/main.ts'),
9+
filename: 'datadog-rum-extra-slim.js',
10+
})

scripts/dev-server.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const cors = require('cors')
77

88
const logsConfig = require('../packages/logs/webpack.config')
99
const rumSlimConfig = require('../packages/rum-slim/webpack.config')
10+
const rumExtraSlimConfig = require('../packages/rum-extra-slim/webpack.config')
1011
const rumConfig = require('../packages/rum/webpack.config')
1112
const workerConfig = require('../packages/worker/webpack.config')
1213
const webpackBase = require('../webpack.base')
@@ -26,7 +27,7 @@ function createStaticSandboxApp() {
2627
const app = express()
2728
app.use(cors())
2829
app.use(express.static(sandboxPath))
29-
for (const config of [rumConfig, logsConfig, rumSlimConfig, workerConfig]) {
30+
for (const config of [rumConfig, logsConfig, rumSlimConfig, rumExtraSlimConfig, workerConfig]) {
3031
// eslint-disable-next-line @typescript-eslint/no-misused-promises
3132
app.use(middleware(webpack(config(null, { mode: 'development' }))))
3233
}

scripts/lib/computeBundleSize.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const fs = require('fs')
33
const zlib = require('zlib')
44
const { glob } = require('glob')
55

6-
const packages = ['rum', 'logs', 'rum-slim', 'worker']
6+
const packages = ['rum', 'logs', 'rum-slim', 'rum-extra-slim', 'worker']
77

88
function getPackageName(file) {
99
if (file.includes('chunk')) {

scripts/show-bundle-size.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { calculateBundleSizes, formatSize } = require('./lib/computeBundleSize.js')
22
const { printLog, runMain } = require('./lib/executionUtils')
33

4-
const COL_WIDTH = 12
4+
const COL_WIDTH = 14
55

66
runMain(() => {
77
const bundleSizes = calculateBundleSizes()

yarn.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,20 @@ __metadata:
325325
languageName: unknown
326326
linkType: soft
327327

328+
"@datadog/browser-rum-extra-slim@workspace:packages/rum-extra-slim":
329+
version: 0.0.0-use.local
330+
resolution: "@datadog/browser-rum-extra-slim@workspace:packages/rum-extra-slim"
331+
dependencies:
332+
"@datadog/browser-core": "npm:6.6.3"
333+
"@datadog/browser-rum-core": "npm:6.6.3"
334+
peerDependencies:
335+
"@datadog/browser-logs": 6.6.3
336+
peerDependenciesMeta:
337+
"@datadog/browser-logs":
338+
optional: true
339+
languageName: unknown
340+
linkType: soft
341+
328342
"@datadog/browser-rum-react@workspace:packages/rum-react":
329343
version: 0.0.0-use.local
330344
resolution: "@datadog/browser-rum-react@workspace:packages/rum-react"

0 commit comments

Comments
 (0)