Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
@aws-lambda-powertools/metrics
Advanced tools
The metrics package for the AWS Lambda Powertools for TypeScript library
@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.
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' };
};
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.
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.
A suite of utilities for AWS Lambda functions to ease the adoption of best practices such as tracing, structured logging, custom metrics, and more.
You can use the library in both TypeScript and JavaScript code bases.
AWS Lambda Powertools for Python and AWS Lambda Powertools for Java are also available.
📜 Documentation | NPM | Roadmap | Examples | Serverless TypeScript Demo
Find the complete project's documentation here.
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.
Install all three core utilities at once with this single command:
npm install @aws-lambda-powertools/logger @aws-lambda-powertools/tracer @aws-lambda-powertools/metrics
Or refer to the installation guide of each utility:
👉 Installation guide for the Tracer utility
👉 Installation guide for the Logger utility
👉 Installation guide for the Metrics utility
The Serverless TypeScript Demo shows how to use Lambda Powertools for TypeScript.
You can find instructions on how to deploy and load test this application in the repository.
If you are interested in contributing to this project, please refer to our Contributing Guidelines.
The roadmap of Powertools is driven by customers’ demand.
Help us prioritize upcoming functionalities or utilities by upvoting existing RFCs and feature requests, or creating new ones, in this GitHub repository.
#lambda-powertools
- Invite, if you don't have an accountCredits for the Lambda Powertools idea go to DAZN and their DAZN Lambda Powertools.
This library is licensed under the MIT-0 License. See the LICENSE file.
FAQs
The metrics package for the Powertools for AWS Lambda (TypeScript) library
The npm package @aws-lambda-powertools/metrics receives a total of 120,463 weekly downloads. As such, @aws-lambda-powertools/metrics popularity was classified as popular.
We found that @aws-lambda-powertools/metrics demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.