monitoring new application with application insights · web viewin this lab, we will begin to...

46
Hands-On Lab Monitoring Applications with Application Insights Lab version: 14.0.23107.0 Last updated: 9/16/2015

Upload: others

Post on 06-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

Hands-On LabMonitoring Applications with Application InsightsLab version: 14.0.23107.0

Last updated: 9/16/2015

Page 2: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

TABLE OF CONTENT

MONITORING APPLICATIONS WITH APPLICATION INSIGHTS...............................................................1

OVERVIEW................................................................................................................................................ 3

Prerequisites...................................................................................................................................... 3

Exercises.............................................................................................................................................3

EXERCISE 1: GET PERFORMANCE DATA FROM A WEB APP...................................................................................4

EXERCISE 2: GETTING TELEMETRY FOR WEB PAGES........................................................................................12

EXERCISE 3: ADDITIONAL INSTRUMENTATION AND CUSTOM EVENTS..................................................................21

EXERCISE 4: PUBLISH THE WEB APP AND MONITOR EVENTS IN PRODUCTION......................................................27

EXERCISE 5: MONITOR AVAILABILITY, ALERTS, AND EXPORT DATA.....................................................................36

Page 3: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

Overview

Application Insights is an analytics service hosted in Microsoft’s newest Azure Portal. It is designed to help developers find performance issues earlier, diagnose crashes faster, and have deep insight into their users with a complete view of their apps and services. Track application health, usage metrics, and information about app crashes. Search results and drill down to specific events, monitor active usage, and get alerts automatically when things go wrong. Adding this instrumentation to a project is very easy, and viewing the results is equally straightforward. In this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications, analyze Application Insights data in the Azure portal, and examine all of the basics of proactive monitoring and alerts.

Prerequisites

In order to complete this lab you will need:

The Visual Studio 2015 virtual machine provided by Microsoft. For more information on acquiring and using this virtual machine, please see this blog post.

A Microsoft Azure account with an active subscription. If you have an MSDN subscription, you already have access to Microsoft Azure; you simply need to activate it in your MSDN benefits portal, if you have not already. Otherwise, you can create a trial Azure account .

An open internet connection from the virtual machine. This means that if you have not already, you’ll need to create a virtual switch in the Hyper-V Manager and associate it with the VM. This is not a difficult task, but if you are unfamiliar with creating virtual switches in Hyper-V, good instructions can be found here.

Exercises

This hands-on lab includes the following exercises:

1: Adding Application Insights to a Project2: Getting Telemetry for a Web App and Web Page3: Additional Instrumentation and Custom Events4: Publish the Web App and Monitor Events in Production5: Monitor availability, Alerts, and Export Data

Estimated time to complete this lab: 30-40 minutes.

Page 4: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

Exercise 1: Get performance data from a web app

In this exercise, you will learn how to add Application Insights telemetry to a web application project.

1. If you have not already set a checkpoint for your VM, do so now. This will allow you to retry this lab or others from a “clean” state without reloading the entire VM.

2. Log in as Brian Keller (VSALM\Brian). All user passwords are P2ssw0rd.

3. Manually set the date and time in the virtual machine to match your current local date and time if it does not already match. Be sure to remember to set the correct time zone! Failing to do so will result in difficulty viewing the telemetry data in Application Insights later.

4. Launch an instance of Visual Studio from the taskbar.

5. Open the FabrikamFiber.CallCenter solution from the Dev branch in TFS source control. It should be the first item at the bottom of the Home tab on Team Explorer:

Figure 1Open the FabrikamFiber.CallCenter solution from Team Explorer

Page 5: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

6. Navigate to the Solution Explorer tab. Right-click on the FabrikamFiber.Web project in the open solution, and check it out for edit. This will ensure that the correct resources are available in the local workspace when you add Application Insights to the project.

Figure 2Check out solution for edit

Page 6: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

7. Now you are ready to add Application Insights to your project. Once again, right-click on the FabrikamFiber.Web project in the solution, and choose “Add Application Insights Telemetry…” from the context menu:

