Capture and view page load times in your Azure web app with Application Insights - VishalPatangay/My-devops-repo GitHub Wiki

Application Insights, a feature of Azure Monitor, is an extensible Application Performance Management (APM) service for developers and DevOps professionals.

Enable Application Insights on an Azure web app

What is Application Insights? Application Insights is an Azure service that helps you to monitor the performance and behavior of web applications.

It mostly captures two kinds of data: events and metrics. Application Insights is represented in Azure as a resource deployed to one of your subscriptions. Each Application Insights resource you create is a repository for application telemetry data. It includes the various displays and configuration tools that you can view from the Azure portal. To send telemetry data to an Application Insights resource from an app, you need to configure the app with the instrumentation key of the Application Insights resource.

Visualizations By viewing an Application Insights resource in the Azure portal, you can visualize the telemetry captured from your app in a variety of ways.

Live metrics streams: Charts that display performance values as they vary in near-real time. Metrics explorer: Tool that shows how metrics vary over time. Alerts: Messages automatically sent to app admins when target metrics exceed specified thresholds. You can use alerts to ensure your team is aware of critical issues immediately. Profiler: Shows how a set of requests, like those for a single web page, were delivered. You can use these profiles, for example, to see which page elements load slowly. Application Map: Displays the components of an application and how they link to each other. You can use the data shown with each component to diagnose performance bottlenecks and failure hotspots. Usage analysis: Information about your app's users. For example, you can see numbers of unique users and sessions and information about user retention.

Two types of configurations for Application insights here are two ways to configure your app to send data to Application Insights:

Runtime instrumentation: this is without any code changes, possibly to the existing apps if a telemetry needs to be added. Note that some advanced data displays aren't available when you use only runtime instrumentation.

Built-in Instrumentation: rich insights can be used in this case, including custom events if required. This is done by developers by importing the insights server side sdk to your code base.

You can enable client-side instrumentation for an app by including a standard Application Insights JavaScript library in pages delivered to your app's users. Client-side instrumentation captures information about the user experience of the app, including page load times, details of browser exceptions, and performance data about AJAX calls.

Enable runtime instrumentation:

To enable client side telemetry, To automatically inject the JavaScript SDK and necessary configuration into pages served by your web app, add a new application setting named APPINSIGHTS_JAVASCRIPT_ENABLED and set the value to true.

You want to make sure that admins know as soon as a web app becomes overloaded. Which feature of Application Insights should you use? Alerts You can create an alert that triggers when a condition is satisfied - for example, when the web app is overloaded. When the alert triggers, you can configure it to send an email, for example, to inform admins.

You have a Linux-based web app that runs in the Azure App Service. You want to display basic performance data in an Application Insights dashboard. Which method should you use to instrument your app? Built-in Instrumentation. Because your app is based on Linux, you need to use build-time instrumentation. Runtime instrumentation and automatic client-side telemetry are available only for Windows apps.

Exercise to create a webapp and enable application insights https://docs.microsoft.com/en-us/learn/modules/capture-page-load-times-application-insights/3-exercise-enable-application-insights

View application insights from Azure portal:

1. You've enabled Application Insights for your web app. In which of the following Azure resources will telemetry data be stored for the app? The web app sends its telemetry data to the Application Insights resource, where it's stored for analysis.

You create a custom chart and pin it to an Application Insights dashboard. Other users of the dashboard can't see the new chart. What should you do to resolve the problem? Republish the application insights dashboard. When you make changes to a dashboard in Azure, other dashboard users won't see the changes until you publish them.

Use the Azure portal to visualize page load times for your app

Instrument server-side web application code with Application Insights

You can use the Application Insights SDK in your web app's code to track custom events and metrics and automatically capture detailed telemetry about performance and behavior. What is the Application Insights SDK? The Application Insights SDK is a software package you reference and use in your application's code. The SDK hooks into the web application framework that powers your app and automatically captures detailed runtime information, including data about HTTP requests, system metrics, service dependencies, and exceptions.

Customized insights When you configure and initialize the SDK in your code, it tracks the kinds of events and metrics that are common to all web applications, like request and response metrics, HTTP queue lengths, and performance counters like CPU and memory utilization.

