prometheus / prometheus/client_python

How to measure time spent by collector.collect?

オープン
#859 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Python
スター
4.4k
フォーク
876
平均マージ
8日 4時間
マージ済み PR(30日)
1

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

示されている Collector.collect ジェネレーターと Gauge.time() デコレーターから始め、次にデコレーターによる時間計測と collect() 内のコードを比較します。2 つの計測結果が異なる理由を説明し、ユーザーが collector の収集時間を正しく計測できるように動作をドキュメント化します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
prometheus, python
領域
observability
issue の種類
ドキュメント
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。