简体   繁体   English

如何在 Prometheus (k8s) 中动态定义指标

[英]How to dynamically define metrics in Prometheus (k8s)

I am writing an exporter and am having trouble with metrics for my collector.我正在编写一个导出器,并且在收集器的指标方面遇到问题。 From what I can tell, metrics in Prometheus need to be defined beforehand.据我所知,Prometheus 中的指标需要事先定义。 Is there any way to define them dynamically at runtime instead?有没有办法在运行时动态定义它们? I won't know how many metrics I'll have or what metrics I'll need until the code is running.在代码运行之前,我不知道我将拥有多少指标或需要哪些指标。 (For example, if k8s finds all volumes connected to the cluster, and I need EACH volume to get its own total_capacity/available_capacity metric). (例如,如果 k8s 找到连接到集群的所有卷,并且我需要每个卷来获取其自己的 total_capacity/available_capacity 指标)。

You would handle this by using dynamic label values rather than dynamic metric names.您可以使用动态 label 值而不是动态指标名称来处理此问题。 That said, you can call prometheus.Register() at runtime just fine, client_golang won't know the difference.也就是说,您可以在运行时调用prometheus.Register()就好了,client_golang 不会知道区别。

I have only used Python and Java Prometheus clients so far, but isn't it just a recommendation to define metrics beforehand?到目前为止,我只使用过 Python 和 Java Prometheus 客户端,但这不只是建议预先定义指标吗? In Java I can create the metrics at run-time inside methods or wherever I want.在 Java 中,我可以在运行时在方法内部或任何我想要的地方创建指标。 I just have to make sure that the same metric is not created again or an exception will be thrown by the library.我只需要确保不会再次创建相同的指标,否则库会抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM