Skip to content

Commit b32e9f8

Browse files
Fixed to track end request from other modules.
1 parent b613b9e commit b32e9f8

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

src/Microsoft.AspNet.TelemetryCorrelation/TelemetryCorrelationHttpModule.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ namespace Microsoft.AspNet.TelemetryCorrelation
1515
public class TelemetryCorrelationHttpModule : IHttpModule
1616
{
1717
private const string BeginCalledFlag = "Microsoft.AspNet.TelemetryCorrelation.BeginCalled";
18+
19+
// ServerVariable set only on rewritten HttpContext by URL Rewrite module.
20+
private const string URLRewriteRewrittenRequest = "IIS_WasUrlRewritten";
21+
22+
// ServerVariable set on every request if URL module is registered in HttpModule pipeline.
23+
private const string URLRewriteModuleVersion = "IIS_UrlRewriteModule";
24+
1825
private static MethodInfo onStepMethodInfo = null;
1926

2027
static TelemetryCorrelationHttpModule()
@@ -106,20 +113,21 @@ private void Application_EndRequest(object sender, EventArgs e)
106113
// BeginRequest has never been called
107114
if (!context.Items.Contains(BeginCalledFlag))
108115
{
109-
// Exception happened before BeginRequest
110-
if (context.Error != null)
116+
// 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.
117+
// 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.
118+
// The parent request jumps ahead in the pipeline to the end request notification, and waits for the child request to complete.
119+
// When the child request completes, the parent request executes the end request notifications and completes itself.
120+
// Do not create activity for parent request. Parent request has IIS_UrlRewriteModule ServerVariable with success response code.
121+
// Child request contains an additional ServerVariable named - IIS_WasUrlRewritten.
122+
// Track failed response activity: Different modules in the pipleline has ability to end the response. For example, authentication module could set HTTP 401 in OnBeginRequest and end the response.
123+
if (context.Request.ServerVariables != null && context.Request.ServerVariables[URLRewriteRewrittenRequest] == null && context.Request.ServerVariables[URLRewriteModuleVersion] != null && context.Response.StatusCode == 200)
111124
{
112-
// Activity has never been started
113-
ActivityHelper.CreateRootActivity(context, ParseHeaders);
125+
trackActivity = false;
114126
}
115127
else
116128
{
117-
// 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.
118-
// 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.
119-
// The parent request jumps ahead in the pipeline to the end request notification, and waits for the child request to complete.
120-
// When the child request completes, the parent request executes the end request notifications and completes itself.
121-
// Ignore creating root activity for parent request as control got transferred from rewrite module to EndRequest with no request flow.
122-
trackActivity = false;
129+
// Activity has never been started
130+
ActivityHelper.CreateRootActivity(context, ParseHeaders);
123131
}
124132
}
125133

0 commit comments

Comments
 (0)