AWS Lambda Powertools for TypeScript is currently released as a beta developer preview and is intended strictly for feedback purposes only.
This version is not stable, and significant breaking changes might incur as part of the upcoming production-ready release.
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 ColdStart metric by Payment service.
The library requires two settings. You can set them as environment variables, or pass them in the constructor.
These settings will be used across all metrics emitted:
Setting
Description
Environment variable
Constructor parameter
Metric namespace
Logical container where all metrics will be placed e.g. serverlessAirline
POWERTOOLS_METRICS_NAMESPACE
namespace
Service
Optionally, sets service metric dimension across all metrics e.g. payment
POWERTOOLS_SERVICE_NAME
service
For a complete list of supported environment variables, refer to this section.
Use your application or main service as the metric namespace to easily group all metrics
Example using AWS Serverless Application Model (SAM)
1234567
import{Metrics}from'@aws-lambda-powertools/metrics';// Sets metric namespace and service via env varconstmetrics=newMetrics();// OR Sets metric namespace, and service as a metrics parametersconstmetrics=newMetrics({namespace:"serverlessAirline",service:"orders"});
MetricUnit enum facilitate finding a supported metric unit by CloudWatch. Alternatively, you can pass the value as a string if you already know them e.g. "Count".
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.
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 behaviour.
Using the Middy middleware or decorator will automatically validate, serialize, and flush all your metrics. During metrics validation, if no metrics are provided then a warning will be logged, but no exception will be raised.
If you do not the middleware or decorator, you have to flush your metrics manually.
Metric validation
If metrics are provided, and any of the following criteria are not met, a RangeError exception will be raised:
Decorators can only be attached to a class declaration, method, accessor, property, or parameter. Therefore, if you prefer to write your handler as a standard function rather than a Class method, check the middleware or manual method sections instead.
See the official TypeScript documentation for more details.
The logMetrics decorator of the metrics utility can be used when your Lambda handler function is implemented as method of a Class.
Throwing a RangeError when no metrics are emitted¶
If you want to ensure that at least one metric is emitted before you flush them, you can use the raiseOnEmptyMetrics parameter and pass it to the middleware or decorator:
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.
Warning
This will not be available during metrics visualization - Use dimensions for this purpose
import{Metrics,MetricUnits,logMetrics}from'@aws-lambda-powertools/metrics';import{Context}from'aws-lambda';importmiddyfrom'@middy/core';constmetrics=newMetrics({namespace:"serverlessAirline",service:"orders"});constlambdaHandler=async(event: any,context: Context)=>{metrics.addDimension('metricUnit','milliseconds');// This metric will have the "metricUnit" dimension, and no "metricType" dimension:metrics.addMetric('latency',MetricUnits.Milliseconds,56);constsingleMetric=metrics.singleMetric();// This metric will have the "metricType" dimension, and no "metricUnit" dimension:singleMetric.addDimension('metricType','business');singleMetric.addMetric('orderSubmitted',MetricUnits.Count,1);}exportconsthandler=middy(lambdaHandler).use(logMetrics(metrics,{captureColdStartMetric: true}}));
1 2 3 4 5 6 7 8 910111213141516171819
import{Metrics,MetricUnits}from'@aws-lambda-powertools/metrics';import{Context,Callback}from'aws-lambda';constmetrics=newMetrics({namespace:"serverlessAirline",service:"orders"});exportclassMyFunction{@metrics.logMetrics()publichandler<TEvent,TResult>(_event: TEvent,_context: Context,_callback: Callback<TResult>):void|Promise<TResult>{metrics.addDimension('metricUnit','milliseconds');// This metric will have the "metricUnit" dimension, and no "metricType" dimension:metrics.addMetric('latency',MetricUnits.Milliseconds,56);constsingleMetric=metrics.singleMetric();// This metric will have the "metricType" dimension, and no "metricUnit" dimension:singleMetric.addDimension('metricType','business');singleMetric.addMetric('orderSubmitted',MetricUnits.Count,1);}}