Search code examples
azureazure-functionskqlazure-monitoringazure-alerts

Alert if Azure function timertrigger not triggered on particular date and time


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.


Solution

  • 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.