Skip to content

Throw exception when Performance custom trace API is provided with non-positive startTime or duration #3866

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 2 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions packages-exp/performance-exp/src/resources/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ describe('Firebase Performance > trace', () => {
});

describe('#record', () => {
it('logs a custom trace with non-positive start time value', () => {
expect(() => trace.record(0, 20)).to.throw();
expect(() => trace.record(-100, 20)).to.throw();
});

it('logs a custom trace with non-positive duration value', () => {
expect(() => trace.record(1000, 0)).to.throw();
expect(() => trace.record(1000, -200)).to.throw();
});

it('logs a trace without metrics or custom attributes', () => {
trace.record(1, 20);

Expand Down
11 changes: 11 additions & 0 deletions packages-exp/performance-exp/src/resources/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ export class Trace implements PerformanceTrace {
attributes?: { [key: string]: string };
}
): void {
if (startTime <= 0) {
throw ERROR_FACTORY.create(ErrorCode.NONPOSITIVE_TRACE_START_TIME, {
traceName: this.name
});
}
if (duration <= 0) {
throw ERROR_FACTORY.create(ErrorCode.NONPOSITIVE_TRACE_DURATION, {
traceName: this.name
});
}

this.durationUs = Math.floor(duration * 1000);
this.startTimeUs = Math.floor(startTime * 1000);
if (options && options.attributes) {
Expand Down
8 changes: 8 additions & 0 deletions packages-exp/performance-exp/src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { SERVICE, SERVICE_NAME } from '../constants';
export const enum ErrorCode {
TRACE_STARTED_BEFORE = 'trace started',
TRACE_STOPPED_BEFORE = 'trace stopped',
NONPOSITIVE_TRACE_START_TIME = '',
NONPOSITIVE_TRACE_DURATION = '',
NO_WINDOW = 'no window',
NO_APP_ID = 'no app id',
NO_PROJECT_ID = 'no project id',
Expand All @@ -37,6 +39,10 @@ export const enum ErrorCode {
const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
[ErrorCode.TRACE_STARTED_BEFORE]: 'Trace {$traceName} was started before.',
[ErrorCode.TRACE_STOPPED_BEFORE]: 'Trace {$traceName} is not running.',
[ErrorCode.NONPOSITIVE_TRACE_START_TIME]:
'Trace {$traceName} startTime should be positive.',
[ErrorCode.NONPOSITIVE_TRACE_DURATION]:
'Trace {$traceName} duration should be positive.',
[ErrorCode.NO_WINDOW]: 'Window is not available.',
[ErrorCode.NO_APP_ID]: 'App id is not available.',
[ErrorCode.NO_PROJECT_ID]: 'Project id is not available.',
Expand All @@ -58,6 +64,8 @@ const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
interface ErrorParams {
[ErrorCode.TRACE_STARTED_BEFORE]: { traceName: string };
[ErrorCode.TRACE_STOPPED_BEFORE]: { traceName: string };
[ErrorCode.NONPOSITIVE_TRACE_START_TIME]: { traceName: string };
[ErrorCode.NONPOSITIVE_TRACE_DURATION]: { traceName: string };
[ErrorCode.INVALID_ATTRIBUTE_NAME]: { attributeName: string };
[ErrorCode.INVALID_ATTRIBUTE_VALUE]: { attributeValue: string };
[ErrorCode.INVALID_CUSTOM_METRIC_NAME]: { customMetricName: string };
Expand Down
10 changes: 7 additions & 3 deletions packages/performance/src/resources/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,14 @@ describe('Firebase Performance > trace', () => {
});

describe('#record', () => {
it('logs a trace without metrics or custom attributes', () => {
trace.record(1, 20);
it('logs a custom trace with non-positive start time value', () => {
expect(() => trace.record(0, 20)).to.throw();
expect(() => trace.record(-100, 20)).to.throw();
});

expect((perfLogger.logTrace as any).calledOnceWith(trace)).to.be.true;
it('logs a custom trace with non-positive duration value', () => {
expect(() => trace.record(1000, 0)).to.throw();
expect(() => trace.record(1000, -200)).to.throw();
});

it('logs a trace with metrics', () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/performance/src/resources/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ export class Trace implements PerformanceTrace {
attributes?: { [key: string]: string };
}
): void {
if (startTime <= 0) {
throw ERROR_FACTORY.create(ErrorCode.NONPOSITIVE_TRACE_START_TIME, {
traceName: this.name
});
}
if (duration <= 0) {
throw ERROR_FACTORY.create(ErrorCode.NONPOSITIVE_TRACE_DURATION, {
traceName: this.name
});
}

this.durationUs = Math.floor(duration * 1000);
this.startTimeUs = Math.floor(startTime * 1000);
if (options && options.attributes) {
Expand Down
8 changes: 8 additions & 0 deletions packages/performance/src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { SERVICE, SERVICE_NAME } from '../constants';
export const enum ErrorCode {
TRACE_STARTED_BEFORE = 'trace started',
TRACE_STOPPED_BEFORE = 'trace stopped',
NONPOSITIVE_TRACE_START_TIME = '',
NONPOSITIVE_TRACE_DURATION = '',
NO_WINDOW = 'no window',
NO_APP_ID = 'no app id',
NO_PROJECT_ID = 'no project id',
Expand All @@ -37,6 +39,10 @@ export const enum ErrorCode {
const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
[ErrorCode.TRACE_STARTED_BEFORE]: 'Trace {$traceName} was started before.',
[ErrorCode.TRACE_STOPPED_BEFORE]: 'Trace {$traceName} is not running.',
[ErrorCode.NONPOSITIVE_TRACE_START_TIME]:
'Trace {$traceName} startTime should be positive.',
[ErrorCode.NONPOSITIVE_TRACE_DURATION]:
'Trace {$traceName} duration should be positive.',
[ErrorCode.NO_WINDOW]: 'Window is not available.',
[ErrorCode.NO_APP_ID]: 'App id is not available.',
[ErrorCode.NO_PROJECT_ID]: 'Project id is not available.',
Expand All @@ -58,6 +64,8 @@ const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
interface ErrorParams {
[ErrorCode.TRACE_STARTED_BEFORE]: { traceName: string };
[ErrorCode.TRACE_STOPPED_BEFORE]: { traceName: string };
[ErrorCode.NONPOSITIVE_TRACE_START_TIME]: { traceName: string };
[ErrorCode.NONPOSITIVE_TRACE_DURATION]: { traceName: string };
[ErrorCode.INVALID_ATTRIBUTE_NAME]: { attributeName: string };
[ErrorCode.INVALID_ATTRIBUTE_VALUE]: { attributeValue: string };
[ErrorCode.INVALID_CUSTOM_METRIC_NAME]: { customMetricName: string };
Expand Down