数据采集 采集源配置 Express 监控采集器
Express 监控采集器
简介
express是nodejs常用的web开发框架,该采集器通过引入prometheus 监控实现包,实现prometheus exporter,通过datakit实现对监控指标采集
前置条件
- 在项目中引入
prom-client
模块并暴露指标接口
参考如下示例
示例
const express = require('express')
const app = express()
const port = 3000
const client = require('prom-client')
const collectDefaultMetrics = client.collectDefaultMetrics
collectDefaultMetrics({ timeout: 1000 })
app.get('/metrics', (req, res) => {
res.set('Content-Type', client.register.contentType)
res.end(client.register.metrics())
})
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
配置
进入 DataKit 安装目录下的 conf.d/expressjs 目录,复制 expressjs.conf.sample 并命名为 expressjs.conf。示例如下:
[[inputs.prom]]
# expressjs metrics from http(https)://HOST:PORT/metrics
# usually modify host and port
# required
url = "http://127.0.0.1:3000/metrics"
# valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h"
# required
interval = "10s"
## Optional TLS Config
tls_open = false
# tls_ca = "/tmp/ca.crt"
# tls_cert = "/tmp/peer.crt"
# tls_key = "/tmp/peer.key"
# [inputs.prom.tags]
# from = "127.0.0.1:3000"
# tags1 = "value1"
说明:
- 如果 指标 接口需要使用 HTTPS 访问,需要在配置文件中将
tls_open
设置为true
,且配置3个证书和私钥文件路径
- 使用命令例如
curl 127.0.0.1:3000/metrics
根据返回内容判断是否需要使用 HTTPS
- 使用命令例如
curl --cacert ca.crt --cert peer.crt --key peer.key -L https://127.0.0.1:3000/metrics
验证 tls 证书和私钥是否可用
- 证书和私钥码只支持 PEM 编码
- 以上示例代码提供了一种监控数据采集的思路,可以根据自己的业务,针对监控的指标进行扩展开发 参考: https://www.npmjs.com/package/prom-client
采集指标
指标 |
类型 |
单位 |
nodejs_active_handles_total |
fields |
float |
nodejs_active_requests_total |
fields |
float |
nodejs_eventloop_lag_max_seconds |
fields |
float |
nodejs_eventloop_lag_mean_seconds |
fields |
float |
nodejs_eventloop_lag_min_seconds |
fields |
float |
nodejs_eventloop_lag_p50_seconds |
fields |
float |
nodejs_eventloop_lag_p90_seconds |
fields |
float |
nodejs_eventloop_lag_p99_seconds |
fields |
float |
nodejs_eventloop_lag_seconds |
fields |
float |
nodejs_eventloop_lag_stddev_seconds |
fields |
float |
nodejs_external_memory_bytes |
fields |
float |
nodejs_heap_size_total_bytes |
fields |
float |
nodejs_heap_size_used_bytes |
fields |
float |
process_cpu_seconds_total |
fields |
float |
process_cpu_system_seconds_total |
fields |
float |
process_cpu_user_seconds_total |
fields |
float |
process_resident_memory_bytes |
fields |
float |
process_start_time_seconds |
fields |
float |
- expressjs_nodejs_active 指标集
指标 |
类型 |
单位 |
type |
tags |
string |
nodejs_active_handles |
fields |
float |
指标 |
类型 |
单位 |
kind |
tags |
string |
nodejs_gc_duration_seconds_count |
fields |
float |
nodejs_gc_duration_seconds_sum |
fields |
float |
- expressjs_nodejs_heap 指标集
指标 |
类型 |
单位 |
space |
tags |
string |
nodejs_heap_space_size_available_bytes |
fields |
float |
nodejs_heap_space_size_total_bytes |
fields |
float |
nodejs_heap_space_size_used_bytes |
fields |
float |
- expressjs_nodejs_version 指标集
指标 |
类型 |
单位 |
major |
tags |
string |
minor |
tags |
string |
patch |
tags |
string |
version |
tags |
string |
nodejs_version_info |
fields |
float |