Skip to content

Commit 89f90c9

Browse files
author
alikn
authored
Fix an issue when an error is thrown for performance oob metrics (#2110)
fix an error in the performance sdk which throws an error for oob metrics
1 parent 919bcbd commit 89f90c9

File tree

3 files changed

+86
-17
lines changed

3 files changed

+86
-17
lines changed

packages/performance/src/resources/trace.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
isValidCustomAttributeName,
3232
isValidCustomAttributeValue
3333
} from '../utils/attributes_utils';
34-
import { isValidCustomMetricName } from '../utils/metric_utils';
34+
import { isValidMetricName } from '../utils/metric_utils';
3535
import { PerformanceTrace } from '@firebase/performance-types';
3636

3737
const enum TraceState {
@@ -163,7 +163,7 @@ export class Trace implements PerformanceTrace {
163163
* @param num Set custom metric to this value
164164
*/
165165
putMetric(counter: string, num: number): void {
166-
if (isValidCustomMetricName(counter)) {
166+
if (isValidMetricName(counter, this.name)) {
167167
this.counters[counter] = num;
168168
} else {
169169
throw ERROR_FACTORY.create(ErrorCode.INVALID_CUSTOM_METRIC_NAME, {
@@ -270,15 +270,15 @@ export class Trace implements PerformanceTrace {
270270
// navigationTimings includes only one element.
271271
if (navigationTimings && navigationTimings[0]) {
272272
trace.setDuration(Math.floor(navigationTimings[0].duration * 1000));
273-
trace.incrementMetric(
273+
trace.putMetric(
274274
'domInteractive',
275275
Math.floor(navigationTimings[0].domInteractive * 1000)
276276
);
277-
trace.incrementMetric(
277+
trace.putMetric(
278278
'domContentLoadedEventEnd',
279279
Math.floor(navigationTimings[0].domContentLoadedEventEnd * 1000)
280280
);
281-
trace.incrementMetric(
281+
trace.putMetric(
282282
'loadEventEnd',
283283
Math.floor(navigationTimings[0].loadEventEnd * 1000)
284284
);
@@ -291,7 +291,7 @@ export class Trace implements PerformanceTrace {
291291
paintObject => paintObject.name === FIRST_PAINT
292292
);
293293
if (firstPaint && firstPaint.startTime) {
294-
trace.incrementMetric(
294+
trace.putMetric(
295295
FIRST_PAINT_COUNTER_NAME,
296296
Math.floor(firstPaint.startTime * 1000)
297297
);
@@ -300,14 +300,14 @@ export class Trace implements PerformanceTrace {
300300
paintObject => paintObject.name === FIRST_CONTENTFUL_PAINT
301301
);
302302
if (firstContentfulPaint && firstContentfulPaint.startTime) {
303-
trace.incrementMetric(
303+
trace.putMetric(
304304
FIRST_CONTENTFUL_PAINT_COUNTER_NAME,
305305
Math.floor(firstContentfulPaint.startTime * 1000)
306306
);
307307
}
308308

309309
if (firstInputDelay) {
310-
trace.incrementMetric(
310+
trace.putMetric(
311311
FIRST_INPUT_DELAY_COUNTER_NAME,
312312
Math.floor(firstInputDelay * 1000)
313313
);

packages/performance/src/utils/metric_utils.test.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,77 @@
1717

1818
import { expect } from 'chai';
1919

20-
import { isValidCustomMetricName } from './metric_utils';
21-
20+
import { isValidMetricName } from './metric_utils';
21+
import {
22+
FIRST_PAINT_COUNTER_NAME,
23+
FIRST_CONTENTFUL_PAINT_COUNTER_NAME,
24+
FIRST_INPUT_DELAY_COUNTER_NAME
25+
} from '../constants';
2226
import '../../test/setup';
2327

2428
describe('Firebase Performance > metric_utils', () => {
25-
describe('#isValidCustomMetricName', () => {
29+
describe('#isValidMetricName', () => {
2630
it('returns true when name is valid', () => {
27-
expect(isValidCustomMetricName('validCustom_Metric_Name')).to.be.true;
31+
expect(isValidMetricName('validCustom_Metric_Name')).to.be.true;
2832
});
2933

3034
it('returns false when name is blank', () => {
31-
expect(isValidCustomMetricName('')).to.be.false;
35+
expect(isValidMetricName('')).to.be.false;
3236
});
3337

3438
it('returns false when name is too long', () => {
3539
const longMetricName =
3640
'too_long_metric_name_over_one_hundred_characters_too_long_metric_name_over_one_' +
3741
'hundred_characters_too';
38-
expect(isValidCustomMetricName(longMetricName)).to.be.false;
42+
expect(isValidMetricName(longMetricName)).to.be.false;
3943
});
4044

4145
it('returns false when name starts with a reserved prefix', () => {
42-
expect(isValidCustomMetricName('_invalidMetricName')).to.be.false;
46+
expect(isValidMetricName('_invalidMetricName')).to.be.false;
47+
});
48+
49+
it('returns true for first paint metric', () => {
50+
expect(
51+
isValidMetricName(FIRST_PAINT_COUNTER_NAME, '_wt_http://example.com')
52+
).to.be.true;
53+
});
54+
55+
it('returns true for first contentful paint metric', () => {
56+
expect(
57+
isValidMetricName(
58+
FIRST_CONTENTFUL_PAINT_COUNTER_NAME,
59+
'_wt_http://example.com'
60+
)
61+
).to.be.true;
62+
});
63+
64+
it('returns true for first input delay metric', () => {
65+
expect(
66+
isValidMetricName(
67+
FIRST_INPUT_DELAY_COUNTER_NAME,
68+
'_wt_http://example.com'
69+
)
70+
).to.be.true;
71+
});
72+
73+
it('returns false if first paint metric name is used outside of page load traces', () => {
74+
expect(isValidMetricName(FIRST_PAINT_COUNTER_NAME, 'some_randome_trace'))
75+
.to.be.false;
76+
});
77+
78+
it('returns false if first contentful paint metric name is used outside of page load traces', () => {
79+
expect(
80+
isValidMetricName(
81+
FIRST_CONTENTFUL_PAINT_COUNTER_NAME,
82+
'some_randome_trace'
83+
)
84+
).to.be.false;
85+
});
86+
87+
it('returns false if first input delay metric name is used outside of page load traces', () => {
88+
expect(
89+
isValidMetricName(FIRST_INPUT_DELAY_COUNTER_NAME, 'some_randome_trace')
90+
).to.be.false;
4391
});
4492
});
4593
});

packages/performance/src/utils/metric_utils.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,33 @@
1515
* limitations under the License.
1616
*/
1717

18+
import {
19+
FIRST_PAINT_COUNTER_NAME,
20+
FIRST_CONTENTFUL_PAINT_COUNTER_NAME,
21+
FIRST_INPUT_DELAY_COUNTER_NAME,
22+
OOB_TRACE_PAGE_LOAD_PREFIX
23+
} from '../constants';
24+
1825
const MAX_METRIC_NAME_LENGTH = 100;
1926
const RESERVED_AUTO_PREFIX = '_';
27+
const oobMetrics = [
28+
FIRST_PAINT_COUNTER_NAME,
29+
FIRST_CONTENTFUL_PAINT_COUNTER_NAME,
30+
FIRST_INPUT_DELAY_COUNTER_NAME
31+
];
2032

21-
export function isValidCustomMetricName(name: string): boolean {
33+
/**
34+
* Returns true if the metric is custom and does not start with reserved prefix, or if
35+
* the metric is one of out of the box page load trace metrics.
36+
*/
37+
export function isValidMetricName(name: string, traceName?: string): boolean {
2238
if (name.length === 0 || name.length > MAX_METRIC_NAME_LENGTH) {
2339
return false;
2440
}
25-
return !name.startsWith(RESERVED_AUTO_PREFIX);
41+
return (
42+
(traceName &&
43+
traceName.startsWith(OOB_TRACE_PAGE_LOAD_PREFIX) &&
44+
oobMetrics.indexOf(name) > -1) ||
45+
!name.startsWith(RESERVED_AUTO_PREFIX)
46+
);
2647
}

0 commit comments

Comments
 (0)