prometheus / prometheus/client_python

Metrics declared at toplevel of program interfere with autoreload

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

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

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

説明

To reproduce:

  1. Create a file bar.py with contents
from prometheus_client import Gauge

my_gauge = Gauge('my_gauge', 'my description')

def add(x, y):
    return x + y
  1. Create a Jupyter notebook with the contents
%load_ext autoreload
%autoreload 2
from bar import add
# ---- cell break ----
add(2, 3)

and run it.
3. Edit bar.py in some way (e.g. add a product(x, y) function).
4. Re-run the add(2, 3) cell in the notebook

Expected behavior

Autoreload works fine; the cell runs without issue.

Actual behavior

After running add(2, 3) a second time, it prints this error to stderr:

[autoreload of bar failed: Traceback (most recent call last):
  File "/dev/shm/uid-21748-gid-32786/39d5b5d2-seed-nspid4026531836-ns-4026531841/lib/python3.10/site-packages/IPython/extensions/autoreload.py", line 245, in check
    superreload(m, reload, self.old_objects)
  File "/dev/shm/uid-21748-gid-32786/39d5b5d2-seed-nspid4026531836-ns-4026531841/lib/python3.10/site-packages/IPython/extensions/autoreload.py", line 394, in superreload
    module = reload(module)
  File "/dev/shm/uid-21748-gid-32786/39d5b5d2-seed-nspid4026531836-ns-4026531841/lib/python3.10/imp.py", line 315, in reload
    return importlib.reload(module)
  File "/dev/shm/uid-21748-gid-32786/39d5b5d2-seed-nspid4026531836-ns-4026531841/lib/python3.10/importlib/__init__.py", line 169, in reload
    _bootstrap._exec(spec, module)
  File "<frozen importlib._bootstrap>", line 619, in _exec
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/j/igm/user/kstaley/pub/Jupyter/play/bar.py", line 3, in <module>
    my_gauge = Gauge('my_gauge', 'my description')
  File "/dev/shm/uid-21748-gid-32786/39d5b5d2-seed-nspid4026531836-ns-4026531841/lib/python3.10/site-packages/prometheus_client/metrics.py", line 365, in __init__
    super().__init__(
  File "/dev/shm/uid-21748-gid-32786/39d5b5d2-seed-nspid4026531836-ns-4026531841/lib/python3.10/site-packages/prometheus_client/metrics.py", line 143, in __init__
    registry.register(self)
ValueError: Duplicated timeseries in CollectorRegistry: {'my_gauge'}
]

Notes

I think instantiating a metric should be idempotent. If you are passing the exact same metric name, docstring, labels, etc., you should get back the same metric object instance. This would imply having a factory function instead of directly exposing the metric classes. Maybe this behavior could be opt-in if enabling it by default is worrisome.

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

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

はじめの一歩

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

調査の方向性

bar.py、IPython autoreload を使用する Jupyter ノートブック、および prometheus_client.Gauge で問題を再現し、その後 duplicate CollectorRegistry の traceback を調べる。トップレベルのメトリクスを再読み込みする際の意図された動作を判断する。done は、bar.py の編集後にノートブックのセルを再実行しても、重複した時系列のエラーなしに完了することを意味する。

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

評価

技術スタック
python
領域
observability-sre
issue の種類
バグ
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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