-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #505 from hjgraca/fix-exception-addmetadata
fix: Metrics throws exception when using AddMetadata
- Loading branch information
Showing
6 changed files
with
149 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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); | ||
} | ||
} |