Figure 3Add Application Insights to the project

Page 7: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

8. After you make this selection, you’ll see a new Application Insights dialog box and will be prompted to enter your Azure credentials. These are necessary because Application Insights collects telemetry data via Azure.

Figure 4Sign in to Azure to add Application Insights to your project

9. You will see another dialog that details your Azure subscription. Press the “Add” button to continue.

Note: You may also get a non-critical error dialog regarding the local workspace. If this happens, simply select OK to clear it.

Page 8: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

10. Once Application Insights has been added to the project, a browser window with App Insights information in it will pop up, and you can safely close this or minimize it to read later. You’ll also notice that there are now new resources in your solution including ApplicationInsights.config:

Figure 5Solution with Application Insights resources added

Page 9: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

11. Use F5 to build and run your web app. Once it opens (it may take a minute or two for the first run), navigate to various pages, including Employees, Customers, Tickets, and Dashboard. Avoid Reports for now. Refresh a page or two, and drill into a couple of ticket or customer records. When you’re done, close the browser window.

12. When you return to Visual Studio, you’ll see the following message at the top of the Solution Explorer:

Figure 9First App Insights data sent

Page 10: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

13. Click Open Application Insights in the dialog. If necessary, sign into Azure using your credentials, click Browse, and select the Application Insights resource named for your app. If you have multiple directories associated with your Azure subscription, you may also need to select the correct subscription first.When your application resource opens, you should see data similar to the following:

Figure 10Initial App Insights telemetry

14. Congratulations! You now have telemetry summarizing the requests processed by your web app, and how quickly it responded to them.

You will notice, however, that the chart titled “browser page load data” does not have any data yet.

Exercise 2: Getting Telemetry for Web Pages

In this exercise, you will learn how to add necessary telemetry code to a web page.

1. In https://portal.azure.com, click Browse and open the Application Insights resource for your app.

Page 11: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

Note: one way to get there is to right-click on the ApplicationInsights.config in Solution Explorer and select the Open Application Insights option:

Figure 7Open Application Insights from config

Page 12: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

You’re now looking at something like this, with missing browser telemetry:

Figure 10Initial App Insights telemetry

Page 13: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

2. In order to see the page-specific data, we need to add some code to our project. Fortunately, Application Insights will generate this code for us. Click on the chart in the portal with the missing data, and you’ll see the script that you need to add to your project appear in yet another new blade to the right:

Figure 11

Generate script to monitor web pages

Page 14: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

3. Copy the script to the clipboard, return to Visual Studio, and then open _Layout.cshtml. Use the search box in the Solution Explorer to find it. Be sure to choose the one from the FabrikamFiber.Web project. Recall that in ASP.NET, this is a master template from which all the web pages are created. So we’re effectively adding the same script to all the pages of the site. This script will enable the collection of usage analytics from the end-user.

Paste the code into the file just before the closing </head> tag:

Figure 12

Paste instrumentation script into a web page

Note: You can take advantage of the JavaScript SDK for any web application, even if it is not built with ASP.NET.

Page 15: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

4. Use F5 to build and run your web application again. Click through the various views like you did the last time, avoiding Reports for the time being. When you are done, close the web app. Your new telemetry will be sent to Application Insights.

Figure 13Build and run the application

Page 16: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

5. Switch back to (or open) the Azure portal in your browser, and refresh the FabrikamFiber.Web App Insights blade. You will now see data for the “browser page load time” chart as well. If you select it, you’ll be able to see detail information about this specific telemetry:

Figure 14New telemetry available

6. You should also notice in Figure 14 above that if you click on the Filters button on your Browsers detail blade, a list of available data filters appears at the right. This is very useful when you have large amounts of data or want to drill in to a specific area. In this example, the “Browser” filter has been expanded. Here, we only have data from one browser captured. However, if we had accessed our web app from multiple browsers, we could compare data for each specific browser or get a cumulative view just by applying the appropriate filter. Keep this in mind when you are analyzing your telemetry from Application Insights.

