Skip to content

Commit a4897a6

Browse files
authored
Fixes a bug where the out-of-the-box core web vital metrics would throw an error when the target element name exceeds the max length of a custom attribute value. (#9178)
1 parent 56fbe52 commit a4897a6

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

.changeset/strong-avocados-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/performance': patch
3+
---
4+
5+
Fixed errors thrown when capturing long target element names for the out-of-the-box metrics.

packages/performance/src/resources/trace.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,4 +306,37 @@ describe('Firebase Performance > trace', () => {
306306
expect(trace.getAttribute('stage')).to.equal('beginning');
307307
});
308308
});
309+
310+
describe('#addWebVitalMetric', () => {
311+
it('has correctly scaled metric', () => {
312+
Trace.addWebVitalMetric(trace, 'metric', 'attributeName', {
313+
value: 0.5,
314+
elementAttribution: 'test'
315+
});
316+
317+
expect(trace.getMetric('metric') === 500);
318+
});
319+
320+
it('has correct attribute', () => {
321+
Trace.addWebVitalMetric(trace, 'metric', 'attributeName', {
322+
value: 0.5,
323+
elementAttribution: 'test'
324+
});
325+
326+
expect(trace.getAttribute('attributeName') === 'test');
327+
});
328+
329+
it('correctly truncates long attribute names', () => {
330+
Trace.addWebVitalMetric(trace, 'metric', 'attributeName', {
331+
value: 0.5,
332+
elementAttribution:
333+
'html>body>main>p>button.my_button_class.really_long_class_name_that_is_above_100_characters.another_long_class_name'
334+
});
335+
336+
expect(
337+
trace.getAttribute('attributeName') ===
338+
'html>body>main>p>button.my_button_class.really_long_class_name_that_is_above_100_characters.another_'
339+
);
340+
});
341+
});
309342
});

packages/performance/src/resources/trace.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { Api } from '../services/api_service';
3434
import { logTrace, flushLogs } from '../services/perf_logger';
3535
import { ERROR_FACTORY, ErrorCode } from '../utils/errors';
3636
import {
37+
MAX_ATTRIBUTE_VALUE_LENGTH,
3738
isValidCustomAttributeName,
3839
isValidCustomAttributeValue
3940
} from '../utils/attributes_utils';
@@ -382,7 +383,14 @@ export class Trace implements PerformanceTrace {
382383
if (metric) {
383384
trace.putMetric(metricKey, Math.floor(metric.value * 1000));
384385
if (metric.elementAttribution) {
385-
trace.putAttribute(attributeKey, metric.elementAttribution);
386+
if (metric.elementAttribution.length > MAX_ATTRIBUTE_VALUE_LENGTH) {
387+
trace.putAttribute(
388+
attributeKey,
389+
metric.elementAttribution.substring(0, MAX_ATTRIBUTE_VALUE_LENGTH)
390+
);
391+
} else {
392+
trace.putAttribute(attributeKey, metric.elementAttribution);
393+
}
386394
}
387395
}
388396
}

packages/performance/src/utils/attributes_utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ interface NavigatorWithConnection extends Navigator {
7070
const RESERVED_ATTRIBUTE_PREFIXES = ['firebase_', 'google_', 'ga_'];
7171
const ATTRIBUTE_FORMAT_REGEX = new RegExp('^[a-zA-Z]\\w*$');
7272
const MAX_ATTRIBUTE_NAME_LENGTH = 40;
73-
const MAX_ATTRIBUTE_VALUE_LENGTH = 100;
73+
export const MAX_ATTRIBUTE_VALUE_LENGTH = 100;
7474

7575
export function getServiceWorkerStatus(): ServiceWorkerStatus {
7676
const navigator = Api.getInstance().navigator;

0 commit comments

Comments
 (0)