Skip to content

Commit 2b72bd3

Browse files
✨ [RUM-14736] Add size to ResourceStopOptions (#4296)
1 parent 016a42b commit 2b72bd3

4 files changed

Lines changed: 55 additions & 36 deletions

File tree

packages/rum-core/src/boot/rumPublicApi.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,8 @@ describe('rum public api', () => {
840840
rumPublicApi.stopResource('https://api.example.com/data', {
841841
type: ResourceType.XHR,
842842
statusCode: 200,
843-
context: { responseSize: 1024 },
843+
size: 1024,
844+
context: { requestId: 'abc' },
844845
})
845846

846847
expect(startResourceSpy).toHaveBeenCalledWith(
@@ -856,7 +857,8 @@ describe('rum public api', () => {
856857
jasmine.objectContaining({
857858
type: ResourceType.XHR,
858859
statusCode: 200,
859-
context: { responseSize: 1024 },
860+
size: 1024,
861+
context: { requestId: 'abc' },
860862
})
861863
)
862864
})

packages/rum-core/src/boot/rumPublicApi.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ export function makeRumPublicApi(
749749
strategy.stopResource(sanitize(url)!, {
750750
type: sanitize(options && options.type) as ResourceType | undefined,
751751
statusCode: options && options.statusCode,
752+
size: options && options.size,
752753
context: sanitize(options && options.context) as Context,
753754
resourceKey: options && options.resourceKey,
754755
})

packages/rum-core/src/domain/resource/trackManualResources.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,26 @@ describe('trackManualResources', () => {
112112
})
113113
})
114114

115+
describe('size', () => {
116+
it('should include size when provided at stop', () => {
117+
startResource('https://api.example.com/data')
118+
stopResource('https://api.example.com/data', { size: 1234 })
119+
120+
expect(rawRumEvents).toHaveSize(1)
121+
const resourceEvent = rawRumEvents[0].rawRumEvent as RawRumResourceEvent
122+
expect(resourceEvent.resource.size).toBe(1234)
123+
})
124+
125+
it('should leave size undefined when not provided', () => {
126+
startResource('https://api.example.com/data')
127+
stopResource('https://api.example.com/data')
128+
129+
expect(rawRumEvents).toHaveSize(1)
130+
const resourceEvent = rawRumEvents[0].rawRumEvent as RawRumResourceEvent
131+
expect(resourceEvent.resource.size).toBeUndefined()
132+
})
133+
})
134+
115135
describe('resourceKey', () => {
116136
it('should support resourceKey for tracking same url multiple times', () => {
117137
startResource('https://api.example.com/data', { resourceKey: 'request1' })

packages/rum-core/src/domain/resource/trackManualResources.ts

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ClocksState, Context, ResourceType } from '@datadog/browser-core'
1+
import type { Context, ResourceType } from '@datadog/browser-core'
22
import { clocksNow, elapsed, ResourceType as ResourceTypeEnum, toServerDuration } from '@datadog/browser-core'
33
import type { RawRumResourceEvent } from '../../rawRumEvent.types'
44
import { RumEventType } from '../../rawRumEvent.types'
@@ -42,6 +42,11 @@ export interface ResourceStopOptions {
4242
*/
4343
statusCode?: number
4444

45+
/**
46+
* Resource size in bytes
47+
*/
48+
size?: number
49+
4550
/**
4651
* Resource context
4752
*/
@@ -61,38 +66,6 @@ export interface ManualResourceData {
6166
}
6267

6368
export function trackManualResources(lifeCycle: LifeCycle, resourceTracker: EventTracker<ManualResourceData>) {
64-
function emitResource(
65-
id: string,
66-
startClocks: ClocksState,
67-
data: ManualResourceData,
68-
statusCode?: number,
69-
endClocks?: ClocksState
70-
) {
71-
const duration = endClocks ? elapsed(startClocks.relative, endClocks.relative) : undefined
72-
73-
const rawRumEvent: RawRumResourceEvent = {
74-
date: startClocks.timeStamp,
75-
type: RumEventType.RESOURCE,
76-
resource: {
77-
id,
78-
type: data.type || ResourceTypeEnum.OTHER,
79-
url: sanitizeIfLongDataUrl(data.url),
80-
duration: duration !== undefined ? toServerDuration(duration) : undefined,
81-
method: data.method,
82-
status_code: statusCode,
83-
},
84-
_dd: {},
85-
context: data.context,
86-
}
87-
88-
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, {
89-
rawRumEvent,
90-
startClocks,
91-
duration,
92-
domainContext: { isManual: true as const },
93-
})
94-
}
95-
9669
function startManualResource(url: string, options: ResourceOptions = {}, startClocks = clocksNow()) {
9770
const lookupKey = options.resourceKey ?? url
9871

@@ -114,7 +87,30 @@ export function trackManualResources(lifeCycle: LifeCycle, resourceTracker: Even
11487
return
11588
}
11689

117-
emitResource(stopped.id, stopped.startClocks, stopped, options.statusCode, stopClocks)
90+
const duration = elapsed(stopped.startClocks.relative, stopClocks.relative)
91+
92+
const rawRumEvent: RawRumResourceEvent = {
93+
date: stopped.startClocks.timeStamp,
94+
type: RumEventType.RESOURCE,
95+
resource: {
96+
id: stopped.id,
97+
type: stopped.type || ResourceTypeEnum.OTHER,
98+
url: sanitizeIfLongDataUrl(stopped.url),
99+
duration: toServerDuration(duration),
100+
method: stopped.method,
101+
status_code: options.statusCode,
102+
size: options.size,
103+
},
104+
_dd: {},
105+
context: stopped.context,
106+
}
107+
108+
lifeCycle.notify(LifeCycleEventType.RAW_RUM_EVENT_COLLECTED, {
109+
rawRumEvent,
110+
startClocks: stopped.startClocks,
111+
duration,
112+
domainContext: { isManual: true },
113+
})
118114
}
119115

120116
return {

0 commit comments

Comments
 (0)