Note: You can also configure Application Insights to disable telemetry for debugging purposes altogether.

Page 17: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

Exercise 3: Additional Instrumentation and Custom Events

In this exercise, you will see how we can add further instrumentation and custom events to our Application Insights data for more advanced monitoring.

Let’s suppose we’d like to know how often customer records are edited. So we’re going to send telemetry whenever that happens.

1. Open _Layout.cshtml once again in Visual Studio and take a look at the IntelliSense for the appInsights object (temporarily remove the period after ‘appInsights’ to see the IntelliSense, and then re-add the period). Note that the JavaScript SDK includes methods to track events, exceptions, metrics, and traces:

Figure 15Intellisense on appInsights object

2. Press Escape to prevent modification to the JavaScript. Although we could add in additional instrumentation in the client, we’ll opt to just track a custom event on the server. We’re going to add this custom event and metrics to CustomersController.cs.

Page 18: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

3. Open CustomersController.cs in the code editor and add using statements for System, System.Collections.Generic, and Application Insights to the top of the file (as shown in Figure 16 below):

using System;

using System.Collections.Generic;

using Microsoft.ApplicationInsights;

Figure 16Add libraries

Page 19: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

4. Locate the Edit method (this method is overloaded; choose the second instance that accepts “customer” as a parameter). Add the code highlighted below, or copy/paste the entire method over the code in CustomersController.cs:

public ActionResult Edit(Customer customer) { if (ModelState.IsValid) {

var properties = new Dictionary<string, string> { { "Customer", customer.ID.ToString() + ' ' + customer.FullName.ToString() }, { "TimeStamp", DateTime.UtcNow.ToLocalTime().ToString() } };

TelemetryClient client = new TelemetryClient();

this.customerRepository.InsertOrUpdate(customer); this.customerRepository.Save();

client.TrackEvent("Edit Customer", properties, null);

return RedirectToAction("Index"); }

return this.View(); }

Note: in the code above, the custom event is named “Edit Customer.” The TrackEvent method sends data to Application Insights whenever it is called in the Edit method. The first parameter passed contains the specific data that we have established in the variable “properties,” which is just the customer’s id number, name, and a date/time stamp.

Figure 17Add further telemetry

Page 20: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

5. Once you’ve added your code, press F5 to once again build and run your web app. This time, click on the Customers tab, then click on the Edit link next to a customer’s name. Change some bit of data, such as address, and save the record. Do this again for a second customer, then close the web app.

Figure 18Edit a customer to launch custom event in Application Insights

Page 21: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

6. Once again, return to your browser with the Azure portal open, and refresh the FabrikamFiber.Web App Insight blade. Once it finishes, scroll down and you should now see that the “Custom events” tile has been updated, signifying that there are two new custom events that have been captured. Click on this tile, which will open the Custom events blade. Your screen should now resemble the following:

Figure 19Custom telemetry captured and displayed in the Custom events blade

Page 22: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

7. You can now drill further into the data by clicking on the event name of our new custom event (“Edit Customer”), and this will open another custom events blade that is filtered on that particular event. Furthermore, you can now click on a specific instance of that custom event to see the details, including our custom data attributes that we added to the event:

Figure 20Detailed information about a specific custom event

We’ve seen how to see how often specific events happen in our web app. As you can imagine, we could easily write code to log how often users perform any action we like. That helps us understand how our application is being used, and gives us a better idea of where we should focus our efforts in developing the app further.

Page 23: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

Exercise 4: Publish the Web App and Monitor Events in Production

In this exercise, you will publish your web app to the local IIS server and monitor events. So far, we’ve only run it in debug mode – but of course, the real value of telemetry is to let us know what real users do with the app, and be aware of how it performs in live conditions.

1. Right-click on the FabrikamFiber.Web project node in Solution Explorer and select Publish:

Figure 21Prepare to publish the web app

