If you get to the stage where you are deploying your application on a very frequent basis and you are relying on automated tests for the bulk of your quality assurance then a mechanism to alert you when things go wrong becomes crucial. You should have something in place anyway of course but in practice I suspect that application monitoring is either frequently overlooked or remains stubbornly on the to-do list.
A successful continuous delivery pipeline implementation shouldn’t rely on the telephone or email as the alerting system and in this post in my blog series on implementing continuous delivery with TFS we look at how to integrate relevant parts of Microsoft’s Application Insights (AI) tooling in to the pipeline. If you need to get up to speed with its capabilities I have a Getting Started blog post here. As a quick refresher AI is a suite of components that integrate with your application and servers and which sends telemetry to the Azure Portal. As a bonus, not only do you get details of diagnostic issues but also rich analytics on how you application is being used.
AI isn’t just one component and in fact there are at least three main ways in which AI can be configured to provide diagnostic and analytic information:
- Adding the Application Insights SDK to your application.
- Installing Status Monitor on an IIS server.
- Creating Web Tests that monitor the availability of an HTTP endpoint available on the public Internet.
One key point to appreciate with AI and continuous delivery pipelines is that unless you do something about it AI will put the data it collects from the different stages of your pipeline in one ‘bucket’ and you won’t easily be able to differentiate what came from where. Happily there is a way to address this as we’ll see below. Before starting to configure AI there are some common preparatory steps that need to be addressed so let’s start with those.
If you have been following along with this series of blog posts you will be aware that so far we have only created DAT and DQA stages of the pipeline. Although not strictly necessary I created a PRD stage of the pipeline to represent production: if nothing else it’s handy for demonstrations where your audience may expect to see the pipeline endpoint. I won’t detail all the configuration steps here as they are all covered by previous blog posts however the whole exercise only took a few minutes. As things stand none of these stages exposes our sample web application to the public Internet however this is necessary for the creation of Web Tests. We can fix this in the Azure portal by adding an HTTP endpoint to the VM that runs IIS:
Our sample application is now available using a URL that begins with the cloud service name and includes the website name, for example http://mycloudservice.cloudapp.net/mywebapp. Be aware that this technique probably falls foul of all manner of security best practices however given that my VMs are only on for a few hours each week and it’s a pure demo environment it’s one I’m happy to live with.
The second item of groundwork is to create the containers that will hold the AI data for each stage of the pipeline. You will need to use the new Azure portal for this at https://portal.azure.com. First of all a disclaimer. There are several techniques at our disposal for segregating AI data as discussed in this blog post by Victor Mushkatin, and the comments of this post are worth reading as well since there are some strong opinions. I tried the tagging method but couldn’t get it to work properly and as Victor says in the post this feature is at the early stages of development. In his post Victor creates a new Azure Resource for each pipeline stage however that seemed overly complicated for a demo environment. Instead I opted to create multiple Application Insights Resources in one Azure Resource group. As an aside, resource groups are fairly new to Azure and for any new Azure deployment they should be carefully considered as part of the planning process. For existing deployments you will find that your cloud service is listed as a resource group (containing your VMs) and I chose to use this as the group to contain the Application Insights Resources. Creating new AI resources is very straightforward. Start with the New button and then choose Developer Services > Application Insights. You’ll need to provide a name and then use the arrow selectors to choose Application Type and Resource Group:
I created the following resource groups which represent the stages of my pipeline: CU-DEV, CU-DAT, CU-DQA and CU-PRD. What differentiates these groups is their instrumentation keys (often abbreviated to ikey). You’ll need to retrieve the ikeys for each group and the way to do that in the new portal is via Browse > Filter By > Application Insights > $ResourceGroup$ > Settings > Properties where you will see the Instrumentation Key selector.
Add the Application Insights SDK to ContosoUniversity
We can now turn our attention to adding the Application Insights SDK to our Contoso University web application:
- Right-click your web project (ContosoUniversity.Web) within your Visual Studio solution and choose Add Application Insights Telemetry.
- The Add Application Insights to Project dialog opens and invites you to sign in to Azure:
- The first few times I tried to connect to Azure I got errors about not being able to find an endpoint but persistence paid off. I eventually arrived at a dialog that allowed me to choose my MSDN subscription via the Use different account link:
- Having already created my AI resources I used Configure settings to choose the CU-DEV resource:
- Back in the Add Application Insights to Project dialog click on the Add Application Insights to Project link to have Visual Studio perform all the necessary configuration.
At this stage we can run the application and click around to generate telemetry. If you are in Debug mode you can see this in the Output window. After a minute or two you should also see the telemetry start to appear in the Azure portal (Browse > Filter By > Application Insights > CU-DEV).
Configure AI in Contoso University for Pipeline Stages
As things stand deploying Contoso University to other stages of the pipeline will cause telemetry for that stage to be added to the CU-DEV AI resource group. To remedy this carry out the following steps:
- Add an iKey attribute to the appSettings section of Web.config:
<!-- Other settings here -->
<add key="iKey" value="8fc11978-dd5b-7b87-addc-965329534108"/>
- Add a transform to Web.Release.config that consists of a token (__IKEY__) that can be used by Release Management:
<add key="iKey" value="__IKEY__" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
- Add the following code to Application_Start in Global.asax.cs:
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = System.Web.Configuration.WebConfigurationManager.AppSettings["iKey"];
- Remove or comment out the InstrumentationKey section in ApplicationInsights.config.
- In the Release Management client at Configure Apps > Components edit the ContosoUniversity\Deploy Web Site component by adding an IKEY variable to Configuration Variables.
- Still in the Release Management client open the Contoso University\DAT>DQA>PRD release template from Configure Apps > Agent-based Release Templates and edit each stage supplying the iKey value for that stage (see above for how to get this) to the newly added IKEY configuration variable.
After completing these steps you should be able to deploy your application to each stage of the pipeline and see that the Web.config of each stage has the correct iKey. Spinning up the website for that stage and clicking around in it should cause telemetry to be sent to the respective AI resource.
Install Status Monitor on the IIS server
The procedure is quite straightforward as follows:
- On your IIS server (ALMWEB01 if you are following the blog series) download and run the Status Monitor installation package from here.
- With the installation complete you’ll need to sign in to your Microsoft Account after which you’ll be presented with a configuration panel where the CU-DAT, CU-DQA and CU-PRD websites should have been discovered. The control panel lets you specify a separate AI resource for each website after which you’ll need to restart IIS:
- In order to ensure that the domain accounts that the websites are running under have sufficient permissions to collect data make sure that they have been added to the Performance Monitor Users Windows local group.
With this configuration complete you should click around in the websites to confirm that telemetry is being sent to the Azure portal.
Creating Web Tests to monitor HTTP Availability
The configuration for Web Tests takes place in the new Azure portal at https://portal.azure.com. There are two types of test – URL ping and a more involved Multi-step test. I’m just describing the former here as follows:
- In the new portal navigate to the AI resource you want to create tests for and choose the Availability tile:
- This opens the Web Tests pane where you choose Add web test:
- In the Create test pane supply a name and a URL and then use the arrow on Test Locations to choose locations to test from:
After clicking Create you should start to see data being generated within a few seconds.
AI is clearly a very sophisticated solution for providing rich telemetry about your application and the web server hosting and I’m exited about the possibilities it offers. I did encounter a few hurdles in getting it to work though. Initial connection to the Azure portal when trying to integrate the SDK with Contoso University was the first problem and this caused quite a bit of messing around as each failed installation had to be undone. I then found that with AI added to Contoso University the build on my TFS server failed every time. I’m using automatic package restore and I could clearly see what’s happening: every AI NuGet package was being restored correctly with the exception of Microsoft.ApplicationInsights and this was quite rightly causing the build to fail. Locally on my development machine the package restore worked flawlessly. The answer turned out to be an outdated nuget.exe on my build server. The fix is to open an Administrative command prompt at C:\Program Files\Microsoft Team Foundation Server 12.0\Tools and run nuget.exe update -self. Instant fix! This isn’t AI’s fault of course, although it is a mystery why one of the AI NuGets brought this problem to light.
Cheers – Graham
The post Continuous Delivery with TFS: Configure Application Insights appeared first on Please Release Me.
Dr Graham Smith is a former research scientist who got bitten by the programming and database bug so badly that in 2000 he changed careers to become a full-time software developer. Life moves on and Graham currently manages a team of software engineers and specialises in continuous delivery and application lifecycle management with the Team Foundation Server ecosystem.