Prometheus是否允许您从端点刮取JSON信息?

问题描述 投票:5回答:5

我正在使用Prometheus来检测Node.js应用程序以进行监控。我目前正在使用以下Node.js客户端进行检测:

prom-client

我已将所有配置为从我的Node.js应用程序收集和收集默认指标,并且监视正在按预期工作。我想知道Prometheus是否有可能从我的应用程序公开的端点中抓取JSON。

例如,Node.js应用程序有一个运行状况检查端点(/ health),它返回有关应用程序整体运行状况及其依赖关系的简单JSON数据(布尔值或0/1)。我是否可以将Prometheus和/或prom-client配置为从健康端点刮取JSON,然后根据该信息记录指标?

json node.js monitoring prometheus
5个回答
6
投票

我相信你可以。

我在下面链接的博客文章详细说明了如何使用Prometheus Python客户端将JSON格式的指标提取到Prometheus中。

https://www.robustperception.io/writing-a-jenkins-exporter-in-python/ https://www.robustperception.io/writing-json-exporters-in-python/


2
投票

如果您想让prom-client收集这些信息,您可以查看作为库的一部分的heap sizes收集器。

在此收集器获取堆大小的情况下,您可以改为抓取JSON端点,或者可以直接调用JSON端点后面的功能以发布一些带有0或1的计量器。


2
投票

我能够使用prom-client找到解决方案并构建我自己的自定义指标。将为可能有兴趣做同样事情的任何人提供以下示例。假设有一个运行状况检查端点返回以下JSON:

{
    "app": {
        "message": "Service is up and running!",
        "success": true
    }
}

我使用包请求来调用端点,解析数据并创建一个计量器以反映基于健康检查状态的值。以下是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;

1
投票

不是直接的,因为普罗米修斯只了解他们的文本格式或GRPC格式。见https://prometheus.io/docs/instrumenting/exposition_formats/

或者当然可以编写一个翻译“桥”或导出器来翻译那种格式的JSON结构,比如他在答案中描述的@ConorB。


© www.soinside.com 2019 - 2024. All rights reserved.