Skip to content

Commit 5c9e0a0

Browse files
committed
add unit tests for attribute extraction
1 parent ce9d7ba commit 5c9e0a0

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

packages/browser-utils/src/metrics/browserMetrics.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ export function _addResourceSpans(
674674
attributes['network.protocol.version'] = version;
675675
}
676676

677-
setResourceRequestAttributes(entry, attributes, [
677+
_setResourceRequestAttributes(entry, attributes, [
678678
// Resource request response status
679679
['responseStatus', 'http.response.status_code'],
680680

@@ -818,8 +818,8 @@ type ExperimentalResourceTimingProperty =
818818
* Assumes that all entry properties might be undefined for browser-specific differences.
819819
* Only accepts string and number values for now and also sets 0-values.
820820
*/
821-
function setResourceRequestAttributes(
822-
entry: Partial<PerformanceResourceTiming> & Partial<Record<ExperimentalResourceTimingProperty, number>>,
821+
export function _setResourceRequestAttributes(
822+
entry: Partial<PerformanceResourceTiming> & Partial<Record<ExperimentalResourceTimingProperty, number | string>>,
823823
attributes: SpanAttributes,
824824
properties: [keyof PerformanceResourceTiming | ExperimentalResourceTimingProperty, string][],
825825
): void {

packages/browser-utils/test/browser/browserMetrics.test.ts

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Span } from '@sentry/core';
1+
import type { Span, SpanAttributes } from '@sentry/core';
22
import {
33
getClient,
44
getCurrentScope,
@@ -10,7 +10,12 @@ import {
1010
spanToJSON,
1111
} from '@sentry/core';
1212
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
13-
import { _addMeasureSpans, _addNavigationSpans, _addResourceSpans } from '../../src/metrics/browserMetrics';
13+
import {
14+
_addMeasureSpans,
15+
_addNavigationSpans,
16+
_addResourceSpans,
17+
_setResourceRequestAttributes,
18+
} from '../../src/metrics/browserMetrics';
1419
import { WINDOW } from '../../src/types';
1520
import { getDefaultClientOptions, TestClient } from '../utils/TestClient';
1621

@@ -709,6 +714,75 @@ describe('_addNavigationSpans', () => {
709714
});
710715
});
711716

717+
describe('_setResourceRequestAttributes', () => {
718+
it('sets resource request attributes', () => {
719+
const attributes: SpanAttributes = {};
720+
721+
const entry = mockPerformanceResourceTiming({
722+
transferSize: 0,
723+
deliveryType: 'cache',
724+
renderBlockingStatus: 'non-blocking',
725+
responseStatus: 200,
726+
redirectStart: 100,
727+
responseStart: 200,
728+
});
729+
730+
_setResourceRequestAttributes(entry, attributes, [
731+
['transferSize', 'http.response_transfer_size'],
732+
['deliveryType', 'http.response_delivery_type'],
733+
['renderBlockingStatus', 'resource.render_blocking_status'],
734+
['responseStatus', 'http.response.status_code'],
735+
['redirectStart', 'http.request.redirect_start'],
736+
['responseStart', 'http.response.start'],
737+
]);
738+
739+
expect(attributes).toEqual({
740+
'http.response_transfer_size': 0,
741+
'http.request.redirect_start': 100,
742+
'http.response.start': 200,
743+
'http.response.status_code': 200,
744+
'http.response_delivery_type': 'cache',
745+
'resource.render_blocking_status': 'non-blocking',
746+
});
747+
});
748+
749+
it("doesn't set other attributes", () => {
750+
const attributes: SpanAttributes = {};
751+
752+
const entry = mockPerformanceResourceTiming({
753+
transferSize: 0,
754+
deliveryType: 'cache',
755+
renderBlockingStatus: 'non-blocking',
756+
});
757+
758+
_setResourceRequestAttributes(entry, attributes, [['transferSize', 'http.response_transfer_size']]);
759+
760+
expect(attributes).toEqual({
761+
'http.response_transfer_size': 0,
762+
});
763+
});
764+
765+
it("doesn't set non-primitive or undefined values", () => {
766+
const attributes: SpanAttributes = {};
767+
768+
const entry = mockPerformanceResourceTiming({
769+
transferSize: undefined,
770+
// @ts-expect-error null is invalid but let's test it anyway
771+
deliveryType: null,
772+
// @ts-expect-error object is invalid but let's test it anyway
773+
renderBlockingStatus: { blocking: 'non-blocking' },
774+
});
775+
776+
_setResourceRequestAttributes(entry, attributes, [
777+
['transferSize', 'http.response_transfer_size'],
778+
['deliveryType', 'http.response_delivery_type'],
779+
['renderBlockingStatus', 'resource.render_blocking_status'],
780+
]);
781+
782+
expect(attributes).toEqual({});
783+
});
784+
});
785+
712786
const setGlobalLocation = (location: Location) => {
713787
// @ts-expect-error need to delete this in order to set to new value
714788
delete WINDOW.location;

0 commit comments

Comments
 (0)