{"id":143,"date":"2015-01-07T15:45:00","date_gmt":"2015-01-07T15:45:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2015\/01\/07\/application-insights-support-for-multiple-environments-stamps-and-app-versions\/"},"modified":"2022-05-25T05:11:03","modified_gmt":"2022-05-25T13:11:03","slug":"application-insights-support-for-multiple-environments-stamps-and-app-versions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/application-insights-support-for-multiple-environments-stamps-and-app-versions\/","title":{"rendered":"Application Insights support for Multiple Environments, Stamps and App Versions"},"content":{"rendered":"<p><a href=\"http:\/\/azure.microsoft.com\/en-us\/services\/application-insights\/\">Application Insights<\/a> can collect telemetry from your application at any stage in its development, from debugging to live production. By default, all these different streams will go into the same bucket and interfere with each other. For example, if you run heavy stress tests, they could give a false impression of the number of visitors you\u2019ve had at your live site. To avoid this problem, you can create separate application resources in Application Insights to receive the data from each lifecycle stage.<\/p>\n<p>The destination of the telemetry is determined by the instrumentation key (iKey), which is sent along with every telemetry message. In the Application Insights portal, similar events and metrics with the same iKey are aggregated to give you charts of average durations, event counts, the sum of users, and so on. The iKey appears in two places in your project. One is in ApplicationInsights.config: <span style=\"font-family: Consolas\"><span><span style=\"color: #0000ff\"><span style=\"font-size: 9.5pt\">&lt;<\/span><\/span><\/span><span style=\"font-size: 9.5pt\"><span><span style=\"color: #a31515\">InstrumentationKey<\/span><\/span><span><span style=\"color: #0000ff\">><\/span><\/span><span>94843456-2345-3456-4567-324562759284<\/span><span><span style=\"color: #0000ff\">&lt;\/<\/span><\/span><span><span style=\"color: #a31515\">InstrumentationKey<\/span><\/span><\/span><span><span style=\"color: #0000ff;font-size: 9.5pt\">><\/span><\/span><\/span><\/p>\n<p>If your application has web pages, the iKey also appears in a script in the head of every web page. Usually it\u2019s only coded once in a master page such as ViewsShared_Layout.cshtml.<\/p>\n<p>To direct telemetry to different application resources, we can create several resources with different iKeys. Then we only have to change the iKeys in the application at each transition in its lifecycle \u2013 along with other configuration data such as connection strings, certificates, and subscriptions.<\/p>\n<p>Microsoft Azure provides a convenient way to group related resources, so we\u2019ll create groups <strong>Dev<\/strong>, <strong>Staging<\/strong> and <strong>Production<\/strong>.<\/p>\n<p><span style=\"font-size: medium\"><strong>Note:<\/strong> You can get <strong>source code<\/strong> for this demo app <strong><a href=\"https:\/\/github.com\/AppInsightsSamples\/EnvironmentSupport\">here<\/a><\/strong>. Please, remember to replace iKey in web.config with your app iKey.<\/span><\/p>\n<h2><strong>Set up several App Insights resources for each app<\/strong><\/h2>\n<p>1) When you add Application Insights to your project, set the Resource Group to DEV:<br \/>\n<img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image001\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/0458.clip_image001_thumb_59425307.png\" alt=\"clip_image001\" width=\"660\" height=\"381\" border=\"0\" \/><\/p>\n<p>2) Create two additional Application Insights resources for each application, and place them in resource groups named STAGING and PRODUCTION.<br \/>\n<img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image002\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/5340.clip_image002_thumb_1BB4F53E.png\" alt=\"clip_image002\" width=\"668\" height=\"364\" border=\"0\" \/><\/p>\n<p>3) Obtain the iKey for each resource for your application. To find it in the Application Insights portal, open Properties.<br \/>\n<img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image003\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/0871.clip_image003_thumb_3B63CF06.png\" alt=\"clip_image003\" width=\"702\" height=\"330\" border=\"0\" \/><\/p>\n<p>Now you have three resources for each Application, one in each resource group. And you have the iKeys: to switch the telemetry stream to one resource or another, you only have to change the iKeys wherever they occur in the application code.<\/p>\n<p>The benefit of grouping the apps like this is that you can manage all the alerts, billing, permissions and other Azure resources like websites, storage, SQL etc. for DEV as one group, all the STAGING as another, and so on.<\/p>\n<h3>Switching target resource reliably<\/h3>\n<p>Changing the iKey in several parts of the application is error-prone, so let\u2019s arrange things so we only have to update the key in one place.<\/p>\n<p>1) Add iKey as a property in <strong>Web.config<\/strong>:<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image004\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/4111.clip_image004_thumb_142985D1.png\" alt=\"clip_image004\" width=\"578\" height=\"71\" border=\"0\" \/><\/p>\n<p>2) Instead of using the iKey from ApplicationInsights.config, we\u2019ll set it in the code. In global.asax.cs:<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image005\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/8836.clip_image005_thumb_0893C892.png\" alt=\"clip_image005\" width=\"664\" height=\"108\" border=\"0\" \/><\/p>\n<p>To avoid confusion, remove the <InstrumentationKey> node from ApplicationInsights.config<\/p>\n<p>3) Configure the web pages to pick up same iKey configuration. This is the script usually found in ViewShared_Layout.cshtml.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image007\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/3652.clip_image007_thumb_041D47CB.png\" alt=\"clip_image007\" width=\"830\" height=\"107\" border=\"0\" \/><\/p>\n<p>4) Don\u2019t forget to update your Web.config with appropriate iKey configuration during the deployment process. You might devise a way of setting it appropriately as part of your build, but I\u2019ll leave that to you.<\/p>\n<h2>Multi stamp support<\/h2>\n<p>While the above solution completely separates the telemetry from different environments, which is good for dev|test|production environments, it doesn\u2019t work if you want to compare the telemetry between them. For example, you might be running the same app on multiple clusters and want to compare their performance; or you might build an on-prem app and want every telemetry item from each on-prem installation to be tagged with CustomerID.<\/p>\n<p>The Application Insights SDK lets you do this by tagging all the telemetry sent from a particular environment with arbitrary tags. Then in Metrics Explorer or Diagnostic Search, you can choose to see the data either aggregated all-up, or segmented or filtered by tag value.<\/p>\n<p>Here is how to do it:<\/p>\n<p>1) Create a new \u201ctag\u201d setting in web.config. Again, you\u2019ll change this value from one environment to another.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image008\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/2465.clip_image008_thumb_51B9744B.png\" alt=\"clip_image008\" width=\"545\" height=\"126\" border=\"0\" \/><\/p>\n<p>2) Create a telemetry initializer class.<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image009\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/4137.clip_image009_thumb_4623B70C.png\" alt=\"clip_image009\" width=\"795\" height=\"172\" border=\"0\" \/><\/p>\n<p>As you can see I\u2019m adding to a context property bag key \u201ctags\u201d with value from web.config.<\/p>\n<p>3) Hook up our initializer:<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image010\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/5226.clip_image010_thumb_1ADF2005.png\" alt=\"clip_image010\" width=\"498\" height=\"111\" border=\"0\" \/><\/p>\n<p>This would add property \u201ctags\u201d to every telemetry sent by the AI components. In Debug mode, you can see it in the Visual Studio Output window:<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image011\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/2577.clip_image011_thumb_41AD3645.png\" alt=\"clip_image011\" width=\"836\" height=\"115\" border=\"0\" \/><\/p>\n<p>Or in search results, for example, in Failed Request details by clicking on \u201c\u2026\u201d you can see it in the list of attributes<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image012\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/7380.clip_image012_thumb_01771CCB.png\" alt=\"clip_image012\" width=\"588\" height=\"315\" border=\"0\" \/><\/p>\n<p>In upcoming releases of Metrics Explorer and Search you will be able to segment and\/or filter your data by \u201ctags\u201d.<\/p>\n<h2>Version to Version comparisons<\/h2>\n<p>When running multiple version at the same time, for example in A\/B testing scenario, it\u2019s good to be able to compare and slice and dice telemetry based on app version. To support this scenario, Application Insights SDK lets you to provide your version id as a standard telemetry attribute.<\/p>\n<p>For example:<\/p>\n<p>1) In AssemblyInfo.cs set the version information<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image013\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/8688.clip_image013_thumb_6F2E5608.png\" alt=\"clip_image013\" width=\"738\" height=\"90\" border=\"0\" \/><\/p>\n<p>2) Add to Context Initializer class ability to set the version<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image014\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/8272.clip_image014_thumb_5CE58F46.png\" alt=\"clip_image014\" width=\"764\" height=\"216\" border=\"0\" \/><\/p>\n<p>Now you can search or slice by Application version field:<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image015\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/4075.clip_image015_thumb_35AB4611.png\" alt=\"clip_image015\" width=\"773\" height=\"395\" border=\"0\" \/><\/p>\n<p>With the support of segmentation in Metrics Explorer you can now compare two version like this:<\/p>\n<p><img decoding=\"async\" style=\"padding-top: 0px;padding-left: 0px;padding-right: 0px;border: 0px\" title=\"clip_image017\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/01\/7230.clip_image017_thumb_75752C96.jpg\" alt=\"clip_image017\" width=\"784\" height=\"326\" border=\"0\" \/><\/p>\n<h2>Summary<\/h2>\n<p>As you\u2019ve seen Application Insight provides rather flexible model. It allow you to segregate telemetry by instrumentation key. It enables you to further segment telemetry within a key by adding custom attributes as well as application version. In this article I\u2019ve used those features to show how to implement multi-environment and multi-stamp support.<\/p>\n<p>Support for multiple environments, stamps and app version is rather broad area. There are multiple ways you can implement that for your particular project. I\u2019m very interested in the feedback on the scenarios you have for multi-environment support and how you manage your telemetry now. Also, if you have ideas on how Application Insight could help you do that better, please, do not hesitate to share them with us. It is up to all of us to make Application Insights better!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Application Insights can collect telemetry from your application at any stage in its development, from debugging to live production. By default, all these different streams will go into the same bucket and interfere with each other. For example, if you run heavy stress tests, they could give a false impression of the number of visitors [&hellip;]<\/p>\n","protected":false},"author":131,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[224,1],"tags":[],"class_list":["post-143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","category-devops"],"acf":[],"blog_post_summary":"<p>Application Insights can collect telemetry from your application at any stage in its development, from debugging to live production. By default, all these different streams will go into the same bucket and interfere with each other. For example, if you run heavy stress tests, they could give a false impression of the number of visitors [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/131"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=143"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}