Skip to content

Commit f08811e

Browse files
DominikB2014narsaynorathAbhiPrasad
authored
feat(tracing): add body size for fetch requests (#7935)
Co-authored-by: Nar Saynorath <nar.saynorath@sentry.io> Co-authored-by: Abhijeet Prasad <aprasad@sentry.io>
1 parent f1c8d36 commit f08811e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

packages/tracing-internal/src/browser/request.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ export function fetchCallback(
186186
return;
187187
}
188188

189+
const contentLength =
190+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
191+
handlerData.response && handlerData.response.headers && handlerData.response.headers.get('content-length');
189192
const currentScope = getCurrentHub().getScope();
190193
const currentSpan = currentScope && currentScope.getSpan();
191194
const activeTransaction = currentSpan && currentSpan.transaction;
@@ -196,6 +199,7 @@ export function fetchCallback(
196199
data: {
197200
url,
198201
type: 'fetch',
202+
...(contentLength ? { 'http.response_content_length': contentLength } : {}),
199203
'http.method': method,
200204
},
201205
description: `${method} ${url}`,

packages/tracing-internal/test/browser/request.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,25 @@ describe('callbacks', () => {
213213

214214
expect(newSpan).toBeUndefined();
215215
});
216+
217+
it('adds content-length to span data', () => {
218+
const spans = {};
219+
fetchHandlerData['response'] = { headers: { 'content-length': 123 } };
220+
221+
// triggered by request being sent
222+
fetchCallback(fetchHandlerData, alwaysCreateSpan, alwaysAttachHeaders, spans);
223+
224+
const newSpan = transaction.spanRecorder?.spans[1] as Span;
225+
226+
expect(newSpan).toBeDefined();
227+
expect(newSpan).toBeInstanceOf(Span);
228+
expect(newSpan.data).toEqual({
229+
'http.response_content_length': 123,
230+
'http.method': 'GET',
231+
type: 'fetch',
232+
url: 'http://dogs.are.great/',
233+
});
234+
});
216235
});
217236

218237
describe('xhrCallback()', () => {

0 commit comments

Comments
 (0)