@@ -195,10 +195,10 @@ class Metrics extends Utility implements MetricsInterface {
195
195
private shouldThrowOnEmptyMetrics = false ;
196
196
197
197
/**
198
- * Storage for metrics before they are published
199
- * @default {}
198
+ * Storage for dimension sets
199
+ * @default []
200
200
*/
201
- private storedMetrics : StoredMetrics = { } ;
201
+ private dimensionSets : DimensionSet [ ] = [ ] ;
202
202
203
203
/**
204
204
* Whether to disable metrics
@@ -291,8 +291,40 @@ class Metrics extends Utility implements MetricsInterface {
291
291
* @param dimensions - An object with key-value pairs of dimensions
292
292
*/
293
293
public addDimensions ( dimensions : Dimensions ) : void {
294
+ const dimensionSet : string [ ] = [ ] ;
295
+
296
+ // Add default dimensions to the set
297
+ for ( const name of Object . keys ( this . defaultDimensions ) ) {
298
+ dimensionSet . push ( name ) ;
299
+ }
300
+
301
+ // Add new dimensions to both the dimension set and the dimensions object
294
302
for ( const [ name , value ] of Object . entries ( dimensions ) ) {
295
- this . addDimension ( name , value ) ;
303
+ if ( ! value ) {
304
+ this . #logger. warn (
305
+ `The dimension ${ name } doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
306
+ ) ;
307
+ continue ;
308
+ }
309
+
310
+ if ( MAX_DIMENSION_COUNT <= this . getCurrentDimensionsCount ( ) + 1 ) {
311
+ throw new RangeError (
312
+ `The number of metric dimensions must be lower than ${ MAX_DIMENSION_COUNT } `
313
+ ) ;
314
+ }
315
+
316
+ // Add to dimensions object for value storage
317
+ this . dimensions [ name ] = value ;
318
+
319
+ // Add to dimension set if not already included
320
+ if ( ! dimensionSet . includes ( name ) ) {
321
+ dimensionSet . push ( name ) ;
322
+ }
323
+ }
324
+
325
+ // Only add the dimension set if it has dimensions beyond the defaults
326
+ if ( dimensionSet . length > Object . keys ( this . defaultDimensions ) . length ) {
327
+ this . dimensionSets . push ( dimensionSet ) ;
296
328
}
297
329
}
298
330
@@ -482,6 +514,7 @@ class Metrics extends Utility implements MetricsInterface {
482
514
*/
483
515
public clearDimensions ( ) : void {
484
516
this . dimensions = { } ;
517
+ this . dimensionSets = [ ] ;
485
518
}
486
519
487
520
/**
@@ -511,6 +544,10 @@ class Metrics extends Utility implements MetricsInterface {
511
544
* Check if there are stored metrics in the buffer.
512
545
*/
513
546
public hasStoredMetrics ( ) : boolean {
547
+ if ( ! this . storedMetrics ) {
548
+ this . storedMetrics = { } ;
549
+ return false ;
550
+ }
514
551
return Object . keys ( this . storedMetrics ) . length > 0 ;
515
552
}
516
553
@@ -735,13 +772,21 @@ class Metrics extends Utility implements MetricsInterface {
735
772
] ) ,
736
773
] ;
737
774
775
+ // Prepare all dimension sets for the EMF output
776
+ const allDimensionSets : DimensionSet [ ] = [ defaultDimensionNames ] ;
777
+
778
+ // Add any additional dimension sets created via addDimensions()
779
+ if ( this . dimensionSets . length > 0 ) {
780
+ allDimensionSets . push ( ...this . dimensionSets ) ;
781
+ }
782
+
738
783
return {
739
784
_aws : {
740
785
Timestamp : this . #timestamp ?? new Date ( ) . getTime ( ) ,
741
786
CloudWatchMetrics : [
742
787
{
743
788
Namespace : this . namespace || DEFAULT_NAMESPACE ,
744
- Dimensions : [ defaultDimensionNames ] ,
789
+ Dimensions : allDimensionSets ,
745
790
Metrics : metricDefinitions ,
746
791
} ,
747
792
] ,
@@ -1039,6 +1084,11 @@ class Metrics extends Utility implements MetricsInterface {
1039
1084
value : number ,
1040
1085
resolution : MetricResolution
1041
1086
) : void {
1087
+ // Initialize storedMetrics if it's undefined
1088
+ if ( ! this . storedMetrics ) {
1089
+ this . storedMetrics = { } ;
1090
+ }
1091
+
1042
1092
if ( Object . keys ( this . storedMetrics ) . length >= MAX_METRICS_SIZE ) {
1043
1093
this . publishStoredMetrics ( ) ;
1044
1094
}
0 commit comments