Skip to content

Commit bbb735f

Browse files
authored
Merge 83fb206 into a10c18f
2 parents a10c18f + 83fb206 commit bbb735f

File tree

7 files changed

+61
-3
lines changed

7 files changed

+61
-3
lines changed

.changeset/modern-parents-worry.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/performance': patch
3+
'firebase': patch
4+
---
5+
6+
Throws exception when startTime or duration is not positive value in `trace.record()` API.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ describe('Firebase Performance > trace', () => {
9494
});
9595

9696
describe('#record', () => {
97+
it('logs a custom trace with non-positive start time value', () => {
98+
expect(() => trace.record(0, 20)).to.throw();
99+
expect(() => trace.record(-100, 20)).to.throw();
100+
});
101+
102+
it('logs a custom trace with non-positive duration value', () => {
103+
expect(() => trace.record(1000, 0)).to.throw();
104+
expect(() => trace.record(1000, -200)).to.throw();
105+
});
106+
97107
it('logs a trace without metrics or custom attributes', () => {
98108
trace.record(1, 20);
99109

packages-exp/performance-exp/src/resources/trace.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,17 @@ export class Trace implements PerformanceTrace {
134134
attributes?: { [key: string]: string };
135135
}
136136
): void {
137+
if (startTime <= 0) {
138+
throw ERROR_FACTORY.create(ErrorCode.NONPOSITIVE_TRACE_START_TIME, {
139+
traceName: this.name
140+
});
141+
}
142+
if (duration <= 0) {
143+
throw ERROR_FACTORY.create(ErrorCode.NONPOSITIVE_TRACE_DURATION, {
144+
traceName: this.name
145+
});
146+
}
147+
137148
this.durationUs = Math.floor(duration * 1000);
138149
this.startTimeUs = Math.floor(startTime * 1000);
139150
if (options && options.attributes) {

packages-exp/performance-exp/src/utils/errors.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { SERVICE, SERVICE_NAME } from '../constants';
2121
export const enum ErrorCode {
2222
TRACE_STARTED_BEFORE = 'trace started',
2323
TRACE_STOPPED_BEFORE = 'trace stopped',
24+
NONPOSITIVE_TRACE_START_TIME = 'nonpositive trace startTime',
25+
NONPOSITIVE_TRACE_DURATION = 'nonpositive trace duration',
2426
NO_WINDOW = 'no window',
2527
NO_APP_ID = 'no app id',
2628
NO_PROJECT_ID = 'no project id',
@@ -37,6 +39,10 @@ export const enum ErrorCode {
3739
const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
3840
[ErrorCode.TRACE_STARTED_BEFORE]: 'Trace {$traceName} was started before.',
3941
[ErrorCode.TRACE_STOPPED_BEFORE]: 'Trace {$traceName} is not running.',
42+
[ErrorCode.NONPOSITIVE_TRACE_START_TIME]:
43+
'Trace {$traceName} startTime should be positive.',
44+
[ErrorCode.NONPOSITIVE_TRACE_DURATION]:
45+
'Trace {$traceName} duration should be positive.',
4046
[ErrorCode.NO_WINDOW]: 'Window is not available.',
4147
[ErrorCode.NO_APP_ID]: 'App id is not available.',
4248
[ErrorCode.NO_PROJECT_ID]: 'Project id is not available.',
@@ -58,6 +64,8 @@ const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
5864
interface ErrorParams {
5965
[ErrorCode.TRACE_STARTED_BEFORE]: { traceName: string };
6066
[ErrorCode.TRACE_STOPPED_BEFORE]: { traceName: string };
67+
[ErrorCode.NONPOSITIVE_TRACE_START_TIME]: { traceName: string };
68+
[ErrorCode.NONPOSITIVE_TRACE_DURATION]: { traceName: string };
6169
[ErrorCode.INVALID_ATTRIBUTE_NAME]: { attributeName: string };
6270
[ErrorCode.INVALID_ATTRIBUTE_VALUE]: { attributeValue: string };
6371
[ErrorCode.INVALID_CUSTOM_METRIC_NAME]: { customMetricName: string };

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ describe('Firebase Performance > trace', () => {
7171
});
7272

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

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

8084
it('logs a trace with metrics', () => {

packages/performance/src/resources/trace.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ export class Trace implements PerformanceTrace {
131131
attributes?: { [key: string]: string };
132132
}
133133
): void {
134+
if (startTime <= 0) {
135+
throw ERROR_FACTORY.create(ErrorCode.NONPOSITIVE_TRACE_START_TIME, {
136+
traceName: this.name
137+
});
138+
}
139+
if (duration <= 0) {
140+
throw ERROR_FACTORY.create(ErrorCode.NONPOSITIVE_TRACE_DURATION, {
141+
traceName: this.name
142+
});
143+
}
144+
134145
this.durationUs = Math.floor(duration * 1000);
135146
this.startTimeUs = Math.floor(startTime * 1000);
136147
if (options && options.attributes) {

packages/performance/src/utils/errors.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import { SERVICE, SERVICE_NAME } from '../constants';
2121
export const enum ErrorCode {
2222
TRACE_STARTED_BEFORE = 'trace started',
2323
TRACE_STOPPED_BEFORE = 'trace stopped',
24+
NONPOSITIVE_TRACE_START_TIME = 'nonpositive trace startTime',
25+
NONPOSITIVE_TRACE_DURATION = 'nonpositive trace duration',
2426
NO_WINDOW = 'no window',
2527
NO_APP_ID = 'no app id',
2628
NO_PROJECT_ID = 'no project id',
@@ -37,6 +39,10 @@ export const enum ErrorCode {
3739
const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
3840
[ErrorCode.TRACE_STARTED_BEFORE]: 'Trace {$traceName} was started before.',
3941
[ErrorCode.TRACE_STOPPED_BEFORE]: 'Trace {$traceName} is not running.',
42+
[ErrorCode.NONPOSITIVE_TRACE_START_TIME]:
43+
'Trace {$traceName} startTime should be positive.',
44+
[ErrorCode.NONPOSITIVE_TRACE_DURATION]:
45+
'Trace {$traceName} duration should be positive.',
4046
[ErrorCode.NO_WINDOW]: 'Window is not available.',
4147
[ErrorCode.NO_APP_ID]: 'App id is not available.',
4248
[ErrorCode.NO_PROJECT_ID]: 'Project id is not available.',
@@ -58,6 +64,8 @@ const ERROR_DESCRIPTION_MAP: { readonly [key in ErrorCode]: string } = {
5864
interface ErrorParams {
5965
[ErrorCode.TRACE_STARTED_BEFORE]: { traceName: string };
6066
[ErrorCode.TRACE_STOPPED_BEFORE]: { traceName: string };
67+
[ErrorCode.NONPOSITIVE_TRACE_START_TIME]: { traceName: string };
68+
[ErrorCode.NONPOSITIVE_TRACE_DURATION]: { traceName: string };
6169
[ErrorCode.INVALID_ATTRIBUTE_NAME]: { attributeName: string };
6270
[ErrorCode.INVALID_ATTRIBUTE_VALUE]: { attributeValue: string };
6371
[ErrorCode.INVALID_CUSTOM_METRIC_NAME]: { customMetricName: string };

0 commit comments

Comments
 (0)