4

I have a azure timmertrigger function that runs at 12:05 every 26th of the month to run batch jobs. I want to setup alert if the timmertrigger is not triggered on this time and date. The function is triggered with cron expression and its triggering as expected, incase if it is not trigger we have to manually go and check if it is triggered from logs.

My first guess is to write custom log alerts based on requests

requests | where cloud_RoleName contains FUNCTION_APP_NAME_HERE

But i am not sure if i can scan the logs on this given time and date. We have already configured alerts if there is error on batch job once the timmertrigger is triggered.

Any suggestion would be welcome.

Peter Bons
  • 26,826
  • 4
  • 50
  • 74
Tilak Raj
  • 135
  • 2
  • 6

1 Answers1

3

One solution would be to write a query that shows all requests during the given time range and create an alert once there are 0 results. Then you know that the trigger hasn't fired (on time).

There is a catch: outside the specific timeframe the number of results will also be 0 so we will use a trick

Something like this will do:

let inTimeFrame = iif(
    datetime_part("day", now()) == 26 and datetime_part("hour", now()) between (12 .. 13),
    0,
    1);
let resultCount = toscalar(requests
| where cloud_RoleName contains FUNCTION_APP_NAME_HERE
| where datetime_part("day", timestamp) == 26
| where datetime_part("hour", timestamp) between (12 .. 13)
| summarize count());
print inTimeFrame + resultCount

First, we create a variable that is either 0 or 1. It will be 0 when it is inside the timeframe of 12:00-13:00 every 26th of the month. Otherwise it will have the value 1.

Then, we count the number of results during the timeframe. Finally we add the number of results to the value of the variable.

  • When there are no results in during the timeframe the final result will be 0 + 0 = 0
  • When there are results in during the timeframe the final result will be 0 + 1 = 1
  • When there are no results outside the timeframe the final result will be 1 + 0 = 1.

Now all you need to do is create an alert that will trigger when the value of the custom search is 0:

enter image description here

Do mind that afaik a timer trigger doesn't result in a request being logged to App Insights so you may want to use a different table to query on but hopefully you get the idea.

Also, do mind that the current query uses UTC time.

Peter Bons
  • 26,826
  • 4
  • 50
  • 74
  • Peter , I will try this approach , might be sufficient in my case. – Tilak Raj Mar 18 '22 at 12:51
  • Peter , I have tried similar log search criteria which is perfect but the problem lies with alerts where we have period and frequency options. I have set period as 10 and frequency to be 5 , so in my case the alerts will be triggered every 5 mins as the requests will always be 0 until 26th of the month. – Tilak Raj Mar 18 '22 at 16:21
  • @TilakRaj I see, haven't thought that out. I've updated my solution to fix this. – Peter Bons Mar 18 '22 at 18:58
  • @TilakRaj feel free to [accept any answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if it was of any help to you. If it isn't, feel free to ask for details. – Peter Bons May 10 '22 at 09:37