2. The default settings should be fine for publication of your web app to IIS. Note the path where the app is being published – this will be important in setting up the web site in IIS. Click on the Publish button.

Figure 22Connection information in publish dialog

Page 24: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

3. Once Visual Studio has finished publishing the web app, you will need to create an appropriate site that maps to this location in IIS. So, open the IIS Manager in Windows Server and expand the tree at the left until you can see the list of sites. Right-click at the top of the list and select “Add Website.”

Figure 23Add a web site to IIS

4. Complete the Add Website dialog with the following information:

Figure 24Fill in specifics for your web site where your web app will run

Note the physical path is the same as our publication path, and that the application pool need to be manually set to DefaultAppPool. Also note that a specific port has been selected.

Page 25: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

5. Once your web site is set up, you can browse directly to it from the IIS Manager. Click on the browse website link at the right:

Figure 25Open your new web site

6. This will open your familiar application, only now from the new location to which you have published it. Try editing another customer record, then navigate to the Reports tab that we’ve been avoiding up until now. You’ll generate an error that will look like this:

Figure 26Error generated in web app

Page 26: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

7. Close the web app and return to your Azure portal. Refresh your FabrikamFiber.Web blade there, and now you will see a couple of interesting things:

Figure 27New telemetry from published web app, including failure data

The custom events counter has gone up by one (because you edited another customer), and now the Failed Requests chart has some data in it. Click on this chart, and you’ll get the failure data in new blade to the right due to the error generated when you clicked on the reports tab.

Page 27: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

8. You can drill into the listed exceptions to get more detailed information about what went wrong. In this example, we probably don’t care too much about the error with fonts, even though that generated the highest number of errors. However, we do care about the two errors that have to do with our Reports tab. Try clicking on the “GET Reports/Employees” request, which will bring up a filtered blade for this common set of failures. Next, pick the most recent one (you may only have one) at the top of the Requests list at the bottom of the blade. This will open an even more granular blade that details this specific instance:

Figure 28

Drilling into failure telemetry

9. You will now see something interesting on the GET Reports/Employees blade: the third section down, Calls to Remote Dependencies, has no data in it. This is because our published web app running in IIS is not being monitored at the server level. In order to get this telemetry data, we need to install the Application Insights Status Monitor.

Page 28: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

10. So, before we go further, let’s do exactly that. Download and run the Status Monitor installer (from the link in this step):

Figure 28Installing the Status Monitor

11. You’ll need to provide the Azure account to which you want to send your telemetry. Make sure that it’s the same one you’ve used throughout this lab.

Page 29: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

12. Once the installation is complete, the App Insights Status Monitor will be running and ready to collect data. You need to set up the information for telemetry collection from IIS, so choose the FabrikamAI web site on the left, then click the Configure Settings link on the right. Fill in the required parameters, choosing your FabrikamFiber.Web web app for the Application Insights Resource from the drop-down list. You may also see a notice at the top of the Status Monitor dialog that IIS needs to be reset to begin collecting data. There is a button right there to make this happen, so if need be, go ahead and push it to restart IIS:

Figure 29Setting up the Status Monitor for first use

Note: If you run into problems restarting IIS, open the IIS Manager and restart it again from there. It is also possible that you may need to restart the World Wide Web publishing service. If so, you can do this from the Services tab in the Task Manager.

13. After this step is complete, your Status Monitor dialog will show updated configuration information for your FabrikamAI web site:

Figure 30Configuration for web site confirmed

Page 30: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

14. Now that the Status Monitor is running, open your web app again (either through the IIS Manager or by pasting the URL into a new browser tab) and “exercise” your application. Click through it, be sure to generate an error via the Reports tab, etc. When you are done, you can close the web app.

15. When you return to the Failures blade in the Azure portal, you’ll see that you now have data for Dependency Failures. Refresh your blades as necessary, and drill all the way back through to the latest GET Reports/Employees Failure from your last run:

Figure 31Failure telemetry with dependencies included

