VOOZH about

URL: https://www.nuget.org/packages/AWS.Lambda.Powertools.Metrics.AspNetCore/

⇱ NuGet Gallery | AWS.Lambda.Powertools.Metrics.AspNetCore 3.2.2




👁 Image
AWS.Lambda.Powertools.Metrics.AspNetCore 3.2.2

Prefix Reserved
dotnet add package AWS.Lambda.Powertools.Metrics.AspNetCore --version 3.2.2
 
 
NuGet\Install-Package AWS.Lambda.Powertools.Metrics.AspNetCore -Version 3.2.2
 
 
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.AspNetCore" Version="3.2.2" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AWS.Lambda.Powertools.Metrics.AspNetCore" Version="3.2.2" />
 
Directory.Packages.props
<PackageReference Include="AWS.Lambda.Powertools.Metrics.AspNetCore" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AWS.Lambda.Powertools.Metrics.AspNetCore --version 3.2.2
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: AWS.Lambda.Powertools.Metrics.AspNetCore, 3.2.2"
 
 
#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.
#:package AWS.Lambda.Powertools.Metrics.AspNetCore@3.2.2
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AWS.Lambda.Powertools.Metrics.AspNetCore&version=3.2.2
 
Install as a Cake Addin
#tool nuget:?package=AWS.Lambda.Powertools.Metrics.AspNetCore&version=3.2.2
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

AWS Lambda Powertools Metrics for ASP.NET Core

This library provides utilities for capturing and publishing custom metrics from your AWS Lambda functions using ASP.NET Core.

Getting Started

This library provides utilities for capturing and publishing custom metrics from your AWS Lambda functions using ASP.NET Core.

Installation

You can install the package via the NuGet package manager just search for AWS.Lambda.Powertools.Metrics.AspNetCore.

You can also install via powershell using the following command.

dotnet add package AWS.Lambda.Powertools.Metrics.AspNetCore

using AWS.Lambda.Powertools.Metrics;
using AWS.Lambda.Powertools.Metrics.AspNetCore.Http;

var builder = WebApplication.CreateBuilder(args);

// Configure metrics
builder.Services.AddSingleton<IMetrics>(_ => new MetricsBuilder()
 .WithNamespace("MyApi") // Namespace for the metrics
 .WithService("WeatherService") // Service name for the metrics
 .WithCaptureColdStart(true) // Capture cold start metrics
 .WithDefaultDimensions(new Dictionary<string, string> // Default dimensions for the metrics
 {
 {"Environment", "Prod"},
 {"Another", "One"}
 })
 .Build()); // Build the metrics

// Add AWS Lambda support. When the application is run in Lambda, Kestrel is swapped out as the web server with Amazon.Lambda.AspNetCoreServer. This
// package will act as the web server translating requests and responses between the Lambda event source and ASP.NET Core.
builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi);

var app = builder.Build();

app.MapGet("/powertools", (IMetrics metrics) => 
 {
 // add custom metrics
 metrics.AddMetric("MyCustomMetric", 1, MetricUnit.Count);
 // flush metrics - this is required to ensure metrics are sent to CloudWatch
 metrics.Flush();
 })
 .WithMetrics();

app.Run();

WithMetrics() filter

The WithMetrics method is an extension method for the RouteHandlerBuilder class.

It adds a metrics filter to the specified route handler builder, which captures cold start metrics (if enabled) and flushes metrics on function exit.

Here is the highlighted WithMetrics method:

/// <summary>
/// Adds a metrics filter to the specified route handler builder.
/// This will capture cold start (if CaptureColdStart is enabled) metrics and flush metrics on function exit.
/// </summary>
/// <param name="builder">The route handler builder to add the metrics filter to.</param>
/// <returns>The route handler builder with the metrics filter added.</returns>
public static RouteHandlerBuilder WithMetrics(this RouteHandlerBuilder builder)
{
 builder.AddEndpointFilter<MetricsFilter>();
 return builder;
}

Explanation:

  • The method is defined as an extension method for the RouteHandlerBuilder class.
  • It adds a MetricsFilter to the route handler builder using the AddEndpointFilter method.
  • The MetricsFilter captures and records metrics for HTTP endpoints, including cold start metrics if the CaptureColdStart option is enabled.
  • The method returns the modified RouteHandlerBuilder instance with the metrics filter added.

UseMetrics() Middleware

The UseMetrics middleware is an extension method for the IApplicationBuilder interface.

It adds a metrics middleware to the specified application builder, which captures cold start metrics (if enabled) and flushes metrics on function exit.

Example
 
using AWS.Lambda.Powertools.Metrics.AspNetCore.Http;

var builder = WebApplication.CreateBuilder(args);

// Configure metrics
builder.Services.AddSingleton<IMetrics>(_ => new MetricsBuilder()
 .WithNamespace("MyApi") // Namespace for the metrics
 .WithService("WeatherService") // Service name for the metrics
 .WithCaptureColdStart(true) // Capture cold start metrics
 .WithDefaultDimensions(new Dictionary<string, string> // Default dimensions for the metrics
 {
 {"Environment", "Prod"},
 {"Another", "One"}
 })
 .Build()); // Build the metrics

builder.Services.AddAWSLambdaHosting(LambdaEventSource.RestApi);

var app = builder.Build();

app.UseMetrics(); // Add the metrics middleware

app.MapGet("/powertools", (IMetrics metrics) => 
 {
 // add custom metrics
 metrics.AddMetric("MyCustomMetric", 1, MetricUnit.Count);
 // flush metrics - this is required to ensure metrics are sent to CloudWatch
 metrics.Flush();
 });
 
app.Run();

Here is the highlighted UseMetrics method:

/// <summary>
/// Adds a metrics middleware to the specified application builder.
/// This will capture cold start (if CaptureColdStart is enabled) metrics and flush metrics on function exit.
/// </summary>
/// <param name="app">The application builder to add the metrics middleware to.</param>
/// <returns>The application builder with the metrics middleware added.</returns>
public static IApplicationBuilder UseMetrics(this IApplicationBuilder app)
{
 app.UseMiddleware<MetricsMiddleware>();
 return app;
}

Explanation:

  • The method is defined as an extension method for the IApplicationBuilder interface.
  • It adds a MetricsMiddleware to the application builder using the UseMiddleware method.
  • The MetricsMiddleware captures and records metrics for HTTP requests, including cold start metrics if the CaptureColdStart option is enabled.
Product Versions Compatible and additional computed target framework versions.
.NET net8.0 net8.0 is compatible.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 is compatible.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.2.2 123 5/25/2026
3.2.1 629 3/19/2026
3.2.0 565 3/12/2026
3.1.0 585 1/6/2026
3.0.2 5,424 11/5/2025
3.0.1 412 10/21/2025
3.0.1-alpha 211 10/15/2025
3.0.0 220 10/8/2025
1.0.0 127 1/6/2026
0.1.0 12,024 2/27/2025