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: Metrics throws exception when using AddMetadata #505

Merged
merged 2 commits into from
Oct 25, 2023

Conversation

hjgraca
Copy link
Contributor

@hjgraca hjgraca commented Oct 20, 2023

Please provide the issue number

Issue number: #504

Summary

There are two issues when calling Metrics.AddMetadata

  • If the same key is used on the Metrics.AddMetric and on the Metrics.AddMetadata - it will throw the An item with the same key has already been added exception
  • On the second Lambda invocation it will throw the An item with the same key has already been added exception

Changes

Please provide a summary of what's being changed

  • Clear Metadata dictionary when clearing metrics after flush, this prevents issue on second call having an existing key.
  • Move metadata node serialization to bottom, and try add, if it fails ignore, this fixes the Metrics and Metadata having the same key. Metadata is ignored
  • Add tests

User experience

Please share what the user experience looks like before and after this change

Checklist

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

Is this a breaking change?

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.

Clear Metadata dictionary when clearing metrics after flush. Move metadata node serialization to bottom, and try add, if it fails ignore. Add tests
@auto-assign auto-assign bot requested review from amirkaws and sliedig October 20, 2023 11:22
@boring-cyborg boring-cyborg bot added area/metrics Core metrics utility tests labels Oct 20, 2023
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 20, 2023
@@ -66,6 +66,7 @@ public Metadata()
internal void ClearMetrics()
{
_metricDirective.Metrics.Clear();
CustomMetadata?.Clear();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clear Metadata dictionary when clearing Metrics. Null check for safety

@github-actions github-actions bot added the bug Unexpected, reproducible and unintended software behaviour label Oct 20, 2023
@@ -158,7 +159,7 @@ internal List<MetricDefinition> GetMetrics()
/// <param name="value">Metadata value</param>
internal void AddMetadata(string key, object value)
{
CustomMetadata.Add(key, value);
CustomMetadata.TryAdd(key, value);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just to be safe and avoid exceptions

foreach (var metricDefinition in AWS.GetMetrics())
{
var values = metricDefinition.Values;
targetMembers.Add(metricDefinition.Name, values.Count == 1 ? values[0] : values);
}

foreach (var metadata in AWS.CustomMetadata) targetMembers.TryAdd(metadata.Key, metadata.Value);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Move to the bottom. If Metrics has key. Metadata will be ignored

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Oct 20, 2023
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@codecov-commenter
Copy link

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (21aff1f) 72.08% compared to head (e921a7b) 71.72%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #505      +/-   ##
===========================================
- Coverage    72.08%   71.72%   -0.37%     
===========================================
  Files          101      101              
  Lines         4063     4064       +1     
  Branches       412      413       +1     
===========================================
- Hits          2929     2915      -14     
- Misses        1020     1035      +15     
  Partials       114      114              
Flag Coverage Δ
unittests 71.72% <75.00%> (-0.37%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...rc/AWS.Lambda.Powertools.Metrics/Model/RootNode.cs 100.00% <100.00%> (ø)
...rc/AWS.Lambda.Powertools.Metrics/Model/Metadata.cs 94.23% <66.66%> (-1.85%) ⬇️

... and 7 files with indirect coverage changes

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

Copy link
Contributor

@amirkaws amirkaws left a comment

Choose a reason for hiding this comment

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

LGTM

@hjgraca hjgraca merged commit 6408dc4 into aws-powertools:develop Oct 25, 2023
6 checks passed
@hjgraca hjgraca deleted the fix-exception-addmetadata branch October 25, 2023 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/metrics Core metrics utility bug Unexpected, reproducible and unintended software behaviour documentation Improvements or additions to documentation size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Metrics throws exception when using AddMetadata (An item with the same key has already been added)
3 participants