8

I have 2 metrics and try to find the difference of average value between them in percentage like 100*(m1+m2)/m1 but this obviously produces NaN if m1 turns to zero.

How should I handle this case if I don't want to alert when the metrics turn to zero?

timurb
  • 5,405
  • 2
  • 22
  • 17

1 Answers1

4

With bools bosun has a short-circuit like behavior. Since Bosun's expression language lacks if statements, you need to use a bool operation to see if the divisor is 0 first:

$foo = 0
$foo && 1/$foo

Since $foo is zero, the statement is "not true" so 1/$foo is not factored into the final calculation:

enter image description here

Kyle Brandt
  • 26,938
  • 37
  • 124
  • 165
  • Thanks! Works for me. I've never thought of using expression editor in that sense. It should really help to try playing with expression in it before actually trying them as rules. – timurb Nov 23 '15 at 06:40
  • @timurb: yea Craig added that about a month ago, nice to do variables earlier in the workflow – Kyle Brandt Nov 23 '15 at 13:43
  • 3
    If you use this approach then you lose the 1/$foo value when $foo is not 0: $foo = 5 $foo && 1/$foo result: 1 (i.e. true) what if you want to have 1/$foo if $foo != 0 but 0 if $foo == 0? – andresp Feb 02 '18 at 14:12