0

I have an MVC web application. It conditionally initializes test data in a database during Application_Start() while running in the debug config. This initialization is exceedingly slow apparently thanks to a fellow developer adding application insights tracking to the project. You see, every time we save to the DB via entity framework, we now hit the following on the callstack:

System.Web.dll!System.Web.HttpContext.Request.get()
Microsoft.AI.Web.dll!Microsoft.ApplicationInsights.Web.Implementation.HttpContextExtensions.GetRequest(System.Web.HttpContext context)
Microsoft.AI.Web.dll!Microsoft.ApplicationInsights.Web.Implementation.WebTelemetryInitializerBase.Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)
Microsoft.ApplicationInsights.dll!Microsoft.ApplicationInsights.TelemetryClient.Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)

HttpContext.Request.Get()!!! No kidding this'll be slow. We are doing lots of saves during the data population. Granted, there are optimizations to be had here, but this is a hairy ball of mess that I didn't write and would rather avoid cracking open.

Basically, I want Application Insights to stop doing any web requests. Preferably, it would do nothing at all in debug, but as long as it performs well, I don't mind. I've tried setting:

TelemetryConfiguration.Active.DisableTelemetry = true;

but this apparently doesn't affect whatever codepath is calling TelemetryClient.Initialize(). Any pointers?

ChiralMichael
  • 1,214
  • 9
  • 20
  • Confused..... all it is doing is accessing the HttpContext.Request object; it's not doing any Web Requests to remote services. Are you sure that is the bottleneck? – tomasr Jul 07 '16 at 23:07
  • @tomasr, No, I'm not sure. I do note that *every time* I break while hung, I am in this call (about 5 seconds to return each time). I'm assuming that this means I'm spending most of my time here. You're right, though, this is the property getter. Not sure why it'd be so slow. I am guessing it's a lazy load. – ChiralMichael Jul 07 '16 at 23:18
  • Possible duplicate of http://stackoverflow.com/a/23254776/4848251 – Ozan Gunceler Jul 07 '16 at 23:31
  • Thanks, Ozan, but I don't want to remove the package. I need it for production. – ChiralMichael Jul 07 '16 at 23:39

2 Answers2

3

The best way I know of to stop AI from running is to use a web.config transform for your Debug configuration to remove it. This will ensure it does not register for any web requests.

Contents of Web.Debug.config

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
      <httpModules>
        <add xdt:Transform="Remove" xdt:Locator="Match(name)" name="ApplicationInsightsWebTracking" />
      </httpModules>
  </system.web>
  <system.webServer>
    <modules>
      <add xdt:Transform="Remove" xdt:Locator="Match(name)" name="ApplicationInsightsWebTracking"  />
    </modules>
  </system.webServer>
</configuration>
James Davis - MSFT
  • 1,736
  • 10
  • 12
  • Nice! Exactly what I'm looking for. Didn't think of that angle. – ChiralMichael Jul 07 '16 at 23:41
  • 1
    Couple of comments for posterity: I ALSO had to condition the setting of instrumentation key in an #if DEBUG. Apparently, ANY initialization of Application Insights will trigger the behavior I saw. Also note that transforms don't run locally by default. Enable them via the gist here: https://gist.github.com/EdCharbeneau/9135216 – ChiralMichael Jul 08 '16 at 17:51
0

I observed slowness in Application Insights internal code when debugger was attached.

It was caused by throwing a lot of exceptions around HttpContext.Request (which were then handled, so they did not cause any other issue than slowness).

Each of these helped for me:

  • call this somewhere early: TelemetryConfiguration.Active.DisableTelemetry = true;
  • Tools -> Options... -> Debugging -> General -> check "Enable Just My Code"
Milan Laslop
  • 199
  • 2
  • 6