prometheus / prometheus/client_python

Add public, read-only access to label names, label values of any metric

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

还没有人认领这个 Issue。

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

描述

Hi,

I am currently working on Prometheus metrics for Celery workers in Flower project and we use this wonderful library :) - thx for your work on it!

One issue I came across is lack of direct access to metric's labelnames and labelvalues.

I think easiest is to give you an example how it is used/why it is needed.

We have multiple celery workers in a kubernetes cluster, they are monitored by Flower which generates Prometheus metrics.
Now the pods in k8s can die or be replaced if a new release of our software is deployed.

The problem with that then is that the final reading for each metric for that pod is retained forever and it shows in grafana forever.

We want to remove any tuples of label values which contain a celery worker name that is deemed offline/dead.

I used a semi-hack (https://github.com/mher/flower/pull/1135) and get those label values through metric.collect(), and from that iterate over samples and the labels in them and then if they contain an offline worker I remove the whole tuple from a given metric by calling metric.remove(*labelvalues_containg_dead_worker)

There could be a better way - access metric._metric.keys() in a thread safe and read-only way.

So I propose to add to MetricWrapperBase a read only property, say all_label_values (name to be improved, suggestions welcome :))

@property
def all_label_values(self) -> List[Tuple[str, ...]]:
    with self._lock:
        all_label_values = list(self._metric.keys().copy())

   return all_label_values

I think I am also badly missing access to actual labelnames set for the given metric to know at which position a label with a given name will be in the labelvalues.

Could kill 2 birds with one stone then and add:

@property
def labelnames(self) -> Tuple[str, ...]:
    return self._labelnames

Please let me know if such a change is acceptable and if I am missing any nuances.

If you think you would be ok merging it I will make a PR with tests soon.

Cheers,

Tom

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 MetricWrapperBase 开始,检查 issue 中描述的现有 _metric、_labelnames、_lock、collect() 和 remove() 行为。定义对标签名称和标签值元组的公共只读访问,然后添加测试以确认公开的值和线程安全的读取行为。

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

评估

技术栈
prometheus, python
领域
observability
Issue 类型
功能
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

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