Prometheus自定义收集器在导出时会挂起

时间:2017-11-14 16:55:49

标签: python prometheus

我正在尝试创建一个收集路由器内存统计信息并导出到prometheus的自定义收集器,我使用的是python prometheus-client,并且具有以下代码:

from prometheus_client import start_http_server
from prometheus_client.core import CounterMetricFamily, REGISTRY
import requests
import json



class MemoryCollector(object):
    def collect(self):
        c = CounterMetricFamily('ODL_memory_monitoring', 'Memory for each process on box', labels=['node', 'process'])
        url = "http://<ip>:8181/restconf/operational/network-topology:network-topology/topology/topology-netconf/node/IDC_R-Leaf3/yang-ext:mount/Cisco-IOS-XR-procmem-oper:processes-memory/no\
des"
        headers = {
                'authorization': "<token>",
                'cache-control': "no-cache",
                'postman-token': "<token>"
        }
        response = requests.request("GET", url, headers=headers)
        reply = json.loads(response.text)
        for node in reply['nodes']['node']:
            for process in node['process-ids']['process-id']:
                if process['name'] == 'tcp':
                    c.add_metric([node['node-name'], process['name']], process['malloc-size'])
                    print("Adding metric {} {}".format(node['node-name'], process['name']))
        yield c



def main():
    start_http_server(8000)
    REGISTRY.register(MemoryCollector())


if __name__ == '__main__':
    main()

当我导航到网页(<ip>:8000)时,它只是挂起并且没有更新指标。有任何性能问题吗?这是创建自定义收集器的正确方法吗?

1 个答案:

答案 0 :(得分:0)

你有一个无限循环,请尝试删除while True: