@@ -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 ) ;
@@ -821,15 +795,20 @@ class Metrics extends Utility implements MetricsInterface {
821
795
*
822
796
* @param dimensions - The dimensions to be added to the default dimensions object
823
797
*/
824
- public setDefaultDimensions ( dimensions : Dimensions | undefined ) : void {
825
- 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 = {
826
809
...this . defaultDimensions ,
827
- ...dimensions ,
810
+ ...newDimensions ,
828
811
} ;
829
- if ( MAX_DIMENSION_COUNT <= Object . keys ( targetDimensions ) . length ) {
830
- throw new Error ( 'Max dimension count hit' ) ;
831
- }
832
- this . defaultDimensions = targetDimensions ;
833
812
}
834
813
835
814
/**
@@ -886,7 +865,6 @@ class Metrics extends Utility implements MetricsInterface {
886
865
public singleMetric ( ) : Metrics {
887
866
return new Metrics ( {
888
867
namespace : this . namespace ,
889
- serviceName : this . dimensions . service ,
890
868
defaultDimensions : this . defaultDimensions ,
891
869
singleMetric : true ,
892
870
logger : this . #logger,
@@ -1056,33 +1034,33 @@ class Metrics extends Utility implements MetricsInterface {
1056
1034
functionName,
1057
1035
} = options ;
1058
1036
1059
- this . setEnvConfig ( ) ;
1060
- this . setConsole ( ) ;
1061
1037
this . setCustomConfigService ( customConfigService ) ;
1062
1038
this . setDisabled ( ) ;
1063
1039
this . setNamespace ( namespace ) ;
1064
- this . setService ( serviceName ) ;
1065
- this . setDefaultDimensions ( defaultDimensions ) ;
1040
+ const resolvedServiceName = this . #resolveServiceName( serviceName ) ;
1041
+ this . setDefaultDimensions (
1042
+ defaultDimensions
1043
+ ? { service : resolvedServiceName , ...defaultDimensions }
1044
+ : { service : resolvedServiceName }
1045
+ ) ;
1066
1046
this . setFunctionNameForColdStartMetric ( functionName ) ;
1067
1047
this . isSingleMetric = singleMetric || false ;
1068
1048
1069
1049
return this ;
1070
1050
}
1071
1051
1072
1052
/**
1073
- * Set the service to be used .
1053
+ * Set the service dimension that will be included in the metrics .
1074
1054
*
1075
1055
* @param service - The service to be used
1076
1056
*/
1077
- private setService ( service : string | undefined ) : void {
1078
- const targetService =
1057
+ #resolveServiceName ( service ? : string ) : string {
1058
+ return (
1079
1059
service ||
1080
1060
this . getCustomConfigService ( ) ?. getServiceName ( ) ||
1081
1061
this . #envConfig. serviceName ||
1082
- this . defaultServiceName ;
1083
- if ( targetService . length > 0 ) {
1084
- this . setDefaultDimensions ( { service : targetService } ) ;
1085
- }
1062
+ this . defaultServiceName
1063
+ ) ;
1086
1064
}
1087
1065
1088
1066
/**
@@ -1189,6 +1167,38 @@ class Metrics extends Utility implements MetricsInterface {
1189
1167
**/
1190
1168
return 0 ;
1191
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
+ }
1192
1202
}
1193
1203
1194
1204
export { Metrics } ;
0 commit comments