Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create single activity for rewrite rule #74

Merged
merged 3 commits into from
Jun 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ namespace Microsoft.AspNet.TelemetryCorrelation
public class TelemetryCorrelationHttpModule : IHttpModule
{
private const string BeginCalledFlag = "Microsoft.AspNet.TelemetryCorrelation.BeginCalled";

// ServerVariable set only on rewritten HttpContext by URL Rewrite module.
private const string URLRewriteRewrittenRequest = "IIS_WasUrlRewritten";

// ServerVariable set on every request if URL module is registered in HttpModule pipeline.
private const string URLRewriteModuleVersion = "IIS_UrlRewriteModule";

private static MethodInfo onStepMethodInfo = null;

static TelemetryCorrelationHttpModule()
Expand Down Expand Up @@ -98,18 +105,36 @@ private void Application_PreRequestHandlerExecute(object sender, EventArgs e)
private void Application_EndRequest(object sender, EventArgs e)
{
AspNetTelemetryCorrelationEventSource.Log.TraceCallback("Application_EndRequest");
bool trackActivity = true;

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

// EndRequest does it's best effort to notify that request has ended
// BeginRequest has never been called
if (!context.Items.Contains(BeginCalledFlag))
{
// Activity has never been started
ActivityHelper.CreateRootActivity(context, ParseHeaders);
// 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.
// Do not create activity for parent request. Parent request has IIS_UrlRewriteModule ServerVariable with success response code.
// Child request contains an additional ServerVariable named - IIS_WasUrlRewritten.
// 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.
if (context.Request.ServerVariables != null && context.Request.ServerVariables[URLRewriteRewrittenRequest] == null && context.Request.ServerVariables[URLRewriteModuleVersion] != null && context.Response.StatusCode == 200)
{
trackActivity = false;
}
else
{
// Activity has never been started
ActivityHelper.CreateRootActivity(context, ParseHeaders);
}
}

ActivityHelper.StopAspNetActivity(context.Items);
if (trackActivity)
{
ActivityHelper.StopAspNetActivity(context.Items);
}
}
}
}