Closed
Description
Expected Behaviour
Metrics - Assert multiple EMF blobs with pytest example should be valid python syntax
Current Behaviour
Current example has a python syntax error of ',' or ')' expected
Code snippet
from aws_lambda_powertools import Metrics
from aws_lambda_powertools.metrics import MetricUnit
from collections import namedtuple
import json
def capture_metrics_output_multiple_emf_objects(capsys):
return [json.loads(line.strip()) for line in capsys.readouterr().out.split("\n") if line]
def test_log_metrics(capsys):
# GIVEN Metrics is initialized
metrics = Metrics(namespace="ServerlessAirline")
# WHEN log_metrics is used with capture_cold_start_metric
@metrics.log_metrics(capture_cold_start_metric=True)
def lambda_handler(evt, ctx):
metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1)
metrics.add_dimension(name="environment", value="prod")
# log_metrics uses function_name property from context to add as a dimension for cold start metric
LambdaContext = namedtuple("LambdaContext", "function_name")
lambda_handler({}, LambdaContext("example_fn")
cold_start_blob, custom_metrics_blob = capture_metrics_output_multiple_emf_objects(capsys)
# THEN ColdStart metric and function_name dimension should be logged
# in a separate EMF blob than the application metrics
assert cold_start_blob["ColdStart"] == [1.0]
assert cold_start_blob["function_name"] == "example_fn"
assert "SuccessfulBooking" in custom_metrics_blob # as per previous example
Possible Solution
Fix the python syntax, like in PR #1122, deployed at https://gyft.github.io/aws-lambda-powertools-python/latest/core/metrics/#functional-testing
import json
from collections import namedtuple
from aws_lambda_powertools import Metrics
from aws_lambda_powertools.metrics import MetricUnit
def capture_metrics_output_multiple_emf_objects(capsys):
return [json.loads(line.strip()) for line in capsys.readouterr().out.split("\n") if line]
def test_log_metrics(capsys):
# GIVEN Metrics is initialized
metrics = Metrics(namespace="ServerlessAirline")
# WHEN log_metrics is used with capture_cold_start_metric
@metrics.log_metrics(capture_cold_start_metric=True)
def lambda_handler(evt, ctx):
metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1)
metrics.add_dimension(name="environment", value="prod")
# log_metrics uses function_name property from context to add as a dimension for cold start metric
LambdaContext = namedtuple("LambdaContext", "function_name")
lambda_handler({}, LambdaContext("example_fn"))
cold_start_blob, custom_metrics_blob = capture_metrics_output_multiple_emf_objects(capsys)
# THEN ColdStart metric and function_name dimension should be logged
# in a separate EMF blob than the application metrics
assert cold_start_blob["ColdStart"] == [1.0]
assert cold_start_blob["function_name"] == "example_fn"
assert "SuccessfulBooking" in custom_metrics_blob # as per previous example
Steps to Reproduce
- Go to https://awslabs.github.io/aws-lambda-powertools-python/latest/core/metrics/#functional-testing
- Copy code for
Assert multiple EMF blobs with pytest
- Try to compile the python file
AWS Lambda Powertools for Python version
latest
AWS Lambda function runtime
3.9
Packaging format used
PyPi
Debugging logs
N/A