@@ -154,6 +154,12 @@ class Metrics extends Utility implements MetricsInterface {
154
154
*/
155
155
private dimensions : Dimensions = { } ;
156
156
157
+ /**
158
+ * Additional dimension sets for the current metrics context
159
+ * @default []
160
+ */
161
+ private dimensionSets : Dimensions [ ] = [ ] ;
162
+
157
163
/**
158
164
* Service for accessing environment variables
159
165
*/
@@ -267,9 +273,35 @@ class Metrics extends Utility implements MetricsInterface {
267
273
* @param dimensions - An object with key-value pairs of dimensions
268
274
*/
269
275
public addDimensions ( dimensions : Dimensions ) : void {
270
- for ( const [ name , value ] of Object . entries ( dimensions ) ) {
271
- this . addDimension ( name , value ) ;
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
+ }
295
+
296
+ const currentCount = this . getCurrentDimensionsCount ( ) ;
297
+ const newSetCount = Object . keys ( newDimensionSet ) . length ;
298
+ if ( currentCount + newSetCount >= MAX_DIMENSION_COUNT ) {
299
+ throw new RangeError (
300
+ `The number of metric dimensions must be lower than ${ MAX_DIMENSION_COUNT } `
301
+ ) ;
272
302
}
303
+
304
+ this . dimensionSets . push ( newDimensionSet ) ;
273
305
}
274
306
275
307
/**
@@ -447,6 +479,7 @@ class Metrics extends Utility implements MetricsInterface {
447
479
*/
448
480
public clearDimensions ( ) : void {
449
481
this . dimensions = { } ;
482
+ this . dimensionSets = [ ] ;
450
483
}
451
484
452
485
/**
@@ -692,26 +725,46 @@ class Metrics extends Utility implements MetricsInterface {
692
725
{ }
693
726
) ;
694
727
695
- const dimensionNames = [
696
- ...new Set ( [
728
+ const dimensionNames = [ ] ;
729
+
730
+ const allDimensionKeys = new Set ( [
731
+ ...Object . keys ( this . defaultDimensions ) ,
732
+ ...Object . keys ( this . dimensions ) ,
733
+ ] ) ;
734
+
735
+ if ( Object . keys ( this . dimensions ) . length > 0 ) {
736
+ dimensionNames . push ( [ ...allDimensionKeys ] ) ;
737
+ }
738
+
739
+ for ( const dimensionSet of this . dimensionSets ) {
740
+ const dimensionSetKeys = new Set ( [
697
741
...Object . keys ( this . defaultDimensions ) ,
698
- ...Object . keys ( this . dimensions ) ,
699
- ] ) ,
700
- ] ;
742
+ ...Object . keys ( dimensionSet ) ,
743
+ ] ) ;
744
+ dimensionNames . push ( [ ...dimensionSetKeys ] ) ;
745
+ }
746
+
747
+ if (
748
+ dimensionNames . length === 0 &&
749
+ Object . keys ( this . defaultDimensions ) . length > 0
750
+ ) {
751
+ dimensionNames . push ( [ ...Object . keys ( this . defaultDimensions ) ] ) ;
752
+ }
701
753
702
754
return {
703
755
_aws : {
704
756
Timestamp : this . #timestamp ?? new Date ( ) . getTime ( ) ,
705
757
CloudWatchMetrics : [
706
758
{
707
759
Namespace : this . namespace || DEFAULT_NAMESPACE ,
708
- Dimensions : [ dimensionNames ] ,
760
+ Dimensions : dimensionNames as [ string [ ] ] ,
709
761
Metrics : metricDefinitions ,
710
762
} ,
711
763
] ,
712
764
} ,
713
765
...this . defaultDimensions ,
714
766
...this . dimensions ,
767
+ ...this . dimensionSets . reduce ( ( acc , dims ) => Object . assign ( acc , dims ) , { } ) ,
715
768
...metricValues ,
716
769
...this . metadata ,
717
770
} ;
@@ -824,9 +877,14 @@ class Metrics extends Utility implements MetricsInterface {
824
877
* Gets the current number of dimensions count.
825
878
*/
826
879
private getCurrentDimensionsCount ( ) : number {
880
+ const dimensionSetsCount = this . dimensionSets . reduce (
881
+ ( total , dimensionSet ) => total + Object . keys ( dimensionSet ) . length ,
882
+ 0
883
+ ) ;
827
884
return (
828
885
Object . keys ( this . dimensions ) . length +
829
- Object . keys ( this . defaultDimensions ) . length
886
+ Object . keys ( this . defaultDimensions ) . length +
887
+ dimensionSetsCount
830
888
) ;
831
889
}
832
890
0 commit comments