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

`InfoMetricFamily`’s `add_metric()` parameters are strangely named and should provide default values

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

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
35/100
Issue 類型
功能
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
python

研究方向

從 prometheus_client/metrics_core.py 第 372 行附近連結的 InfoMetricFamily.add_metric() 實作開始,然後將其參數處理與 Info 進行比較。確認 labels 和 value 的預期預設值及相容性影響;完成的標準是,在不破壞現有呼叫端的情況下實作已達成共識的 API 變更,並透過測試涵蓋其行為。

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

描述

Hey.

I was looking into InfoMetricFamily’s add_metric() and I either I just don't get it or the API is a bit strange.

With an Info object I'd have done something like this:

m = prometheus_client.Info("logical_drive", "foobar", ("controller_name",
                                                       "array_name",
                                                       "logical_drive_name",
                                                       "caching",
                                                       "device",
                                                       "raid_level",
                                                       "logical_drive_label",
                                                       "multidomain_status",
                                                       "parity_initialization_status",
                                                       "status"
                                                      )
                      )

and then set it like:

m.labels(controller_name=controller_name, array_name=array_name, logical_drive_name=logical_drive_name,
         **{n: logical_drive_properties.get(n, "")  for n in (
                                                              "device",
                                                              "raid_level",
                                                              "logical_drive_label",
                                                              "multidomain_status",
                                                              "parity_initialization_status",
                                                              "status"
                                                             )
           },
         caching=bool_or_none_to_label_value( logical_drive_properties.get("caching") )
        )

(here an example where I set some of the properties directly, and some via dict unpacking)

  • It would fail if I forgot a label.
  • I could use label names as attribute names, like controller_name= rather than "controller_name"=... which is however not super important

With InfoMetricFamily seem quite a bit different:

  • add_metric() now has the parameters labels and value.
  • As far as I understand the code, labels are actually not labels, but values for these, namely the ones set with labels in the constructor of InfoMetricFamily.
  • It's no longer possible to give labels as dict, one really needs to give them in the right order as sequence. Why does Info allow that but not this?
  • AFAICS, there is no check if exactly those labels are set, that were given in the constructor. Why over at Info but not here? What sense does it then even make to set the labels in the constructor?
  • value is misleading in so many ways. It's not the value of the metric (that is 1) and even if it relates to the labels being the “value”, then it should be values.
  • It might have perhaps even been better to swap the two names... or rater use some completely different names.

Anyway... guess this can't be changed now without breaking the ABI.

However, as far as I understand the code:
https://github.com/prometheus/client_python/blob/09a5ae30602a7a81f6174dae4ba08b93ee7feed2/prometheus_client/metrics_core.py#L372

the idea is one can give labels and/or value and both are merged in a final dict of label/value pairs.... but then it would be nice if labels and value were not required.

Why not simply give them default values () respectively {}?

I could provide a patch if nothing speaks against that particular change. But the above points are IMO still problematic. Especially also that the behaviour is considerable different from Info, which is not really obvious.

Cheers,
Chris.

主要語言
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 摘要。