Skip to content

Commit 60a6fa5

Browse files
committed
test: resource spans
1 parent bba7f3e commit 60a6fa5

File tree

3 files changed

+123
-9
lines changed

3 files changed

+123
-9
lines changed

dangerfile.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ async function eslint(): Promise<void[]> {
1414
// eslint-disable-next-line deprecation/deprecation
1515
const cli = new CLIEngine({});
1616
// let eslint filter down to non-ignored, matching the extensions expected
17-
const filesToLint = allFiles.filter(f => !cli.isPathIgnored(f) && EXTENSIONS.some(ext => f.endsWith(ext)));
17+
const filesToLint = allFiles.filter(
18+
f => !f.includes('packages/typescript') && !cli.isPathIgnored(f) && EXTENSIONS.some(ext => f.endsWith(ext)),
19+
);
1820
return Promise.all(filesToLint.map(f => lintFile(cli, f)));
1921
}
2022

packages/tracing/src/browser/metrics.ts

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

213+
export interface ResourceEntry extends Record<string, unknown> {
214+
initiatorType?: string;
215+
transferSize?: number;
216+
encodedBodySize?: number;
217+
decodedBodySize?: number;
218+
}
219+
213220
/** Create resource related spans */
214-
function addResourceSpans(
221+
export function addResourceSpans(
215222
transaction: Transaction,
216-
entry: Record<string, unknown>,
223+
entry: ResourceEntry,
217224
resourceName: string,
218225
startTime: number,
219226
duration: number,
@@ -225,15 +232,15 @@ function addResourceSpans(
225232
return undefined;
226233
}
227234

228-
const tags: Record<string, string> = {};
235+
const data: Record<string, any> = {};
229236
if (entry.transferSize) {
230-
tags.transferSize = (entry.transferSize as number).toString();
237+
data.transferSize = entry.transferSize;
231238
}
232239
if (entry.encodedBodySize) {
233-
tags.encodedBodySize = (entry.encodedBodySize as number).toString();
240+
data.encodedBodySize = entry.encodedBodySize;
234241
}
235242
if (entry.decodedBodySize) {
236-
tags.decodedBodySize = (entry.decodedBodySize as number).toString();
243+
data.decodedBodySize = entry.decodedBodySize;
237244
}
238245

239246
const startTimestamp = timeOrigin + startTime;
@@ -244,7 +251,7 @@ function addResourceSpans(
244251
endTimestamp,
245252
op: entry.initiatorType && entry.initiatorType !== '' ? `resource.${entry.initiatorType}` : 'resource',
246253
startTimestamp,
247-
tags,
254+
data,
248255
});
249256

250257
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)