Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

n-metrics

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

n-metrics

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

latest
npmnpm
Version
1.0.4
Version published
Weekly downloads
12
-20%
Maintainers
1
Weekly downloads
 
Created
Source

搜集&汇报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不存在时会自动创建

FAQs

Package last updated on 27 Jun 2018

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