Skip to content

Commit e13bb88

Browse files
authored
Fix Lambda CloudWatch metric recording upon invocation error (#9752)
1 parent a3d8c9d commit e13bb88

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

localstack/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ def populate_edge_configuration(
896896
)
897897
# SEMI-PUBLIC: not actively communicated
898898
LAMBDA_LIMITS_TOTAL_CODE_SIZE = int(os.environ.get("LAMBDA_LIMITS_TOTAL_CODE_SIZE", 80_530_636_800))
899-
# SEMI-PUBLIC: not actively communicated
899+
# PUBLIC: documented after AWS changed validation around 2023-11
900900
LAMBDA_LIMITS_CODE_SIZE_ZIPPED = int(os.environ.get("LAMBDA_LIMITS_CODE_SIZE_ZIPPED", 52_428_800))
901901
# SEMI-PUBLIC: not actively communicated
902902
LAMBDA_LIMITS_CODE_SIZE_UNZIPPED = int(

localstack/services/lambda_/invocation/version_manager.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
VersionState,
2626
)
2727
from localstack.services.lambda_.invocation.logs import LogHandler, LogItem
28-
from localstack.services.lambda_.invocation.metrics import record_cw_metric_invocation
28+
from localstack.services.lambda_.invocation.metrics import (
29+
record_cw_metric_error,
30+
record_cw_metric_invocation,
31+
)
2932
from localstack.services.lambda_.invocation.runtime_executor import get_runtime_executor
3033
from localstack.utils.strings import truncate
3134
from localstack.utils.threads import FuncThread, start_thread
@@ -215,14 +218,24 @@ def invoke(self, *, invocation: Invocation) -> InvocationResult:
215218
)
216219

217220
function_id = self.function_version.id
218-
# MAYBE: reuse threads
219-
start_thread(
220-
lambda *args, **kwargs: record_cw_metric_invocation(
221-
function_name=self.function.function_name,
222-
region_name=self.function_version.id.region,
223-
),
224-
name=f"record-cloudwatch-metric-{function_id.function_name}:{function_id.qualifier}",
225-
)
221+
# Record CloudWatch metrics in separate threads
222+
# MAYBE reuse threads rather than starting new threads upon every invocation
223+
if invocation_result.is_error:
224+
start_thread(
225+
lambda *args, **kwargs: record_cw_metric_error(
226+
function_name=self.function.function_name,
227+
region_name=self.function_version.id.region,
228+
),
229+
name=f"record-cloudwatch-metric-error-{function_id.function_name}:{function_id.qualifier}",
230+
)
231+
else:
232+
start_thread(
233+
lambda *args, **kwargs: record_cw_metric_invocation(
234+
function_name=self.function.function_name,
235+
region_name=self.function_version.id.region,
236+
),
237+
name=f"record-cloudwatch-metric-{function_id.function_name}:{function_id.qualifier}",
238+
)
226239
# MAYBE: consider using the same prefix logging as in error case for execution environment.
227240
# possibly as separate named logger.
228241
LOG.debug("Got logs for invocation '%s'", invocation.request_id)

localstack/services/lambda_/runtimes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
# 6. Review special tests including:
1919
# a) [ext] tests.aws.services.lambda_.test_lambda_endpoint_injection
2020
# 7. Before merging, run the ext integration tests to cover transparent endpoint injection testing.
21+
# 8. Add the new runtime to the K8 image build: https://github.com/localstack/lambda-cve-mitigation
22+
# 9. Inform the web team to update the resource browser (consider offering an endpoint in the future)
2123

2224
# Mapping from a) AWS Lambda runtime identifier => b) official AWS image on Amazon ECR Public
2325
# a) AWS Lambda runtimes: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html

tests/aws/services/cloudwatch/test_cloudwatch_metrics.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from datetime import datetime, timedelta
22
from typing import TYPE_CHECKING
33

4-
import pytest
5-
64
from localstack.testing.pytest import markers
75
from localstack.testing.pytest.snapshot import is_aws
86
from localstack.utils.strings import short_uid
@@ -56,7 +54,6 @@ def test_lambda_invoke_successful(self, aws_client, create_lambda_function, snap
5654
)
5755
snapshot.match("get-metric-data", result)
5856

59-
@pytest.mark.skipif(not is_aws(), reason="'Errors' metrics not reported by LS")
6057
@markers.aws.validated
6158
def test_lambda_invoke_error(self, aws_client, create_lambda_function, snapshot):
6259
"""

0 commit comments

Comments
 (0)