不上监控你永远不知道线上代码跑成什么样, 不生孩子你就不知道肿么回事儿

放假前突然接到需求要给消息队列上个监控...

平时不基建临时抱佛脚...

比较大的问题是 exporter 的数据展示, 当时为了工期, 写了个定时任务自动更新 metrcis, 自己都觉得蠢的一逼

今天看了下官方客户端, 优雅太多了. 留个 demo 下来, 省的以后临时需求又没时间看文档

import time

from prometheus_client import start_http_server
from prometheus_client.core import GaugeMetricFamily, REGISTRY
import requests


class OverviewCollector(object):
    def collect(self):
        print("collecting...")
        metric = GaugeMetricFamily(name="queue_totals", documentation="message_ready",
                                   labels=["msg_type"])

        payload = {}
        headers = {}

        url = "http://root:pwd@127.0.0.1:15672/api/overview"
        response = requests.get(url=url)
        data = response.json()
        queue_totals = data.get("queue_totals", {})
        msg_ready = queue_totals.get("messages_ready", 0)
        msg = queue_totals.get("messages", 0)
        msg_unacknowledged = queue_totals.get(
            "messages_unacknowledged", 0)

        metric.add_metric(["msg_ready"], msg_ready)
        metric.add_metric(["msg"], msg)
        metric.add_metric(["msg_unacknowledged"], msg_unacknowledged)

        yield metric
        
        c = CounterMetricFamily("HttpRequests", 'Help text', labels=['app'])
        c.add_metric(["example"], 2000)
        yield c


if __name__ == "__main__":
    REGISTRY.register(OverviewCollector())
    start_http_server(10111)
    print("start...")
    while True:
        time.sleep(1)

TODO

  • \while true 总是怪怪的, 比较喜欢
  • 这个 Collector 怎么优雅的抽象出来啊... 一个 metrcs 就是一个 Collector

吐槽

golang 的 demo 就那么简洁???