|
7 | 7 |
|
8 | 8 | from aws_lambda_powertools import Metrics, single_metric
|
9 | 9 | from aws_lambda_powertools.metrics import MetricUnit, MetricUnitError, MetricValueError, SchemaValidationError
|
| 10 | +from aws_lambda_powertools.metrics import metrics as metrics_global |
10 | 11 | from aws_lambda_powertools.metrics.base import MetricManager
|
11 | 12 |
|
12 | 13 |
|
13 | 14 | @pytest.fixture(scope="function", autouse=True)
|
14 | 15 | def reset_metric_set():
|
15 | 16 | metrics = Metrics()
|
16 | 17 | metrics.clear_metrics()
|
| 18 | + metrics_global.is_cold_start = True # ensure each test has cold start |
17 | 19 | yield
|
18 | 20 |
|
19 | 21 |
|
@@ -112,6 +114,10 @@ def capture_metrics_output(capsys):
|
112 | 114 | return json.loads(capsys.readouterr().out.strip())
|
113 | 115 |
|
114 | 116 |
|
| 117 | +def capture_metrics_output_multiple_emf_objects(capsys): |
| 118 | + return [json.loads(line.strip()) for line in capsys.readouterr().out.split("\n") if line] |
| 119 | + |
| 120 | + |
115 | 121 | def test_single_metric_logs_one_metric_only(capsys, metric, dimension, namespace):
|
116 | 122 | # GIVEN we try adding more than one metric
|
117 | 123 | # WHEN using single_metric context manager
|
@@ -495,7 +501,7 @@ def lambda_handler(evt, context):
|
495 | 501 |
|
496 | 502 | LambdaContext = namedtuple("LambdaContext", "function_name")
|
497 | 503 | lambda_handler({}, LambdaContext("example_fn"))
|
498 |
| - _ = capture_metrics_output(capsys) # ignore first stdout captured |
| 504 | + _, _ = capture_metrics_output_multiple_emf_objects(capsys) # ignore first stdout captured |
499 | 505 |
|
500 | 506 | # THEN ColdStart metric and function_name dimension should be logged once
|
501 | 507 | lambda_handler({}, LambdaContext("example_fn"))
|
@@ -630,3 +636,34 @@ def test_serialize_metric_set_metric_definition(metric, dimension, namespace, se
|
630 | 636 | assert "Timestamp" in metric_definition_output["_aws"]
|
631 | 637 | remove_timestamp(metrics=[metric_definition_output, expected_metric_definition])
|
632 | 638 | assert metric_definition_output == expected_metric_definition
|
| 639 | + |
| 640 | + |
| 641 | +def test_log_metrics_capture_cold_start_metric_separately(capsys, namespace, service, metric, dimension): |
| 642 | + # GIVEN Metrics is initialized |
| 643 | + my_metrics = Metrics(service=service, namespace=namespace) |
| 644 | + |
| 645 | + # WHEN log_metrics is used with capture_cold_start_metric |
| 646 | + @my_metrics.log_metrics(capture_cold_start_metric=True) |
| 647 | + def lambda_handler(evt, context): |
| 648 | + my_metrics.add_metric(**metric) |
| 649 | + my_metrics.add_dimension(**dimension) |
| 650 | + |
| 651 | + LambdaContext = namedtuple("LambdaContext", "function_name") |
| 652 | + lambda_handler({}, LambdaContext("example_fn")) |
| 653 | + |
| 654 | + cold_start_blob, custom_metrics_blob = capture_metrics_output_multiple_emf_objects(capsys) |
| 655 | + |
| 656 | + # THEN ColdStart metric and function_name dimension should be logged |
| 657 | + # in a separate EMF blob than the application metrics |
| 658 | + assert cold_start_blob["ColdStart"] == 1 |
| 659 | + assert cold_start_blob["function_name"] == "example_fn" |
| 660 | + assert cold_start_blob["service"] == service |
| 661 | + |
| 662 | + # and that application metrics dimensions are not part of ColdStart EMF blob |
| 663 | + assert "test_dimension" not in cold_start_blob |
| 664 | + |
| 665 | + # THEN application metrics EMF blob should not have function_name dimension |
| 666 | + assert "function_name" not in custom_metrics_blob |
| 667 | + assert custom_metrics_blob["service"] == service |
| 668 | + assert custom_metrics_blob["single_metric"] == metric["value"] |
| 669 | + assert custom_metrics_blob["test_dimension"] == dimension["value"] |
0 commit comments