Do you notice what is now available at the bottom of the rightmost blade? All of the calls to remote dependencies, collected via the Status Monitor. This will help you establish whether the failure you’re investigated caused/affected any other failures on dependent resources. You will also now have data available in the Servers and Performance blades (which you can select from the main FabrikamFiber.Web AI blade).

Page 31: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

16. Look at the Failed Request Details blade. Notice that the specific exception is included in the middle of the blade, followed by any remote dependency calls. Click on the exception to see all of the specifics about the exception right down to call stack information:

Figure 32Specifics about the generated exception

You can see in the detail the exact file that pertains to the code that generated the error, including the line number for the call. This allows you to go directly to your source control to begin debugging.

As you can imagine, this provides a wealth of information that can help you debug your web application, especially if you encounter errors in a deployed, production environment. Application Insights can capture all kinds of standard and custom telemetry about your running product without creating significant overhead.

Page 32: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

Exercise 5: Monitor Availability, Alerts, and Export Data

In this exercise, you will see how we can set alerts, monitor availability of the web app, and export telemetry data to Azure. Availability monitoring tells you if your live website goes down, or responds slowly. In the same way, you can set alerts on other performance metrics.

1. First, let’s take a look at availability monitoring. We can add web tests to our Application Insights object to monitor the current availability of particular web apps (by URL). What we monitor doesn’t necessarily have to be the primary web app associated with the particular Application Insights object. There are many potential reasons for this, but most commonly we may want to monitor an additional site to which our current web app has a dependency. Start by click on the Availability tile under the charts on our FabrikamFiber.Web blade:

Figure 33Navigate to Availabilty web tests

Page 33: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

2. The Web Tests blade will open, where you can add new web tests for various resources. Click on the “Add web test” button, and a Create Test blade will open to the right. While we might normally track the availability of our specific web app, we haven’t published it publicly, so let’s just check the availability of a common site, www.bing.com (or use any URL of your choice). Fill out the dialog as follows:

Figure 34Create a web availability test

Page 34: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

3. Once you’ve created the web test, it will appear in the Web tests blade. Right now, you won’t have any data, but as it runs over time, it will begin to return data for web execution time and how it performed across multiple time slices, including any failures. The test runs every five minutes, so if you come back after five minutes, the first telemetry data will be available.

Figure 35A running web availability test

4. Next, let’s look at setting up alerts for our monitored web app. Go to the FabrikamFiber.Web blade, and select the Alert Rules tile:

Figure 36Select the Alert Rules tile

Page 35: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

5. This will open the Alert Rules blade, and you will clearly see that there currently isn’t anything in it. Click on the Add alert button at the top, and the “Add an alert rule” blade will open to the right. Let’s monitor our web app for high CPU usage. Fill out the dialog as shown below and then click on OK:

Figure 37Add a new alert rule

As you can see, if the CPU for our web app spikes past 75% over a period of 5 minutes, an alert will be sent.

Page 36: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

6. You will now see your new alert rule active in the Alert rules blade. If it is ever triggered, you will see the results here, and receive an email if you choose to do so.

Figure 38An active alert rule

7. Last, but not least, let’s take a quick look at how to export our telemetry data to various locations. By default, Application Insights will allow you to quickly and easily export your data in Excel format, simply saving it to the location of your choosing. An Export button is available on most blades that show data. In this case, we’ve selected the Browser Page Load Time Chart, which displayed the Browsers blade, and then clicked on the Export button:

Figure 39Exporting data in Excel format

Page 37: Monitoring New Application with Application Insights · Web viewIn this lab, we will begin to explore how developers can add Application Insights instrumentation to your applications,

8. You can also perform a continuous export of your data to Azure Storage. This is a more advanced topic than we are getting into for the purpose of this lab. However, it is important that you bear this capability in mind in case you’d like to do any custom, advanced analysis of your data. If you’d like to learn more about how to do this, please read this excellent instructional article on how this functionality works here.

To give feedback please write to [email protected]

Copyright © 2023 by Microsoft Corporation. All rights reserved.