@aws-lambda-powertools/metrics

中文文档
2.36.0发布于 2026/10/06MIT-0TypeScriptESM + CJS构建来源已验证近 30 天下载 230.8万

The metrics package for the Powertools for AWS Lambda (TypeScript) library

原始使用文档
文档目录13 节
  1. Usage
  2. Flushing metrics
  3. Capturing cold start as a metric
  4. Class method decorator
  5. Middy.js middleware
  6. Contribute
  7. Roadmap
  8. Connect
  9. How to support Powertools for AWS Lambda (TypeScript)?
  10. Becoming a reference customer
  11. Sharing your work
  12. Using Lambda Layer
  13. License

Powertools for AWS Lambda (TypeScript)

Powertools for AWS Lambda (TypeScript) is a developer toolkit to implement Serverless best practices and increase developer velocity.

You can use the library in both TypeScript and JavaScript code bases.

Usage

The library provides a utility function to emit metrics to CloudWatch using Embedded Metric Format (EMF).

To get started, install the library by running:

npm i @aws-lambda-powertools/metrics

After initializing the Metrics class, you can add metrics using the https://docs.aws.amazon.com/powertools/typescript/latest/core/metrics/#creating-metrics method. The metrics are stored in a buffer and are flushed when calling https://docs.aws.amazon.com/powertools/typescript/latest/core/metrics/#flushing-metrics.

Each metric can also have dimensions and metadata added to it.

import { Metrics, MetricUnit } from '@aws-lambda-powertools/metrics';

const metrics = new Metrics({
  namespace: 'serverlessAirline',
  serviceName: 'orders',
  defaultDimensions: { environment: process.env.ENVIRONMENT },
});

export const handler = async (event: { requestId: string }) => {
  metrics.addMetadata('request_id', event.requestId);
  metrics.addMetric('successfulBooking', MetricUnit.Count, 1);
  metrics.publishStoredMetrics();
};

Flushing metrics

As you finish adding all your metrics, you need to serialize and "flush them" by calling publishStoredMetrics(), which will emit the metrics to stdout in the Embedded Metric Format (EMF). The metrics are then picked up by the Lambda runtime and sent to CloudWatch.

The publishStoredMetrics() method is synchronous and will block the event loop until the metrics are flushed. If you want Metrics to flush automatically at the end of your Lambda function, you can use the @logMetrics() decorator or the logMetrics() middleware.

Capturing cold start as a metric

With Metrics, you can capture cold start as a metric by calling the captureColdStartMetric() method. This method will add a metric with the name ColdStart and the value 1 to the metrics buffer.

This metric is flushed automatically as soon as the method is called, to ensure that the cold start is captured regardless of whether the metrics are flushed manually or automatically.

import { Metrics, MetricUnit } from '@aws-lambda-powertools/metrics';

const metrics = new Metrics({
  namespace: 'serverlessAirline',
  serviceName: 'orders',
});

export const handler = async (event: { requestId: string }) => {
  metrics.captureColdStartMetric();
};

Note that we don't emit a ColdStart metric with value 0 when the function is warm, as this would result in a high volume of metrics being emitted to CloudWatch, so you'll need to rely on the absence of the ColdStart metric to determine if the function is warm.

Class method decorator

If you are using TypeScript and are comfortable with writing classes, you can use the @logMetrics() decorator to automatically flush metrics at the end of your Lambda function as well as configure additional options such as throwing an error if no metrics are added, capturing cold start as a metric, and more.

import type { Context } from 'aws-lambda';
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
import { Metrics, MetricUnit } from '@aws-lambda-powertools/metrics';

const metrics = new Metrics({
  namespace: 'serverlessAirline',
  serviceName: 'orders',
});

class Lambda implements LambdaInterface {
  ⁣@metrics.logMetrics({ captureColdStartMetric: true, throwOnEmptyMetrics: true })
  public async handler(event: { requestId: string }, _: Context) {
    metrics.addMetadata('request_id', event.requestId);
    metrics.addMetric('successfulBooking', MetricUnit.Count, 1);
  }
}

