|
| 1 | +from datetime import datetime, timedelta |
| 2 | +from typing import TYPE_CHECKING |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from localstack.testing.pytest import markers |
| 7 | +from localstack.testing.pytest.snapshot import is_aws |
| 8 | +from localstack.utils.strings import short_uid |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from mypy_boto3_cloudwatch import CloudWatchClient |
| 12 | +from localstack.utils.sync import retry |
| 13 | + |
| 14 | +TEST_SUCCESSFUL_LAMBDA = """ |
| 15 | +def handler(event, context): |
| 16 | + return {"success": "ok"} |
| 17 | +""" |
| 18 | + |
| 19 | +TEST_FAILING_LAMBDA = """ |
| 20 | +def handler(event, context): |
| 21 | + raise Exception('fail on purpose') |
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +class TestCloudWatchLambdaMetrics: |
| 26 | + """ |
| 27 | + Tests for metrics that are reported automatically by Lambda |
| 28 | + see also https://docs.aws.amazon.com/lambda/latest/dg/monitoring-metrics.html |
| 29 | + """ |
| 30 | + |
| 31 | + @markers.aws.validated |
| 32 | + def test_lambda_invoke_successful(self, aws_client, create_lambda_function, snapshot): |
| 33 | + """ |
| 34 | + successful invocation of lambda should report "Invocations" metric |
| 35 | + """ |
| 36 | + fn_name = f"fn-cw-{short_uid()}" |
| 37 | + create_lambda_function( |
| 38 | + func_name=fn_name, |
| 39 | + handler_file=TEST_SUCCESSFUL_LAMBDA, |
| 40 | + runtime="python3.9", |
| 41 | + ) |
| 42 | + result = aws_client.lambda_.invoke(FunctionName=fn_name) |
| 43 | + assert result["StatusCode"] == 200 |
| 44 | + snapshot.match("invoke", result) |
| 45 | + |
| 46 | + # wait for metrics |
| 47 | + result = retry( |
| 48 | + lambda: self._wait_for_lambda_metric( |
| 49 | + aws_client.cloudwatch, |
| 50 | + fn_name=fn_name, |
| 51 | + metric_name="Invocations", |
| 52 | + expected_return=[1.0], |
| 53 | + ), |
| 54 | + retries=200 if is_aws() else 20, |
| 55 | + sleep=10 if is_aws() else 1, |
| 56 | + ) |
| 57 | + snapshot.match("get-metric-data", result) |
| 58 | + |
| 59 | + @pytest.mark.skipif(not is_aws(), reason="'Errors' metrics not reported by LS") |
| 60 | + @markers.aws.validated |
| 61 | + def test_lambda_invoke_error(self, aws_client, create_lambda_function, snapshot): |
| 62 | + """ |
| 63 | + Unsuccessful Invocation -> resulting in error, should report |
| 64 | + "Errors" and "Invocations" metrics |
| 65 | + """ |
| 66 | + fn_name = f"fn-cw-{short_uid()}" |
| 67 | + create_lambda_function( |
| 68 | + func_name=fn_name, |
| 69 | + handler_file=TEST_FAILING_LAMBDA, |
| 70 | + runtime="python3.9", |
| 71 | + ) |
| 72 | + result = aws_client.lambda_.invoke(FunctionName=fn_name) |
| 73 | + snapshot.match("invoke", result) |
| 74 | + |
| 75 | + # wait for metrics |
| 76 | + invocation_res = retry( |
| 77 | + lambda: self._wait_for_lambda_metric( |
| 78 | + aws_client.cloudwatch, |
| 79 | + fn_name=fn_name, |
| 80 | + metric_name="Invocations", |
| 81 | + expected_return=[1.0], |
| 82 | + ), |
| 83 | + retries=200 if is_aws() else 20, |
| 84 | + sleep=10 if is_aws() else 1, |
| 85 | + ) |
| 86 | + snapshot.match("get-metric-data-invocations", invocation_res) |
| 87 | + |
| 88 | + # wait for "Errors" |
| 89 | + error_res = retry( |
| 90 | + lambda: self._wait_for_lambda_metric( |
| 91 | + aws_client.cloudwatch, |
| 92 | + fn_name=fn_name, |
| 93 | + metric_name="Errors", |
| 94 | + expected_return=[1.0], |
| 95 | + ), |
| 96 | + retries=200 if is_aws() else 20, |
| 97 | + sleep=10 if is_aws() else 1, |
| 98 | + ) |
| 99 | + snapshot.match("get-metric-data-errors", error_res) |
| 100 | + |
| 101 | + def _wait_for_lambda_metric( |
| 102 | + self, |
| 103 | + cloudwatch_client: "CloudWatchClient", |
| 104 | + fn_name: str, |
| 105 | + metric_name: str, |
| 106 | + expected_return: list[float], |
| 107 | + ): |
| 108 | + namespace = "AWS/Lambda" |
| 109 | + dimension = [{"Name": "FunctionName", "Value": fn_name}] |
| 110 | + metric_query = { |
| 111 | + "Id": "m1", |
| 112 | + "MetricStat": { |
| 113 | + "Metric": { |
| 114 | + "Namespace": namespace, |
| 115 | + "MetricName": metric_name, |
| 116 | + "Dimensions": dimension, |
| 117 | + }, |
| 118 | + "Period": 3600, |
| 119 | + "Stat": "Sum", |
| 120 | + }, |
| 121 | + } |
| 122 | + res = cloudwatch_client.get_metric_data( |
| 123 | + MetricDataQueries=[metric_query], |
| 124 | + StartTime=datetime.utcnow() - timedelta(hours=1), |
| 125 | + EndTime=datetime.utcnow(), |
| 126 | + ) |
| 127 | + assert res["MetricDataResults"][0]["Values"] == expected_return |
| 128 | + return res |
0 commit comments