Skip to content

Allow log_metrics to be used as a context manager for testing subset functions #1227

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

Closed
1 of 2 tasks
offbyone opened this issue May 26, 2022 · 4 comments
Closed
1 of 2 tasks

Comments

@offbyone
Copy link

Use case

I am attempting to write a unit test for a piece of functionality that isn't in the top of the lambda function call stack, but is emitting metrics. Moreover, the functionality is an async coroutine, so it's not amenable to being wrapped directly with the @metrics.log_metrics() decorator.

What I want is to be able to do this:

@pytest.mark.asyncio
async def test_sub_functionality(capsys):
    metrics = Metrics(namespace="ThisTest")

    # this is the new bit:
    with metrics.log_metrics():
        await fixture.awaitable_thing()

    log = capsys.readouterr().out.strip()  # remove any extra line
    metrics_output = json.loads(log)  # deserialize JSON str
   assert "TheMetric" in metrics_output["_aws"]["CloudWatchMetrics"][0]["Metrics"][0]["Name"]

Solution/User Experience

Provide a context manager facade for the log_metrics decorator; either by factoring out the contents of that method into a context manager directly, or by implementing it in a different way.

Alternative solutions

Alternately, if `log_metrics()` worked with arbitrary functions (and coroutines) instead of being limited to applying to functions that match the Lambda function interface, I could probably manage using it in my tests more easily.

Acknowledgment

@offbyone offbyone added feature-request feature request triage Pending triage from maintainers labels May 26, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented May 26, 2022

Thanks for opening your first issue here! We'll come back to you as soon as we can.

@heitorlessa
Copy link
Contributor

heitorlessa commented May 30, 2022

hey @offbyone thanks a lot for raising this to us :) I have a few questions

  • Are you primarily looking to confirm whether those metrics are being emitted? Or are they being flushed sooner than those coroutines hence the context manager?
    • e.g. assert "my_metric" in metrics.metric_set in the first case, as metric_set is our dict that holds metrics in memory before log_metrics flushed them at the end
  • Alternatively, would a synchronous method to serialize and flush metrics suffice here? Thinking of challenges we faced with colored functions, ctx manager, and generators before

Let's assume we go with a new context manager (need a new name), would you be open to contribute it after our pause? If we do hear from more customers that they're blocked, or we complete our E2E/Integ tests earlier, we can revisit this before end of July.

Look forward to hearing from you

Thanks!!

@heitorlessa heitorlessa added need-more-information Pending information to continue and removed triage Pending triage from maintainers labels May 30, 2022
@heitorlessa heitorlessa added the help wanted Could use a second pair of eyes/hands label Mar 10, 2023
@sthulb sthulb moved this from Triage to Backlog in Powertools for AWS Lambda (Python) Jun 19, 2023
@heitorlessa heitorlessa added need-customer-feedback Requires more customers feedback before making or revisiting a decision and removed need-more-information Pending information to continue labels Jun 24, 2024
@anafalcao
Copy link
Contributor

Hey everyone!
Closing this issue as it's been open for a while now, and we didn't have much customer feedback.
Please open a new one if it's still an issue.

@anafalcao anafalcao closed this as not planned Won't fix, can't repro, duplicate, stale Jan 22, 2025
@github-project-automation github-project-automation bot moved this from Backlog to Coming soon in Powertools for AWS Lambda (Python) Jan 22, 2025
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@dreamorosi dreamorosi moved this from Coming soon to Closed in Powertools for AWS Lambda (Python) Jan 22, 2025
@dreamorosi dreamorosi added rejected and removed help wanted Could use a second pair of eyes/hands need-customer-feedback Requires more customers feedback before making or revisiting a decision labels Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

4 participants