Including calculated value from condition script in output

I have a watcher that looks for transactions with a specific status code in proxy traffic, and will alert if the percentage is above a threshold (eg if the number of 401 errors is above 5% of total, alert).

I use chained input to get the values for total traffic, and for the number of errors, and I use the following condition to work out whether to fire:

"condition": {
"script": {
"source": "return (ctx.payload.errors.hits.total * 100 / ctx.payload.traffic.hits.total) >=5",
"lang": "painless"
}
},

So this fires if the number of errors seen exceeds 5% of the total traffic.

What I want to do is include the actual error percentage in the email that gets triggered - so if the number of errors is say 8%, I include that figure in the email subject or body.

Can anyone tell me if I can include that value in the email somehow?

Hi @mdrowl1 -- You should be able to calculate this value in the watcher actions using transforms. These exist to process values from the payload for use in watcher actions.

Specifically for your case, you'll want to look into a script transform to handle the calculation of the error percentage, which you can then use in the email subject using mustache templates as shown in the example here.

Thanks Luke - I'll read up on the script transform and see if I can make it do what I need.

Hi @lukeelmers - I tried using transform on my alert, and I got it to work - that is, I added a transform and can calculate the percentage based on the two values I have, and get it in the email that is sent out. Here's the transform I added to the watcher:

  "transform": {
    "script": {
      "source": "return [ 'percent' : (ctx.payload.errors.hits.total * 100 / ctx.payload.traffic.hits.total) ]"
    }
  }

But the problem now is that by doing the transform, I lose those two values. Ideally what I'd like to do is get the two values from my data, calculate the percentage, and then include all three fields in my email.

Is there a way (using transform or something else) where I can keep the errors and traffic hits values while calculating the percentage, so I can include all three in my email?

I figured there must be a way to return multiple values, so I played around with it and got it to work:

    "transform": {
    "script": {
      "source": "return [ 'errors': ctx.payload.errors.hits.total, 'traffics': ctx.payload.traffic.hits.total, 'percent' : (ctx.payload.errors.hits.total*100/ctx.payload.traffic.hits.total) ]",
      "lang": "painless"
    }
  },

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.