@@ -466,7 +466,7 @@ def lambda_handler(evt, ctx):
466
466
467
467
output = json .loads (capsys .readouterr ().out .strip ())
468
468
469
- dimensions .insert ( 0 , {"name" : "service" , "value" : "test_service" })
469
+ dimensions .append ( {"name" : "service" , "value" : "test_service" })
470
470
expected = serialize_metrics (metrics = metrics , dimensions = dimensions , namespace = namespace )
471
471
472
472
remove_timestamp (metrics = [output , expected ]) # Timestamp will always be different
@@ -503,33 +503,31 @@ def lambda_handler(evt, ctx):
503
503
assert expected == output
504
504
505
505
506
- def test_log_metrics_with_renamed_service (capsys , metrics ):
506
+ def test_log_metrics_with_renamed_service (capsys , metrics , metric ):
507
507
# GIVEN Metrics is initialized with service specified
508
508
my_metrics = Metrics (service = "test_service" , namespace = "test_application" )
509
509
for metric in metrics :
510
510
my_metrics .add_metric (** metric )
511
511
512
- # WHEN we manually call add_dimension to change the value of the service dimension
513
- my_metrics .add_dimension (name = "service" , value = "another_test_service" )
514
-
515
512
@my_metrics .log_metrics
516
513
def lambda_handler (evt , ctx ):
514
+ # WHEN we manually call add_dimension to change the value of the service dimension
515
+ my_metrics .add_dimension (name = "service" , value = "another_test_service" )
516
+ my_metrics .add_metric (** metric )
517
517
return True
518
518
519
519
lambda_handler ({}, {})
520
520
521
521
output = json .loads (capsys .readouterr ().out .strip ())
522
+ lambda_handler ({}, {})
523
+ second_output = json .loads (capsys .readouterr ().out .strip ())
522
524
523
- expected_dimensions = [{"name" : "service" , "value" : "test_service" }]
524
- expected = serialize_metrics (
525
- metrics = metrics , dimensions = expected_dimensions , namespace = {"name" : "test_application" }
526
- )
527
-
528
- remove_timestamp (metrics = [output , expected ]) # Timestamp will always be different
525
+ remove_timestamp (metrics = [output ]) # Timestamp will always be different
529
526
530
527
# THEN we should have no exceptions and the dimensions should be set to the name provided in the
531
528
# add_dimension call
532
529
assert output ["service" ] == "another_test_service"
530
+ assert second_output ["service" ] == "another_test_service"
533
531
534
532
535
533
def test_log_metrics_with_namespace_overridden (capsys , metrics , dimensions ):
@@ -649,3 +647,40 @@ def lambda_handler(evt, context):
649
647
lambda_handler ({}, {})
650
648
assert len (w ) == 1
651
649
assert str (w [- 1 ].message ) == "No metrics to publish, skipping"
650
+
651
+
652
+ def test_log_metrics_with_implicit_dimensions_called_twice (capsys , metrics ):
653
+ # GIVEN Metrics is initialized with service specified
654
+ my_metrics = Metrics (service = "test_service" , namespace = "test_application" )
655
+
656
+ # WHEN we utilize log_metrics to serialize and don't explicitly add any dimensions,
657
+ # and the lambda function is called more than once
658
+ @my_metrics .log_metrics
659
+ def lambda_handler (evt , ctx ):
660
+ for metric in metrics :
661
+ my_metrics .add_metric (** metric )
662
+ return True
663
+
664
+ lambda_handler ({}, {})
665
+ output = json .loads (capsys .readouterr ().out .strip ())
666
+
667
+ lambda_handler ({}, {})
668
+ second_output = json .loads (capsys .readouterr ().out .strip ())
669
+
670
+ expected_dimensions = [{"name" : "service" , "value" : "test_service" }]
671
+ expected = serialize_metrics (
672
+ metrics = metrics , dimensions = expected_dimensions , namespace = {"name" : "test_application" }
673
+ )
674
+
675
+ remove_timestamp (metrics = [output , expected , second_output ]) # Timestamp will always be different
676
+
677
+ # THEN we should have no exceptions and the dimensions should be set to the name provided in the
678
+ # service passed to Metrics constructor
679
+ assert output ["service" ] == "test_service"
680
+ assert second_output ["service" ] == "test_service"
681
+
682
+ for metric_record in output ["_aws" ]["CloudWatchMetrics" ]:
683
+ assert ["service" ] in metric_record ["Dimensions" ]
684
+
685
+ for metric_record in second_output ["_aws" ]["CloudWatchMetrics" ]:
686
+ assert ["service" ] in metric_record ["Dimensions" ]
0 commit comments