prometheus / prometheus/client_python

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

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

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

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

説明

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.

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

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

はじめの一歩

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

調査の方向性

prometheus_client/metrics_core.py の372行目付近にある、リンク先の InfoMetricFamily.add_metric() の実装から始め、次にそのパラメーター処理を Info と比較します。labels と value に対して想定されるデフォルト値と互換性への影響を確認します。合意された API 変更が既存の呼び出し元を壊すことなく実装され、その動作がテストでカバーされれば完了です。

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

評価

技術スタック
python
領域
observability-sre
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

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

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