Jos*_*ger 5 monitoring json node.js prometheus
我正在使用Prometheus来检测Node.js应用程序以进行监控.我目前正在使用以下Node.js客户端进行检测:
我已将所有配置为从我的Node.js应用程序收集和收集默认指标,并且监视正在按预期工作.我想知道Prometheus是否有可能从我的应用程序公开的端点中抓取JSON.
例如,Node.js应用程序有一个运行状况检查端点(/ health),它返回有关应用程序整体运行状况及其依赖关系的简单JSON数据(布尔值或0/1).我可以配置普罗米修斯和/或舞会,客户端从健康端点基于该信息刮JSON,然后记录指标?
我相信你可以.
我在下面链接的博客文章详细说明了如何使用Prometheus Python客户端将JSON格式的指标提取到Prometheus中.
https://www.robustperception.io/writing-a-jenkins-exporter-in-python/ https://www.robustperception.io/writing-json-exporters-in-python/
我能够使用舞会客户端并构建自己的自定义指标找到解决方案。将为任何有兴趣这样做的人提供下面的示例。假设有一个运行状况检查端点返回以下 JSON:
{
"app": {
"message": "Service is up and running!",
"success": true
}
}
Run Code Online (Sandbox Code Playgroud)
我使用包请求来调用端点、解析数据并创建一个仪表来反映基于运行状况检查状态的值。下面是 JavaScript 中 /metrics 端点的示例:
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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11068 次 |
| 最近记录: |