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 2 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 @@ -98,18 +98,35 @@ 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);
// Exception happened before BeginRequest
if (context.Error != null)
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
{
// Activity has never been started
ActivityHelper.CreateRootActivity(context, ParseHeaders);
}
else
{
// 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally somebody from asp.net team needs to comment on any other situation this may have happened. I can think of one off the top of my head - if there is another module registered before this module and it returned 401 or some actual content from the BeginRequest call. Than Begin will not be called for this module at all. And we will end up loosing the fact of this call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically for UrlRewrite, @TimothyMothra was working on logic that ensured that those will be parented to each other. This logic is somewhere in Application Insights http module. I wonder if this is a different case or the same

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SergeyKanzhelev, @TimothyMothra has worked on a change in application insights where parent and child request has same HttpContext. In this scenario, HttpContext generated at parent and child are different. I internally worked with IIS engineering team to check for the scenarios and came up with this change.

I will investigate the 401 error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Created new HttpModule and registered before TelemetryCorrelationHttpModule in pipeline
	  <modules>
		  <add name="One" type="WebApplication1.MyHttpModule1"/>
		  <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="managedHandler"/>
	  </modules>
  • Added below code to BeginRequest of custom module to respond with HTTP 401.
        private void Application_BeginRequest(object sender, EventArgs e)
        {
            var context = ((HttpApplication)sender).Context;
            context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
        }

Requested got executed in below order and Activity got created

  1. MyHttpModule1.BeginRequest
  2. TelemetryCorrelationHttpModule.BeginRequest
  3. MyHttpModule1.EndRequest
  4. TelemetryCorrelationHttpModule.EndRequest

Will check if we could get someone from ASP.NET team to review this one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to end the response after setting the status. Otherwise the module doesn't actually behave as authentication module as it continues an execution.

// 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.
// Ignore creating root activity for parent request as control got transferred from rewrite module to EndRequest with no request flow.
trackActivity = false;
}
}

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