Skip to content

Commit 8c27e4b

Browse files
📈 [PANA-5371] Add telemetry to help evaluate new DOM mutation encoding (#4077)
1 parent 338cda6 commit 8c27e4b

File tree

7 files changed

+102
-40
lines changed

7 files changed

+102
-40
lines changed

‎packages/rum/src/domain/record/serialization/serializeNode.spec.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,10 +1084,12 @@ describe('serializeNode', () => {
10841084

10851085
describe('serializeDocumentNode handles', function testAllowDomTree() {
10861086
const toJSONObj = (data: any) => JSON.parse(JSON.stringify(data)) as unknown
1087+
let stats: SerializationStats
10871088
let transaction: SerializationTransaction
10881089

10891090
beforeEach(() => {
1090-
transaction = createSerializationTransactionForTesting()
1091+
stats = createSerializationStats()
1092+
transaction = createSerializationTransactionForTesting({ stats })
10911093
registerCleanupTask(() => {
10921094
if (isAdoptedStyleSheetsSupported()) {
10931095
document.adoptedStyleSheets = []
@@ -1120,6 +1122,7 @@ describe('serializeDocumentNode handles', function testAllowDomTree() {
11201122
},
11211123
],
11221124
})
1125+
expect(stats.cssText).toEqual({ count: 1, max: 20, sum: 20 })
11231126
})
11241127
})
11251128

‎packages/rum/src/domain/record/serialization/serializeNode.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function serializeDocumentNode(
8282
type: NodeType.Document,
8383
id: transaction.assignId(document),
8484
childNodes: serializeChildNodes(document, parentNodePrivacyLevel, transaction),
85-
adoptedStyleSheets: serializeStyleSheets(document.adoptedStyleSheets),
85+
adoptedStyleSheets: serializeStyleSheets(document.adoptedStyleSheets, transaction),
8686
}
8787
}
8888

@@ -101,7 +101,7 @@ function serializeDocumentFragmentNode(
101101
id: transaction.assignId(element),
102102
childNodes: serializeChildNodes(element, parentNodePrivacyLevel, transaction),
103103
isShadowRoot,
104-
adoptedStyleSheets: isShadowRoot ? serializeStyleSheets(element.adoptedStyleSheets) : undefined,
104+
adoptedStyleSheets: isShadowRoot ? serializeStyleSheets(element.adoptedStyleSheets, transaction) : undefined,
105105
}
106106
}
107107

‎packages/rum/src/domain/record/serialization/serializeNodeAsChange.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ function serializeStyleSheetsAsChange(
216216
function serializeStyleSheetAsChange(sheet: CSSStyleSheet, transaction: ChangeSerializationTransaction): StyleSheetId {
217217
const rules = Array.from(sheet.cssRules || sheet.rules, (rule) => rule.cssText)
218218
const mediaList = sheet.media.length > 0 ? Array.from(sheet.media) : undefined
219+
transaction.addMetric(
220+
'cssText',
221+
rules.reduce((totalLength, rule) => totalLength + rule.length, 0)
222+
)
219223
transaction.addStyleSheet(rules, mediaList, sheet.disabled)
220224
return transaction.scope.styleSheetIds.getOrInsert(sheet)
221225
}
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
import { isAdoptedStyleSheetsSupported } from '@datadog/browser-core/test'
2+
import { createSerializationTransactionForTesting } from '../test/serialization.specHelper'
23
import { serializeStyleSheets } from './serializeStyleSheets'
4+
import type { SerializationStats } from './serializationStats'
5+
import { createSerializationStats } from './serializationStats'
6+
import type { SerializationTransaction } from './serializationTransaction'
37

48
describe('serializeStyleSheets', () => {
9+
let stats: SerializationStats
10+
let transaction: SerializationTransaction
11+
512
beforeEach(() => {
613
if (!isAdoptedStyleSheetsSupported()) {
714
pending('no adoptedStyleSheets support')
815
}
16+
stats = createSerializationStats()
17+
transaction = createSerializationTransactionForTesting({ stats })
918
})
19+
1020
it('should return undefined if no stylesheets', () => {
11-
expect(serializeStyleSheets(undefined)).toBe(undefined)
12-
expect(serializeStyleSheets([])).toBe(undefined)
21+
expect(serializeStyleSheets(undefined, transaction)).toBe(undefined)
22+
expect(stats.cssText).toEqual({ count: 0, max: 0, sum: 0 })
23+
expect(serializeStyleSheets([], transaction)).toBe(undefined)
24+
expect(stats.cssText).toEqual({ count: 0, max: 0, sum: 0 })
1325
})
1426

1527
it('should return serialized stylesheet', () => {
@@ -18,9 +30,10 @@ describe('serializeStyleSheets', () => {
1830
const printStylesheet = new CSSStyleSheet({ disabled: false, media: 'print' })
1931
printStylesheet.insertRule('a { color: red; }')
2032

21-
expect(serializeStyleSheets([disabledStylesheet, printStylesheet])).toEqual([
33+
expect(serializeStyleSheets([disabledStylesheet, printStylesheet], transaction)).toEqual([
2234
{ cssRules: ['div { width: 100%; }'], disabled: true, media: undefined },
2335
{ cssRules: ['a { color: red; }'], disabled: undefined, media: ['print'] },
2436
])
37+
expect(stats.cssText).toEqual({ count: 2, max: 20, sum: 37 })
2538
})
2639
})

‎packages/rum/src/domain/record/serialization/serializeStyleSheets.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import type { StyleSheet } from '../../../types'
2+
import type { SerializationTransaction } from './serializationTransaction'
23

3-
export function serializeStyleSheets(cssStyleSheets: CSSStyleSheet[] | undefined): StyleSheet[] | undefined {
4+
export function serializeStyleSheets(
5+
cssStyleSheets: CSSStyleSheet[] | undefined,
6+
transaction: SerializationTransaction
7+
): StyleSheet[] | undefined {
48
if (cssStyleSheets === undefined || cssStyleSheets.length === 0) {
59
return undefined
610
}
711
return cssStyleSheets.map((cssStyleSheet) => {
812
const rules = cssStyleSheet.cssRules || cssStyleSheet.rules
913
const cssRules = Array.from(rules, (cssRule) => cssRule.cssText)
14+
transaction.addMetric(
15+
'cssText',
16+
cssRules.reduce((totalLength, rule) => totalLength + rule.length, 0)
17+
)
1018

1119
const styleSheet: StyleSheet = {
1220
cssRules,

‎packages/rum/src/domain/segmentCollection/startSegmentTelemetry.spec.ts‎

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { Telemetry, HttpRequestEvent, BandwidthStats } from '@datadog/browser-core'
2-
import { Observable } from '@datadog/browser-core'
2+
import {
3+
addExperimentalFeatures,
4+
ExperimentalFeature,
5+
Observable,
6+
resetExperimentalFeatures,
7+
} from '@datadog/browser-core'
38
import type { MockTelemetry } from '@datadog/browser-core/test'
49
import { registerCleanupTask } from '@datadog/browser-core/test'
510
import { startMockTelemetry } from '../../../../core/test'
@@ -53,40 +58,51 @@ describe('segmentTelemetry', () => {
5358
setupSegmentTelemetryCollection()
5459

5560
for (const result of ['failure', 'queue-full', 'success'] as const) {
56-
generateReplayRequest({ result, isFullSnapshot: true })
61+
for (const enableChangeRecords of [true, false] as const) {
62+
if (enableChangeRecords) {
63+
addExperimentalFeatures([ExperimentalFeature.USE_CHANGE_RECORDS])
64+
}
5765

58-
expect(await telemetry.getEvents()).toEqual([
59-
jasmine.objectContaining({
60-
type: 'log',
61-
status: 'debug',
62-
message: 'Segment network request metrics',
63-
metrics: {
64-
cssText: {
65-
count: 2,
66-
max: 300,
67-
sum: 500,
68-
},
69-
isFullSnapshot: true,
70-
ongoingRequests: {
71-
count: 2,
72-
totalSize: 3000,
73-
},
74-
recordCount: 3,
75-
result,
76-
size: {
77-
compressed: 1000,
78-
raw: 2000,
79-
},
80-
serializationDuration: {
81-
count: 3,
82-
max: 65,
83-
sum: 105,
66+
generateReplayRequest({ result, isFullSnapshot: true })
67+
68+
expect(await telemetry.getEvents()).toEqual([
69+
jasmine.objectContaining({
70+
type: 'log',
71+
status: 'debug',
72+
message: 'Segment network request metrics',
73+
metrics: {
74+
cssText: {
75+
count: 2,
76+
max: 300,
77+
sum: 500,
78+
},
79+
encoding: {
80+
fullSnapshot: enableChangeRecords ? 'change' : 'v1',
81+
incrementalSnapshot: 'v1',
82+
},
83+
isFullSnapshot: true,
84+
ongoingRequests: {
85+
count: 2,
86+
totalSize: 3000,
87+
},
88+
recordCount: 3,
89+
result,
90+
size: {
91+
compressed: 1000,
92+
raw: 2000,
93+
},
94+
serializationDuration: {
95+
count: 3,
96+
max: 65,
97+
sum: 105,
98+
},
8499
},
85-
},
86-
}),
87-
])
100+
}),
101+
])
88102

89-
telemetry.reset()
103+
telemetry.reset()
104+
resetExperimentalFeatures()
105+
}
90106
}
91107
})
92108

@@ -107,6 +123,10 @@ describe('segmentTelemetry', () => {
107123
max: 300,
108124
sum: 500,
109125
},
126+
encoding: {
127+
fullSnapshot: 'v1',
128+
incrementalSnapshot: 'v1',
129+
},
110130
isFullSnapshot: false,
111131
ongoingRequests: {
112132
count: 2,

‎packages/rum/src/domain/segmentCollection/startSegmentTelemetry.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { BandwidthStats, Context, HttpRequestEvent, Observable, Telemetry } from '@datadog/browser-core'
2-
import { TelemetryMetrics, addTelemetryMetrics, noop } from '@datadog/browser-core'
2+
import {
3+
ExperimentalFeature,
4+
TelemetryMetrics,
5+
addTelemetryMetrics,
6+
isExperimentalFeatureEnabled,
7+
noop,
8+
} from '@datadog/browser-core'
39
import type { ReplayPayload } from './buildReplayPayload'
410

511
interface SegmentMetrics extends Context {
@@ -8,6 +14,10 @@ interface SegmentMetrics extends Context {
814
max: number
915
sum: number
1016
}
17+
encoding: {
18+
fullSnapshot: 'v1' | 'change'
19+
incrementalSnapshot: 'v1' | 'change'
20+
}
1121
isFullSnapshot: boolean
1222
ongoingRequests: {
1323
count: number
@@ -62,6 +72,10 @@ function createSegmentMetrics(
6272
max: payload.cssText.max,
6373
sum: payload.cssText.sum,
6474
},
75+
encoding: {
76+
fullSnapshot: isExperimentalFeatureEnabled(ExperimentalFeature.USE_CHANGE_RECORDS) ? 'change' : 'v1',
77+
incrementalSnapshot: 'v1',
78+
},
6579
isFullSnapshot: payload.isFullSnapshot,
6680
ongoingRequests: {
6781
count: bandwidthStats.ongoingRequestCount,

0 commit comments

Comments
 (0)