Skip to content

Commit 943e297

Browse files
committed
test: resource spans
1 parent f45595c commit 943e297

File tree

2 files changed

+120
-8
lines changed

2 files changed

+120
-8
lines changed

packages/tracing/src/browser/metrics.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,17 @@ function addMeasureSpans(
212212
return measureStartTimestamp;
213213
}
214214

215+
export interface ResourceEntry extends Record<string, unknown> {
216+
initiatorType?: string;
217+
transferSize?: number;
218+
encodedBodySize?: number;
219+
decodedBodySize?: number;
220+
}
221+
215222
/** Create resource related spans */
216-
function addResourceSpans(
223+
export function addResourceSpans(
217224
transaction: Transaction,
218-
entry: Record<string, unknown>,
225+
entry: ResourceEntry,
219226
resourceName: string,
220227
startTime: number,
221228
duration: number,
@@ -227,15 +234,15 @@ function addResourceSpans(
227234
return undefined;
228235
}
229236

230-
const tags: Record<string, string> = {};
237+
const data: Record<string, any> = {};
231238
if (entry.transferSize) {
232-
tags.transferSize = (entry.transferSize as number).toString();
239+
data.transferSize = entry.transferSize;
233240
}
234241
if (entry.encodedBodySize) {
235-
tags.encodedBodySize = (entry.encodedBodySize as number).toString();
242+
data.encodedBodySize = entry.encodedBodySize;
236243
}
237244
if (entry.decodedBodySize) {
238-
tags.decodedBodySize = (entry.decodedBodySize as number).toString();
245+
data.decodedBodySize = entry.decodedBodySize;
239246
}
240247

241248
const startTimestamp = timeOrigin + startTime;
@@ -246,7 +253,7 @@ function addResourceSpans(
246253
endTimestamp,
247254
op: entry.initiatorType && entry.initiatorType !== '' ? `resource.${entry.initiatorType}` : 'resource',
248255
startTimestamp,
249-
tags,
256+
data,
250257
});
251258

252259
return endTimestamp;

packages/tracing/test/browser/metrics.test.ts

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Span, Transaction } from '../../src';
2-
import { _startChild } from '../../src/browser/metrics';
2+
import { _startChild, addResourceSpans, ResourceEntry } from '../../src/browser/metrics';
33

44
describe('_startChild()', () => {
55
it('creates a span with given properties', () => {
@@ -38,3 +38,108 @@ describe('_startChild()', () => {
3838
expect(transaction.startTimestamp).toEqual(123);
3939
});
4040
});
41+
42+
describe('addResourceSpans', () => {
43+
const transaction = new Transaction({ name: 'hello' });
44+
beforeEach(() => {
45+
transaction.startChild = jest.fn();
46+
});
47+
48+
// We already track xhr, we don't need to use
49+
it('does not create spans for xmlhttprequest', () => {
50+
const entry: ResourceEntry = {
51+
initiatorType: 'xmlhttprequest',
52+
transferSize: 256,
53+
encodedBodySize: 256,
54+
decodedBodySize: 256,
55+
};
56+
addResourceSpans(transaction, entry, '/assets/to/me', 123, 456, 100);
57+
58+
// eslint-disable-next-line @typescript-eslint/unbound-method
59+
expect(transaction.startChild).toHaveBeenCalledTimes(0);
60+
});
61+
62+
it('does not create spans for fetch', () => {
63+
const entry: ResourceEntry = {
64+
initiatorType: 'fetch',
65+
transferSize: 256,
66+
encodedBodySize: 256,
67+
decodedBodySize: 256,
68+
};
69+
addResourceSpans(transaction, entry, '/assets/to/me', 123, 456, 100);
70+
71+
// eslint-disable-next-line @typescript-eslint/unbound-method
72+
expect(transaction.startChild).toHaveBeenCalledTimes(0);
73+
});
74+
75+
it('creates spans for resource spans', () => {
76+
const entry: ResourceEntry = {
77+
initiatorType: 'css',
78+
transferSize: 256,
79+
encodedBodySize: 456,
80+
decodedBodySize: 593,
81+
};
82+
83+
const timeOrigin = 100;
84+
const startTime = 23;
85+
const duration = 356;
86+
87+
const endTimestamp = addResourceSpans(transaction, entry, '/assets/to/css', startTime, duration, timeOrigin);
88+
89+
// eslint-disable-next-line @typescript-eslint/unbound-method
90+
expect(transaction.startChild).toHaveBeenCalledTimes(1);
91+
// eslint-disable-next-line @typescript-eslint/unbound-method
92+
expect(transaction.startChild).toHaveBeenLastCalledWith({
93+
data: {
94+
decodedBodySize: entry.decodedBodySize,
95+
encodedBodySize: entry.encodedBodySize,
96+
transferSize: entry.transferSize,
97+
},
98+
description: '/assets/to/css',
99+
endTimestamp: timeOrigin + startTime + duration,
100+
op: 'resource.css',
101+
startTimestamp: timeOrigin + startTime,
102+
});
103+
104+
expect(endTimestamp).toBe(timeOrigin + startTime + duration);
105+
});
106+
107+
it('creates a variety of resource spans', () => {
108+
const table = [
109+
{
110+
initiatorType: undefined,
111+
op: 'resource',
112+
},
113+
{
114+
initiatorType: '',
115+
op: 'resource',
116+
},
117+
{
118+
initiatorType: 'css',
119+
op: 'resource.css',
120+
},
121+
{
122+
initiatorType: 'image',
123+
op: 'resource.image',
124+
},
125+
{
126+
initiatorType: 'script',
127+
op: 'resource.script',
128+
},
129+
];
130+
131+
for (const { initiatorType, op } of table) {
132+
const entry: ResourceEntry = {
133+
initiatorType,
134+
};
135+
addResourceSpans(transaction, entry, '/assets/to/me', 123, 234, 465);
136+
137+
// eslint-disable-next-line @typescript-eslint/unbound-method
138+
expect(transaction.startChild).toHaveBeenLastCalledWith(
139+
expect.objectContaining({
140+
op,
141+
}),
142+
);
143+
}
144+
});
145+
});

0 commit comments

Comments
 (0)