Skip to content

Commit

Permalink
fix(tracing): Filter out invalid resource sizes (#9641)
Browse files Browse the repository at this point in the history
There's a bug in some browsers that attaches huge resource sizes that
are completely unrealistic. Here's an example in chromium:
https://bugs.chromium.org/p/chromium/issues/detail?id=1324812#c25

To get around this, we add a filter to enforce that resource sizes
should only be attached if the size is < 2147483647 bytes (size of
maximum value of integer in c/c++).
  • Loading branch information
AbhiPrasad committed Nov 22, 2023
1 parent 2787643 commit 08818d8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
26 changes: 17 additions & 9 deletions packages/tracing-internal/src/browser/metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { getVisibilityWatcher } from '../web-vitals/lib/getVisibilityWatcher';
import type { NavigatorDeviceMemory, NavigatorNetworkInformation } from '../web-vitals/types';
import { _startChild, isMeasurementValue } from './utils';

const MAX_INT_AS_BYTES = 2147483647;

/**
* Converts from milliseconds to seconds
* @param time time in ms
Expand Down Expand Up @@ -402,15 +404,9 @@ export function _addResourceSpans(

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const data: Record<string, any> = {};
if ('transferSize' in entry) {
data['http.response_transfer_size'] = entry.transferSize;
}
if ('encodedBodySize' in entry) {
data['http.response_content_length'] = entry.encodedBodySize;
}
if ('decodedBodySize' in entry) {
data['http.decoded_response_content_length'] = entry.decodedBodySize;
}
setResourceEntrySizeData(data, entry, 'transferSize', 'http.response_transfer_size');
setResourceEntrySizeData(data, entry, 'encodedBodySize', 'http.response_content_length');
setResourceEntrySizeData(data, entry, 'decodedBodySize', 'http.decoded_response_content_length');
if ('renderBlockingStatus' in entry) {
data['resource.render_blocking_status'] = entry.renderBlockingStatus;
}
Expand Down Expand Up @@ -493,3 +489,15 @@ function _tagMetricInfo(transaction: Transaction): void {
);
}
}

function setResourceEntrySizeData(
data: Record<string, unknown>,
entry: ResourceEntry,
key: keyof Pick<ResourceEntry, 'transferSize' | 'encodedBodySize' | 'decodedBodySize'>,
dataKey: 'http.response_transfer_size' | 'http.response_content_length' | 'http.decoded_response_content_length',
): void {
const entryVal = entry[key];
if (entryVal !== undefined && entryVal < MAX_INT_AS_BYTES) {
data[dataKey] = entryVal;
}
}
20 changes: 20 additions & 0 deletions packages/tracing-internal/test/browser/metrics/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,24 @@ describe('_addResourceSpans', () => {
}),
);
});

it('does not attach resource sizes that exceed MAX_INT bytes', () => {
const entry: ResourceEntry = {
initiatorType: 'css',
transferSize: 2147483647,
encodedBodySize: 2147483647,
decodedBodySize: 2147483647,
};

_addResourceSpans(transaction, entry, '/assets/to/css', 100, 23, 345);

// eslint-disable-next-line @typescript-eslint/unbound-method
expect(transaction.startChild).toHaveBeenCalledTimes(1);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(transaction.startChild).toHaveBeenLastCalledWith(
expect.objectContaining({
data: {},
}),
);
});
});

0 comments on commit 08818d8

Please sign in to comment.