Closed jessepoulton-mydeal closed 3 months ago
Thanks for opening your first issue here! We'll come back to you as soon as we can. In the meantime, check out the #dotnet channel on our Powertools for AWS Lambda Discord: Invite link
Hey @jessepoulton-mydeal thank you for reporting the issue and the possible solution. I will be looking at this today and keep you posted. If you have the time and want to contribute feel free to create a pull request.
@jessepoulton-mydeal Fix released. Metrics 1.6.2
Expected Behaviour
I am able to add metrics from multiple threads without issue.
Current Behaviour
Metrics throws an exception:
Code snippet
To make this test fail reliably you need to set the PowertoolsConfigurations.MaxMetrics to a low number like 1
Add this method to the FunctionHandler.cs in the metrics tests project:
and this test to FunctionHandlerTests.cs:
Possible Solution
You could either add additional locks to the Metrics.AddMetric method:
Or you could possibly use concurrent collections under the hood and the Interlocked class to handle incrementing counts and swapping objects when a flush is requried.
Steps to Reproduce
Unit test that shows the issue is in the Code Snippet section. Ensure the MaxMetrics is set to a low value like 1, otherwise the test will inconsistently pass/fail.
Powertools for AWS Lambda (.NET) version
latest
AWS Lambda function runtime
dotnet6
Debugging logs
No response