@@ -240,8 +240,10 @@ class Metrics extends Utility implements MetricsInterface {
240240 super ( ) ;
241241
242242 this . dimensions = { } ;
243- this . setOptions ( options ) ;
243+ this . setEnvConfig ( ) ;
244+ this . setConsole ( ) ;
244245 this . #logger = options . logger || this . console ;
246+ this . setOptions ( options ) ;
245247 }
246248
247249 /**
@@ -293,38 +295,16 @@ class Metrics extends Utility implements MetricsInterface {
293295 * @param dimensions - An object with key-value pairs of dimensions
294296 */
295297 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 ) ;
319299 const currentCount = this . getCurrentDimensionsCount ( ) ;
320- const newSetCount = Object . keys ( newDimensionSet ) . length ;
300+ const newSetCount = Object . keys ( newDimensions ) . length ;
321301 if ( currentCount + newSetCount >= MAX_DIMENSION_COUNT ) {
322302 throw new RangeError (
323303 `The number of metric dimensions must be lower than ${ MAX_DIMENSION_COUNT } `
324304 ) ;
325305 }
326306
327- this . dimensionSets . push ( newDimensionSet ) ;
307+ this . dimensionSets . push ( newDimensions ) ;
328308 }
329309
330310 /**
@@ -432,12 +412,6 @@ class Metrics extends Utility implements MetricsInterface {
432412 public captureColdStartMetric ( functionName ?: string ) : void {
433413 if ( ! this . getColdStart ( ) ) return ;
434414 const singleMetric = this . singleMetric ( ) ;
435-
436- if ( this . defaultDimensions . service ) {
437- singleMetric . setDefaultDimensions ( {
438- service : this . defaultDimensions . service ,
439- } ) ;
440- }
441415 const value = this . functionName ?. trim ( ) ?? functionName ?. trim ( ) ;
442416 if ( value && value . length > 0 ) {
443417 singleMetric . addDimension ( 'function_name' , value ) ;
@@ -821,15 +795,20 @@ class Metrics extends Utility implements MetricsInterface {
821795 *
822796 * @param dimensions - The dimensions to be added to the default dimensions object
823797 */
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 = {
826809 ...this . defaultDimensions ,
827- ...dimensions ,
810+ ...newDimensions ,
828811 } ;
829- if ( MAX_DIMENSION_COUNT <= Object . keys ( targetDimensions ) . length ) {
830- throw new Error ( 'Max dimension count hit' ) ;
831- }
832- this . defaultDimensions = targetDimensions ;
833812 }
834813
835814 /**
@@ -886,7 +865,6 @@ class Metrics extends Utility implements MetricsInterface {
886865 public singleMetric ( ) : Metrics {
887866 return new Metrics ( {
888867 namespace : this . namespace ,
889- serviceName : this . dimensions . service ,
890868 defaultDimensions : this . defaultDimensions ,
891869 singleMetric : true ,
892870 logger : this . #logger,
@@ -1056,33 +1034,33 @@ class Metrics extends Utility implements MetricsInterface {
10561034 functionName,
10571035 } = options ;
10581036
1059- this . setEnvConfig ( ) ;
1060- this . setConsole ( ) ;
10611037 this . setCustomConfigService ( customConfigService ) ;
10621038 this . setDisabled ( ) ;
10631039 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+ ) ;
10661046 this . setFunctionNameForColdStartMetric ( functionName ) ;
10671047 this . isSingleMetric = singleMetric || false ;
10681048
10691049 return this ;
10701050 }
10711051
10721052 /**
1073- * Set the service to be used .
1053+ * Set the service dimension that will be included in the metrics .
10741054 *
10751055 * @param service - The service to be used
10761056 */
1077- private setService ( service : string | undefined ) : void {
1078- const targetService =
1057+ #resolveServiceName ( service ? : string ) : string {
1058+ return (
10791059 service ||
10801060 this . getCustomConfigService ( ) ?. getServiceName ( ) ||
10811061 this . #envConfig. serviceName ||
1082- this . defaultServiceName ;
1083- if ( targetService . length > 0 ) {
1084- this . setDefaultDimensions ( { service : targetService } ) ;
1085- }
1062+ this . defaultServiceName
1063+ ) ;
10861064 }
10871065
10881066 /**
@@ -1189,6 +1167,38 @@ class Metrics extends Utility implements MetricsInterface {
11891167 **/
11901168 return 0 ;
11911169 }
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+ }
11921202}
11931203
11941204export { Metrics } ;
0 commit comments