Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

How to measure time spent by collector.collect?

未關閉
#859 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
35/100
Issue 類型
文件
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
prometheus, python
領域
observability

研究方向

從所示的 Collector.collect 生成器和 Gauge.time() 裝飾器開始,然後將裝飾器的計時與 collect() 內的程式碼進行比較。說明兩次測量結果為何不同,並記錄此行為,以便使用者能正確測量 collector 的收集時間。

由索引模型根據 Issue 內容生成。

描述

Having a 3rd-party application which can not expose any metrics by itself I wrote my own exporter which takes data from the app and exposes metrics. The next idea was to measure and expose to prometheus an exact time it takes for all of the collectors to get actual data from the app. But two different approaches - with collector.collect() decorator and measurements taken directly inside of collector.collect() yields different results, and I don't understand what the decorator is actually measures.

Could you please explain this difference or maybe I'm missing something?

import time
from typing import Iterable
from prometheus_client import make_wsgi_app, Gauge
from prometheus_client.core import REGISTRY, GaugeMetricFamily, Metric
from prometheus_client.registry import Collector
from wsgiref.simple_server import make_server


COLLECTOR_TIME = Gauge('collector_spent_secods', 'Time spent by collector to get data', ['collector'])


class MetricsCollector(Collector):
    @COLLECTOR_TIME.labels('decorator').time()
    def collect(self) -> Iterable[Metric]:
        st = time.perf_counter()
        gauge = GaugeMetricFamily(
            name='some_metric_seconds',
            documentation='Some seconds metric',
            labels=['somename']
        )
        time.sleep(1)
        gauge.add_metric(['foo'], 3.0)
        COLLECTOR_TIME.labels('inside').set(time.perf_counter() - st)
        yield gauge


if __name__ == "__main__":
    REGISTRY.register(MetricsCollector())
    app = make_wsgi_app(REGISTRY)
    host = '0.0.0.0'
    port = 6543
    httpd = make_server(host, port, app)
    httpd.serve_forever()
$ curl localhost:6543 2>/dev/null | egrep -v '^#' | grep 'collector_spent_secods'
collector_spent_secods{collector="decorator"} 7.499998901039362e-06
collector_spent_secods{collector="inside"} 1.0012022000155412
主要語言
Python
星號
4.4k
分支
876
平均合併
8 天 4 小時
30 天內合併 PR
1

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

prometheus/client_python 的其他 Issue

查看 prometheus/client_python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。