简体   繁体   English

Prometheus 在不使用组语句的情况下在加入请求中添加额外的 label?

[英]Prometheus add extra label on join request without using group statement?

I am working with Prometheus.我正在与普罗米修斯合作。 I have the following metrics:我有以下指标:

Metric1指标 1

{"metric":{"namespace":"ns1", "instance_id":"someid", "label2":"somevalue", "label4":somethingelse"},"values":[[1675145655,"1"]]}

Metric2指标 2

{"metric":{"namespace":"ns1", "instance_id":"someid", "label3":"someothervalue"},"values":[[1675145655,"1"]]}

So both metrics have namespace and instance_id label, and then they have additional labels (label2, 3, 4).所以这两个指标都有命名空间和 instance_id label,然后它们有额外的标签(label2、3、4)。

I am doing a join request on them with the following request:我正在通过以下请求对他们进行加入请求:

(metric1{namespace="ns1"} == 1) * on (namespace, instance_id)  (metric2{namespace="ns1", label3="something"} == 1)

Now, this is 1:1 join, for a specific time, I should never have two metrics with the same instance_id, so no need to perform many_to_1 or many_to_many here.现在,这是 1:1 连接,在特定时间,我不应该有两个具有相同 instance_id 的指标,所以不需要在这里执行 many_to_1 或 many_to_many。

However, I wish to include in the result label present in metric1, but not metric2.但是,我希望在结果中包含 metric1 中存在的 label,而不是 metric2。 If I do the following:如果我执行以下操作:

(metric1{namespace="ns1"} == 1) * on (namespace, instance_id) group_left() (metric2{namespace="ns1", label3="something"} == 1)

It works well, my result will contain the extra label from metric1:它运作良好,我的结果将包含来自 metric1 的额外 label:

{"metric":{"label2":"somevalue", "label4":"somethingelse"},"values":[[1675145655,"1"]]} 

What I am trying to confirm is:我要确认的是:

  • From my understanding, group_left/right command is made for many:many.根据我的理解,group_left/right 命令是为 many:many 设计的。 I guess that since 1:1 is included in many:many, group_left/right also works for 1:1.我想由于 1:1 包含在 many:many 中,group_left/right 也适用于 1:1。 But, can I add extra label in a simple 1:1 request?但是,我可以在一个简单的 1:1 请求中添加额外的 label 吗? Or do I have to use the many:many join if I want to do so?或者我是否必须使用 many:many join 如果我想这样做?

-On top of that, when I read documentation , it states that -最重要的是,当我阅读文档时,它指出

Label lists can be provided to the group modifier which contain labels from the "one"-side to be included in the result metrics.

But here this the reverse I have, the label I get in my results are the one I did not put in the group_left.但这与我相反,我在结果中得到的 label 是我没有放在 group_left 中的那个。 So for example if I do this request instead:因此,例如,如果我改为执行此请求:

(metric1{namespace="ns1"} == 1) * on (namespace, instance_id) group_left(label2) (metric2{namespace="ns1", label3="something"} == 1)

Then the result give me metrics like this:然后结果给我这样的指标:

{"metric":{"label4":"somethingelse"},"values":[[1675145655,"1"]]} 

I have all extra label BUT the one I put my group_left statement.我有所有额外的 label 但我把我的 group_left 声明。 Is that expected?这是预期的吗? Am I mis reading the documentation here?我在这里误读了文档吗?

Thank you!谢谢你!

It is OK to use group_left() for 1:1 query if you need retaining all the labels from time series on the left side (or from the right side if group_right() is used).如果您需要保留左侧时间序列的所有标签(如果使用group_right() () 则保留右侧),可以使用group_left()进行1:1查询。 This doesn't introduce performance penalty.这不会引入性能损失。

If you put a list of labels inside group_left() , then these labels will be copied from time series on the right side additionally to all the labels from time series on the left side of the specified binary operator .如果将标签列表放在group_left()中,那么这些标签将从右侧的时间序列复制到指定二元运算符左侧的时间序列的所有标签中。

See more details in the official docs .官方文档中查看更多详细信息。

If you need retaining only some labels from the left side, then just drop unneeded labels after the calculations of q1 * on (...) group_left() q2 .如果您只需要保留左侧的一些标签,则只需在计算q1 * on (...) group_left() q2后删除不需要的标签。 Prometheus doesn't provide functions, which can drop unneeded labels from time series (or leave only the needed labels), but this functionality can be emulated with sum(...) without (...) or sum(...) by (...) in most cases. Prometheus 不提供可以从时间序列中删除不需要的标签(或只留下需要的标签)的功能,但是可以使用sum(...) without (...)sum(...) by (...)来模拟此功能在大多数情况下sum(...) by (...) For example, the following query leaves only (namespace, instance_id, label2) labels from time series matching metric1{namespace="ns1"} in the result:例如,以下查询仅在结果中保留时间序列匹配metric1{namespace="ns1"}(namespace, instance_id, label2)标签:

sum(
  (metric1{namespace="ns1"} == 1)
    * on (namespace, instance_id) group_left()
  (metric2{namespace="ns1", label3="something"} == 1)
) by (namespace, instance_id, label2)

Note that the sum() may return unexpected results if multiple time series have the same set of output labels specified inside by (...) .请注意,如果多个时间序列具有by (...)指定的同一组 output 标签,则sum()可能会返回意外结果。

PS Take a look also at label_del and label_keep functions provided by VictoriaMetrics - the Prometheus-like monitoring solution I work on. PS 另请查看 VictoriaMetrics 提供的label_dellabel_keep函数——我正在研究的类似 Prometheus 的监控解决方案。 These functions are easier to use instead of sum(...) by (...) or sum(...) without (...) trick when some labels need to be dropped / left in the result.当需要在结果中删除/保留某些标签时,这些函数比sum(...) by (...)sum(...) without (...)更容易使用。

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

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