Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tracing): Use integer for content length #8152

Merged
merged 3 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ test('should correctly instrument `fetch` for performance tracing', async ({ pag
expect(transaction[0].spans).toEqual(
expect.arrayContaining([
expect.objectContaining({
data: { 'http.method': 'GET', url: 'http://example.com', type: 'fetch' },
data: {
'http.method': 'GET',
url: 'http://example.com',
type: 'fetch',
'http.response_content_length': expect.any(Number),
},
description: 'GET http://example.com',
op: 'http.client',
parent_span_id: expect.any(String),
Expand Down
8 changes: 5 additions & 3 deletions packages/tracing-internal/src/browser/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,13 @@ export function fetchCallback(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
span.setHttpStatus(handlerData.response.status);

const contentLength =
const contentLength: string =
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
handlerData.response && handlerData.response.headers && handlerData.response.headers.get('content-length');
if (contentLength > 0) {
span.setData('http.response_content_length', contentLength);

const contentLengthNum = parseInt(contentLength);
if (contentLengthNum > 0) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this check takes care of NaN also

span.setData('http.response_content_length', contentLengthNum);
}
} else if (handlerData.error) {
span.setStatus('internal_error');
Expand Down
22 changes: 17 additions & 5 deletions packages/tracing-internal/test/browser/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,30 @@ describe('callbacks', () => {
expect(newSpan).toBeUndefined();
});

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

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

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

expect(newSpan).toBeDefined();
expect(newSpan).toBeInstanceOf(Span);
expect(newSpan.data).toEqual({

const postRequestFetchHandlerData = {
...fetchHandlerData,
endTimestamp,
response: { status: 404, headers: { get: () => 123 } },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shimming out the headers obj since not available in test

Copy link
Member

@Lms24 Lms24 May 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah its painful but I also had to do this somewhere 😅

};

// triggered by response coming back
fetchCallback(postRequestFetchHandlerData, alwaysCreateSpan, alwaysAttachHeaders, spans);

const finishedSpan = transaction.spanRecorder?.spans[1] as Span;

expect(finishedSpan).toBeDefined();
expect(finishedSpan).toBeInstanceOf(Span);
expect(finishedSpan.data).toEqual({
'http.response_content_length': 123,
'http.method': 'GET',
type: 'fetch',
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/test/envelope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('envelope', () => {
expect(headers).toEqual({ event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2', sent_at: '123' });
});

it.only('serializes an envelope with attachments', () => {
it('serializes an envelope with attachments', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it.only 😭

const items: EventEnvelope[1] = [
[{ type: 'event' }, { event_id: 'aa3ff046696b4bc6b609ce6d28fde9e2' }],
[{ type: 'attachment', filename: 'bar.txt', length: 6 }, Uint8Array.from([1, 2, 3, 4, 5, 6])],
Expand Down