aspnet / Microsoft.AspNet.TelemetryCorrelation

Microsoft.AspNet.CorrelationIdTracker module for tracking multi-tier activities
Other
13 stars 18 forks source link

Application Insights logs double request telemetry with rewrite rule #73

Closed rajkumar-rangaraj closed 4 years ago

rajkumar-rangaraj commented 4 years ago

Describe your environment.

Application Insights creates two request telemetry for ASP.NET requests which are processed with IIS UrlRewrite module

Steps to reproduce. Describe exactly how to reproduce the error. Include a code sample if applicable.

  1. Create default ASP.NET MVC app, install ApplicationInsights SDK, set up ikey to send data
  2. Host it on IIS
  3. Create rewrite rule as following:
<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <remove name="TelemetryCorrelationHttpModule" />
      <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="managedHandler" />
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
        <rewrite>
            <rules>
                <rule name="AboutRedirect" stopProcessing="true">
                    <match url="Home/About" />
                    <action type="Rewrite" url="home/index" />
                </rule>
            </rules>
        </rewrite>
  </system.webServer>
</configuration>
  1. Hit the webpage “http://localhost/Home/About” – as expected the index page will be displayed.
  2. Navigate to Application Insights resource and observe following:

image

What is the expected behavior? What did you expect to see? Only one entry should be displayed.

What is the actual behavior? What did you see instead? Right now this is a double logging for the same request.

More Information

Rewrite

In case of rewrite, a new request context is created, called the child request, and it goes through the entire IIS/ASP.NET integrated pipeline. The child request can be mapped to any of the handlers configured in IIS, and it's execution is no different than it would be if it was received via the HTTP stack. The parent request jumps ahead in the pipeline to the end request notification, and waits for the child request to complete. When the child request completes, the parent request executes the end request notifications and completes itself.

Original Request (Parent Request) Rewritten Request (Child Request)
image image

Reference: https://docs.microsoft.com/en-us/archive/msdn-magazine/2002/september/asp-net-request-processing-filtering-and-content-redirection

If a module aborts normal message handling during an event handler by calling CompleteRequest, the ASP.NET HTTP pipeline interrupts processing after that event completes. However, EndRequest and the events that follow are still fired. Thus, any modules that acquired valuable resources before processing was terminated have a chance to clean up those resources. For instance, if a module acquired a lock against shared state in its BeginRequest event handler, it can release the lock in its EndRequest event handler and be confident that the right thing will happen. The EndRequest event will fire even if some other module calls CompleteRequest and the HTTP message is never delivered to a handler.

What causes double telemetry issue?

Why Endrequest of TelemetryCorrelationHttpModule creates new Activity when it does not have BeginRequest?

Proposal