AZ-400: Designing and Implementing Microsoft DevOps Solutions
Analyze Metrics
Inspect distributed tracing by using Application Insights
Discover how to leverage Application Insights—a core component of Azure Monitor—for real-time telemetry, anomaly detection, and insightful analytics. This powerful tool seamlessly integrates with Azure services, making it the ideal choice for monitoring and diagnosing your web applications.
Key Features
- Real-time application telemetry
- Automatic anomaly detection
- Robust analytics tools
- Seamless integration with development tools
Enabling Application Insights in Your Web Application
For this demo, we begin by creating a web application and enabling Application Insights during the setup process. When creating your web app in the Azure portal, proceed to the Deployment and then Networking sections to complete your application details. In the Monitor and Secure section, you'll find an option to enable Application Insights.
Select "Yes" to enable Application Insights for your app. In this demonstration, we are using the "KodeKloud Support" app. Although Microsoft Defender is also available, our focus here will remain solely on Application Insights.
After reviewing your settings, click "Create" to deploy the web application with Application Insights enabled.
Installing the Application Insights Package
With your web app running, the next step is integrating Application Insights into your code. Open Visual Studio and navigate to the Package Manager Console. Execute the following command to install the Application Insights package for ASP.NET Core:
PM> Install-Package Microsoft.ApplicationInsights.AspNetCore
This command installs the required Package for enabling Application Insights in your application. Below is an example of a Razor Page that might be part of your project:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="hero-image" style="background-image: url('/img/frontpage.jpg'); background-size: cover; background-position: center;">
<h1 class="display-4 text-white">Welcome to Kode Kloud</h1>
</div>
<div class="container mt-4">
<div class="row">
<div class="col-md-8 offset-md-2 text-center">
<h2>Expert Tech Support at Your Service</h2>
<p class="lead">
At Kode Kloud, our dedicated tech support team is committed to providing you with exceptional service. With our extensive knowledge and passion for problem-solving, we're here to ensure your tech runs smoothly, allowing you to focus on what matters most - your success.
</p>
</div>
</div>
</div>
After installing the package, your Package Manager Console output should look similar to:
Package Manager Console
Default project: KodeKloudSupport
Each package is licensed to you by its owner. NuGet is not responsible for, nor does it grant any licenses to, third-party packages. Some packages may include dependencies which are governed by additional licenses. Follow the package source (feed) URL to determine any dependencies.
Package Manager Console Host Version 6.11.0.119
Type 'get-help NuGet' to see all available NuGet commands.
PM> Install-Package Microsoft.ApplicationInsights.AspNetCore
Configuring Application Insights
To configure Application Insights, you first need your instrumentation key. In the Azure portal, navigate to your "KodeKloud Support" application, select Monitoring, and click on Application Insights. Follow the prompt to instrument your application via the language selection (in this case, .NET).
Under the Collection Level settings, choose the "Recommended" option. Optionally, you can enable the Profiler. Once you apply these settings, copy your instrumentation key and update your application's configuration.
Edit your appsettings.json file:
{
"ApplicationInsights": {
"InstrumentationKey": "your-instrumentation-key-here"
},
// other settings
}
Then, update your startup code in Program.cs to include Application Insights telemetry:
using Microsoft.ApplicationInsights;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddApplicationInsightsTelemetry(builder.Configuration);
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
When running your application locally, you should see telemetry outputs similar to:
Application Insights Telemetry: {"name":"AppRequests","time":"2024-10-11T21:59:42.925744Z","iKey":"a2de747f-9743-45eb-89264de5be8e60b","tags":{"ai.application.ver":"1.0.0.0"}}
Application Insights Telemetry: {"name":"AppRequests","time":"2024-10-11T21:59:42.925680Z","iKey":"a2de747f-9743-45eb-89264de5be8e60b","tags":{"ai.application.ver":"1.0.0.0"}}
The program '[32680] KodeKlaudSupport.exe' has exited with code 4294676295 (0xffffffff).
Once your CI/CD pipeline pushes these updates, your website will be fully integrated with Application Insights.
Analyzing Application Insights Data
After your website is live, dive into the robust data provided by Application Insights. When you navigate to the Monitor section in the Azure portal, you'll encounter various metrics such as failed requests, server response times, overall server requests, and availability.
As traffic increases, observe how the "server requests" graph refreshes in real time. Occasionally, spikes may occur due to caching behaviors inherent to Azure Web Apps.
Application Dashboard
The application dashboard offers a comprehensive view of your app’s performance and user interactions. Key metrics include:
- Unique sessions and users
- Failed requests
- Server response times
- Average page load times
Live Metrics Stream
The live metrics stream displays real-time data from your server, including request rates, response times, and CPU usage. This feature is particularly useful under high-load conditions to quickly pinpoint performance bottlenecks.
Application Map and Failures
The application map provides an overview of the instances running in your environment. It highlights failing requests, for example, displaying 404 errors prominently if they occur frequently.
Performance Metrics and Dependency Tracking
Understanding performance is crucial. Application Insights allows you to track and analyze operation durations—like jQuery file loading times—and gain insights into dependencies affecting overall performance.
Configuring Alert Rules
Set up alert rules to receive notifications when thresholds are exceeded. For instance, configure an alert to trigger when more than three failed requests occur per minute. Notifications can be delivered via email or through action groups targeting the appropriate teams.
Use the following threshold settings:
- Condition: More than three failed requests per minute
- Frequency: Check every minute
- Action: Utilize quick actions (e.g., send an email)
Advanced features also allow you to fine-tune alerts with severity levels—critical, error, warning, informational, and verbose—and even include custom payloads for enhanced notifications.
Other Notable Features
Custom Charts:
Visualize metrics such as availability, test duration, and browser load times by creating custom charts.Diagnostic Settings:
Configure diagnostic settings to export logs or stream exception data, which is useful for further analysis.Log Analytics:
Leverage Log Analytics for in-depth querying and analysis of logs, an invaluable tool for diagnosing complex issues.
Conclusion
Application Insights delivers a comprehensive suite of tools to monitor, diagnose, and optimize your web applications. Whether tracking performance metrics, identifying failures, or observing live activity, this indispensable solution supports both developers and DevOps professionals. To fully harness its robust capabilities, explore each feature and elevate your proficiency in advanced monitoring and performance analysis.
Thank you for reading this article.
Watch Video
Watch video content