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

fix(logging): check if ILambdaContext is valid in LoggingLambdaContext.Extract #791

Merged

Conversation

gregsinclair42
Copy link
Contributor

@gregsinclair42 gregsinclair42 commented Feb 26, 2025

Issue number: 792

Summary

Check if the ILambdaContext is valid before extracting and using it. Powertools Logging already checks if the ILambdaContext is null, but only if it is the FIRST parameter, not the second or third, etc.

Changes

  • added more checks to determine if the argument is castable to ILambdaContext and not null.
  • added unit test

User experience

Previously, unit tests could be hard to write if a method we call had the [Logging] attribute on it, because then we had to ensure the LambdaContext was set and valid. Now we don't have to worry about the lambda context and can just focus on the code.

This would occur if there was a method like:

[Logging(LogEvent = true)]
public async Task<APIGatewayProxyResponse> Handle(APIGatewayProxyRequest request, ILambdaContext context)
{ 
// handle stuff
}

// then if it was called by a unit test like 
[Fact]
void Task Oops()
{
    // test setup...
    Assert.NotNull(await myHandler.Handle(apiGatewayProxyRequest, null)); // this throws an exception
}

Checklist

Please leave checklist items unchecked if they do not apply to your change.

Is this a breaking change? **NO**

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

… it. Do this to avoid null reference exceptions.
@boring-cyborg boring-cyborg bot added the area/logging Core logging utility label Feb 26, 2025
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Feb 26, 2025
Copy link

boring-cyborg bot commented Feb 26, 2025

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #dotnet channel on our Powertools for AWS Lambda Discord: Invite link

@gregsinclair42 gregsinclair42 changed the title Check for a valid ILambdaContext in the args before attempting to use… fixes https://github.com/aws-powertools/powertools-lambda-dotnet/issues/792 Feb 26, 2025
@gregsinclair42 gregsinclair42 changed the title fixes https://github.com/aws-powertools/powertools-lambda-dotnet/issues/792 fix(logging): check if ILambdaContext is valid Feb 26, 2025
@gregsinclair42 gregsinclair42 marked this pull request as ready for review February 26, 2025 20:34
@boring-cyborg boring-cyborg bot added the tests label Feb 26, 2025
@gregsinclair42 gregsinclair42 changed the title fix(logging): check if ILambdaContext is valid fix(logging): check if ILambdaContext is valid in LoggingLambdaContext.Extract Feb 26, 2025
@github-actions github-actions bot added the bug Unexpected, reproducible and unintended software behaviour label Feb 27, 2025
Copy link
Contributor

@hjgraca hjgraca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks great!

Copy link

codecov bot commented Feb 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.04%. Comparing base (08efcb2) to head (1dc566e).
Report is 11 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #791      +/-   ##
===========================================
- Coverage    74.04%   74.04%   -0.01%     
===========================================
  Files          210      210              
  Lines         8404     8402       -2     
  Branches       910      910              
===========================================
- Hits          6223     6221       -2     
  Misses        1883     1883              
  Partials       298      298              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@hjgraca hjgraca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! GTM

@gregsinclair42
Copy link
Contributor Author

Thank you! GTM

Thanks for the quick review @hjgraca. Can you let me know what is the expected timeline to get it merged and released into a new nuget package? Much appreciated.

@hjgraca
Copy link
Contributor

hjgraca commented Feb 27, 2025

Thank you! GTM

Thanks for the quick review @hjgraca. Can you let me know what is the expected timeline to get it merged and released into a new nuget package? Much appreciated.

Merged, right now :)
We release on Tuesdays, the next release date is Tuesday 11th March. Do you need it before that date?

@hjgraca
Copy link
Contributor

hjgraca commented Feb 27, 2025

Did you intend to close the pull request or was a mistake?

@hjgraca hjgraca reopened this Feb 27, 2025
@gregsinclair42
Copy link
Contributor Author

Did you intend to close the pull request or was a mistake?

sorry, my mistake! i meant to close a comment. Thanks for re-opening.

@gregsinclair42
Copy link
Contributor Author

Thank you! GTM

Thanks for the quick review @hjgraca. Can you let me know what is the expected timeline to get it merged and released into a new nuget package? Much appreciated.

Merged, right now :) We release on Tuesdays, the next release date is Tuesday 11th March. Do you need it before that date?

These buttons are misleading :)

image

We can wait until March 11th.

@hjgraca hjgraca merged commit 0e10c0c into aws-powertools:develop Feb 27, 2025
14 checks passed
Copy link

boring-cyborg bot commented Feb 27, 2025

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@gregsinclair42 gregsinclair42 deleted the CheckForValidLambdaContext branch February 27, 2025 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/logging Core logging utility bug Unexpected, reproducible and unintended software behaviour size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
2 participants