AWS.Lambda.Powertools.Metrics 1.5.3

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package AWS.Lambda.Powertools.Metrics --version 1.5.3                
NuGet\Install-Package AWS.Lambda.Powertools.Metrics -Version 1.5.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AWS.Lambda.Powertools.Metrics" Version="1.5.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AWS.Lambda.Powertools.Metrics --version 1.5.3                
#r "nuget: AWS.Lambda.Powertools.Metrics, 1.5.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install AWS.Lambda.Powertools.Metrics as a Cake Addin
#addin nuget:?package=AWS.Lambda.Powertools.Metrics&version=1.5.3

// Install AWS.Lambda.Powertools.Metrics as a Cake Tool
#tool nuget:?package=AWS.Lambda.Powertools.Metrics&version=1.5.3                

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
}
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on AWS.Lambda.Powertools.Metrics:

Package Downloads
AWS.Lambda.Powertools.Metrics.AspNetCore

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

GitHub repositories (4)

Showing the top 4 popular GitHub repositories that depend on AWS.Lambda.Powertools.Metrics:

Repository Stars
aws/aws-lambda-dotnet
Libraries, samples and tools to help .NET Core developers develop AWS Lambda functions.
aws-samples/serverless-test-samples
This repository is designed to provide guidance for implementing comprehensive test suites for serverless applications.
aws-powertools/powertools-lambda-dotnet
Powertools is a developer toolkit to implement Serverless best practices and increase developer velocity.
aws-samples/serverless-dotnet-demo
Version Downloads Last updated
2.0.0-alpha1 2,108 10/21/2024
1.8.1-alpha 119 10/9/2024
1.7.1 63,496 7/12/2024
1.7.0-alpha3 104 7/14/2024
1.7.0-alpha2 94 7/12/2024
1.7.0-alpha 2,501 2/21/2024
1.6.2 7,237 5/23/2024
1.6.1 31,409 3/10/2024
1.6.0 18,833 2/16/2024
1.5.3 34,849 11/16/2023
1.4.3 1,540 10/30/2023
1.4.2 8,596 9/19/2023
1.3.3 3,729 8/22/2023
1.3.2 4,581 6/29/2023
1.3.1 36,506 6/21/2023
1.2.0 11,872 5/5/2023
1.1.0 166 5/5/2023
1.0.1 14,941 4/6/2023
1.0.0 3,446 2/24/2023
0.0.3-preview 123 9/18/2023
0.0.2-preview 217 1/18/2023
0.0.1-preview.1 4,751 8/1/2022