What is @aws-lambda-powertools/metrics?
@aws-lambda-powertools/metrics is an npm package designed to simplify the creation and management of custom metrics in AWS Lambda functions. It provides utilities to easily capture and publish metrics to Amazon CloudWatch, enabling better observability and monitoring of serverless applications.
What are @aws-lambda-powertools/metrics's main functionalities?
Creating Custom Metrics
This feature allows you to create custom metrics and publish them to CloudWatch. The code sample demonstrates how to add metrics for successful invocations and processing time, and then publish them.
const { Metrics, MetricUnits } = require('@aws-lambda-powertools/metrics');
const metrics = new Metrics();
exports.handler = async (event) => {
metrics.addMetric('SuccessfulInvocations', MetricUnits.Count, 1);
metrics.addMetric('ProcessingTime', MetricUnits.Milliseconds, 200);
await metrics.publishStoredMetrics();
return { statusCode: 200, body: 'Metrics published' };
};
Namespace and Dimensions
This feature allows you to set a namespace and dimensions for your metrics, which helps in organizing and filtering them in CloudWatch. The code sample shows how to set a namespace and dimensions for a payment service.
const { Metrics, MetricUnits } = require('@aws-lambda-powertools/metrics');
const metrics = new Metrics({ namespace: 'MyApp', dimensions: { Service: 'Payment' } });
exports.handler = async (event) => {
metrics.addMetric('PaymentSuccess', MetricUnits.Count, 1);
await metrics.publishStoredMetrics();
return { statusCode: 200, body: 'Payment metrics published' };
};
Automatic Cold Start Metric
This feature automatically captures a cold start metric, which is useful for understanding the performance impact of cold starts in your Lambda functions. The code sample demonstrates how to enable this feature.
const { Metrics, MetricUnits } = require('@aws-lambda-powertools/metrics');
const metrics = new Metrics({ captureColdStartMetric: true });
exports.handler = async (event) => {
metrics.addMetric('HandlerInvoked', MetricUnits.Count, 1);
await metrics.publishStoredMetrics();
return { statusCode: 200, body: 'Cold start metric captured' };
};
Other packages similar to @aws-lambda-powertools/metrics
aws-sdk
The aws-sdk package is the official AWS SDK for JavaScript, which includes support for CloudWatch metrics. However, it requires more boilerplate code to achieve the same functionality as @aws-lambda-powertools/metrics. It is more general-purpose and not specifically tailored for Lambda functions.
cloudwatch-metrics
The cloudwatch-metrics package is a lightweight library for publishing metrics to CloudWatch. It provides a simpler interface compared to the aws-sdk but lacks some of the advanced features and integrations offered by @aws-lambda-powertools/metrics, such as automatic cold start metrics.
AWS Lambda Powertools for TypeScript
⚠️ WARNING: Do not use this library in production ⚠️ |
---|
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. |
A suite of TypeScript utilities for AWS Lambda functions to ease adopting best practices such as tracing, structured logging, custom metrics, and more. (AWS Lambda Powertools for Python and AWS Lambda Powertools for Java are also available).
📜 Documentation | NPM | Roadmap | Examples
An AWS Developer Acceleration (DevAx) initiative by Specialist Solution Architects | aws-devax-open-source@amazon.com
Features
- Tracer - Utilities to trace Lambda function handlers, and both synchronous and asynchronous functions
- Logger - Structured logging made easier, and a middleware to enrich log items with key details of the Lambda context
- Metrics - Custom Metrics created asynchronously via CloudWatch Embedded Metric Format (EMF)
Getting started
Find the complete project's documentation here.
Installation
The AWS Lambda Powertools for TypeScript utilities follow a modular approach, similar to the official AWS SDK v3 for JavaScript.
Each TypeScript utility is installed as standalone NPM package.
👉 Installation guide for the Tracer utility
👉 Installation guide for the Logger utility
👉 Installation guide for the Metrics utility
Examples
Credits
Connect
License
This library is licensed under the MIT-0 License. See the LICENSE file.