HomeBlogTexas Ranger

Previous arcticle: APM - Overview - what does it mean for DevOps?

The previous blog post gave an overview over the various APM use-cases and how Application Insights can help with them. This post covers the installation of Application Insights for web applications.

Pitfall 1 - Application Insights documentation does not keep up to speed

The advancements of Application Insights are quite dynamic and the documentation cannot always keep up with them.

A word on .NET core web-apps

Microsoft is constantly working on Application Insights-support for .NET core and many features are there already. However, some important things like exposing the SQL-statements for a service call or the so called Live stream only work with "plain" .NET at the moment.

Install Status Monitor and Insights Extension

In case of an azure app-service web-app, the Insights Extension gets installed the first time you click the Application Insights link in the web-apps blade. It will automatically create an Application Insights instance in azure for you. The status monitor/app extension is essential for correlating the various parts of a transaction, e.g. ajax request -> service call -> other service call -> database. It is also needed for exposing clear-text SQL-statements.

"Application Insights menu in app service blade"

Installing Application Insights from Visual Studio is easy: Just use option Add application insights to project during project-creation:

"install Application Insights from Visual Studio"

Or use the context menu and Add => Application Insights Telemetry:

"add Application Insights from Visual Studio"

Pitfall 2 - Application Insights extension installed in azure?

I might happen that the the Application Insights extension is not installed correctly when the Application Insights instance is created from Visual Studio. To recheck, go to your web app instance in azure an open Extensions. If the Application Insights extension ist not there, use Add to add it

"azure app service extensions"

Pitfall 3 - Instrumenation Key no set correctly

The instrumenation key links you app to the Application Insights instance in azure. It's located in ApplicationInsights.config in your project's root folder:

"azure app service extensions"

Install Application Insights SDK

In Visual Studio, the Application Insights SDK is added automatically during project-creation.

To add more Application Insights features use the context menu and Configure Application Insights.

"Configure Application Insights from Visual Studio"

Install JavaScript Telemetry

For JS telemetry you need to include the Application Insights JavaScript snippet to each of your web-pages.

  <script type="text/javascript">
    var appInsights = window.appInsights || function (config) { function i(config) { t[config] = function () { var i = arguments; t.queue.push(function () { t[config].apply(t, i) }) } } var t = { config: config }, u = document, e = window, o = "script", s = "AuthenticatedUserContext", h = "start", c = "stop", l = "Track", a = l + "Event", v = l + "Page", y = u.createElement(o), r, f; y.src = config.url || "https://az416426.vo.msecnd.net/scripts/a/ai.0.js"; u.getElementsByTagName(o)[0].parentNode.appendChild(y); try { t.cookie = u.cookie } catch (p) { } for (t.queue = [], t.version = "1.0", r = ["Event", "Exception", "Metric", "PageView", "Trace", "Dependency"]; r.length;)i("track" + r.pop()); return i("set" + s), i("clear" + s), i(h + a), i(c + a), i(h + v), i(c + v), i("flush"), config.disableExceptionTracking || (r = "onerror", i("_" + r), f = e[r], e[r] = function (config, i, u, e, o) { var s = f && f(config, i, u, e, o); return s !== !0 && t["_" + r](config, i, u, e, o), s }), t } ({
      instrumentationKey: "<your AI instrumenation key>",
      disableCorrelationHeaders: false,
      enableDebug: true
    });
    window.appInsights = appInsights;
  </script>

Pitfall 4 - Instrumentation key not set end-to-end correlation not working

Please pay special attention to the following config parameters:

instrumentationKey: "<your AI instrumenation key>",
disableCorrelationHeaders: false,
enableDebug: true

Similar to "Pitfall 3", the JS telemetry API needs to know where to send its data. This is what the instrumentation key is needed for. The switch disableCorrelationHeaders seems very strange. Setting it to "false" means: "Enable correlation". :-)

We will cover the client- and server-side telematry API in a later post but we will anticipate this: In order to trace user actions end-to-end, we have to tell Application Insights how to correlate them. This is done by operation.id. For example:

function searchProducts() {
    ai.context.operation.id = createGuid();
    ai.trackEvent("search products");
    search...
}