Prometheus 是否允许您从端点抓取 JSON 信息?
Does Prometheus allow you to scrape JSON information from an endpoint?
我正在使用 Prometheus 检测 Node.js 应用程序以进行监控。我目前正在使用以下 Node.js 客户端进行检测:
我已将所有内容配置为从我的 Node.js 应用程序收集和收集默认指标,并且监控按预期工作。我想知道 Prometheus 是否可以从我的应用程序公开的端点抓取 JSON。
例如,Node.js 应用程序有一个健康检查端点 (/health),returns 简单 JSON 数据(布尔值或 0/1)关于整体健康状况应用程序及其依赖项。我可以配置 Prometheus and/or prom-client 从健康端点抓取 JSON 然后根据该信息记录指标吗?
我相信你可以。
我在下面链接的博文详细说明了如何使用 Prometheus Python 客户端将 JSON 格式的指标提取到 Prometheus 中。
https://www.robustperception.io/writing-a-jenkins-exporter-in-python/
https://www.robustperception.io/writing-json-exporters-in-python/
不直接,因为 Prometheus 只理解它们的文本格式或 GRPC 格式。参见 https://prometheus.io/docs/instrumenting/exposition_formats/
或者当然可以编写一个翻译 "bridge" 或导出器,以那种格式翻译 JSON 结构,就像@ConorB 在他的回答中描述的那样。
如果您想让 prom-client 收集这些信息,您可以查看库中的 heap sizes 收集器。
在这个收集器获取堆大小的地方,您可以改为抓取 JSON 端点,或者直接调用 JSON 端点背后的功能来发布一些 0 或 1 的仪表。
我能够使用 prom-client 找到解决方案并构建我自己的自定义指标。将为可能有兴趣这样做的任何人提供下面的示例。假设有一个健康检查端点 returns 以下 JSON:
{
"app": {
"message": "Service is up and running!",
"success": true
}
}
我使用包 request 调用端点,解析数据并创建一个仪表来反映基于健康检查状态的值。以下是 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;
现在可以使用黑盒导出器:https://github.com/prometheus/blackbox_exporter。
这是 prometheus/grafana/blackbox - https://medium.com/the-telegraph-engineering/how-prometheus-and-the-blackbox-exporter-makes-monitoring-microservice-endpoints-easy-and-free-of-a986078912ee.
的分步指南
我正在使用 Prometheus 检测 Node.js 应用程序以进行监控。我目前正在使用以下 Node.js 客户端进行检测:
我已将所有内容配置为从我的 Node.js 应用程序收集和收集默认指标,并且监控按预期工作。我想知道 Prometheus 是否可以从我的应用程序公开的端点抓取 JSON。
例如,Node.js 应用程序有一个健康检查端点 (/health),returns 简单 JSON 数据(布尔值或 0/1)关于整体健康状况应用程序及其依赖项。我可以配置 Prometheus and/or prom-client 从健康端点抓取 JSON 然后根据该信息记录指标吗?
我相信你可以。
我在下面链接的博文详细说明了如何使用 Prometheus Python 客户端将 JSON 格式的指标提取到 Prometheus 中。
https://www.robustperception.io/writing-a-jenkins-exporter-in-python/ https://www.robustperception.io/writing-json-exporters-in-python/
不直接,因为 Prometheus 只理解它们的文本格式或 GRPC 格式。参见 https://prometheus.io/docs/instrumenting/exposition_formats/
或者当然可以编写一个翻译 "bridge" 或导出器,以那种格式翻译 JSON 结构,就像@ConorB 在他的回答中描述的那样。
如果您想让 prom-client 收集这些信息,您可以查看库中的 heap sizes 收集器。
在这个收集器获取堆大小的地方,您可以改为抓取 JSON 端点,或者直接调用 JSON 端点背后的功能来发布一些 0 或 1 的仪表。
我能够使用 prom-client 找到解决方案并构建我自己的自定义指标。将为可能有兴趣这样做的任何人提供下面的示例。假设有一个健康检查端点 returns 以下 JSON:
{
"app": {
"message": "Service is up and running!",
"success": true
}
}
我使用包 request 调用端点,解析数据并创建一个仪表来反映基于健康检查状态的值。以下是 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;
现在可以使用黑盒导出器:https://github.com/prometheus/blackbox_exporter。
这是 prometheus/grafana/blackbox - https://medium.com/the-telegraph-engineering/how-prometheus-and-the-blackbox-exporter-makes-monitoring-microservice-endpoints-easy-and-free-of-a986078912ee.
的分步指南