Skip to content

Commit

Permalink
feat(metrics): enhance documentation for Cold Start Function Name dim…
Browse files Browse the repository at this point in the history
…ension and update test classes
  • Loading branch information
hjgraca committed Feb 25, 2025
1 parent 97dc991 commit 75ac8ef
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
50 changes: 49 additions & 1 deletion docs/core/metrics-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ By default it will skip all previously defined dimensions including default dime
Metrics.PushSingleMetric("SingleMetric", 1, MetricUnit.Count, defaultDimensions: Metrics.DefaultDimensions );
...
```
=== "Default Dimensions Options / Builder patterns .cs"
=== "Default Dimensions Options / Builder patterns"

```csharp hl_lines="9-13 18"
using AWS.Lambda.Powertools.Metrics;
Expand All @@ -682,6 +682,54 @@ By default it will skip all previously defined dimensions including default dime
...
```

### Cold start Function Name dimension

In cases where you want to customize the `FunctionName` dimension in Cold Start metrics.

This is useful where you want to maintain the same name in case of auto generated handler names (cdk, top-level statement functions, etc.)

Example:

=== "In decorator"

```csharp hl_lines="5"
using AWS.Lambda.Powertools.Metrics;

public class Function {

[Metrics(FunctionName = "MyFunctionName", Namespace = "ExampleApplication", Service = "Booking")]
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
{
Metrics.AddMetric("SuccessfulBooking", 1, MetricUnit.Count);
...
}
```
=== "Configure / Builder patterns"

```csharp hl_lines="12"
using AWS.Lambda.Powertools.Metrics;

public class Function {
public Function()
{
Metrics.Configure(options =>
{
options.Namespace = "dotnet-powertools-test";
options.Service = "testService";
options.CaptureColdStart = true;
options.FunctionName = "MyFunctionName";
});
}

[Metrics]
public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigProxyEvent, ILambdaContext context)
{
Metrics.AddMetric("SuccessfulBooking", 1, MetricUnit.Count);
...
}
```

## AspNetCore

### Installation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("AWS.Lambda.Powertools.Metrics.Tests")]
[assembly: InternalsVisibleTo("AWS.Lambda.Powertools.Metrics.Tests")]
[assembly: InternalsVisibleTo("AWS.Lambda.Powertools.Metrics.AspNetCore.Tests")]
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@

namespace AWS.Lambda.Powertools.Metrics.AspNetCore.Tests;

public class MetricsFilterTests
[Collection("Sequential")]
public class MetricsFilterTests : IDisposable
{
public void Dispose()
{
MetricsHelper.ResetColdStart();
MetricsAspect.ResetForTest();
}

private readonly IMetrics _metrics;
private readonly EndpointFilterInvocationContext _context;
private readonly ILambdaContext _lambdaContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@

namespace AWS.Lambda.Powertools.Metrics.AspNetCore.Tests;

public class MetricsHelperTests
[Collection("Sequential")]
public class MetricsHelperTests : IDisposable
{
public void Dispose()
{
MetricsHelper.ResetColdStart();
MetricsAspect.ResetForTest();
}

[Fact]
public async Task CaptureColdStartMetrics_WhenEnabled_ShouldPushMetric()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@

namespace AWS.Lambda.Powertools.Metrics.AspNetCore.Tests;

[Collection("Sequential")]
public class MetricsMiddlewareExtensionsTests : IDisposable
{
public MetricsMiddlewareExtensionsTests()
{
MetricsHelper.ResetColdStart();
MetricsAspect.ResetForTest();
}

public void Dispose()
{
MetricsHelper.ResetColdStart();
MetricsAspect.ResetForTest();
}

[Fact]
Expand Down

0 comments on commit 75ac8ef

Please sign in to comment.