Skip to content

Commit 73fcec9

Browse files
authored
Fix for Fireperf counter value might be null (#5903)
* Fix trace putMetric(...) and record(...) to not put null values in counter values map
1 parent af92348 commit 73fcec9

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ describe('Firebase Performance > trace', () => {
134134
expect(trace.getAttributes()).to.eql({ level: '1' });
135135
expect(trace.getMetric('cacheHits')).to.eql(1);
136136
});
137+
138+
it('does not log counter with invalid counter value', () => {
139+
trace.record(1, 20, {
140+
metrics: { level: NaN }
141+
});
142+
143+
expect((perfLogger.logTrace as any).calledOnceWith(trace)).to.be.true;
144+
expect(trace.getMetric('level')).to.eql(0);
145+
});
137146
});
138147

139148
describe('#incrementMetric', () => {
@@ -189,6 +198,12 @@ describe('Firebase Performance > trace', () => {
189198
expect(trace.getMetric('cacheHits')).to.eql(400);
190199
});
191200

201+
it('replaces undefined metrics with 0', () => {
202+
trace.putMetric('cacheHits', undefined);
203+
204+
expect(trace.getMetric('cacheHits')).to.eql(0);
205+
});
206+
192207
it('throws error if metric doesnt exist and has invalid name', () => {
193208
expect(() => trace.putMetric('_invalidMetric', 1)).to.throw();
194209
expect(() => trace.putMetric('_fid', 1)).to.throw();

packages/performance/src/resources/trace.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,11 @@ export class Trace implements PerformanceTrace {
151151
this.customAttributes = { ...options.attributes };
152152
}
153153
if (options && options.metrics) {
154-
for (const metric of Object.keys(options.metrics)) {
155-
if (!isNaN(Number(options.metrics[metric]))) {
156-
this.counters[metric] = Number(Math.floor(options.metrics[metric]));
154+
for (const metricName of Object.keys(options.metrics)) {
155+
if (!isNaN(Number(options.metrics[metricName]))) {
156+
this.counters[metricName] = Math.floor(
157+
Number(options.metrics[metricName])
158+
);
157159
}
158160
}
159161
}
@@ -183,7 +185,7 @@ export class Trace implements PerformanceTrace {
183185
*/
184186
putMetric(counter: string, numAsInteger: number): void {
185187
if (isValidMetricName(counter, this.name)) {
186-
this.counters[counter] = convertMetricValueToInteger(numAsInteger);
188+
this.counters[counter] = convertMetricValueToInteger(numAsInteger ?? 0);
187189
} else {
188190
throw ERROR_FACTORY.create(ErrorCode.INVALID_CUSTOM_METRIC_NAME, {
189191
customMetricName: counter

0 commit comments

Comments
 (0)