@@ -240,8 +240,10 @@ class Metrics extends Utility implements MetricsInterface {
240
240
super ( ) ;
241
241
242
242
this . dimensions = { } ;
243
- this . setOptions ( options ) ;
243
+ this . setEnvConfig ( ) ;
244
+ this . setConsole ( ) ;
244
245
this . #logger = options . logger || this . console ;
246
+ this . setOptions ( options ) ;
245
247
}
246
248
247
249
/**
@@ -293,38 +295,16 @@ class Metrics extends Utility implements MetricsInterface {
293
295
* @param dimensions - An object with key-value pairs of dimensions
294
296
*/
295
297
public addDimensions ( dimensions : Dimensions ) : void {
296
- const newDimensionSet : Dimensions = { } ;
297
- for ( const [ key , value ] of Object . entries ( dimensions ) ) {
298
- if (
299
- isStringUndefinedNullEmpty ( key ) ||
300
- isStringUndefinedNullEmpty ( value )
301
- ) {
302
- this . #logger. warn (
303
- `The dimension ${ key } doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
304
- ) ;
305
- continue ;
306
- }
307
- if (
308
- Object . hasOwn ( this . dimensions , key ) ||
309
- Object . hasOwn ( this . defaultDimensions , key ) ||
310
- Object . hasOwn ( newDimensionSet , key )
311
- ) {
312
- this . #logger. warn (
313
- `Dimension "${ key } " has already been added. The previous value will be overwritten.`
314
- ) ;
315
- }
316
- newDimensionSet [ key ] = value ;
317
- }
318
-
298
+ const newDimensions = this . #sanitizeDimensions( dimensions ) ;
319
299
const currentCount = this . getCurrentDimensionsCount ( ) ;
320
- const newSetCount = Object . keys ( newDimensionSet ) . length ;
300
+ const newSetCount = Object . keys ( newDimensions ) . length ;
321
301
if ( currentCount + newSetCount >= MAX_DIMENSION_COUNT ) {
322
302
throw new RangeError (
323
303
`The number of metric dimensions must be lower than ${ MAX_DIMENSION_COUNT } `
324
304
) ;
325
305
}
326
306
327
- this . dimensionSets . push ( newDimensionSet ) ;
307
+ this . dimensionSets . push ( newDimensions ) ;
328
308
}
329
309
330
310
/**
@@ -432,12 +412,6 @@ class Metrics extends Utility implements MetricsInterface {
432
412
public captureColdStartMetric ( functionName ?: string ) : void {
433
413
if ( ! this . getColdStart ( ) ) return ;
434
414
const singleMetric = this . singleMetric ( ) ;
435
-
436
- if ( this . defaultDimensions . service ) {
437
- singleMetric . setDefaultDimensions ( {
438
- service : this . defaultDimensions . service ,
439
- } ) ;
440
- }
441
415
const value = this . functionName ?. trim ( ) ?? functionName ?. trim ( ) ;
442
416
if ( value && value . length > 0 ) {
443
417
singleMetric . addDimension ( 'function_name' , value ) ;
@@ -598,17 +572,15 @@ class Metrics extends Utility implements MetricsInterface {
598
572
// access `myClass` as `this` in a decorated `myClass.myMethod()`.
599
573
descriptor . value = async function (
600
574
this : Handler ,
601
- event : unknown ,
602
- context : Context ,
603
- callback : Callback
575
+ ...args : [ unknown , Context , Callback ]
604
576
) : Promise < unknown > {
605
577
if ( captureColdStartMetric ) {
606
- metricsRef . captureColdStartMetric ( context . functionName ) ;
578
+ metricsRef . captureColdStartMetric ( args [ 1 ] . functionName ) ;
607
579
}
608
580
609
581
let result : unknown ;
610
582
try {
611
- result = await originalMethod . apply ( this , [ event , context , callback ] ) ;
583
+ result = await originalMethod . apply ( this , args ) ;
612
584
} finally {
613
585
metricsRef . publishStoredMetrics ( ) ;
614
586
}
@@ -823,15 +795,20 @@ class Metrics extends Utility implements MetricsInterface {
823
795
*
824
796
* @param dimensions - The dimensions to be added to the default dimensions object
825
797
*/
826
- public setDefaultDimensions ( dimensions : Dimensions | undefined ) : void {
827
- const targetDimensions = {
798
+ public setDefaultDimensions ( dimensions : Dimensions ) : void {
799
+ const newDimensions = this . #sanitizeDimensions( dimensions ) ;
800
+ const currentCount = Object . keys ( this . defaultDimensions ) . length ;
801
+ const newSetCount = Object . keys ( newDimensions ) . length ;
802
+ if ( currentCount + newSetCount >= MAX_DIMENSION_COUNT ) {
803
+ throw new RangeError (
804
+ `The number of metric dimensions must be lower than ${ MAX_DIMENSION_COUNT } `
805
+ ) ;
806
+ }
807
+
808
+ this . defaultDimensions = {
828
809
...this . defaultDimensions ,
829
- ...dimensions ,
810
+ ...newDimensions ,
830
811
} ;
831
- if ( MAX_DIMENSION_COUNT <= Object . keys ( targetDimensions ) . length ) {
832
- throw new Error ( 'Max dimension count hit' ) ;
833
- }
834
- this . defaultDimensions = targetDimensions ;
835
812
}
836
813
837
814
/**
@@ -888,7 +865,6 @@ class Metrics extends Utility implements MetricsInterface {
888
865
public singleMetric ( ) : Metrics {
889
866
return new Metrics ( {
890
867
namespace : this . namespace ,
891
- serviceName : this . dimensions . service ,
892
868
defaultDimensions : this . defaultDimensions ,
893
869
singleMetric : true ,
894
870
logger : this . #logger,
@@ -1058,33 +1034,33 @@ class Metrics extends Utility implements MetricsInterface {
1058
1034
functionName,
1059
1035
} = options ;
1060
1036
1061
- this . setEnvConfig ( ) ;
1062
- this . setConsole ( ) ;
1063
1037
this . setCustomConfigService ( customConfigService ) ;
1064
1038
this . setDisabled ( ) ;
1065
1039
this . setNamespace ( namespace ) ;
1066
- this . setService ( serviceName ) ;
1067
- this . setDefaultDimensions ( defaultDimensions ) ;
1040
+ const resolvedServiceName = this . #resolveServiceName( serviceName ) ;
1041
+ this . setDefaultDimensions (
1042
+ defaultDimensions
1043
+ ? { service : resolvedServiceName , ...defaultDimensions }
1044
+ : { service : resolvedServiceName }
1045
+ ) ;
1068
1046
this . setFunctionNameForColdStartMetric ( functionName ) ;
1069
1047
this . isSingleMetric = singleMetric || false ;
1070
1048
1071
1049
return this ;
1072
1050
}
1073
1051
1074
1052
/**
1075
- * Set the service to be used .
1053
+ * Set the service dimension that will be included in the metrics .
1076
1054
*
1077
1055
* @param service - The service to be used
1078
1056
*/
1079
- private setService ( service : string | undefined ) : void {
1080
- const targetService =
1057
+ #resolveServiceName ( service ? : string ) : string {
1058
+ return (
1081
1059
service ||
1082
1060
this . getCustomConfigService ( ) ?. getServiceName ( ) ||
1083
1061
this . #envConfig. serviceName ||
1084
- this . defaultServiceName ;
1085
- if ( targetService . length > 0 ) {
1086
- this . setDefaultDimensions ( { service : targetService } ) ;
1087
- }
1062
+ this . defaultServiceName
1063
+ ) ;
1088
1064
}
1089
1065
1090
1066
/**
@@ -1191,6 +1167,38 @@ class Metrics extends Utility implements MetricsInterface {
1191
1167
**/
1192
1168
return 0 ;
1193
1169
}
1170
+
1171
+ /**
1172
+ * Sanitizes the dimensions by removing invalid entries and skipping duplicates.
1173
+ *
1174
+ * @param dimensions - The dimensions to sanitize.
1175
+ */
1176
+ #sanitizeDimensions( dimensions : Dimensions ) : Dimensions {
1177
+ const newDimensions : Dimensions = { } ;
1178
+ for ( const [ key , value ] of Object . entries ( dimensions ) ) {
1179
+ if (
1180
+ isStringUndefinedNullEmpty ( key ) ||
1181
+ isStringUndefinedNullEmpty ( value )
1182
+ ) {
1183
+ this . #logger. warn (
1184
+ `The dimension ${ key } doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
1185
+ ) ;
1186
+ continue ;
1187
+ }
1188
+ if (
1189
+ Object . hasOwn ( this . dimensions , key ) ||
1190
+ Object . hasOwn ( this . defaultDimensions , key ) ||
1191
+ Object . hasOwn ( newDimensions , key )
1192
+ ) {
1193
+ this . #logger. warn (
1194
+ `Dimension "${ key } " has already been added. The previous value will be overwritten.`
1195
+ ) ;
1196
+ }
1197
+ newDimensions [ key ] = value ;
1198
+ }
1199
+
1200
+ return newDimensions ;
1201
+ }
1194
1202
}
1195
1203
1196
1204
export { Metrics } ;
0 commit comments