Skip to content

Truncate long element names for OOB core web vital metrics #9178

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 1 commit into from
Jul 22, 2025
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
5 changes: 5 additions & 0 deletions .changeset/strong-avocados-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/performance': patch
---

Fixed errors thrown when capturing long target element names for the out-of-the-box metrics.
33 changes: 33 additions & 0 deletions packages/performance/src/resources/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,37 @@ describe('Firebase Performance > trace', () => {
expect(trace.getAttribute('stage')).to.equal('beginning');
});
});

describe('#addWebVitalMetric', () => {
it('has correctly scaled metric', () => {
Trace.addWebVitalMetric(trace, 'metric', 'attributeName', {
value: 0.5,
elementAttribution: 'test'
});

expect(trace.getMetric('metric') === 500);
});

it('has correct attribute', () => {
Trace.addWebVitalMetric(trace, 'metric', 'attributeName', {
value: 0.5,
elementAttribution: 'test'
});

expect(trace.getAttribute('attributeName') === 'test');
});

it('correctly truncates long attribute names', () => {
Trace.addWebVitalMetric(trace, 'metric', 'attributeName', {
value: 0.5,
elementAttribution:
'html>body>main>p>button.my_button_class.really_long_class_name_that_is_above_100_characters.another_long_class_name'
});

expect(
trace.getAttribute('attributeName') ===
'html>body>main>p>button.my_button_class.really_long_class_name_that_is_above_100_characters.another_'
);
});
});
});
10 changes: 9 additions & 1 deletion packages/performance/src/resources/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { Api } from '../services/api_service';
import { logTrace, flushLogs } from '../services/perf_logger';
import { ERROR_FACTORY, ErrorCode } from '../utils/errors';
import {
MAX_ATTRIBUTE_VALUE_LENGTH,
isValidCustomAttributeName,
isValidCustomAttributeValue
} from '../utils/attributes_utils';
Expand Down Expand Up @@ -382,7 +383,14 @@ export class Trace implements PerformanceTrace {
if (metric) {
trace.putMetric(metricKey, Math.floor(metric.value * 1000));
if (metric.elementAttribution) {
trace.putAttribute(attributeKey, metric.elementAttribution);
if (metric.elementAttribution.length > MAX_ATTRIBUTE_VALUE_LENGTH) {
trace.putAttribute(
attributeKey,
metric.elementAttribution.substring(0, MAX_ATTRIBUTE_VALUE_LENGTH)
);
} else {
trace.putAttribute(attributeKey, metric.elementAttribution);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/performance/src/utils/attributes_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ interface NavigatorWithConnection extends Navigator {
const RESERVED_ATTRIBUTE_PREFIXES = ['firebase_', 'google_', 'ga_'];
const ATTRIBUTE_FORMAT_REGEX = new RegExp('^[a-zA-Z]\\w*$');
const MAX_ATTRIBUTE_NAME_LENGTH = 40;
const MAX_ATTRIBUTE_VALUE_LENGTH = 100;
export const MAX_ATTRIBUTE_VALUE_LENGTH = 100;

export function getServiceWorkerStatus(): ServiceWorkerStatus {
const navigator = Api.getInstance().navigator;
Expand Down
Loading