prometheus / prometheus/client_python

Metrics declared at toplevel of program interfere with autoreload

未关闭
#1,042 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Python
星标
4.4k
派生
876
平均合并
8 天 4 小时
30 天内合并 PR
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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

使用 bar.py、采用 IPython autoreload 的 Jupyter notebook 以及 prometheus_client.Gauge 重现该问题,然后检查 duplicate CollectorRegistry 的 traceback。确定重新加载顶层 metric 时的预期行为;done 应表示编辑 bar.py 后重新运行 notebook 单元能够完成,且不会出现重复时间序列错误。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
observability-sre
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。