Skip to content

Commit c924443

Browse files
committed
Update Metrics constructor to not return and set all values.
Add tests
1 parent d8e60bf commit c924443

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

libraries/src/AWS.Lambda.Powertools.Metrics/Metrics.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ public class Metrics : IMetrics
6565
internal Metrics(IPowertoolsConfigurations powertoolsConfigurations, string nameSpace = null, string service = null,
6666
bool raiseOnEmptyMetrics = false, bool captureColdStartEnabled = false)
6767
{
68-
if (_instance != null) return;
68+
_instance ??= this;
6969

70-
_instance = this;
7170
_powertoolsConfigurations = powertoolsConfigurations;
7271
_raiseOnEmptyMetrics = raiseOnEmptyMetrics;
7372
_captureColdStartEnabled = captureColdStartEnabled;
7473
_context = InitializeContext(nameSpace, service, null);
7574

7675
_powertoolsConfigurations.SetExecutionEnvironment(this);
76+
7777
}
7878

7979
/// <summary>

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/Handlers/ExceptionFunctionHandler.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,28 @@ namespace AWS.Lambda.Powertools.Metrics.Tests.Handlers;
77
public class ExceptionFunctionHandler
88
{
99
[Metrics(Namespace = "ns", Service = "svc")]
10-
public async Task<string> Handle(string input)
10+
public Task<string> Handle(string input)
1111
{
1212
ThisThrows();
13+
return Task.FromResult(input.ToUpper(CultureInfo.InvariantCulture));
14+
}
15+
16+
[Metrics(Namespace = "ns", Service = "svc")]
17+
public Task<string> HandleDecoratorOutsideHandler(string input)
18+
{
19+
MethodDecorated();
20+
21+
Metrics.AddMetric($"Metric Name", 1, MetricUnit.Count);
1322

14-
await Task.Delay(1);
23+
return Task.FromResult(input.ToUpper(CultureInfo.InvariantCulture));
24+
}
1525

16-
return input.ToUpper(CultureInfo.InvariantCulture);
26+
[Metrics(Namespace = "ns", Service = "svc")]
27+
private void MethodDecorated()
28+
{
29+
// NOOP
30+
Metrics.AddMetric($"Metric Name", 1, MetricUnit.Count);
31+
Metrics.AddMetric($"Metric Name Decorated", 1, MetricUnit.Count);
1732
}
1833

1934
private void ThisThrows()

libraries/tests/AWS.Lambda.Powertools.Metrics.Tests/Handlers/ExceptionFunctionHandlerTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,21 @@ public async Task Stack_Trace_Included_When_Decorator_Present()
2222
Assert.StartsWith("at AWS.Lambda.Powertools.Metrics.Tests.Handlers.ExceptionFunctionHandler.ThisThrows()", tracedException.StackTrace?.TrimStart());
2323

2424
}
25+
26+
[Fact]
27+
public async Task Decorator_In_Non_Handler_Method_Does_Not_Throw_Exception()
28+
{
29+
// Arrange
30+
Metrics.ResetForTest();
31+
var handler = new ExceptionFunctionHandler();
32+
33+
// Act
34+
Task Handle() => handler.HandleDecoratorOutsideHandler("whatever");
35+
36+
// Assert
37+
var tracedException = await Record.ExceptionAsync(Handle);
38+
Assert.Null(tracedException);
39+
//Assert.StartsWith("at AWS.Lambda.Powertools.Metrics.Tests.Handlers.ExceptionFunctionHandler.ThisThrows()", tracedException.StackTrace?.TrimStart());
40+
41+
}
2542
}

0 commit comments

Comments
 (0)