如何区分同名的 Prometheus 指标与 Kubernetes 中动态发现的微服务

How to differentiate between equally-named Prometheus metrics from dynamically discovered micro-services in Kubernetes

我正在寻找一种方法来区分从 Kubernetes 集群中的不同动态发现服务 运行 收集的 Prometheus 指标(我们正在使用 https://github.com/coreos/prometheus-operator)。例如。对于写入数据库的指标,我想了解它们实际上来自哪个服务。 我想您可以通过相应服务中的标签来执行此操作,但是,swagger-stats (http://swaggerstats.io/) which we’re using does not yet offer this functionality (to enhance this, there is an issue open: https://github.com/slanatech/swagger-stats/issues/50)。 有没有办法在普罗米修斯本身上实现这个,例如Prometheus 在抓取后为每个时间序列添加一个特定于服务的标签?

感谢您的反馈!

Is there a way to implement this over Prometheus itself, e.g. that Prometheus adds a service-specific label per time series after a scrape?

这就是 Prometheus 的设计用途,因为目标不知道监控系统如何查看它,加上前缀指标名称会使跨服务分析变得更加困难。在整个目标上设置标签和为指标名称添加前缀都被视为反模式。

你想要的是所谓的目标标签,这些通常来自服务发现中应用于元数据的重新标记。

使用 Prometheus Operator 时,您可以指定 targetLabels 作为要从 Kubernetes 服务复制到 Prometheus 目标的标签列表。