PromQL if then 语句等效 [英] PromQL if then statement equivalent

查看:201
本文介绍了PromQL if then 语句等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 PromQL 查询来执行计数:

I have a simple PromQL query that performs a count:

sum(up{container_name="my-container",environment_name="$env"})

这是 Grafana 仪表板的一部分,允许从下拉菜单中选择 ${env}.

This is part of a Grafana Dashboard and allows for ${env} to be selected from a drop down menu.

我想根据环境执行不同的查询.

I would like to perform different queries depending on the environment.

我如何在 PromQL 中构建这样的东西:

How do I construct something like this in PromQL:

if ${env} == 'dev' or ${env} == 'integration':
  if sum(up{container_name="my-container",environment_name="$env"}) == 1:
    sum(up{container_name="my-container",environment_name="$env"}) + 1
  else:
    sum(up{container_name="my-container",environment_name="$env"})
else:
  sum(up{container_name="my-container",environment_name="$env"})

目的是在运行单个容器的环境中错误地增加计数以触发 RAG 状态面板中的健康阈值.

The purpose is to falsely inflate the count to trigger a healthy threshold in a RAG status panel in environments that are running single containers.

推荐答案

你可以这样查询:

(
   sum(up{container_name="my-container", environment_name=~"dev|integration", environment_name="$env"}) + 1 
   AND
   (sum(up{container_name="my-container", environment_name=~"dev|integration", environment_name="$env"} == 1)
)
OR
sum(up{container_name="my-container", environment_name=~"dev|integration", environment_name="$env"}) != 1
OR
sum(up{container_name="my-container", environment_name="prod", environment_name="$env"})
OR
on() vector(0)

当实例数量为 1(这就是 所做的)并且用户选择了开发或集成环境时,这将返回增加 1 的实例数量.or 将同时包含三个指标,但由于查询和用户选择,只能返回一个指标.

this will return the amount of instances increased by one when the amount of instances is 1 (thats what the and does) and the user selected the dev or integration environment. The or would al three metrics at once, but due to the query and the users selection, there can be only one metric that is returned.

这篇关于PromQL if then 语句等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