Skip to main content
net6.0net8.0

Powertools for AWS Lambda (.NET) - Metrics package.

Published
Mar 7, 2025
1y ago
Total Downloads
972.3K
972,266

Install

dotnet add package AWS.Lambda.Powertools.Metrics --version 2.0.0
ProductVersions
.NET
net6.0net8.0

README

AWS.Lambda.Powertools.Metrics

Metrics creates custom metrics asynchronously by logging metrics to standard output following Amazon CloudWatch Embedded Metric Format (EMF).

These metrics can be visualized through Amazon CloudWatch Console.

Key features

  • Aggregate up to 100 metrics using a single CloudWatch EMF object (large JSON blob)
  • Validate against common metric definitions mistakes (metric unit, values, max dimensions, max metrics, etc)
  • Metrics are created asynchronously by CloudWatch service, no custom stacks needed
  • Context manager to create a one off metric with a different dimension

Read the docs

For a full list of features go to docs.powertools.aws.dev/lambda/dotnet/core/metrics/

GitHub: https://github.com/aws-powertools/powertools-lambda-dotnet/

Sample Function

View the full example here: https://github.com/aws-powertools/powertools-lambda-dotnet/tree/develop/examples/Metrics

public class Function
{
    /// <summary>
    /// Lambda Handler
    /// </summary>
    /// <param name="apigwProxyEvent">API Gateway Proxy event</param>
    /// <param name="context">AWS Lambda context</param>
    /// <returns>API Gateway Proxy response</returns>
    [Logging(LogEvent = true)]
    [Metrics(CaptureColdStart = true)]
    public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest apigwProxyEvent,
        ILambdaContext context)
    {
        var requestContextRequestId = apigwProxyEvent.RequestContext.RequestId;

        Logger.LogInformation("Getting ip address from external service");

        // Add Metric to capture the amount of time 
        Metrics.PushSingleMetric(
            metricName: "CallingIP",
            value: 1,
            unit: MetricUnit.Count,
            service: "lambda-powertools-metrics-example",
            defaultDimensions: new Dictionary<string, string>
            {
                { "Metric Type", "Single" }
            });
        
        var watch = Stopwatch.StartNew();
        var location = await GetCallingIp();
        watch.Stop();
        
        Metrics.AddMetric("ElapsedExecutionTime", watch.ElapsedMilliseconds, MetricUnit.Milliseconds);
        Metrics.AddMetric("SuccessfulLocations", 1, MetricUnit.Count);
        
        var lookupRecord = new LookupRecord(lookupId: requestContextRequestId,
            greeting: "Hello Powertools for AWS Lambda (.NET)", ipAddress: location);

        try
        {
            Metrics.PushSingleMetric(
                metricName: "RecordsSaved",
                value: 1,
                unit: MetricUnit.Count,
                service: "lambda-powertools-metrics-example",
                defaultDimensions: new Dictionary<string, string>
                {
                    { "Metric Type", "Single" }
                });
            
            await SaveRecordInDynamo(lookupRecord);
            
            Metrics.AddMetric("SuccessfulWrites", 1, MetricUnit.Count);
            
            return new APIGatewayProxyResponse
            {
                Body = JsonSerializer.Serialize(lookupRecord),
                StatusCode = 200,
                Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
            };
        }
        catch (Exception e)
        {
            Logger.LogError(e.Message);
            
            return new APIGatewayProxyResponse
            {
                Body = e.Message,
                StatusCode = 500,
                Headers = new Dictionary<string, string> { { "Content-Type", "application/json" } }
            };
        }
    }
}

Sample output

{
    "_aws": {
        "Timestamp": 1648181318790,
        "CloudWatchMetrics": [
            {
                "Namespace": "AWSLambdaPowertools",
                "Metrics": [
                    {
                        "Name": "CallingIP",
                        "Unit": "Count"
                    }
                ],
                "Dimensions": [
                    [
                        "Metric Type"
                    ],
                    [
                        "Service"
                    ]
                ]
            }
        ]
    },
    "Metric Type": "Single",
    "Service": "lambda-powertools-metrics-example",
    "CallingIP": 1
}

Changes from Previous Version

No structural changes between v2.0.0-alpha1 and v2.0.0.

Adoption Assessment

A verified publisher signal is available. Assess fit from the package's supported frameworks, license and dependency graph rather than popularity alone.

Compatibility metadata lists 2 target frameworks; confirm the exact target used by your application.

Related Guides & Workflows

Learn how to evaluate, integrate, or audit this package in your .NET project.