🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

@aws-lambda-powertools/metrics

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
p

@aws-lambda-powertools/metrics

The metrics package for the AWS Lambda Powertools for TypeScript library

0.9.0
100

Supply Chain Security

100

Vulnerability

85

Quality

95

Maintenance

100

License

Version published
Weekly downloads
139K
-23%
Maintainers
4
Weekly downloads
 
Created
Issues
33

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

FAQs

Package last updated on 16 May 2022

Did you know?

Socket

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.

Install

Related posts