When we create any web application, our main concern is that the desired functionality, should just simply “work”!
Over the time, after that application is deployed and running, we realize that although the functionality works, it also behaves erratically at times. At these times, we become aware of issues related to application availability, performance, usability etc. The application simply does not meet the expectations of the users in these critical areas.
Although the application is alive and kicking, it is not “healthy”. As a DevOps team, we are responsible for making it healthy.
Usually in a live setup, we do not dare to go back and add some code to check how healthy the application is, and where is it failing. This is because we fear introducing regressive bugs in the code.
However Azure Application Insights gives us a way to measure the metrices related to availability, performance, analytics, exceptions without changing anything in the code. It also provides much more information about the health of the deployed application. Off course, Azure Application Insights may also be available if you insert some code in the form of Application Insights SDK, but that is possible only if you are allowed to change the code of the deployed application.
In this tutorial, I am going to show you how we can use Azure Application Insights to monitor the health of the application without changing code of the deployed web application .
Enable Application Insights on a Web Application hosted on Azure App Service
Application Insights is a service offered by Azure which can target any type of application that is created using any technology.
Combinations like .NET – MVC Web application, Node – Angular – SPA, Java – Web APIs and many more like these are fair enough targets of Application Insights monitoring.
These applications may be based on PaaS offered by cloud, like Web App Service on Azure or they may be applications installed on your premises. In this tutorial, I am going to restrict my examples to an ASP.NET application that is deployed as a web application on Azure Web App Service and the same application deployed on an IIS hosted on a Virtual Machine On-Premise. In both the cases, application is already deployed and running. In this article, we will not be concerned about how it is built and deployed.
1. Web application deployed to Azure App Service
The first observation that we make is in both the cases, Application Insights is not already configured for our application. We start with the case of Web Application as Azure Web App Service. When we open the Overview page, we can view its URL and browse to it, if needed.
2. Application Insights tab of the web application app service
Scroll down on the same page to see the left pane that shows various available services and option. Find the option of Application Insights and click on that. It shows a page where we find a button to “Turn On Application Insights”.
3. Confirmation to create a new Application Insights resource
When we click that button, it starts the wizard that confirms if we want to create a new Application Insights resource.
4. Level of Application Insights data collection
It will also collect information regarding what is the level of data to be collected for each type of application.
5. Application Insights App Settings for web app service
The wizard now creates an Application Insights resource for us in the same resource group as the web app and links it with our App Service. Now we can view the data collected by Application Insights.
If we look at the Application Settings of the App Service, we find that number of settings related to Application Insights are now added.
This way, we have enabled the Application Insights for a web application that is deployed on Azure App Services.
Enabling Application insights for web application deployed on an IIS hosted on a VM on premises or in Azure
This is little more complex than enabling it on an Azure hosted web application. It requires an entity called Application Insights Status Monitor V2 to be installed on that VM, that will collect the telemetry data and send it to Application Insights. This component has a limitation at the moment – it can collect telemetry data only from application that uses ASP.NET Application (.NET Framework) and is hosted on IIS.
In this scenario, first of all, we need to manually create an Application Insights resource. We will do that in the same resource group where other resources are existing.
We will now install Application Insights Status Monitor V2 on the VM that hosts our ASP.NET Application. Application Insights Status Monitor V2 allows us to gather telemetry data from the applications on the VMs in Azure, or from On-Premises without making any change in the code. Although Application Insights Agent superseded the Application Insights Status Monitor V2; in my experience, Application Insights Agent is not yet mature enough and does not work as intended. Until it becomes mature enough, I suggest continued use of Application Insights Status Monitor V2 for gathering telemetry data.
Install Application Insights Status Monitor V2 using Web Platform Installer
Application Insights Status Monitor V2 can be installed in multiple ways. I found that installing it as part of Web Platform Installer is the easiest.
Configure Application Insights Status Monitor V2 using Web Platform Installer
Once it is installed, we need to configure it on IIS by providing details like our Azure Subscription, Resource Group name and the Azure Application Insight resource that we have created earlier. We need to selectively enable it for various web applications that we require to monitor.
It is my experience that if the application already contains the Application Insights SDK embedded in it, then Application Insights Status Monitor V2 does not collect telemetry data and allows Application Insights SDK to collect it to avoid duplicate collection of telemetry data.
To check that Application Insights Status Monitor V2 is sending data to Application Insights resource we had created earlier, we can open that resource in Azure Portal and open the Live Metrics Streaming panel. If it shows the data coming in from the application, then Application Insights has got correctly configured.
Monitoring Health of an Application – Parameters Required
Now that the Application Insights is configured and is collecting the data from our application, let us create a list of desirable parameters with which we can monitor the health of the application.
1. Availability – We would like to know the percentage of availability of the application and would like to be notified if and when the application becomes unavailable due to some reason.
2. Performance – We want the application performance to be always at a certain level. That includes the page response time and server execution time.
3. Quality Assurance – We are interested in knowing if any pages are not found (404 error) or if any unhandled exceptions are thrown by the application (500 errors).
Let us now check the results of the telemetry data collected by Application Insights, in view of these expectations.
To check the availability, we can create an availability test in the Application Insights resource.
This test can either be a simple check of a specific URL being accessible or it can be a complex multistep webtest that we can be uploaded to Application Insights after creating it using Visual Studio. We will go ahead with the simple test for our tutorial.
To create the availability test, let us click the Availability tab on the Application Insight resource on Azure portal. This is the resource that is either created automatically or manually for our deployed web application. On this panel, we will be able to see the overall availability of the application that is measured by the built-in test.
We can add a new test by clicking “+Add Test” on this panel. On the test creation wizard, let us select the type of test as “URL ping test” and then provide the URL of the application that is to be monitored. There is an option to collect data of dependent requests like for images, .CSS files etc. There also is an option to rerun the test in a few minutes if it fails in the first attempt.
Once we create such a test, it will start pinging the configured URL from five different places in the world to monitor availability of that page. We can monitor many such pages, which do not require any data to be sent, by creating multiple tests like this. These tests will be executed at certain periodicity to check the availability of the pages and the combined results will be shown.
Figure: Application Insights – Availability Test Results
We can also get a scatter plot of test results over time. It includes the execution time during the test.
Figure: Application Insights – Availability Test Results Scatter Plot
If for a test, the availability is less than a certain threshold (95% by default), it sends an alert to your email. Once the test is created, using its context menu, it is possible to edit the alert settings like the conditions, actions and severity level.
Figure: Alert Email
More about the alerts, later in the article.
Application performance is monitored, measured and shown graphically in multiple ways by Application Insights.
a. Operation Time – On the time scale, this graph shows the time it took for each operation to execute.
Figure: Performance – Operation Times and Request Count
b. Request Count – Requests received by the application over time
c. Hardware related Performance Counters – CPU, Available Memory and Storage Access
Figure: Hardware Performance Counters
d. Average Execution Time for each Operation
Figure: Average Execution Time for each Operation
e. When a specific operation is selected, the graph provides us the information of distribution of execution time (Request count on Y axis and Execution time of X axis).
Figure: Execution Duration Distribution for a Selected Operation
f. Dependency Response Time – For example how much time did the SQL Server take to respond to a query in the code.
Figure: Performance – Dependency Response Time
3. Quality Assurance
Failures are monitored to know where the application is failing and which parameter is causing it to fail. The primary graph gives the details of overall failures against the total request over time. It also provides the list of requests that are failing, and the statistics of those failures.
Figure: Request and Failure Count for an Operation
We can also drill down into each failing request and the type of exception that are predominant.
Figure: Failure Types for an Operation
We can also find out if there are any dependency failures and the details of exceptions that are encountered.
Figure: Dependency Failure Details
In addition to monitoring these desirable parameters, Application Insights also provides the following data:
Application Map: This provides the detected call patterns using the architectural components of the application. It also provides the statistics of the calls and failures.
Figure: Application Insights – Application Map
Live Metrics: It is the streaming view of the various parameters that are monitored by Application Insights. It shows the parameters for incoming requests, outgoing requests (outside the application) and overall health of the application along with its environment.
Figure: Application Insights – Live Metrics
Search: This graph shows the various events like requests, exceptions, availability tests, dependency calls that have taken place in a specified time.
Figure: Application Insights – Search in all Events
It also allows us to search by keywords in the total data to show limited results.
Figure: Application Insights – Custom Search
Metrics: This monitoring tools provides many built-in metrics that we can view, for example – Availability Test Results, Server Exceptions, Request Rate and many more.
Figure: Application Insights – Built In Metrics – Server Exceptions
Figure: Application Insights – Built In Metrics Average HTTP Request Rate
Alerts: Through Application Insights, we can set an alert to be sent when some predefined condition happens. This condition can be related to load, failures, availability, request statistics and many more. List of signals that can be selected for monitoring are:
Figure: Application Insights – Alert Conditions List
The action on crossing the threshold value can be any of these following in the list:
- Send email, SMS, voice message or Push notification
- Call an Azure Function, Azure Logic App or a Webhook
- Send information to ITSM
- Trigger an Azure Automation Runbook
To create an alert rule, we have to set a condition and an action:
Figure: Alert – Setting the Rule
Figure: Alert – Configure Signal Logic (Condition to Monitor)
Figure: Alert – Add an Action
Log Analytics: This is a feature of Azure Monitor that is integrated with Application Insights. It facilitates querying the logs collected by Application Insights. We can create custom queries using a language called Log Analytics Query Language also called as Kusto. These queries are based upon the tables that can be filtered with multiple criteria and stored for repeated execution. These queries can be executed manually or through Azure Automation Workbooks. Queries return tabular data or graphical views.
Figure: Application Insights – Log Analytics Query and Results
Besides these features, Application Insights also has features to gather data of Usage Analytics like User Flow, Retention, Funnels, Sessions etc. These features are out of bound for the scope of this article but will be covered in another article specially for those features.
In this article, we have seen how we can use Application Insights to do health monitoring. We took a very common case where Application Insights SDK was not inserted in the code of the application before the application is deployed. We also took an overview of features of Application Insights that are used for monitoring the health of the application after deployment. Ina future article I intend to cover the features of application Insights that are used for Usage Analytics and Log Analytics in more details.
This article was technically reviewed by Gouri Sohoni and Vikram Pendse.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!