How to chain conditions with AND/OR

I have a question to see if i can set alert condition by chaining multiple value_name from below Prometheus data.

  • Data source: Prometheus
    Data format as below:
equipment_value{eqp_id="19986",value="18.11",value_datetime="2024-11-21T10:00:00Z",value_name="Amps A"} 18.11
equipment_value{eqp_id="19986",value="0.0",value_datetime="2024-11-21T10:00:00Z",value_name="Overspeed"} 0.0
equipment_value{eqp_id="19986",value="0.0",value_datetime="2024-11-21T10:00:00Z",value_name="Emergency Stop"} 0.0
equipment_value{eqp_id="19986",value="-1.0",value_datetime="2024-11-21T10:00:00Z",value_name="Power Factor"} -1.0
equipment_value{eqp_id="19986",value="58.91",value_datetime="2024-11-21T10:00:00Z",value_name="Frequency"} 58.91
equipment_value{eqp_id="19986",value="-0.14",value_datetime="2024-11-21T10:00:00Z",value_name="Oil Pressure"} -0.14
equipment_value{eqp_id="19986",value="3534.0",value_datetime="2024-11-21T10:00:00Z",value_name="RPM"} 3534.0
equipment_value{eqp_id="19986",value="0.0",value_datetime="2024-11-21T10:00:00Z",value_name="Coolant Temp"} 0.0

I was able to create individual alert (see screenshot below)

While this works, I want something else as below: Amps A = 0 and Emergency Stop = 1

From my readings, i’ve to use some transformers in Grafana, which i’m still unclear about. Appreciate if anyone can comment on this.
Thanks!

Hi,

So, you want the alert to fire if

equipment_value{eqp_id="19986",value="18.11",value_datetime="2024-11-21T10:00:00Z",value_name="Amps A"} 0

and (at the same time)

equipment_value{eqp_id="19986",value="0.0",value_datetime="2024-11-21T10:00:00Z",value_name="Emergency Stop"} 1

Right? If so, I can think of two ways you can do that:

1. Prometheus query

You should be able to achieve that in single prometheus query (I’m more familiar with Victoria Metrics, so the query might be a bit inaccurate) with query:

equipment_value{eqp_id="19986",value_name="Amps A"} == 0 and on(eqp_id) 
equipment_value{eqp_id="19986",value_name="Emergency Stop"} == 1

(I think that should work, but I’m not 100% sure).

With that (I’m not sure you can achieve that in Builder editor) you receive data from prometheus directly. You can use instant query (in query options) and threshold (without reduce). It’s less readable but operations are performed on prometheus, not grafana, so it’s more effective. Be aware that it might not produce data, so set the settings accordingly.

2. Within Grafana

You can create two queries:

equipment_value{eqp_id="19986",value_name="Amps A"}

and

equipment_value{eqp_id="19986",value_name="Emergency Stop"}

and then:

2.1 Use Classic Condition

like this:


(you can add AND and OR conditions with the plus button).

OR

2.2 Use math calculation

something like that:

BTW transformations work only in dashboard, they won’t work in alerts.

Hope that helps!

2 Likes

Thanks for the reply.

To confirm, you’ve created 2 queries.

  • Amps A
  • Emergency Stop
    And then, you’ve added 2 expressions
  • Reduce for Amps A
  • Reduce for Emergency Stop
    where Reduce will return the last value of both.

Finally, we write a combined math expression to check if both are above 1.

Thanks

Just to be clear:

  • math is also an expression
  • math checks if reduced (last) value of ApmsA is higher than 0 and if reduced (last) value of Emergency Stop is equal to 1
1 Like

Hi, thanks for the answer again.

The main question that we have now is something like this. Lets say i want to monitor my Fuel Level. In mind mind, below are the conditions when i should be getting alerts

  • 0-20: Very low fuel
  • 20-70: Medium fuel
  • 70-90: Good fuel
  • 90-100: Refueled

What would be an ideal way to set these conditions? I’m currently having three different alert conditions for these three conditions. But its quickly becoming un-maintainable to write N number of alerts for N number of conditions.

Do you have any suggestions?

Thanks

It can be hard to make, since Grafana doesn’t really support something like that - the threshold of the alert is somewhat constant. The question is - do you need to have an alert for Medium/Good fuel? In my usage, we do 2 alerts - one warning, one critical in such cases. You could try some promql logic that would set the label based on the query result but it would be troublesome, if possible at all.