@@ -273,19 +273,35 @@ class Metrics extends Utility implements MetricsInterface {
273
273
* @param dimensions - An object with key-value pairs of dimensions
274
274
*/
275
275
public addDimensions ( dimensions : Dimensions ) : void {
276
- const newDimensionKeys = Object . keys ( dimensions ) . filter (
277
- ( key ) => ! this . defaultDimensions [ key ] && ! this . dimensions [ key ]
278
- ) ;
276
+ const newDimensionSet : Dimensions = { } ;
277
+ for ( const [ key , value ] of Object . entries ( dimensions ) ) {
278
+ if ( ! value ) {
279
+ this . #logger. warn (
280
+ `The dimension ${ key } doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
281
+ ) ;
282
+ continue ;
283
+ }
284
+ if (
285
+ Object . hasOwn ( this . dimensions , key ) ||
286
+ Object . hasOwn ( this . defaultDimensions , key ) ||
287
+ Object . hasOwn ( newDimensionSet , key )
288
+ ) {
289
+ this . #logger. warn (
290
+ `Dimension "${ key } " has already been added. The previous value will be overwritten.`
291
+ ) ;
292
+ }
293
+ newDimensionSet [ key ] = value ;
294
+ }
279
295
280
- if (
281
- newDimensionKeys . length + this . getCurrentDimensionsCount ( ) >=
282
- MAX_DIMENSION_COUNT
283
- ) {
296
+ const currentCount = this . getCurrentDimensionsCount ( ) ;
297
+ const newSetCount = Object . keys ( newDimensionSet ) . length ;
298
+ if ( currentCount + newSetCount >= MAX_DIMENSION_COUNT ) {
284
299
throw new RangeError (
285
300
`The number of metric dimensions must be lower than ${ MAX_DIMENSION_COUNT } `
286
301
) ;
287
302
}
288
- this . dimensionSets . push ( dimensions ) ;
303
+
304
+ this . dimensionSets . push ( newDimensionSet ) ;
289
305
}
290
306
291
307
/**
@@ -746,14 +762,11 @@ class Metrics extends Utility implements MetricsInterface {
746
762
} ,
747
763
] ,
748
764
} ,
749
- ...Object . assign (
750
- { } ,
751
- this . defaultDimensions ,
752
- this . dimensions ,
753
- this . dimensionSets . reduce ( ( acc , dims ) => Object . assign ( acc , dims ) , { } ) ,
754
- metricValues ,
755
- this . metadata
756
- ) ,
765
+ ...this . defaultDimensions ,
766
+ ...this . dimensions ,
767
+ ...this . dimensionSets . reduce ( ( acc , dims ) => Object . assign ( acc , dims ) , { } ) ,
768
+ ...metricValues ,
769
+ ...this . metadata ,
757
770
} ;
758
771
}
759
772
@@ -864,9 +877,14 @@ class Metrics extends Utility implements MetricsInterface {
864
877
* Gets the current number of dimensions count.
865
878
*/
866
879
private getCurrentDimensionsCount ( ) : number {
880
+ const dimensionSetsCount = this . dimensionSets . reduce (
881
+ ( total , dimensionSet ) => total + Object . keys ( dimensionSet ) . length ,
882
+ 0
883
+ ) ;
867
884
return (
868
885
Object . keys ( this . dimensions ) . length +
869
- Object . keys ( this . defaultDimensions ) . length
886
+ Object . keys ( this . defaultDimensions ) . length +
887
+ dimensionSetsCount
870
888
) ;
871
889
}
872
890
0 commit comments