Nodejs Client for Monitor
安装
npm install node-client --registry=https://npm.finogeeks.club
启动参数(环境变量)
变量 | 说明 | 默认值 |
---|---|---|
ENABLE_MONITOR | 是否打开监控 | false |
MONITOR_DEFAULT_INTERVAL | 采集nodejs的cpu/mem等指标的时间间隔 | undefined, 即关闭该功能 |
MONITOR_PORT | exporter端口 | 9092 |
MONITOR_PATH | exporter路径 | /metrics |
获取监控数据
访问:http://<SERVER_URL>:<MONITOR_PORT>/<MONITOR_PATH>
.
比如,本地调试时可以直接在浏览器访问http://127.0.0.1:9092/metrics
查看已添加的监控指标。
接口说明
指标名和标签名
- 指标名唯一确定了一个指标,比如:
mesons_synapse_requests
- 创建相同名字的指标时,实际上是拿到同一个指标的实例,即不能得到两个不同类型的指标使其指标名是相同的,如下代码是非法的:
const monitor = require('node-client');
const counter = monitor.Counter('mesons_synapse_request', ['code', 'method']);
// 实际上得到了一个Counter类型的指标
const gauge = monitor.Gauge('mesons_synapse_request', ['code', 'status']);
// error: Couter类型的指标不存在dec方法
gauge.dec();
- 任意指标的labelNames集合的子集都是合法的
- 指标的命名必须能够唯一区分该指标,推荐使用
<namespace>_<subsystem>_<metric>
的格式 - 更多指标和标签命名的最佳实践请参考METRIC AND LABEL NAMING
例子
Output
mesons_synapse_counter 1
mesons_synapse_counter{code="200",method="GET"} 10
mesons_synapse_counter{code="404",method="POST"} 20 1497341287055
mesons_synapse_gauge 1
mesons_synapse_gauge{code="400"} 1497341287056
mesons_synapse_gauge{code="200",method="GET"} -2
mesons_synapse_gauge{code="404",method="POST"} 3 1497341287056
mesons_synapse_summary{quantile="0.5",method="GET",code="200"} 20
mesons_synapse_summary{quantile="0.9",method="GET",code="200"} 30
mesons_synapse_summary_sum{code="200",method="GET"} 60
mesons_synapse_summary_count{code="200",method="GET"} 3
mesons_synapse_histogram_bucket{le="10",method="GET",code="200"} 1
mesons_synapse_histogram_bucket{le="15",method="GET",code="200"} 2
mesons_synapse_histogram_bucket{le="+Inf",method="GET",code="200"} 3
mesons_synapse_histogram_sum{code="200",method="GET"} 37
mesons_synapse_histogram_count{code="200",method="GET"} 3
mesons_synapse_gauge_timer 0.000040917
mesons_synapse_summary_timer{quantile="0.5",method="GET",code="200"} 0.000035099
mesons_synapse_summary_timer{quantile="0.9",method="GET",code="200"} 0.000035099
mesons_synapse_summary_timer_sum{code="200",method="GET"} 0.000035099
mesons_synapse_summary_timer_count{code="200",method="GET"} 1
mesons_synapse_histogram_timer_bucket{le="10",code="400",method="POST"} 1
mesons_synapse_histogram_timer_bucket{le="15",code="400",method="POST"} 1
mesons_synapse_histogram_timer_bucket{le="+Inf",code="400",method="POST"} 1
mesons_synapse_histogram_timer_sum{method="POST",code="400"} 0.000043813
mesons_synapse_histogram_timer_count{method="POST",code="400"} 1
mesons_synapse_histogram_timer_bucket{le="10",method="POST",code="500"} 1
mesons_synapse_histogram_timer_bucket{le="15",method="POST",code="500"} 1
mesons_synapse_histogram_timer_bucket{le="+Inf",method="POST",code="500"} 1
mesons_synapse_histogram_timer_sum{code="500",method="POST"} 0.000002668
mesons_synapse_histogram_timer_count{code="500",method="POST"} 1
# 默认指标
process_cpu_user_seconds_total 0.001006 1498200452791
process_cpu_system_seconds_total 0.000042 1498200452791
process_cpu_seconds_total 0.001048 1498200452791
process_start_time_seconds 1498200453 1498200452626
process_resident_memory_bytes 36163584 1498200452792
nodejs_eventloop_lag_seconds 0.015018044 1498200452807
nodejs_active_handles_total 2 1498200452793
nodejs_active_requests_total 0 1498200452793
nodejs_heap_size_total_bytes 19853312 1498200452793
nodejs_heap_size_used_bytes 11901432 1498200452793
nodejs_external_memory_bytes 78171 1498200452793
nodejs_heap_space_size_total_bytes{space="new"} 8388608 1498200452793
nodejs_heap_space_size_total_bytes{space="old"} 7704576 1498200452793
nodejs_heap_space_size_total_bytes{space="code"} 2154496 1498200452793
nodejs_heap_space_size_total_bytes{space="map"} 1069056 1498200452793
nodejs_heap_space_size_total_bytes{space="large_object"} 536576 1498200452793
nodejs_heap_space_size_used_bytes{space="new"} 1648120 1498200452793
nodejs_heap_space_size_used_bytes{space="old"} 7061616 1498200452793
nodejs_heap_space_size_used_bytes{space="code"} 2032832 1498200452793
nodejs_heap_space_size_used_bytes{space="map"} 643192 1498200452793
nodejs_heap_space_size_used_bytes{space="large_object"} 524328 1498200452793
nodejs_heap_space_size_available_bytes{space="new"} 2476552 1498200452793
nodejs_heap_space_size_available_bytes{space="old"} 136 1498200452793
nodejs_heap_space_size_available_bytes{space="code"} 2144 1498200452793
nodejs_heap_space_size_available_bytes{space="map"} 80 1498200452793
nodejs_heap_space_size_available_bytes{space="large_object"} 1478471168 1498200452793
nodejs_version_info{version="v7.7.4",major="7",minor="7",patch="4"} 1 1498200452630
HTTP中间件监控
自动监控http中间件,支持express和restify。
指标
- Summary: http_request_duration_seconds{'method', 'path', 'code'}
- Histogram: http_request_buckets_seconds{'method', 'path', 'code'}
使用方法
Express
const express = require('express');
const monitor = require('node-client');
const app = express();
monitor.Instrument(app);
app.get('/', (req, res) => {
res.send();
});
app.listen(3000, () => {
console.log('express server listening on port 3000');
});
Restify
const restify = require('restify');
const mointor = require('node-client');
const server = restify.createServer();
monitor.Instrument(server);
server.get('/', (req, res, done) => {
res.send();
done();
});
server.listen(3000, () => {
console.log('restify server listening on port 3000');
});