-
Notifications
You must be signed in to change notification settings - Fork 26
fix: Metrics throws exception when using AddMetadata #505
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
Merged
hjgraca
merged 2 commits into
aws-powertools:develop
from
hjgraca:fix-exception-addmetadata
Oct 25, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
|
@@ -29,7 +29,7 @@ public class Metadata | |
/// </summary> | ||
public Metadata() | ||
{ | ||
CloudWatchMetrics = new List<MetricDirective> {new()}; | ||
CloudWatchMetrics = new List<MetricDirective> { new() }; | ||
CustomMetadata = new Dictionary<string, object>(); | ||
} | ||
|
||
|
@@ -66,6 +66,7 @@ public Metadata() | |
internal void ClearMetrics() | ||
{ | ||
_metricDirective.Metrics.Clear(); | ||
CustomMetadata?.Clear(); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to be safe and avoid exceptions |
||
} | ||
|
||
/// <summary> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,14 +43,14 @@ public Dictionary<string, object> MetricData | |
var targetMembers = new Dictionary<string, object>(); | ||
|
||
foreach (var dimension in AWS.ExpandAllDimensionSets()) targetMembers.Add(dimension.Key, dimension.Value); | ||
|
||
foreach (var metadata in AWS.CustomMetadata) targetMembers.Add(metadata.Key, metadata.Value); | ||
|
||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to the bottom. If Metrics has key. Metadata will be ignored |
||
|
||
return targetMembers; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/Handlers/FunctionHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using System.Globalization; | ||
using System.Threading.Tasks; | ||
|
||
namespace AWS.Lambda.Powertools.Metrics.Tests.Handlers; | ||
|
||
public class FunctionHandler | ||
{ | ||
[Metrics(Namespace = "ns", Service = "svc")] | ||
public async Task<string> HandleSameKey(string input) | ||
{ | ||
Metrics.AddMetric("MyMetric", 1); | ||
Metrics.AddMetadata("MyMetric", "meta"); | ||
|
||
await Task.Delay(1); | ||
|
||
return input.ToUpper(CultureInfo.InvariantCulture); | ||
} | ||
|
||
[Metrics(Namespace = "ns", Service = "svc")] | ||
public async Task<string> HandleTestSecondCall(string input) | ||
{ | ||
Metrics.AddMetric("MyMetric", 1); | ||
Metrics.AddMetadata("MyMetadata", "meta"); | ||
|
||
await Task.Delay(1); | ||
|
||
return input.ToUpper(CultureInfo.InvariantCulture); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/Handlers/FunctionHandlerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace AWS.Lambda.Powertools.Metrics.Tests.Handlers; | ||
|
||
[Collection("Sequential")] | ||
public class FunctionHandlerTests | ||
{ | ||
[Fact] | ||
public async Task When_Metrics_Add_Metadata_Same_Key_Should_Ignore_Metadata() | ||
{ | ||
// Arrange | ||
Metrics.ResetForTest(); | ||
var handler = new FunctionHandler(); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync( () => handler.HandleSameKey("whatever")); | ||
|
||
// Assert | ||
Assert.Null(exception); | ||
} | ||
|
||
[Fact] | ||
public async Task When_Metrics_Add_Metadata_Second_Invocation_Should_Not_Throw_Exception() | ||
{ | ||
// Arrange | ||
Metrics.ResetForTest(); | ||
var handler = new FunctionHandler(); | ||
|
||
// Act | ||
var exception = await Record.ExceptionAsync( () => handler.HandleTestSecondCall("whatever")); | ||
Assert.Null(exception); | ||
|
||
exception = await Record.ExceptionAsync( () => handler.HandleTestSecondCall("whatever")); | ||
Assert.Null(exception); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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