代码之家  ›  专栏  ›  技术社区  ›  Alexander Rainchik

比较普罗米修斯的两个指标

  •  0
  • Alexander Rainchik  · 技术社区  · 7 年前

    我是普罗米修斯的新手,需要帮助。

    我在2台服务器上有自定义指标(仅显示应用程序的版本):

    app_version{plant="dev",env="demo"} 55.119
    app_version{plant="dev",env="live"} 55.211
    

    我想比较这些指标,如果它们不相等,我想发送警报,尝试smth如下:

    alert: Compare
    expr: app_version{env="demo"} != app_version{env="live"}
    for: 5s
    labels:
      severity: page
    annotations:
      summary: Compare
    

    此警报为绿色。 比较两个指标的正确方法是什么?

    enter image description here enter image description here

    2 回复  |  直到 7 年前
        1
  •  1
  •   brian-brazil    7 年前

    的不同值 env 标签意味着表达式的每一侧都没有要匹配的内容,因此它不返回任何内容,也没有警报。您可以使用 ignoring :

    app_version{env="demo"} != ignoring (env) app_version{env="live"}
    
        2
  •  0
  •   valyala    3 年前

    普罗米修斯表演 app_version{env="demo"} != app_version{env="live"} 按以下方式查询:

    1. 它选择与 app_version{env="demo"} .所有这些时间序列都有 app_version 和标签 env="demo" 根据 time series selector rules
    2. 它选择与 app_version{env="live"} .所有这些时间序列都有 app\U版本 和标签 env="live"
    3. 它在 != 具有相同标签集的操作员,根据 these rules
    4. 它将找到的时间序列对与 哦= 操作人员

    遗憾的是,查询中没有具有相同标签集的时间序列对 app\u版本{env=“demo”}!=app\u版本{env=“live”} ,因为左侧的所有时间序列 env=“演示” 标签,而右侧的所有时间序列 env=“现场” 标签这可以通过指示普罗米修斯忽略 env 根据查找匹配时间序列时的标签 these docs 。有以下选项:

    1. 要枚举标签,在搜索具有相同标签集的时间序列对时必须忽略这些标签,请通过 ignoring() 修改器:
    app_version{env="demo"} != ignoring(env) app_version{env="live"}
    

    此查询也可能返回空结果,因为 app\U版本 时间序列可能因其他标签而不同,例如 instance (参见 these docs 关于 例子 标签)。虽然可以添加 例子 标记为 忽略() 列表中,下一个选项可能会更好。

    1. 要枚举标签,在搜索具有相同标签集的时间序列对时必须考虑这些标签,请通过 on() 修改器。例如,以下查询只考虑 job 查找具有相同标签集的时间序列对时的标签:
    app_version{env="demo"} != on(job) app_version{env="live"}
    

    此查询可能导致 multiple matches for labels many-to-many matching not allowed 如果一方有多个时间序列,则会出现错误 哦= 操作员具有来自的相同标签集 on() 列表例如,如果普罗米修斯从多个目标中获取应用程序指标,那么 app\u版本{env=“live”} 可以匹配多个时间序列,这些时间序列是从不同的目标中提取的:

    app_version{env="live",job="my-app",instance="host1"}
    app_version{env="live",job="my-app",instance="host2"}
    

    在这种情况下 aggregate functions 可用于将多个时间序列转换为单个时间序列或一组时间序列。例如,以下查询比较 env=“演示” 至最低版本 env=“现场” 每个时间序列组具有相同的 工作 标签:

    max(app_version{env="demo"}) by (job)
      !=
    min(app_version{env="live"}) by (job)
    

    另一种选择是使用 group_left() group_right() 修饰符,指示在操作符的一侧匹配多个时间序列,例如 哦= 到操作员另一侧的单个时间序列。看见 these docs 有关详细信息。请注意 many-to-many Prometheus不支持时间序列匹配。

    P、 S.不需要的标签也可以用 label_del 函数if MetricsQL 已使用。例如:

    label_del(app_version{env="demo"}, "env") != label_del(app_version{env="live"}, "env")
    
    推荐文章