@bonniernews/exp-metrics

中文文档
4.0.0发布于 2026/10/27MITTypeScriptCommonJS近 30 天下载 2328

Simple bootstrapping of basic prometheus metrics for your express based application.

原始使用文档
文档目录7 节
  1. Types of metrics and API
  2. Installing
  3. Usage
  4. Options
  5. Histogram buckets
  6. Shutdown
  7. Google Cloud

exp-metrics

Run tests

Simple wrapper of OpenTelemetry metrics with the same API as prom-client, to make the transition from prom-client easy, and also add functionality that OpenTelemetry lacks.

Types of metrics and API

  • Counter
    • inc([labels,] [value])
  • Gauge
    • set([labels,] value)
    • inc([labels,] [value])
    • dec([labels,] [value])
  • Summary (histogram in OpenTelemetry terms)
    • observe([labels,] value) or observe(value[, labels])

Creating a metric with a name that already exists returns the existing metric, so metrics.counter({ name: "x" }) is safe to call per request.

A gauge keeps one value per label set, i.e. gauge.set({ queue: "a" }, 1) and gauge.set({ queue: "b" }, 2) are reported as two series.

Installing

npm install @bonniernews/exp-metrics

Usage

const expMetrics = require("@bonniernews/exp-metrics");

metrics = expMetrics("my-service-name", {
  // optional overrides, see more below
});

const myCounter = metrics.counter({
  name: "my_counter",
  help: "My counter",
})

myCounter.inc(2);
myCounter.inc({ foo: "bar" }, 3);

See more in the prom-client documentation.

TypeScript types are included.

Options

  • enabled (default true): false keeps the same API but exports nothing and never calls Google Cloud, e.g. for tests and local development
  • buckets: explicit histogram buckets for every summary, see Histogram buckets

Any other key is set as an OpenTelemetry resource attribute, overriding the defaults (gcp.project_id, service.name, service.namespace, service.instance.id). See the OpenTelemetry documentation.

Histogram buckets

Summaries are exported as exponential histograms by default. They have fine resolution per series, but every series picks its own bucket layout, so percentiles aggregated across series (e.g. p95 over all instances) in Cloud Monitoring can be very coarse.

Explicit buckets give every series the same layout, so they aggregate exactly, with a precision of the bucket width:

// one summary
metrics.summary({ name: "my_summary", buckets: [ 10, 50, 100, 250, 500, 1000 ] });

// every summary, including http_response_time_milliseconds
// true = OpenTelemetry defaults [ 0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 7500, 10000 ]
expMetrics("my-service-name", { buckets: true });
expMetrics("my-service-name", { buckets: [ 10, 50, 100, 250, 500, 1000 ] });

Summaries with their own buckets are exported with the instrumentation scope exp-metrics-buckets instead of exp-metrics.

Shutdown

Metrics are exported every 60 seconds. Flush the remaining metrics before the process exits:

process.on("SIGTERM", async () => {
  await metrics.shutdown();
  process.exit(0);
});

Google Cloud

Metrics are exported every 60 seconds over OTLP to the Google Cloud Telemetry API, authenticated with Application Default Credentials. The project is resolved from the credentials (or GOOGLE_CLOUD_PROJECT), and can be overridden with "gcp.project_id" in the config.

Requirements:

  • The Telemetry API is enabled: gcloud services enable telemetry.googleapis.com
  • The service account has the roles/telemetry.writer role

Metrics show up in Cloud Monitoring as prometheus.googleapis.com/<name>/<counter|gauge|histogram> on the prometheus_target resource, e.g. prometheus.googleapis.com/http_responses_total/counter.

版本与发布记录

@bonniernews/exp-metrics 共发布 9 个版本,首次发布于 2018/10/01;过去 12 个月发布了 1 个版本。

版本发布日期距今
4.0.0latest2026/10/2712 天前
3.0.02024/05/212 年前
2.0.32024/05/202 年前
2.0.22024/05/162 年前
2.0.12024/05/032 年前
2.0.02024/04/252 年前

关于 @bonniernews/exp-metrics 的常见问题

@bonniernews/exp-metrics 是什么?

Simple bootstrapping of basic prometheus metrics for your express based application.

如何安装 @bonniernews/exp-metrics?

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

@bonniernews/exp-metrics 支持 TypeScript 吗?

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

@bonniernews/exp-metrics 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bonniernews/exp-metrics 需要什么版本的 Node.js?

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

@bonniernews/exp-metrics 可以商用吗?

@bonniernews/exp-metrics 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bonniernews/exp-metrics 的下载量有多少?

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

@bonniernews/exp-metrics 还在维护吗?

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