Skip to content

fix(tracing): Filter out invalid resource sizes #9641

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

Merged
merged 2 commits into from
Nov 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: {},
}),
);
});
});