Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Metrics - Assert multiple EMF blobs with pytest has invalid python syntax #1212

Closed
michaelbrewer opened this issue May 18, 2022 · 1 comment
Labels
documentation Improvements or additions to documentation

Comments

@michaelbrewer
Copy link
Contributor

michaelbrewer commented May 18, 2022

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

Screen Shot 2022-05-17 at 6 02 25 PM

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

  1. Go to https://awslabs.github.io/aws-lambda-powertools-python/latest/core/metrics/#functional-testing
  2. Copy code for Assert multiple EMF blobs with pytest
  3. 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
@michaelbrewer michaelbrewer added bug Something isn't working triage Pending triage from maintainers labels May 18, 2022
@michaelbrewer michaelbrewer changed the title Bug: Assert multiple EMF blobs with pytest has invalid python syntax Bug: Metrics, Assert multiple EMF blobs with pytest has invalid python syntax May 18, 2022
@michaelbrewer michaelbrewer changed the title Bug: Metrics, Assert multiple EMF blobs with pytest has invalid python syntax Bug: Metrics - Assert multiple EMF blobs with pytest has invalid python syntax May 18, 2022
@sthulb sthulb changed the title Bug: Metrics - Assert multiple EMF blobs with pytest has invalid python syntax Docs: Metrics - Assert multiple EMF blobs with pytest has invalid python syntax May 18, 2022
@heitorlessa heitorlessa added documentation Improvements or additions to documentation and removed bug Something isn't working labels May 20, 2022
@sthulb sthulb closed this as completed Jun 14, 2022
@github-actions
Copy link
Contributor

Comments on closed issues are hard for our team to see.

@heitorlessa heitorlessa removed the triage Pending triage from maintainers label Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants