Skip to content

Commit 8f74eb3

Browse files
authored
fix(tracing): Use integer for content length (#8152)
1 parent aa5286e commit 8f74eb3

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

packages/nextjs/test/integration/test/client/tracingFetch.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ test('should correctly instrument `fetch` for performance tracing', async ({ pag
3131
expect(transaction[0].spans).toEqual(
3232
expect.arrayContaining([
3333
expect.objectContaining({
34-
data: { 'http.method': 'GET', url: 'http://example.com', type: 'fetch' },
34+
data: {
35+
'http.method': 'GET',
36+
url: 'http://example.com',
37+
type: 'fetch',
38+
'http.response_content_length': expect.any(Number),
39+
},
3540
description: 'GET http://example.com',
3641
op: 'http.client',
3742
parent_span_id: expect.any(String),

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,13 @@ export function fetchCallback(
176176
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
177177
span.setHttpStatus(handlerData.response.status);
178178

179-
const contentLength =
179+
const contentLength: string =
180180
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
181181
handlerData.response && handlerData.response.headers && handlerData.response.headers.get('content-length');
182-
if (contentLength > 0) {
183-
span.setData('http.response_content_length', contentLength);
182+
183+
const contentLengthNum = parseInt(contentLength);
184+
if (contentLengthNum > 0) {
185+
span.setData('http.response_content_length', contentLengthNum);
184186
}
185187
} else if (handlerData.error) {
186188
span.setStatus('internal_error');

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,30 @@ describe('callbacks', () => {
214214
expect(newSpan).toBeUndefined();
215215
});
216216

217-
it('adds content-length to span data', () => {
218-
const spans = {};
219-
fetchHandlerData['response'] = { headers: { 'content-length': 123 } };
217+
it('adds content-length to span data on finish', () => {
218+
const spans: Record<string, Span> = {};
220219

221220
// triggered by request being sent
222221
fetchCallback(fetchHandlerData, alwaysCreateSpan, alwaysAttachHeaders, spans);
223222

224223
const newSpan = transaction.spanRecorder?.spans[1] as Span;
225224

226225
expect(newSpan).toBeDefined();
227-
expect(newSpan).toBeInstanceOf(Span);
228-
expect(newSpan.data).toEqual({
226+
227+
const postRequestFetchHandlerData = {
228+
...fetchHandlerData,
229+
endTimestamp,
230+
response: { status: 404, headers: { get: () => 123 } },
231+
};
232+
233+
// triggered by response coming back
234+
fetchCallback(postRequestFetchHandlerData, alwaysCreateSpan, alwaysAttachHeaders, spans);
235+
236+
const finishedSpan = transaction.spanRecorder?.spans[1] as Span;
237+
238+
expect(finishedSpan).toBeDefined();
239+
expect(finishedSpan).toBeInstanceOf(Span);
240+
expect(finishedSpan.data).toEqual({
229241
'http.response_content_length': 123,
230242
'http.method': 'GET',
231243
type: 'fetch',

packages/utils/test/envelope.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('envelope', () => {
3636
expect(headers).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' });
3737
});
3838

39-
it.only('serializes an envelope with attachments', () => {
39+
it('serializes an envelope with attachments', () => {
4040
const items: EventEnvelope[1] = [
4141
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }],
4242
[{ type: 'attachment', filename: 'bar.txt', length: 6 }, Uint8Array.from([1, 2, 3, 4, 5, 6])],

0 commit comments

Comments
 (0)