const handlerClass = new Lambda();
export const handler = handlerClass.handler.bind(handlerClass);

Decorators are a Stage 3 proposal for JavaScript and are not yet part of the ECMAScript standard. The current implmementation in this library is based on the legacy TypeScript decorator syntax enabled by the experimentalDecorators flag set to true in the tsconfig.json file.

Middy.js middleware

If instead you are using Middy.js and prefer to use middleware, you can use the @logMetrics() middleware to do the same as the class method decorator.

The @logMetrics() middleware can be used with Middy.js to automatically flush metrics at the end of your Lambda function as well as configure additional options such as throwing an error if no metrics are added, capturing cold start as a metric, and set default dimensions.

import { Metrics, MetricUnit } from '@aws-lambda-powertools/metrics';
import { logMetrics } from '@aws-lambda-powertools/metrics/middleware';
import middy from '@middy/core';

const metrics = new Metrics({
  namespace: 'serverlessAirline',
  serviceName: 'orders',
});

export const handler = middy(async (event) => {
  metrics.addMetadata('request_id', event.requestId);
  metrics.addMetric('successfulBooking', MetricUnit.Count, 1);
}).use(logMetrics(metrics, {
  captureColdStartMetric: true,
  throwOnEmptyMetrics: true,
}));

The logMetrics() middleware is compatible with @middy/[email protected] and above.

Contribute

If you are interested in contributing to this project, please refer to our Contributing Guidelines.

Roadmap

The roadmap of Powertools for AWS Lambda (TypeScript) 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.

Connect

How to support Powertools for AWS Lambda (TypeScript)?

Becoming a reference customer

Knowing which companies are using this library is important to help prioritize the project internally. If your company is using Powertools for AWS Lambda (TypeScript), you can request to have your name and logo added to the README file by raising a Support Powertools for AWS Lambda (TypeScript) (become a reference) issue.

The following companies, among others, use Powertools:

Sharing your work

Share what you did with Powertools for AWS Lambda (TypeScript) 💞💞. Blog post, workshops, presentation, sample apps and others. Check out what the community has already shared about Powertools for AWS Lambda (TypeScript).

Using Lambda Layer

This helps us understand who uses Powertools for AWS Lambda (TypeScript) in a non-intrusive way, and helps us gain future investments for other Powertools for AWS Lambda languages. When using Layers, you can add Powertools as a dev dependency to not impact the development process.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

版本与发布记录

@aws-lambda-powertools/metrics 共发布 131 个版本,首次发布于 2021/12/07;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.36.0latest2026/10/061 个月前
2.35.02026/08/182 个月前
2.34.02026/07/103 个月前
2.33.12026/05/255 个月前
2.33.02026/04/146 个月前
2.32.02026/03/197 个月前

其他发布通道:next → 2.0.0-alpha.2。

关于 @aws-lambda-powertools/metrics 的常见问题

@aws-lambda-powertools/metrics 是什么?

The metrics package for the Powertools for AWS Lambda (TypeScript) library

如何安装 @aws-lambda-powertools/metrics?

在项目目录运行 npm install @aws-lambda-powertools/metrics,或使用 pnpm add / yarn add / bun add。

@aws-lambda-powertools/metrics 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws-lambda-powertools/metrics 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@aws-lambda-powertools/metrics 可以商用吗?

@aws-lambda-powertools/metrics 使用 MIT-0 许可。声明的许可为 MIT-0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@aws-lambda-powertools/metrics 的下载量有多少?

根据 npm 官方下载统计,@aws-lambda-powertools/metrics 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,307,620 次,日均约 76,921 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws-lambda-powertools/metrics 还在维护吗?

最新版本 2.36.0 发布于 2026/10/06(1 个月前);至今共发布 131 个版本,其中过去 12 个月发布了 11 个;首次发布于 2021/12/07。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。