prometheus / prometheus/client_python

write_to_textfile: tmp file is left behind if generator code fails

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

还没有人认领这个 Issue。

enhancement
主要语言
Python
星标
4.4k
派生
876
平均合并
8 天 4 小时
30 天内合并 PR
1

描述

In the write_to_textfile function, the code to run the collector registry is in the temp file creation block: https://github.com/prometheus/client_python/blob/master/prometheus_client/exposition.py#L202-L203

In my case, I had an error in my collector code (run by generate_latest(registry)). This resulted in many tmp files left over from the errors.

I'm wondering if it would be preferable to run the code before the temp file block, e.g.:

    tmppath = '%s.%s.%s' % (path, os.getpid(), threading.current_thread().ident)
    out = generate_latest(registry)
    with open(tmppath, 'wb') as f:
        f.write(out)
    # rename(2) is atomic.
    os.rename(tmppath, path)

Unless leaving temp files behind is the desired/intended behavior.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 prometheus_client/exposition.py 中 write_to_textfile 的实现开始,查看第 202-203 行附近的位置;在创建临时文件期间会运行 generate_latest(registry)。跟踪 collector 代码引发异常时的失败路径,并验证最终行为不会留下临时文件,同时保留原子重命名流程。

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

评估

技术栈
python
领域
backend
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
45/100

把新 issue 发到你的邮箱

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