Monitor application via Application Insights and send alert: Part 1 Set Rule and Action in Unified Alert
Tags: applicationinsights, monitor, operation
Published: 2020-02-27 05:08:53

---
title: Monitor application via Application Insights and send alert: Part 1 Set Rule and Action in Unified Alert
published: true
description: Monitor application via Application Insights
tags: ApplicationInsights,Monitor,Operation
---

I always use Application Insights to monitor my Web applications.

- I can simply enable integration by clicking for Azure Resources such as Web Apps and Azure Functions.
- For my own application, I can simply include SDK and add several lines of code.

If you are interested in how its SDK works, please take a look at my [Application Insights SDK for Node.js series](https://dev.to/kenakamu/application-insights-sdk-for-node-js-3ki1).

In this article, I look into how I can get notification when **"Something went wrong"**.

# What does Application Insights monitor?

According to [overview](https://docs.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview) page, it monitors followings:

- **Request rates, response times, and failure rates** - Find out which pages are most popular, at what times of day, and where your users are. See which pages perform best. If your response times and failure rates go high when there are more requests, then perhaps you have a resourcing problem.
- **Dependency rates, response times, and failure rates** - Find out whether external services are slowing you down.
- **Exceptions** - Analyze the aggregated statistics, or pick specific instances and drill into the stack trace and related requests. Both server and browser exceptions are reported.
- **Page views and load performance** - reported by your users' browsers.
AJAX calls from web pages - rates, response times, and failure rates.
User and session counts.
- **Performance counters** from your Windows or Linux server machines, such as CPU, memory, and network usage.
Host diagnostics from Docker or Azure.
- **Diagnostic trace logs from your app** - so that you can correlate trace events with requests.
- **Custom events and metrics** that you write yourself in the client or server code, to track business events such as items sold or games won.

It sends all logs and telemetry data to central database, which I can see them in many ways.

- Application Map
- Kusto query
- Profiler
- Dashboard
- etc.

But I am not monitoring these monitors all the time, that's why I need Application Insights to send me alerts whenever something went wrong.

# Azure Monitor Alert

Application Insights is part of [Azure Monitor](https://docs.microsoft.com/en-us/azure/azure-monitor/overview), and it utilize the alert capabilities of Azure Monitor. This is great as I can unify the alert for both Application Insights and Log Analytics.

At the moment, there are two types of alerts.

- Classic Alerts: This is the alerts when Application Insights and Log Analytics alerts are separated.
- Unified Alerts: This is the latest way which I introduce in this article.

See [Alert Overview](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/alerts-overview) for more detail.

# Setup Alert

Because the official page has very detailed information, I just go ahead to configure one simple alert based on my Application Insights results.

1\. Go to Application Insight resource and click "Alerts" in "Monitoring" section.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/huvne4uhngxuhjycgdu0.jpg)

2\. Click "+ New alert rule". 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ay4sgmgtx80otgueaybl.jpg)

3\. Make sure "Resource" has your application insights with correct hierarchy. If not, click "Select" to change it. Then click "Add" in "CONDITION".

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2sojcfev0id5lh66foh0.jpg)

4\. Here I can select which "signal" I want to alert against. I selected "Page view" here. Once I select the signal, it shows options how to monitor the signal. This varies depending on which signal I selected.

One important feature here is "Dynamic" monitoring, which uses machine learning (ML) and judge the difference learnt from historical data. See [Metric Alert with Dynamic Thresholds in Azure Monitor](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/alerts-dynamic-thresholds) for more detail. 

I set very low threshold to test the alert. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/p3abob355u2kqb9bwee3.jpg)

5\. Add another condition if you need, but keep in mind that when you add multiple conditions in a rule, they becomes "and" condition. I won't add additional condition here. Click "Create" under "ACTIONS GROUPS".

As "Condition" and "Action" are loosely coupled, you can add existing rule as well if you have any.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h69t36fqjvuzu70223pn.jpg)

6\. Create action by filling the form. I have so many options for action, from simply email/SMS to Logic App/Function App or other system integrations. I try email notification for now.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/t9bz3i49lbrc3p41617a.jpg)

7\. Once you created the rule, then click "Add" to add the created rule.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i8al9wmsfrr9xu4u6nqx.jpg)

8\. Finally add name, description and severity. Hit create to save it.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/km8523rvit36l0d0qmyo.jpg)

9\. Click "Manage alert rules" to see the created alert. Or I click "Manage actions" to see actions. I can see the rule I just created and one out of box alert.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/irfjzpk2wyop3qdjyl3e.jpg)

# When alert fires

It's time to fire the alert. Simply access to my web page several times and wait for up to 5 minutes as that's the interval I set.

Once the alert is fired, I can see the log in Azure Portal as well as in my mail box.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eto6wsu70b30nq51l4ah.jpg)

Then when I click the graph, I can see the alert is "Fired".

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hmz8e3n9x5ax9pfsnllt.jpg)

When I wait another 5 minutes without touching the site, then alert is considered to be "Resolved" as the threshold is not met, but alert status remains as "new".

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1g9187jeoxh9tfurpzmu.jpg)

### Monitor Condition and Alert Status
One thing to note is that "Monitor Condition" and "Alert Status" are totally separate. The monitor condition automatically becomes "Resolved" state once it stop exceeding threshold. On the other hand, "Alert State" is something I have to manually solve.

1\. Open the alert and click "Change alert state".

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qzd2o3k6fj1lerw8vf84.jpg)

2\. Change the status to appropriate one and put comment.
 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uz4noec21uhvg37bs42b.jpg)

3\. Alert state will be updated.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2ir09mntqfdcdxwrxcqv.jpg)

I can also change the state for multiple alerts at once. See [Manage alert and smart group states](https://docs.microsoft.com/en-us/azure/azure-monitor/platform/alerts-managing-alert-states) for more detail.

# Summary

In this article, I explain the very basics of alert, which applies not only for Application Insights but also Log Analytics. In the next article, I will explain more advanced alerting.

[Next article](https://dev.to/kenakamu/monitor-application-via-application-insights-part-2-understand-telemetry-types-and-use-advanced-monitoring-2h20)