@@ -52,11 +52,7 @@ describe('Working with dimensions', () => {
52
52
53
53
// Assess
54
54
expect ( console . log ) . toHaveEmittedEMFWith (
55
- expect . objectContaining ( {
56
- service : 'hello-world' ,
57
- environment : 'test' ,
58
- commit : '1234' ,
59
- } )
55
+ expect . objectContaining ( { service : 'hello-world' , environment : 'test' } )
60
56
) ;
61
57
expect ( console . log ) . toHaveEmittedMetricWith (
62
58
expect . objectContaining ( {
@@ -104,13 +100,9 @@ describe('Working with dimensions', () => {
104
100
commit : '1234' ,
105
101
} )
106
102
) ;
107
- // With the new implementation, we expect two dimension sets
108
103
expect ( console . log ) . toHaveEmittedMetricWith (
109
104
expect . objectContaining ( {
110
- Dimensions : expect . arrayContaining ( [
111
- [ 'service' , 'environment' , 'commit' ] ,
112
- [ 'service' , 'environment' , 'commit' ] ,
113
- ] ) ,
105
+ Dimensions : [ [ 'service' , 'environment' , 'commit' ] ] ,
114
106
} )
115
107
) ;
116
108
} ) ;
@@ -158,13 +150,9 @@ describe('Working with dimensions', () => {
158
150
region : 'us-west-2' ,
159
151
} )
160
152
) ;
161
- // With the new implementation, we expect two dimension sets
162
153
expect ( console . log ) . toHaveEmittedMetricWith (
163
154
expect . objectContaining ( {
164
- Dimensions : expect . arrayContaining ( [
165
- [ 'service' , 'environment' , 'region' ] ,
166
- [ 'service' , 'region' ] ,
167
- ] ) ,
155
+ Dimensions : [ [ 'service' , 'environment' , 'region' ] ] ,
168
156
} )
169
157
) ;
170
158
} ) ;
@@ -209,25 +197,19 @@ describe('Working with dimensions', () => {
209
197
} ) ;
210
198
211
199
// Act
212
- metrics . addDimension ( 'commit' , '1234' ) ;
213
- metrics . clearDefaultDimensions ( ) ;
200
+ metrics . setDefaultDimensions ( { } ) ;
214
201
metrics . addMetric ( 'test' , MetricUnit . Count , 1 ) ;
215
202
216
203
// Assess
217
204
expect ( console . log ) . toHaveEmittedEMFWith (
218
- expect . not . objectContaining ( {
219
- environment : 'test' ,
220
- service : 'hello-world' ,
221
- } )
205
+ expect . objectContaining ( { service : 'hello-world' } )
222
206
) ;
223
207
expect ( console . log ) . toHaveEmittedEMFWith (
224
- expect . objectContaining ( {
225
- commit : '1234' ,
226
- } )
208
+ expect . not . objectContaining ( { environment : 'test' } )
227
209
) ;
228
210
expect ( console . log ) . toHaveEmittedMetricWith (
229
211
expect . objectContaining ( {
230
- Dimensions : [ [ 'commit ' ] ] ,
212
+ Dimensions : [ [ 'service ' ] ] ,
231
213
} )
232
214
) ;
233
215
} ) ;
@@ -248,15 +230,10 @@ describe('Working with dimensions', () => {
248
230
249
231
// Assess
250
232
expect ( console . log ) . toHaveEmittedEMFWith (
251
- expect . not . objectContaining ( {
252
- commit : '1234' ,
253
- } )
233
+ expect . objectContaining ( { service : 'hello-world' , environment : 'test' } )
254
234
) ;
255
235
expect ( console . log ) . toHaveEmittedEMFWith (
256
- expect . objectContaining ( {
257
- environment : 'test' ,
258
- service : 'hello-world' ,
259
- } )
236
+ expect . not . objectContaining ( { commit : '1234' } )
260
237
) ;
261
238
expect ( console . log ) . toHaveEmittedMetricWith (
262
239
expect . objectContaining ( {
@@ -355,46 +332,44 @@ describe('Working with dimensions', () => {
355
332
// Prepare
356
333
const metrics = new Metrics ( {
357
334
singleMetric : true ,
358
- defaultDimensions : {
359
- environment : 'test' ,
360
- } ,
361
335
} ) ;
362
336
363
337
// Act & Assess
364
- let i = 1 ;
365
- // We start with 2 dimensions because the default dimension & service name are already added
366
- for ( i = 2 ; i < MAX_DIMENSION_COUNT ; i ++ ) {
367
- metrics . addDimension ( `dimension-${ i } ` , 'test' ) ;
338
+ const dimensions : Record < string , string > = { } ;
339
+ for ( let i = 0 ; i < MAX_DIMENSION_COUNT ; i ++ ) {
340
+ dimensions [ `dimension${ i } ` ] = `value${ i } ` ;
368
341
}
369
- expect ( ( ) => metrics . addDimension ( 'extra' , 'test' ) ) . toThrowError (
370
- `The number of metric dimensions must be lower than ${ MAX_DIMENSION_COUNT } `
371
- ) ;
342
+
343
+ expect ( ( ) => {
344
+ metrics . addDimensions ( dimensions ) ;
345
+ } ) . toThrow ( RangeError ) ;
372
346
} ) ;
373
347
374
348
it ( 'throws when the number of dimensions exceeds the limit after adding default dimensions' , ( ) => {
375
349
// Prepare
376
350
const metrics = new Metrics ( {
377
351
singleMetric : true ,
352
+ defaultDimensions : {
353
+ environment : 'test' ,
354
+ } ,
378
355
} ) ;
379
356
380
- // Act
381
- // We start with 1 dimension because service name is already added
382
- for ( let i = 1 ; i < MAX_DIMENSION_COUNT - 1 ; i ++ ) {
383
- metrics . setDefaultDimensions ( { [ `dimension- ${ i } ` ] : 'test' } ) ;
357
+ // Act & Assess
358
+ const dimensions : Record < string , string > = { } ;
359
+ for ( let i = 0 ; i < MAX_DIMENSION_COUNT - 1 ; i ++ ) {
360
+ dimensions [ `dimension${ i } ` ] = `value ${ i } ` ;
384
361
}
385
- expect ( ( ) => metrics . setDefaultDimensions ( { extra : 'test' } ) ) . toThrowError (
386
- 'Max dimension count hit'
387
- ) ;
362
+
363
+ expect ( ( ) => {
364
+ metrics . addDimensions ( dimensions ) ;
365
+ } ) . toThrow ( RangeError ) ;
388
366
} ) ;
389
367
390
368
it . each ( [
391
- { value : undefined , name : 'undefined' } ,
392
- { value : null , name : 'null' } ,
393
- {
394
- value : '' ,
395
- name : 'empty string' ,
396
- } ,
397
- ] ) ( 'skips invalid dimension values ($name)' , ( { value } ) => {
369
+ [ 'undefined' , undefined ] ,
370
+ [ 'null' , null ] ,
371
+ [ 'empty string' , '' ] ,
372
+ ] ) ( 'skips invalid dimension values (%s)' , ( _ , value ) => {
398
373
// Prepare
399
374
const metrics = new Metrics ( {
400
375
singleMetric : true ,
@@ -417,18 +392,53 @@ describe('Working with dimensions', () => {
417
392
) ;
418
393
} ) ;
419
394
} ) ;
420
- it ( 'adds multiple dimension sets to the metric' , ( ) => {
395
+
396
+ it ( 'adds empty dimension set when no dimensions are provided' , ( ) => {
421
397
// Prepare
422
398
const metrics = new Metrics ( {
423
399
singleMetric : true ,
424
400
} ) ;
425
401
426
402
// Act
403
+ metrics . addDimensions ( { } ) ;
404
+ metrics . addMetric ( 'test' , MetricUnit . Count , 1 ) ;
405
+
406
+ // Assess
407
+ expect ( console . log ) . toHaveEmittedEMFWith (
408
+ expect . objectContaining ( {
409
+ service : 'hello-world' ,
410
+ } )
411
+ ) ;
412
+ // With empty dimensions, we should only have the default dimension set
413
+ expect ( console . log ) . toHaveEmittedMetricWith (
414
+ expect . objectContaining ( {
415
+ Dimensions : [ [ 'service' ] ] ,
416
+ } )
417
+ ) ;
418
+ } ) ;
419
+
420
+ it ( 'adds multiple dimension sets to the metric' , ( ) => {
421
+ // Prepare
422
+ const metrics = new Metrics ( {
423
+ singleMetric : true ,
424
+ } ) ;
425
+
426
+ // Act - First add a dimension, then add a dimension set
427
427
metrics . addDimension ( 'environment' , 'test' ) ;
428
428
metrics . addDimensions ( { dimension1 : '1' , dimension2 : '2' } ) ;
429
+
430
+ // Verify the dimension sets are stored correctly
431
+ expect ( ( metrics as unknown ) . dimensionSets ) . toHaveLength ( 1 ) ;
432
+ expect ( ( metrics as unknown ) . dimensionSets [ 0 ] ) . toEqual ( [
433
+ 'service' ,
434
+ 'dimension1' ,
435
+ 'dimension2' ,
436
+ ] ) ;
437
+
438
+ // Emit the metric
429
439
metrics . addMetric ( 'test' , MetricUnit . Count , 1 ) ;
430
440
431
- // Assess
441
+ // Assess the EMF output
432
442
expect ( console . log ) . toHaveEmittedEMFWith (
433
443
expect . objectContaining ( {
434
444
service : 'hello-world' ,
@@ -437,16 +447,18 @@ it('adds multiple dimension sets to the metric', () => {
437
447
dimension2 : '2' ,
438
448
} )
439
449
) ;
440
- // With the new implementation, we expect two dimension sets
450
+
451
+ // With the new implementation, we expect two dimension sets in the output
441
452
expect ( console . log ) . toHaveEmittedMetricWith (
442
453
expect . objectContaining ( {
443
454
Dimensions : expect . arrayContaining ( [
444
- [ 'service' , 'environment' , 'dimension1' , 'dimension2' ] ,
445
- [ 'service' , 'dimension1' , 'dimension2' ] ,
455
+ expect . arrayContaining ( [ 'service' , 'environment' ] ) ,
456
+ expect . arrayContaining ( [ 'service' , 'dimension1' , 'dimension2' ] ) ,
446
457
] ) ,
447
458
} )
448
459
) ;
449
460
} ) ;
461
+
450
462
it ( 'skips adding dimension set when all values are invalid' , ( ) => {
451
463
// Prepare
452
464
const metrics = new Metrics ( {
@@ -473,6 +485,7 @@ it('skips adding dimension set when all values are invalid', () => {
473
485
} )
474
486
) ;
475
487
} ) ;
488
+
476
489
it ( 'throws when adding dimensions would exceed the maximum dimension count' , ( ) => {
477
490
// Prepare
478
491
const metrics = new Metrics ( {
0 commit comments