Skip to content

Commit b9ba2ce

Browse files
committed
feat(metrics): enhance cold start handling with default dimensions and add corresponding tests
1 parent 3c80a2d commit b9ba2ce

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Collections.Generic;
1718
using System.Linq;
1819
using System.Reflection;
1920
using Amazon.Lambda.Core;
@@ -96,7 +97,8 @@ public void Before(
9697

9798
if (context is not null)
9899
{
99-
defaultDimensions?.Add("FunctionName", context.FunctionName);
100+
defaultDimensions ??= new Dictionary<string, string>();
101+
defaultDimensions.Add("FunctionName", context.FunctionName);
100102
_metricsInstance.SetDefaultDimensions(defaultDimensions);
101103
}
102104

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,89 @@ public void Handler_With_Builder_Should_Raise_Empty_Metrics()
282282
var exception = Assert.Throws<SchemaValidationException>(() => handler.HandlerEmpty());
283283
Assert.Equal("No metrics have been provided.", exception.Message);
284284
}
285+
286+
[Fact]
287+
public void When_ColdStart_Should_Use_DefaultDimensions_From_Options()
288+
{
289+
// Arrange
290+
var metricsMock = Substitute.For<IMetrics>();
291+
var expectedDimensions = new Dictionary<string, string>
292+
{
293+
{ "Environment", "Test" },
294+
{ "Region", "us-east-1" }
295+
};
296+
297+
metricsMock.Options.Returns(new MetricsOptions
298+
{
299+
Namespace = "dotnet-powertools-test",
300+
Service = "testService",
301+
CaptureColdStart = true,
302+
DefaultDimensions = expectedDimensions
303+
});
304+
305+
Metrics.UseMetricsForTests(metricsMock);
306+
307+
var context = new TestLambdaContext
308+
{
309+
FunctionName = "TestFunction"
310+
};
311+
312+
// Act
313+
_handler.HandleWithLambdaContext(context);
314+
315+
// Assert
316+
metricsMock.Received(1).PushSingleMetric(
317+
"ColdStart",
318+
1.0,
319+
MetricUnit.Count,
320+
"dotnet-powertools-test",
321+
"testService",
322+
Arg.Is<Dictionary<string, string>>(d =>
323+
d.ContainsKey("Environment") && d["Environment"] == "Test" &&
324+
d.ContainsKey("Region") && d["Region"] == "us-east-1" &&
325+
d.ContainsKey("FunctionName") && d["FunctionName"] == "TestFunction"
326+
)
327+
);
328+
}
329+
330+
[Fact]
331+
public void When_ColdStart_And_DefaultDimensions_Is_Null_Should_Only_Add_Service_And_FunctionName()
332+
{
333+
// Arrange
334+
var metricsMock = Substitute.For<IMetrics>();
335+
336+
metricsMock.Options.Returns(new MetricsOptions
337+
{
338+
Namespace = "dotnet-powertools-test",
339+
Service = "testService",
340+
CaptureColdStart = true,
341+
DefaultDimensions = null
342+
});
343+
344+
Metrics.UseMetricsForTests(metricsMock);
345+
346+
var context = new TestLambdaContext
347+
{
348+
FunctionName = "TestFunction"
349+
};
350+
351+
// Act
352+
_handler.HandleWithLambdaContext(context);
353+
354+
// Assert
355+
metricsMock.Received(1).PushSingleMetric(
356+
"ColdStart",
357+
1.0,
358+
MetricUnit.Count,
359+
"dotnet-powertools-test",
360+
"testService",
361+
Arg.Is<Dictionary<string, string>>(d =>
362+
d.Count == 1 &&
363+
d.ContainsKey("FunctionName") &&
364+
d["FunctionName"] == "TestFunction"
365+
)
366+
);
367+
}
285368

286369
public void Dispose()
287370
{

0 commit comments

Comments
 (0)