Skip to content

Fix for Fireperf counter value might be null #5903

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 6 commits into from
Jan 27, 2022
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
15 changes: 15 additions & 0 deletions packages/performance/src/resources/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ describe('Firebase Performance > trace', () => {
expect(trace.getAttributes()).to.eql({ level: '1' });
expect(trace.getMetric('cacheHits')).to.eql(1);
});

it('does not log counter with invalid counter value', () => {
trace.record(1, 20, {
metrics: { level: NaN }
});

expect((perfLogger.logTrace as any).calledOnceWith(trace)).to.be.true;
expect(trace.getMetric('level')).to.eql(0);
});
});

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

it('replaces undefined metrics with 0', () => {
trace.putMetric('cacheHits', undefined);

expect(trace.getMetric('cacheHits')).to.eql(0);
});

it('throws error if metric doesnt exist and has invalid name', () => {
expect(() => trace.putMetric('_invalidMetric', 1)).to.throw();
expect(() => trace.putMetric('_fid', 1)).to.throw();
Expand Down
10 changes: 6 additions & 4 deletions packages/performance/src/resources/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,11 @@ export class Trace implements PerformanceTrace {
this.customAttributes = { ...options.attributes };
}
if (options && options.metrics) {
for (const metric of Object.keys(options.metrics)) {
if (!isNaN(Number(options.metrics[metric]))) {
this.counters[metric] = Number(Math.floor(options.metrics[metric]));
for (const metricName of Object.keys(options.metrics)) {
if (!isNaN(Number(options.metrics[metricName]))) {
this.counters[metricName] = Math.floor(
Number(options.metrics[metricName])
);
}
}
}
Expand Down Expand Up @@ -183,7 +185,7 @@ export class Trace implements PerformanceTrace {
*/
putMetric(counter: string, numAsInteger: number): void {
if (isValidMetricName(counter, this.name)) {
this.counters[counter] = convertMetricValueToInteger(numAsInteger);
this.counters[counter] = convertMetricValueToInteger(numAsInteger ?? 0);
} else {
throw ERROR_FACTORY.create(ErrorCode.INVALID_CUSTOM_METRIC_NAME, {
customMetricName: counter
Expand Down