Integrating Azure Functions and Azure App Insights for Email Alerts

Integrating Azure Functions and Azure App Insights for Email Alerts

This article is a continuation to the previous article, Azure Functions: Trigger, Bindings and Blobs Processing . In the previous article, a use case on automation of invoice processing in a manufacturing company based on three categories such as Sales, Purchase and Travel is achieved using Azure Functions.

In this article, the focus is to utilize Azure Application Insights service to send out an alert notification to all the stakeholders whenever the Azure Functions for invoice processing is failed to process the incoming invoices.

Prerequisites

  • An Active Azure Subscription.
  • Azure Functions – A running instance of Azure Functions is required for integrating with Application Insights for Email alerts. Please refer the previous article on creating Azure Functions and configuring Function App to run every time when a new blob is created in the configured storage account.

Architecture

Diagram 1.1, features the architecture design for invoice processing. In this architecture, Application Insights service is introduced to monitor Function App logs and to setup alerts based on the queries configured.

1.JPG Diagram 1.1. Architecture for Blob Processing

  • Blob Storage Trigger (direction: in): The Blob storage trigger starts a function when a new or updated blob is detected.

  • Input Binding (direction: in): The contents of the blob are provided as input to the functions.

  • Output Binding (direction: out): Creates new blob and copy the contents of bob received from input binding.

  • Azure Functions:Process blobs based on the invoice file name as shown below,

  • Sales Invoice: PY-SALINV-<Batch.No>-<Date>-<Invoice.No>

  • Purchase Invoice: PY-PURINV-<Batch.No>-<Date>-<Invoice.No>
  • Travel Invoice: PY-TRAINV-<Batch.No>-<Date>-<Invoice.No>

  • Application Insights: Collects Azure Function logs and Alert rule is triggered to send an email notification to the stakeholders, if an error occurred while processing the blobs.

Flow Diagram to Process Invoices and Setting Up Alert Mechanism

2.JPG

Enabling App Insights for Azure Functions

To monitor Azure Function logs, it is needed to enable Application Insights in the respective Function App instance.

Enable Application Insights while creating Function App instance:

While creating Function App, under Monitoring section users can enable Application Insights and select an existing application insights instance or create new instance.

3.png

Enable Application Insights in the existing Function App:

This section describes the steps to integrate existing Function App with newly created Application Insights service instance.

Step1: Create Application Insights service from portal

Step 1.1: Search for Application Insights service from search window

4.png

Step 1.2: Click on Create

5.png

Step1.3: In the Application Insights creation pane, provide the necessary data as below

  • Subscription
  • Resource Group: Create new resource group or select from existing resource group.
  • Region: Location in which the Azure Function App instance is created.
  • Resource Mode: Classic/Workspace-based. The Workspace-based resource mode lets users to send the telemetry data collected at App Insights service to Log Analytics.

Click on Review + Create to start the instance creation.

6.png

Once the deployment is successful, navigate to Application Insights resource and the newly created instance is up and running

7.png

Step2: Integrate newly created App Insights to the existing Azure Function instance

Step2.1: Navigate to the existing function app and select Application Insights section

8.png

Step 2.2: In Application Insights section, click on Enable Application Insights button, once it is selected two options will be displayed.

Option1: To create a new Application Insights from Function App and integrate. Option2: Integrate Function App with existing Application Insights service.

Since new Application Insights service has already been created in Step1, Option2 is selected in this article.

9.png

10.png

11.png

Click on Apply to complete the integration. Once the integration is completed, the Application Insights sections displays details of App Insights service to which the Function App is linked.

12.png

Users can also observe a new item APPINSIGHTS_INSTRUMENTATIONKEY in Configuration section has been added automatically.

Configure Azure Application Insight Alert for Email Notification

This section provides the steps to configure Application Insights service to send out an Email alerts based on the query applied.

Step1: Navigate to the Application Insights resource which is linked to the Function App demolabs.

Step2: In Application Insights, click on Alerts and click on New Alert Rule to create an alert rule for sending email notifications

13.png

Step3: Create alert rule

Step3.1: Configure condition to which alert should get triggered. This can achieved by selecting signal and defining its logic

  • Signal Type: Log
  • Signal Name: Custom Log search (To define custom trigger logic)

14.png

In Custom log search, add Search query to be executed against the Function App logs.

Query

traces
| where message contains "Incorrect filename. Please check the filename adhered to standards"

The above query will fetch the logs containing the message content as Incorrect filename. Please check the filename adhered to standards. From the demolabs function app, this log appears only when the invoice with improper filename is uploaded to Azure Storage account container.

15.png

When the result of the above query is greater than 0, then an alert has to be triggered. To configure this, Alert logic and polling time is used.

16.png

Click Done once all the above data are provided, to complete the creation of Condition.

Step3.2: Once Condition is configured, now an Action needs to created. This basically instructs Azure to perform an action (send notification or invoke actions) when the alert rule gets triggered.

In Actions section, click on Add Action groups and select Create action group button

In Basics section, provide necessary details

  • Subscription
  • Resource Group
  • Action Group Name
  • Display Name

Select Notification Type as Email/SMS message/Push/Voice from notification section and provide the email id to which notifications needs to be forwarded.

17.png

Once provided the necessary information, click on Review + Create button to complete the creation of Action.

Step3.3: If desired, customization can also be applied to the alert rule. Here enabling Email Subject customization to send notification with custom subject name.

18.png

Step 3.4: Provide alert rule details and click on Create alert rule button to complete the creation

The newly created alert rules can be found in Manage Alert rule section

19.png

Testing Alert Mechanism

In this section, Alert mechanism is tested by uploading a new invoice file with improper file name. The intention here is to receive an email notification from Azure to all the configured stakeholder’s email ID.

Refer the article Azure Functions: Trigger, Bindings and Blobs Processing , to understand the complete functionality of the demolabs function app.

Step1: In Azure Storage Account’s invoice-bucket container, uploading an invoice with incorrect file name.

20.png

Step2: Backend Operation

Once the upload is completed, function app demolabs will gets triggered. As per the flow diagram, the function app will check for the invoice file name format and here the format of the uploaded invoice name will not match with none of the known categories (Sales/Purchase/Travel).

So, the function app will log a warning message as Incorrect filename. Please check the filename adhered to standards.

Step3: The Application Insights integrated with function app, continuously monitors its logs. Based on the polling time and alert logic configured, the alert rule gets triggered as the function app logs the message which matched the query configured.

Step4: Once the alert rule triggered, action group will perform the configured operation. In this case, a new email notification will be sent to all the provided email IDs

Received an email from Azure for Alert: Incorrect Filename to the configured email ID

21.png

References