Skip to content

Automatically create work items from Application Insights exceptions

In the last blog post we looked how we can manually create work items into Azure DevOps from Application Insights. Manual labor is something that we want to avoid in DevOps world, so let’s take a look how we can automatically create bug type of work item into Azure DevOps when error is detected in Application Insights. The guide seems quite long, but actually you can do it in 15 minutes.

Create the Logic App

To connect Application Insights into Azure DevOps we need to use Logic Apps (that is the easiest way too). Logic Apps has a ready build integration with Azure DevOps so it is really easy to setup.

First let’s create a new Logic App in the Azure Portal with suitable name like “CreateAdoBug”. In the Logic App designer we want to select HTTP trigger, so that our Logic App is run when HTTP request is received. Next we need to define the request body JSON schema. Easiest way to do it is to use the “Use sample payload to generate schema” button and paste the sample schema from Application Insights docs: https://docs.microsoft.com/en-us/azure/azure-monitor/platform/alerts-common-schema-definitions

Then we need to choose the operation that is invoked when the HTTP request is received. We want to create an Azure DevOps work item, so lets use that operation.

After choosing the operation, we need to define what kind of data we want to report into work item. Remember that bugs in Azure DevOps by default does not show description field, so put the interesting parts into Repro Steps parameter. I decided to place the original alert id into title, because it is unique data and helps to separate different detections. AlertContext in Repro Steps contains all the basic information about detected exception.

Create the alert rule

Now we have the Logic App ready, so we can start creating the Application Insights rule, that will trigger and pass the information into our Logic App. Currently Application Insights does not allow to create complicated alert rules directly from the alert page, so we need to create it by using the “View in Logs” functionality in “Transaction Search” page.

I used following kind of query to find exceptions that I’m interested of. You can edit the query easily and add more filtering rules if that formula is not suitable for your needs. The severity rules seems to be so, that error log events are 3 and critical 4. If you are working with legacy app, that writes a lot of error log entries, you could start using the critical level as fresh start.

exceptions
| where timestamp > ago(1h) and severityLevel == 4

To create the alert rule, click the “+ New alert rule” button from top.

Now we have “complex” alert rule in our Application Insights that triggers when the right kind of exception is detected. By default the alert rule does not have “Evaluated based on” selected, so click the Condition name and define it. Cost of the check is based on the frequency of the evaluation. Also set the threshold value into Alert logic. I used 0 in mine, so every detection triggers the Logic App.

Next we have to link our Logic App into this alert rule and that is done by creating new action group.

Action group requires unique name and some kind of display name. In this alert rule we don’t want to send any notifications, so let’s skip it and add the action.

We should have now our Logic App ready to be selected in “select a logic app” dropdown. Remember to change “yes” into “Enable the common alert schema.” selection, because we used that one when the Logic App was created. If the OK button is greyed out like it is in my example picture, wait few minutes and try again. I don’t know why there is a delay, but the alert rule does not seems to detect the Logic App immediately.

Now we just click the “Create alert rule” button and all is set.

You can easily refine the bug report by adjusting the Logic App parameter handling. The alert rule will send aggregated bug report within the period, so if you have 10 matching errors inside the period, you will get only one bug into Azure DevOps.