3
3
*
4
4
* @group unit/metrics/class
5
5
*/
6
- import type { LambdaInterface } from '@aws-lambda-powertools/commons/types' ;
6
+ import type {
7
+ GenericLogger ,
8
+ LambdaInterface ,
9
+ } from '@aws-lambda-powertools/commons/types' ;
7
10
import context from '@aws-lambda-powertools/testing-utils/context' ;
8
11
import type { Context , Handler } from 'aws-lambda' ;
9
12
import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js' ;
@@ -350,7 +353,13 @@ describe('Class: Metrics', () => {
350
353
351
354
test ( 'it should update existing dimension value if same dimension is added again' , ( ) => {
352
355
// Prepare
353
- const metrics : Metrics = new Metrics ( { namespace : TEST_NAMESPACE } ) ;
356
+ const logger = {
357
+ warn : jest . fn ( ) ,
358
+ } as unknown as GenericLogger ;
359
+ const metrics : Metrics = new Metrics ( {
360
+ namespace : TEST_NAMESPACE ,
361
+ logger,
362
+ } ) ;
354
363
const dimensionName = 'test-dimension' ;
355
364
356
365
// Act
@@ -365,6 +374,9 @@ describe('Class: Metrics', () => {
365
374
} ,
366
375
} )
367
376
) ;
377
+ expect ( logger . warn ) . toHaveBeenCalledWith (
378
+ `Dimension "test-dimension" has already been added. The previous value will be overwritten.`
379
+ ) ;
368
380
} ) ;
369
381
370
382
test ( 'it should throw error if the number of dimensions exceeds the maximum allowed' , ( ) => {
@@ -521,7 +533,7 @@ describe('Class: Metrics', () => {
521
533
const dimensionName = 'test-dimension' ;
522
534
const dimensionValue = 'test-value' ;
523
535
const dimensionsToBeAdded : LooseObject = { } ;
524
- for ( let i = 0 ; i < MAX_DIMENSION_COUNT ; i ++ ) {
536
+ for ( let i = 0 ; i < MAX_DIMENSION_COUNT - 1 ; i ++ ) {
525
537
dimensionsToBeAdded [ `${ dimensionName } -${ i } ` ] = `${ dimensionValue } -${ i } ` ;
526
538
}
527
539
@@ -531,7 +543,7 @@ describe('Class: Metrics', () => {
531
543
) . not . toThrowError ( ) ;
532
544
// biome-ignore lint/complexity/useLiteralKeys: This needs to be accessed with literal key for testing
533
545
expect ( Object . keys ( metrics [ 'dimensions' ] ) . length ) . toBe (
534
- MAX_DIMENSION_COUNT
546
+ MAX_DIMENSION_COUNT - 1 // Starts from 1 because the service dimension is already added by default
535
547
) ;
536
548
} ) ;
537
549
@@ -541,22 +553,22 @@ describe('Class: Metrics', () => {
541
553
const dimensionName = 'test-dimension' ;
542
554
const dimensionValue = 'test-value' ;
543
555
const dimensionsToBeAdded : LooseObject = { } ;
544
- for ( let i = 0 ; i < MAX_DIMENSION_COUNT ; i ++ ) {
556
+ for ( let i = 0 ; i < MAX_DIMENSION_COUNT - 1 ; i ++ ) {
545
557
dimensionsToBeAdded [ `${ dimensionName } -${ i } ` ] = `${ dimensionValue } -${ i } ` ;
546
558
}
547
559
548
560
// Act & Assess
549
561
metrics . addDimensions ( dimensionsToBeAdded ) ;
550
562
// biome-ignore lint/complexity/useLiteralKeys: This needs to be accessed with literal key for testing
551
563
expect ( Object . keys ( metrics [ 'dimensions' ] ) . length ) . toBe (
552
- MAX_DIMENSION_COUNT
564
+ MAX_DIMENSION_COUNT - 1 // Starts from 1 because the service dimension is already added by default
553
565
) ;
554
566
expect ( ( ) =>
555
567
metrics . addDimensions ( {
556
568
'another-dimension' : 'another-dimension-value' ,
557
569
} )
558
570
) . toThrowError (
559
- `Unable to add 1 dimensions: the number of metric dimensions must be lower than ${ MAX_DIMENSION_COUNT } `
571
+ `The number of metric dimensions must be lower than ${ MAX_DIMENSION_COUNT } `
560
572
) ;
561
573
} ) ;
562
574
0 commit comments