What Are Labels?

标签是与时间序列关联的键值对,除了度量标准名称之外,它还有唯一地标识来识别它们。 这有点拗口,让我们来看一个例子。 如果您有按路径划分的HTTP请求的度量标准,则可以尝试将该路径放在度量标准名称中,例如在Graphite中常见的: http_requests_login_total http_requests_logout_total http_requests_adduser_total http_requests_comment_total http_requests_view_total

这回导致你很难在PromQL中使用它。 为了计算总请求,您需要知道每个可能的HTTP的metrics的名字,或者在所有度量标准名称中进行某种形式的潜在昂贵匹配。 因此,这是一个你应该避免的反模式。 相反,为了处理这个常见用例,prometheus有标签。 在上述情况下,您可以使用路径标签 http_requests_total{path="/login"} http_requests_total{path="/logout"} http_requests_total{path="/adduser"} http_requests_total{path="/comment"} http_requests_total{path="/view"}

然后,您可以使用http_requests_total指标及其所有路径标签作为一个标签。 使用PromQL即可获得总体聚合请求比率,例如一个路径的速率,或每个请求的整体比例都可以利用标签统计出来。

您还可以拥有包含多个标签的指标。 标签上没有排序,因此您可以按任何给定标签进行聚合,同时忽略其他标签,甚至可以同时按几个标签聚合。

Last updated

Was this helpful?