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)
- Validating your metrics against common metric definitions mistakes (for example, metric unit, values, max dimensions, max metrics)
- Metrics are created asynchronously by the CloudWatch service. You do not need any custom stacks, and there is no impact to Lambda function latency
- Context manager to create a one off metric with a different dimension
- Ahead-of-Time compilation to native code support AOT from version 1.7.0
- Support for AspNetCore middleware and filters to capture metrics for HTTP requests
Migrating to v3
If you're upgrading to v3, please review the Migration Guide v3 for important breaking changes including .NET 8 requirement and AWS SDK v4 migration.
Installation¶
Powertools for AWS Lambda (.NET) are available as NuGet packages. You can install the packages from NuGet Gallery or from Visual Studio editor by searching AWS.Lambda.Powertools* to see various utilities available.
-
AWS.Lambda.Powertools.Metrics:
dotnet add package AWS.Lambda.Powertools.Metrics
Terminologies¶
If you're new to Amazon CloudWatch, there are two terminologies you must be aware of before using this utility:
- Namespace. It's the highest level container that will group multiple metrics from multiple services for a given application, for example
ServerlessEcommerce. - Dimensions. Metrics metadata in key-value format. They help you slice and dice metrics visualization, for example
ColdStartmetric by Paymentservice. - Metric. It's the name of the metric, for example: SuccessfulBooking or UpdatedBooking.
- Unit. It's a value representing the unit of measure for the corresponding metric, for example: Count or Seconds.
- Resolution. It's a value representing the storage resolution for the corresponding metric. Metrics can be either Standard or High resolution. Read more here.
Visit the AWS documentation for a complete explanation for Amazon CloudWatch concepts.
Getting started¶
Metrics is implemented as a Singleton to keep track of your aggregate metrics in memory and make them accessible anywhere in your code. To guarantee that metrics are flushed properly the MetricsAttribute must be added on the lambda handler.
Metrics has three global settings that will be used across all metrics emitted. Use your application or main service as the metric namespace to easily group all metrics:
| Setting | Description | Environment variable | Decorator parameter |
|---|---|---|---|
| Metric namespace | Logical container where all metrics will be placed e.g. MyCompanyEcommerce |
POWERTOOLS_METRICS_NAMESPACE |
Namespace |
| Service | Optionally, sets Service metric dimension across all metrics e.g. payment |
POWERTOOLS_SERVICE_NAME |
Service |
| Disable Powertools Metrics | Optionally, disables all Powertools metrics | POWERTOOLS_METRICS_DISABLED |
N/A |
Info
POWERTOOLS_METRICS_DISABLED will not disable default metrics created by AWS services.
Autocomplete Metric Units
All parameters in Metrics Attribute are optional. Following rules apply:
- Namespace:
Emptystring by default. You can either specify it in code or environment variable. If not present before flushing metrics, aSchemaValidationExceptionwill be thrown. - Service:
service_undefinedby default. You can either specify it in code or environment variable. - CaptureColdStart:
falseby default. - RaiseOnEmptyMetrics:
falseby default.
Metrics object¶
Attribute¶
The MetricsAttribute is a class-level attribute that can be used to set the namespace and service for all metrics emitted by the lambda handler.
1 2 3 4 5 6 7 | |
Methods¶
The Metrics class provides methods to add metrics, dimensions, and metadata to the metrics object.
1 2 3 4 5 6 7 8 9 | |
Initialization¶
The Metrics object is initialized as a Singleton and can be accessed anywhere in your code.
But can also be initialize with Configure or Builder patterns in your Lambda constructor, this the best option for testing.
Configure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
Builder:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
Creating metrics¶
You can create metrics using AddMetric, and you can create dimensions for all your aggregate metrics using AddDimension method.
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 | |
Autocomplete Metric Units
MetricUnit enum facilitates finding a supported metric unit by CloudWatch.
Metrics overflow
CloudWatch EMF supports a max of 100 metrics per batch. Metrics utility will flush all metrics when adding the 100th metric. Subsequent metrics, e.g. 101th, will be aggregated into a new EMF object, for your convenience.
Metric value must be a positive number
Metric values must be a positive number otherwise an ArgumentException will be thrown.
Do not create metrics or dimensions outside the handler
Metrics or dimensions added in the global scope will only be added during cold start. Disregard if that's the intended behavior.
Adding high-resolution metrics¶
You can create high-resolution metrics passing MetricResolution as parameter to AddMetric.
When is it useful?
High-resolution metrics are data with a granularity of one second and are very useful in several situations such as telemetry, time series, real-time incident management, and others.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
Autocomplete Metric Resolutions
Use the MetricResolution enum to easily find a supported metric resolution by CloudWatch.
Adding default dimensions¶
You can use SetDefaultDimensions method to persist dimensions across Lambda invocations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Adding default dimensions with cold start metric¶
You can use the Builder or Configure patterns in your Lambda class constructor to set default dimensions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Adding dimensions¶
You can add dimensions to your metrics using AddDimension method.
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
Flushing metrics¶
With MetricsAttribute all your metrics are validated, serialized and flushed to standard output when lambda handler completes execution or when you had the 100th metric to memory.
You can also flush metrics manually by calling Flush method.
During metrics validation, if no metrics are provided then a warning will be logged, but no exception will be raised.
1 2 3 4 5 6 7 8 9 10 11 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
Metric validation
If metrics are provided, and any of the following criteria are not met, SchemaValidationException will be raised:
- Maximum of 30 dimensions
- Namespace is set
- Metric units must be supported by CloudWatch
We do not emit 0 as a value for ColdStart metric for cost reasons. Let us know if you'd prefer a flag to override it
Raising SchemaValidationException on empty metrics¶
If you want to ensure that at least one metric is emitted, you can pass RaiseOnEmptyMetrics to the Metrics attribute:
1 2 3 4 5 6 7 8 | |
Capturing cold start metric¶
You can optionally capture cold start metrics by setting CaptureColdStart parameter to true.
1 2 3 4 5 6 7 8 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
If it's a cold start invocation, this feature will:
- Create a separate EMF blob solely containing a metric named
ColdStart - Add
FunctionNameandServicedimensions
This has the advantage of keeping cold start metric separate from your application metrics, where you might have unrelated dimensions.
Advanced¶
Adding metadata¶
You can add high-cardinality data as part of your Metrics log with AddMetadata method. This is useful when you want to search highly contextual information along with your metrics in your logs.
Info
This will not be available during metrics visualization - Use dimensions for this purpose
Info
Adding metadata with a key that is the same as an existing metric will be ignored
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
Single metric with a different dimension¶
CloudWatch EMF uses the same dimensions across all your metrics. Use PushSingleMetric if you have a metric that should have different dimensions.
Info
Generally, this would be an edge case since you pay for unique metric. Keep the following formula in mind:
unique metric = (metric_name + dimension_name + dimension_value)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
By default it will skip all previously defined dimensions including default dimensions. Use dimensions argument if you want to reuse default dimensions or specify custom dimensions from a dictionary.
Metrics.DefaultDimensions: Reuse default dimensions when using static MetricsOptions.DefaultDimensions: Reuse default dimensions when using Builder or Configure patterns
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
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:
1 2 3 4 5 6 7 8 9 10 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
AspNetCore¶
Installation¶
To use the Metrics middleware in an ASP.NET Core application, you need to install the AWS.Lambda.Powertools.Metrics.AspNetCore NuGet package.
1 | |
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¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
Here is the highlighted UseMetrics method:
1 2 3 4 5 6 7 8 9 10 11 | |
Explanation:
- The method is defined as an extension method for the
IApplicationBuilderinterface. - It adds a
MetricsMiddlewareto the application builder using theUseMiddlewaremethod. - The
MetricsMiddlewarecaptures and records metrics for HTTP requests, including cold start metrics if theCaptureColdStartoption is enabled.
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.
Example¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
Here is the highlighted WithMetrics method:
1 2 3 4 5 6 7 8 9 10 11 | |
Explanation:
- The method is defined as an extension method for the
RouteHandlerBuilderclass. - It adds a
MetricsFilterto the route handler builder using theAddEndpointFiltermethod. - The
MetricsFiltercaptures and records metrics for HTTP endpoints, including cold start metrics if theCaptureColdStartoption is enabled. - The method returns the modified
RouteHandlerBuilderinstance with the metrics filter added.
Testing your code¶
Unit testing¶
To test your code that uses the Metrics utility, you can use the TestLambdaContext class from the Amazon.Lambda.TestUtilities package.
You can also use the IMetrics interface to mock the Metrics utility in your tests.
Here is an example of how you can test a Lambda function that uses the Metrics utility:
Lambda Function¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
Unit Tests¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
Environment variables¶
Tip
Ignore this section, if:
- You are explicitly setting namespace/default dimension via
namespaceandserviceparameters - You're not instantiating
Metricsin the global namespace
For example, Metrics(namespace="ExampleApplication", service="booking")
Make sure to set POWERTOOLS_METRICS_NAMESPACE and POWERTOOLS_SERVICE_NAME before running your tests to prevent failing on SchemaValidation exception. You can set it before you run tests by adding the environment variable.
| Injecting Metric Namespace before running tests | |
|---|---|
1 | |