![]() |
VOOZH | about |
dotnet add package AWS.Lambda.Powertools.Metrics.AspNetCore --version 3.2.2
NuGet\Install-Package AWS.Lambda.Powertools.Metrics.AspNetCore -Version 3.2.2
<PackageReference Include="AWS.Lambda.Powertools.Metrics.AspNetCore" Version="3.2.2" />
<PackageVersion Include="AWS.Lambda.Powertools.Metrics.AspNetCore" Version="3.2.2" />Directory.Packages.props
<PackageReference Include="AWS.Lambda.Powertools.Metrics.AspNetCore" />Project file
paket add AWS.Lambda.Powertools.Metrics.AspNetCore --version 3.2.2
#r "nuget: AWS.Lambda.Powertools.Metrics.AspNetCore, 3.2.2"
#:package AWS.Lambda.Powertools.Metrics.AspNetCore@3.2.2
#addin nuget:?package=AWS.Lambda.Powertools.Metrics.AspNetCore&version=3.2.2Install as a Cake Addin
#tool nuget:?package=AWS.Lambda.Powertools.Metrics.AspNetCore&version=3.2.2Install as a Cake Tool
This library provides utilities for capturing and publishing custom metrics from your AWS Lambda functions using ASP.NET Core.
This library provides utilities for capturing and publishing custom metrics from your AWS Lambda functions using ASP.NET Core.
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();
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:
RouteHandlerBuilder class.MetricsFilter to the route handler builder using the AddEndpointFilter method.MetricsFilter captures and records metrics for HTTP endpoints, including cold start metrics if the CaptureColdStart option is enabled.RouteHandlerBuilder instance with the metrics filter added.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.
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:
IApplicationBuilder interface.MetricsMiddleware to the application builder using the UseMiddleware method.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. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.