DEV Community

Kenichiro Nakamura
Kenichiro Nakamura

Posted on

Monitor application via Application Insights and send alert: Part 1 Set Rule and Action in Unified Alert

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.

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

What does Application Insights monitor?

According to 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, 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 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

2. Click "+ New alert rule".

Alt Text

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

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 for more detail.

I set very low threshold to test the alert.

Alt Text

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

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

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

Alt Text

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

Alt Text

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

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

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

Alt Text

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

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

2. Change the status to appropriate one and put comment.

Alt Text

3. Alert state will be updated.

Alt Text

I can also change the state for multiple alerts at once. See Manage alert and smart group 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

Top comments (0)