How to use the SDK Assuming you already have a web app and an Application Insights resource in Azure, to install the SDK in your web app project, you need to:

Reference the SDK in your app. Configure your app with the instrumentation key of your Application Insights resource. Initialize the SDK in code to begin generating telemetry.

Install the SDK package

dotnet add package Microsoft.ApplicationInsights.AspNetCore

Configure the instrumentation key

Every Application Insights resource in Azure is uniquely identified by a GUID instrumentation key. You can find the instrumentation key for an Application Insights resource in the Azure portal, in the overview panel of the resource. or az resource show
--resource-group <resource_group_name>
--name <resource_name>
--resource-type "Microsoft.Insights/components"
--query properties.InstrumentationKey

Configuration file: In the appsettings.json file for your application, create a section called ApplicationInsights and add a new configuration value called InstrumentationKey, like so:

JSON

Copy { "ApplicationInsights": { "InstrumentationKey": "11111111-2222-3333-4444-555555555555" } } Environment variable: Create an environment variable called APPINSIGHTS_INSTRUMENTATIONKEY that contains the value of the key. This value needs to be present in the environment when the application is started. The best practice is to use a different instrumentation key and Application Insights resource for each environment in which your application runs to prevent unrelated telemetry from being grouped together. For this reason, it's often simpler to use an environment variable and include this configuration as part of the setup of your app's deployment environment.

Initialize the SDK in code

Finally, you must initialize Application Insights in your web app.

ASP.NET Core applications start up by calling CreateWebHostBuilder() in Program.cs. You can configure many aspects of the application and the runtime by chaining additional method calls here, including Application Insights. A call to UseApplicationInsights() will load your instrumentation key from configuration, trigger the SDK to automatically generate telemetry about many application events, and enable access to a TelemetryClient object from controllers and other components for custom event and metric tracking.

Here's what the relevant section of Program.cs will look like after you initialize Application Insights:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseApplicationInsights() .UseStartup();

Exercise to initiate appinsights in your project:

  1. Create a new mvc project dotnet new mvc -o videowebapp

  2. Add Application insights SDK cd videowebapp dotnet add package Microsoft.ApplicationInsights.AspNetCore

  3. Initialize Applications Insight SDK Open the source code in cloud shell editor by running the below command: code . Open program.cs()

locate the following code: public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup();

Add a call to useApplicationInsights method public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup() .UseApplicationInsights();

The above steps have configured the application Insights to our code.

Configure a deployment environment

We've created our app and initialized the SDK in our code. Now we need a deployment environment for it. We need a place to host the web app, and we also need an Application Insights resource. We'll follow the best practice mentioned in the last unit and configure the instrumentation key by using an environment variable in the deployment environment.

We'll host our app in Azure App Service. Instead of manually creating an Application Insights resource and configuring its instrumentation key with an application setting, we'll enable Application Insights runtime instrumentation on the app.

IN the Azure portal select the webapp and go to Application Insights option and set it to enabled,

Instrument the application

The TelemetryClient object is used to record information about application-specific events and metrics. Events and metrics are the two primary types of custom data that can be tracked by Application Insights.

**Events **represent any kind of event that occurs in your application that you're interested in tracking. For the video sharing application, you might want to track an event each time a video is added to or deleted from the site, or every time a comment is made on a video. Each kind of event that occurs in your application needs a unique name. You might have events named VideoAdded, VideoDeleted, and CommentAdded.

https://docs.microsoft.com/en-us/learn/modules/instrument-web-app-code-with-application-insights/4-instrument-the-application Metrics are numeric measurements taken independently of any event, typically on an interval. For example, you might want to track the number of videos being viewed simultaneously every 10 seconds. Like events, each metric must have a unique name.

More on implementing Events and Metrics in code: https://docs.microsoft.com/en-us/learn/modules/instrument-web-app-code-with-application-insights/4-instrument-the-application https://docs.microsoft.com/en-us/learn/modules/instrument-web-app-code-with-application-insights/5-instrument-the-application

⚠️ **GitHub.com Fallback** ⚠️