Monitor an application with autoinstrumentation

Code VelocityCode Velocity
4 min read

In this article, we will create an Azure App Service web app with Application Insights enabled, configure autoinstrumentation without modifying code, create and deploy a Blazor application, and then view application metrics and error data in Application Insights. Implementing comprehensive application monitoring and observability, without having to make changes to your code, makes deployments and migrations simpler.

Create resources in Azure

Navigate to portal.azure.com and sign in using your Azure credentials.

+Create a resource > Search Web App in the Marketplace > Create

Create a new resource group and give your desired name. Give your desired name for Instance. Off the slider. Choose Code option and choose a region you like. Follow the rest as below:

Next, navigate to the Monitor + secure tab. Enable Yes to Application Insights. Click New for Application Insights. Follow the rest below. Then click Ok > Review + create > Create > Go to resource when deploying is done.

Deployment will take a few minutes complete.

To enable monitoring without changes to our code, we need to configure the instrumentation for our app at the service level.

  1. In the left-navigation menu expand Monitoring and select Application Insights.

  2. Locate the Instrument your application section and select .NET Core.

  3. Select Recommended in the Collection level section.

  4. Select Apply and then confirm the changes.

  5. In the left-navigation menu, select Overview.

Create and deploy a Blazor app

In this section of the article, we will create a Blazor app in the Cloud Shell and deploy it to the web app you created. All of the steps in this section are performed in the Cloud Shell.

Use [>_] button > No storage account required > Apply > Switch it to bash if not yet.

So, here it only installs .net 9 sdk based. So we have to adjust in accordingly.

Azur web app > Configuration > General settings > Stack settings > .NET version: .NET 9 (STS) > Save. > Go to Overview > Restart

rm command is to remove earlier residual app.

mkdir to create folder and cd to navigate into the created folder.

dotnet new blazor to create a new blazor app

dotnet build to ensure there are no build errors.

Deploy the app to App Service

To deploy the app you first need to publish it with the dotnet publish command, and then create a .zip file for deployment.

dotnet publish -c Release -o ./publish

Run the following commands to create a .zip file of the published app. The .zip file will be located in the root directory of the application.

cd publish
zip -r ../app.zip .
cd ..

Run the following command to deploy the app to App Service. Replace YOUR-WEB-APP-NAME AND YOUR-RESOURCE-GROUP with the values you used when creating the App Service resources earlier in the exercise.

az webapp deploy --name MY-INITIALS-monitorapp \
    --resource-group rg-WebApp \
    --src-path ./app.zip

When the deployment is completed, select the link in the Default domain field located in the Essentials section to open the app in a new tab in your browser. It is on overview.

Now it's time to view some basic application metrics in Application Insights. Don't close this tab, you'll use it in the rest of the exercise.

View metrics in Application Insights

Return the tab with the Azure Portal and navigate to the Application Insights resource you created earlier. The Overview tab displays some basic charts:

  • Failed requests

  • Server response time

  • Server requests

  • Availability

Perform the following steps in the web app.

  1. Navigate between the Home, + Counter, and Weather navigation options in the menu of the web app.

  2. Refresh the web page several times to generate Server response time and Server requests data.

  3. To create some errors, select the Home button and then append the URL with /failures. This route doesn't exist in the web app and will generate an error. Refresh the page several times to generate error data.

  4. Return to the tab where Application Insights is running, and wait a minute or two for the information to appear in the charts.

  5. In the left-navigation expand the Investigate section and select Failures. It displays the failed request count along with more detailed information about the response codes for the failures.

Clean up resources

rm -rf blazor : remove the application folder first from the cloud shell.

Delete the resource group that will consequently delete everything.

References:

https://microsoftlearning.github.io/mslearn-azure-developer/instructions/azure-app-insights/01-app-insights-auto-instrument.html

0
Subscribe to my newsletter

Read articles from Code Velocity directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Code Velocity
Code Velocity

Have dual bachelor degrees in BSc in Control Engineering and Computer Engineering and MSc in Artificial Intelligence. Have worked as .net fullstack/ backend developer almost 5 years now. Passionate about both Software and AI Engineering. Specializing in Azure Cloud currently.