Skip to content

Commit 8cc3127

Browse files
authored
Merge pull request #74 from rajkumar-rangaraj/master
2 parents 3818245 + b32e9f8 commit 8cc3127

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

src/Microsoft.AspNet.TelemetryCorrelation/TelemetryCorrelationHttpModule.cs

Lines changed: 28 additions & 3 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()
@@ -98,18 +105,36 @@ private void Application_PreRequestHandlerExecute(object sender, EventArgs e)
98105
private void Application_EndRequest(object sender, EventArgs e)
99106
{
100107
AspNetTelemetryCorrelationEventSource.Log.TraceCallback("Application_EndRequest");
108+
bool trackActivity = true;
101109

102110
var context = ((HttpApplication)sender).Context;
103111

104112
// EndRequest does it's best effort to notify that request has ended
105113
// BeginRequest has never been called
106114
if (!context.Items.Contains(BeginCalledFlag))
107115
{
108-
// Activity has never been started
109-
ActivityHelper.CreateRootActivity(context, ParseHeaders);
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)
124+
{
125+
trackActivity = false;
126+
}
127+
else
128+
{
129+
// Activity has never been started
130+
ActivityHelper.CreateRootActivity(context, ParseHeaders);
131+
}
110132
}
111133

112-
ActivityHelper.StopAspNetActivity(context.Items);
134+
if (trackActivity)
135+
{
136+
ActivityHelper.StopAspNetActivity(context.Items);
137+
}
113138
}
114139
}
115140
}

0 commit comments

Comments
 (0)