Introduction

Azure Application Insights is a powerful tool for monitoring and diagnosing performance issues in your applications. It provides deep insights into how your applications are performing and helps you understand how users are interacting with them. This module will cover the key features, setup, and usage of Azure Application Insights.

Key Concepts

  1. Telemetry Data: Data collected from your application, including metrics, logs, and traces.
  2. Instrumentation: The process of adding code to your application to collect telemetry data.
  3. Metrics: Quantitative measurements, such as response times, CPU usage, and memory usage.
  4. Logs: Detailed records of events that occur within your application.
  5. Traces: Records of the execution path of your application, useful for diagnosing issues.

Setting Up Azure Application Insights

Step 1: Create an Application Insights Resource

  1. Navigate to the Azure Portal: Go to Azure Portal.
  2. Create a New Resource: Click on "Create a resource" and search for "Application Insights".
  3. Configure the Resource:
    • Name: Provide a unique name for your Application Insights resource.
    • Resource Group: Select an existing resource group or create a new one.
    • Region: Choose the region closest to your application.
  4. Review and Create: Review your settings and click "Create".

Step 2: Instrument Your Application

For .NET Applications

  1. Install the SDK: Add the Application Insights SDK to your project using NuGet.
    Install-Package Microsoft.ApplicationInsights.AspNetCore
    
  2. Initialize the SDK: Add the following code to your Startup.cs file.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddApplicationInsightsTelemetry(Configuration["ApplicationInsights:InstrumentationKey"]);
    }
    

For JavaScript Applications

  1. Add the SDK: Include the Application Insights JavaScript SDK in your HTML file.
    <script type="text/javascript">
        var appInsights = window.appInsights || function (config) {
            function r(config) { t[config] = function () { var i = arguments; t.queue.push(function () { t[config].apply(t, i) }) } }
            var t = { config: config }, u = document, e = window, o = "script", s = u.createElement(o), i, f; for (s.src = config.url || "https://az416426.vo.msecnd.net/scripts/a/ai.0.js", u.getElementsByTagName(o)[0].parentNode.appendChild(s), t.cookie = u.cookie, t.queue = [], i = ["Event", "Exception", "Metric", "PageView", "Trace", "Dependency"]; i.length;)r("track" + i.pop()); return r("setAuthenticatedUserContext"), r("clearAuthenticatedUserContext"), config.disableExceptionTracking || (i = "onerror", r("_" + i), f = e[i], e[i] = function (config, r, u, e, o) { var s = f && f(config, r, u, e, o); return s !== !0 && t["_" + i](config, r, u, e, o), s }), t
        }({
            instrumentationKey: "YOUR_INSTRUMENTATION_KEY"
        });
    
        window.appInsights = appInsights;
        appInsights.trackPageView();
    </script>
    

Using Azure Application Insights

Viewing Telemetry Data

  1. Navigate to Your Application Insights Resource: In the Azure Portal, go to your Application Insights resource.
  2. Overview: The overview page provides a summary of key metrics such as server response time, page load time, and failed requests.
  3. Metrics Explorer: Use the Metrics Explorer to create custom charts and dashboards.
  4. Logs (Analytics): Use the Logs feature to write custom queries and analyze your telemetry data.

Common Queries

Request Count by Response Code

requests
| summarize count() by resultCode

Average Response Time

requests
| summarize avg(duration) by bin(timestamp, 1h)

Failed Requests

requests
| where success == "False"
| summarize count() by bin(timestamp, 1h)

Practical Exercise

Exercise: Monitor a Sample Application

  1. Create a Sample Application: Create a simple web application using your preferred language (e.g., .NET, Node.js).
  2. Instrument the Application: Follow the steps above to add Application Insights to your application.
  3. Deploy the Application: Deploy your application to Azure App Service.
  4. Generate Traffic: Use a tool like Apache JMeter to generate traffic to your application.
  5. Analyze Telemetry Data: Use the Azure Portal to view and analyze the telemetry data collected by Application Insights.

Solution

  1. Sample Application: Create a basic ASP.NET Core application.
  2. Instrumentation: Add the Application Insights SDK and initialize it in Startup.cs.
  3. Deployment: Deploy the application to Azure App Service.
  4. Traffic Generation: Use Apache JMeter to simulate user traffic.
  5. Analysis: Use the Metrics Explorer and Logs features in the Azure Portal to analyze the collected data.

Summary

In this module, you learned about Azure Application Insights, a powerful tool for monitoring and diagnosing performance issues in your applications. You learned how to set up Application Insights, instrument your application, and analyze telemetry data. By completing the practical exercise, you gained hands-on experience in using Application Insights to monitor a sample application. This knowledge will help you ensure the reliability and performance of your applications in production environments.

© Copyright 2024. All rights reserved