File tree Expand file tree Collapse file tree 2 files changed +21
-4
lines changed
packages/performance/src/resources Expand file tree Collapse file tree 2 files changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -134,6 +134,15 @@ describe('Firebase Performance > trace', () => {
134
134
expect ( trace . getAttributes ( ) ) . to . eql ( { level : '1' } ) ;
135
135
expect ( trace . getMetric ( 'cacheHits' ) ) . to . eql ( 1 ) ;
136
136
} ) ;
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
+ } ) ;
137
146
} ) ;
138
147
139
148
describe ( '#incrementMetric' , ( ) => {
@@ -189,6 +198,12 @@ describe('Firebase Performance > trace', () => {
189
198
expect ( trace . getMetric ( 'cacheHits' ) ) . to . eql ( 400 ) ;
190
199
} ) ;
191
200
201
+ it ( 'replaces undefined metrics with 0' , ( ) => {
202
+ trace . putMetric ( 'cacheHits' , undefined ) ;
203
+
204
+ expect ( trace . getMetric ( 'cacheHits' ) ) . to . eql ( 0 ) ;
205
+ } ) ;
206
+
192
207
it ( 'throws error if metric doesnt exist and has invalid name' , ( ) => {
193
208
expect ( ( ) => trace . putMetric ( '_invalidMetric' , 1 ) ) . to . throw ( ) ;
194
209
expect ( ( ) => trace . putMetric ( '_fid' , 1 ) ) . to . throw ( ) ;
Original file line number Diff line number Diff line change @@ -151,9 +151,11 @@ export class Trace implements PerformanceTrace {
151
151
this . customAttributes = { ...options . attributes } ;
152
152
}
153
153
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
+ ) ;
157
159
}
158
160
}
159
161
}
@@ -183,7 +185,7 @@ export class Trace implements PerformanceTrace {
183
185
*/
184
186
putMetric ( counter : string , numAsInteger : number ) : void {
185
187
if ( isValidMetricName ( counter , this . name ) ) {
186
- this . counters [ counter ] = convertMetricValueToInteger ( numAsInteger ) ;
188
+ this . counters [ counter ] = convertMetricValueToInteger ( numAsInteger ?? 0 ) ;
187
189
} else {
188
190
throw ERROR_FACTORY . create ( ErrorCode . INVALID_CUSTOM_METRIC_NAME , {
189
191
customMetricName : counter
You can’t perform that action at this time.
0 commit comments