npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

n-metrics

v1.0.4

Published

搜集&汇报node应用metrics的二方库

Readme

搜集&汇报node应用metrics的二方库,目前暂时只提供一种一种在应用内将指标计算好的收集方式。

安装

npm install n-metrics 或
yarn add n-metrics

使用

const { registry, registryReporter, MeasurementKey, InfluxdbReporter } = require('n-metrics');
// 或者
import { registry, registryReporter, MeasurementKey, InfluxdbReporter } from 'n-metrics';

在应用内计算指标: metrics

在应用中搜集metrics数据(QPS,RT,COUNT)等数据,然后汇报给influxdb,最后可以通过grafana进行展示

注意: 每个应用建议单独用一个库. 避免互相影响

例子

koa2例子

express例子

  • 统计的数据会按照设定的时间间隔进行定期统计,汇报,清空,然后重新统计.

几个重要概念

registry

所有的metrics的集中营

getMeasurement(MeasurementKey)

根据MeasurementKey取出表Measurement,相同MeasurementKey会取出相同的Measurement

MeasurementKey

name,[,key, value]+ 相同的measurementKey会进行累加,比如

`measurementKey("api.memcached", "host", "192.168.1.1")`

就可以理解为

应用api,机器名为192.168.1.1的memcached指标

MeasurementKey支持多个tag,比如

key = new MeasurementKey("name", "host", "localhost", "cluster", "cluster-basic")

host用于举例,实际使用讲默认带此tag,不需要添加 在influxdb中一条记录的唯一性是通过表名+Tags确定的

Measurement

对应influxdb中的一张表

counter & timer & v8gauge ...

counter、timer、v8gauge都是具体的指标

  • counter主要用来统计单元时间某时间发生的次数,例如QPS,
  • timer主要用来统计某段时间内,某个时间持续的平均时间,例如RT
  • v8gauge用于收集v8引擎的内存快照,包含memory.used_heap_size等

counter、timer和v8gauge都是实现了IMetrics,后续如果有更多类型的指标,可以继续扩张此类. 三者第一个参数对应influxdb中的field,如counter('tps')表示操作表的tps字段,其中v8gauge有第二个参数,指的是获取快照的时间间隔,单位,默认为5s,v8gauge只需要调用一次,内部就会定时收集内存快照,具体操作:

  • counter(field).mark(n?) 设置field字段的值为n,默认为1
  • timer(field).stop() 从timer(field)获取到timer开始就开始计时,调用stop后停止计时,期间如果多次调用timer(field),stop时将会获取平均值
  • v8gauge(field, dataTTL?) 定时获取v8内存快照,此处field字段其实仅是用于保证实例唯一,不会写入influxdb,dataTTL时收集快照的时间间隔,单位s,默认5s

registryReporter

单例,所有报告注册中心,目前只提供一种报告方式,未来扩展可以往此添加即可

init(options?)

应用要上报数据,必须调用registryReporter.init进行初始化,可选项如下:

  • reportIntervalMillis 报告上报的时间间隔,单位毫秒,默认3000ms。
  • reporters 报告实例,实现IReporter接口

InfluxdbReporter(config?)

报告实例,实现IReporter接口 用于将metrics数据报告给influxdb config配置参见node-influx 另外提供了一个参数

  • allowedIpPrefixes 允许的ip前缀,只有被允许的ip前缀会添加host的tag标签,默认192.168,172.16,10.21,10.57

注意

数据库database、表measurement不存在时会自动创建