Skip to content

Commit e46b90f

Browse files
committed
move metrics instanciation to aspect. reset test after run
1 parent ae64408 commit e46b90f

File tree

4 files changed

+41
-31
lines changed

4 files changed

+41
-31
lines changed

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ public class MetricsAspect
3939
/// </summary>
4040
private bool _clearLambdaContext;
4141

42-
private IMetrics _metricsInstance;
42+
/// <summary>
43+
/// Gets the metrics instance.
44+
/// </summary>
45+
/// <value>The metrics instance.</value>
46+
private static IMetrics _metricsInstance;
4347

4448
static MetricsAspect()
4549
{
4650
_isColdStart = true;
4751
}
48-
52+
4953
/// <summary>
5054
/// Runs before the execution of the method marked with the Metrics Attribute
5155
/// </summary>
@@ -66,13 +70,18 @@ public void Before(
6670
[Argument(Source.ReturnType)] Type returnType,
6771
[Argument(Source.Triggers)] Attribute[] triggers)
6872
{
69-
7073
// Before running Function
71-
74+
7275
var trigger = triggers.OfType<MetricsAttribute>().First();
73-
74-
_metricsInstance = trigger.MetricsInstance;
75-
76+
77+
_metricsInstance ??= new Metrics(
78+
PowertoolsConfigurations.Instance,
79+
trigger.Namespace,
80+
trigger.Service,
81+
trigger.RaiseOnEmptyMetrics,
82+
trigger.CaptureColdStart
83+
);
84+
7685
var eventArgs = new AspectEventArgs
7786
{
7887
Instance = instance,
@@ -112,25 +121,27 @@ public void Before(
112121
);
113122
}
114123
}
115-
124+
116125
/// <summary>
117126
/// OnExit runs after the execution of the method marked with the Metrics Attribute
118127
/// </summary>
119128
[Advice(Kind.After)]
120-
public void Exit() {
129+
public void Exit()
130+
{
121131
_metricsInstance.Flush();
122132
if (_clearLambdaContext)
123133
PowertoolsLambdaContext.Clear();
124134
}
125-
126-
135+
136+
127137
/// <summary>
128138
/// Reset the aspect for testing purposes.
129139
/// </summary>
130-
internal static void ResetForTest()
131-
{
132-
_isColdStart = true;
133-
Metrics.ResetForTest();
134-
PowertoolsLambdaContext.Clear();
135-
}
140+
internal static void ResetForTest()
141+
{
142+
_metricsInstance = null;
143+
_isColdStart = true;
144+
Metrics.ResetForTest();
145+
PowertoolsLambdaContext.Clear();
146+
}
136147
}

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,4 @@ public class MetricsAttribute : Attribute
130130
/// </summary>
131131
/// <value><c>true</c> if [raise on empty metrics]; otherwise, <c>false</c>.</value>
132132
public bool RaiseOnEmptyMetrics { get; set; }
133-
134-
/// <summary>
135-
/// Gets the metrics instance.
136-
/// </summary>
137-
/// <value>The metrics instance.</value>
138-
internal IMetrics MetricsInstance => new Metrics(
139-
PowertoolsConfigurations.Instance,
140-
Namespace,
141-
Service,
142-
RaiseOnEmptyMetrics,
143-
CaptureColdStart
144-
);
145133
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace AWS.Lambda.Powertools.Metrics.Tests.Handlers;
66

77
[Collection("Sequential")]
8-
public sealed class ExceptionFunctionHandlerTests
8+
public sealed class ExceptionFunctionHandlerTests : IDisposable
99
{
1010
[Fact]
1111
public async Task Stack_Trace_Included_When_Decorator_Present()
@@ -51,4 +51,9 @@ public async Task Decorator_In_Non_Handler_Method_Does_Not_Throw_Exception()
5151
var tracedException = await Record.ExceptionAsync(Handle);
5252
Assert.Null(tracedException);
5353
}
54+
55+
public void Dispose()
56+
{
57+
MetricsAspect.ResetForTest();
58+
}
5459
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
using System;
1617
using System.Threading.Tasks;
1718
using Xunit;
1819

1920
namespace AWS.Lambda.Powertools.Metrics.Tests.Handlers;
2021

2122
[Collection("Sequential")]
22-
public class FunctionHandlerTests
23+
public class FunctionHandlerTests : IDisposable
2324
{
2425
[Fact]
2526
public async Task When_Metrics_Add_Metadata_Same_Key_Should_Ignore_Metadata()
@@ -61,4 +62,9 @@ public async Task When_Metrics_Add_Metadata_FromMultipleThread_Should_Not_Throw_
6162
var exception = await Record.ExceptionAsync(() => handler.HandleMultipleThreads("whatever"));
6263
Assert.Null(exception);
6364
}
65+
66+
public void Dispose()
67+
{
68+
MetricsAspect.ResetForTest();
69+
}
6470
}

0 commit comments

Comments
 (0)