@@ -32,6 +32,14 @@ def metrics() -> List[Dict[str, str]]:
32
32
]
33
33
34
34
35
+ @pytest .fixture
36
+ def metrics_same_name () -> List [Dict [str , str ]]:
37
+ return [
38
+ {"name" : "metric_one" , "unit" : MetricUnit .Count , "value" : 1 },
39
+ {"name" : "metric_one" , "unit" : MetricUnit .Count , "value" : 5 },
40
+ ]
41
+
42
+
35
43
@pytest .fixture
36
44
def dimension () -> Dict [str , str ]:
37
45
return {"name" : "test_dimension" , "value" : "test" }
@@ -485,7 +493,7 @@ def lambda_handler(evt, context):
485
493
output = capture_metrics_output (capsys )
486
494
487
495
# THEN ColdStart metric and function_name dimension should be logged
488
- assert output ["ColdStart" ] == 1
496
+ assert output ["ColdStart" ] == [ 1.0 ]
489
497
assert output ["function_name" ] == "example_fn"
490
498
491
499
@@ -607,7 +615,7 @@ def lambda_handler(evt, ctx):
607
615
608
616
def test_serialize_metric_set_metric_definition (metric , dimension , namespace , service , metadata ):
609
617
expected_metric_definition = {
610
- "single_metric" : 1.0 ,
618
+ "single_metric" : [ 1.0 ] ,
611
619
"_aws" : {
612
620
"Timestamp" : 1592237875494 ,
613
621
"CloudWatchMetrics" : [
@@ -655,7 +663,7 @@ def lambda_handler(evt, context):
655
663
656
664
# THEN ColdStart metric and function_name dimension should be logged
657
665
# in a separate EMF blob than the application metrics
658
- assert cold_start_blob ["ColdStart" ] == 1
666
+ assert cold_start_blob ["ColdStart" ] == [ 1.0 ]
659
667
assert cold_start_blob ["function_name" ] == "example_fn"
660
668
assert cold_start_blob ["service" ] == service
661
669
@@ -669,5 +677,65 @@ def lambda_handler(evt, context):
669
677
670
678
# and that application metrics are recorded as normal
671
679
assert custom_metrics_blob ["service" ] == service
672
- assert custom_metrics_blob ["single_metric" ] == metric ["value" ]
680
+ assert custom_metrics_blob ["single_metric" ] == [ float ( metric ["value" ]) ]
673
681
assert custom_metrics_blob ["test_dimension" ] == dimension ["value" ]
682
+
683
+
684
+ def test_log_multiple_metrics (capsys , metrics_same_name , dimensions , namespace ):
685
+ # GIVEN Metrics is initialized
686
+ my_metrics = Metrics (namespace = namespace )
687
+
688
+ for dimension in dimensions :
689
+ my_metrics .add_dimension (** dimension )
690
+
691
+ # WHEN we utilize log_metrics to serialize
692
+ # and flush multiple metrics with the same name at the end of a function execution
693
+ @my_metrics .log_metrics
694
+ def lambda_handler (evt , ctx ):
695
+ for metric in metrics_same_name :
696
+ my_metrics .add_metric (** metric )
697
+
698
+ lambda_handler ({}, {})
699
+ output = capture_metrics_output (capsys )
700
+ expected = serialize_metrics (metrics = metrics_same_name , dimensions = dimensions , namespace = namespace )
701
+
702
+ # THEN we should have no exceptions
703
+ # and a valid EMF object should be flushed correctly
704
+ remove_timestamp (metrics = [output , expected ])
705
+ assert expected == output
706
+
707
+
708
+ def test_serialize_metric_set_metric_definition_multiple_values (
709
+ metrics_same_name , dimension , namespace , service , metadata
710
+ ):
711
+ expected_metric_definition = {
712
+ "metric_one" : [1.0 , 5.0 ],
713
+ "_aws" : {
714
+ "Timestamp" : 1592237875494 ,
715
+ "CloudWatchMetrics" : [
716
+ {
717
+ "Namespace" : "test_namespace" ,
718
+ "Dimensions" : [["test_dimension" , "service" ]],
719
+ "Metrics" : [{"Name" : "metric_one" , "Unit" : "Count" }],
720
+ }
721
+ ],
722
+ },
723
+ "service" : "test_service" ,
724
+ "username" : "test" ,
725
+ "test_dimension" : "test" ,
726
+ }
727
+
728
+ # GIVEN Metrics is initialized and multiple metrics are added with the same name
729
+ my_metrics = Metrics (service = service , namespace = namespace )
730
+ for metric in metrics_same_name :
731
+ my_metrics .add_metric (** metric )
732
+ my_metrics .add_dimension (** dimension )
733
+ my_metrics .add_metadata (** metadata )
734
+
735
+ # WHEN metrics are serialized manually
736
+ metric_definition_output = my_metrics .serialize_metric_set ()
737
+
738
+ # THEN we should emit a valid embedded metric definition object
739
+ assert "Timestamp" in metric_definition_output ["_aws" ]
740
+ remove_timestamp (metrics = [metric_definition_output , expected_metric_definition ])
741
+ assert metric_definition_output == expected_metric_definition
0 commit comments