我正在使用Prometheus来测试用于监视的Node.js应用程序。我目前正在使用以下Node.js客户端进行检测:
我已经配置了一切,以便从我的Node.js应用程序中收集和收集默认的度量,并且监控正在如愿以偿。我想知道Prometheus是否有可能从我的应用程序公开的端点中刮取JSON。
例如,Node.js应用程序有一个健康检查端点(/health),它返回简单的JSON数据(布尔值或0/1),该数据涉及应用程序的总体健康状况及其依赖关系。我是否可以配置Prometheus和/或prom-客户机从健康端点中刮取JSON,然后根据这些信息记录度量?
发布于 2017-07-20 14:13:36
我能够使用舞会客户找到一个解决方案,并构建自己的自定义度量。将为任何可能有兴趣这样做的人提供下面的一个例子。假设有一个健康检查端点,它返回以下JSON:
{
"app": {
"message": "Service is up and running!",
"success": true
}
}我使用包request对端点进行调用,解析数据,并创建一个基于健康检查状态的值来反映值。下面是/metrics端点在JavaScript中的一个示例:
const express = require('express');
const router = express.Router();
const request = require('request');
// Config for health check endpoint
const healthCheckURL = 'https://SOME_ENDPOINT/health';
const zone = 'DEV';
// Initialize Prometheus
const Prometheus = require('prom-client');
const collectDefaultMetrics = Prometheus.collectDefaultMetrics;
collectDefaultMetrics({
timeout: 5000
});
router.get('/', (req, res) => {
res.end(Prometheus.register.metrics());
});
const serviceHealthGauge = new Prometheus.Gauge({
name: 'service_health',
help: 'Health of service component',
labelNames: ['zone']
});
setInterval(() => {
request({
url: healthCheckURL,
method: "GET",
},
function(error, response, body) {
if (!error && response.statusCode == 200) {
const JSONBody = JSON.parse(body);
// check service health
if (JSONBody.app && JSONBody.app.success) {
serviceHealthGauge.set({
zone: zone
}, 1);
} else {
serviceHealthGauge.set({
zone: zone
}, 0);
}
} else {
serviceHealthGauge.set({
zone: zone
}, 0);
}
}
);
}, 10000);
module.exports.metricNames = ['service_health'];
module.exports = router;发布于 2017-07-13 15:16:06
我相信你可以。
下面我所链接的博客详细介绍了如何使用Prometheus客户端将JSON格式的指标摄取到Prometheus中。
https://www.robustperception.io/writing-a-jenkins-exporter-in-python/ https://www.robustperception.io/writing-json-exporters-in-python/
发布于 2017-07-14 11:11:46
如果您想让prom客户端收集这些信息,可以查看库中的堆大小收集器。
在这个收集器获取堆大小的地方,您可以抓取JSON端点,也可以直接调用JSON端点后面的功能,以便使用0或1发布一些度量。
https://stackoverflow.com/questions/45083590
复制相似问题