@defra/cdp-metrics

中文文档
0.6.1发布于 2026/10/05OGL-UK-3.0ES Module构建来源已验证近 30 天下载 3.5万

Library for custom metrics on the CDP platform

原始使用文档
文档目录12 节
  1. Features
  2. Installation
  3. Configuration
  4. Local development
  5. Production
  6. Usage
  7. Basic Metrics
  8. Timed Function
  9. With dimensions
  10. Hapi Plugin
  11. Testing
  12. Error Handling

cdp-metrics

A simple wrapper around AWS Embedded Metrics Format (EMF) for use in Node.js applications. It provides utilities for emitting CloudWatch compatible metrics using counters, timers, gauges, and size measurements with minimal boilerplate.

Features

  • Emit counters (Count unit)
  • Time async function
  • Emit gauges, byte sizes, and durations
  • Optional logger injection for error tracking
  • Easy to mock in unit tests

Installation

npm install @defra/cdp-metrics

Configuration

Local development

This library uses AWS EMF to emit metrics. To safely disable CloudWatch writes during development, set the following environment variable:

AWS_EMF_ENVIRONMENT=Local

It is advised you do this in your package.json to prevent accidentally turning off metrics in your deployed services.

{
  "scripts": {
    "dev": "AWS_EMF_ENVIRONMENT=Local run-p frontend:watch server:watch",
    "dev:debug": "AWS_EMF_ENVIRONMENT=Local run-p frontend:watch server:debug"
  }
}

Production

Do not set AWS_EMF_ENVIRONMENT=Local, or metrics will be silently discarded. AWS EMF will automatically emit to CloudWatch metrics ECS environments.

Usage

Basic Metrics

import { counter, timer, millis, gauge, byteSize } from '@defra/cdp-metrics'

const metrics = new Metrics(logger)

await metrics.counter('processedItems', 5)
await metrics.millis('dbQueryTime', 128)
await metrics.gauge('inFlightJobs', 3)
await metrics.byteSize('responseSize', 1024)

Metric calls can be fired without awaiting to avoid blocking execution.

Timed Function

const metrics = new Metrics(logger)
await metrics.timer('myHandlerTime', async () => {
  // Do some async work
})

With dimensions

await counter('apiCall', 1, { service: 'user', route: '/login' })

Hapi Plugin

To attach metrics to the server and request contexts and configure the logger:

import { metrics } from '@defra/cdp-metrics'

await server.register(metrics)

server.metrics.counter('startup', 1)
request.metrics.timer('dbFetch', async () => fetchUsers())

Testing

You can use AWS_EMF_ENVIRONMENT=Local in your tests if you don't want metrics mocked.

To mock the internal aws-embedded-metrics methods to verify emitted metrics:

import * as embedded from 'aws-embedded-metrics'

vi.spyOn(embedded, 'createMetricsLogger').mockImplementation(() => ({
  putMetric: vi.fn(),
  flush: vi.fn(),
  setDimensions: vi.fn()
}))

Error Handling

If any metric fails to emit, a warning is logged (if a logger is set). You can inject your own logger:

import pino from 'pino'
new Metrics(pino())

版本与发布记录

@defra/cdp-metrics 共发布 8 个版本,首次发布于 2025/07/08;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.6.1latest2026/10/051 个月前
0.6.02026/04/226 个月前
0.5.02026/01/299 个月前
0.4.02026/01/289 个月前
0.3.02025/11/2811 个月前
0.2.02025/11/071 年前

关于 @defra/cdp-metrics 的常见问题

@defra/cdp-metrics 是什么?

Library for custom metrics on the CDP platform

如何安装 @defra/cdp-metrics?

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

@defra/cdp-metrics 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/defra__cdp-metrics 是否存在。

@defra/cdp-metrics 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@defra/cdp-metrics 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@defra/cdp-metrics 可以商用吗?

@defra/cdp-metrics 使用 OGL-UK-3.0 许可。声明的许可为 OGL-UK-3.0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@defra/cdp-metrics 的下载量有多少?

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

@defra/cdp-metrics 还在维护吗?

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