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

AspNet HttpModule: Fix bug when adding to web.config #584

Merged

Conversation

zacharycmontoya
Copy link
Contributor

Changes proposed in this pull request:
The previous implementation of the HttpModule did not behave correctly when it was invoked more than once in one ASP.NET request. Multiple scopes would be created and only the innermost one would be closed, so the root span would not be closed and the Datadog UI would show no traces. The new implementation registers the first HttpModule to hit the event handlers as the only one allowed to create/modify Scope objects, so only one scope is created and closed.

This fix is important to support customers who have other HttpModules in their system web.config that subscribe to the BeginRequest and would like our Tracer to start spans before hitting them. As a result, the customer will enter our HttpModule before the other HttpModule's in their web.config which causes our the ASP.NET application to create and invoke two instances of our HttpModule: once from the web.config declaration and once from the dynamic module after referencing our NuGet package.

@DataDog/apm-dotnet

… Currently, going to the root '/' causes both the DatadogModule and the CustomLoggingModule to run twice.
…t and still operate correctly. Only creates the trace on the first OnBegin and ends the trace on the last OnEnd or OnError.

Small fix

Another small fix

Last small fix

OMG last small fix I swear
…n simultaneously handle adding the module both statically and dynamically.
…rst HttpModule invoked and only respond to the callbacks with that object instance.
@zacharycmontoya zacharycmontoya added type:bug area:automatic-instrumentation Automatic instrumentation managed C# code (Datadog.Trace.ClrProfiler.Managed) labels Dec 10, 2019
@zacharycmontoya zacharycmontoya added this to the 1.11.0 milestone Dec 10, 2019
@zacharycmontoya zacharycmontoya requested a review from a team as a code owner December 10, 2019 19:42
@zacharycmontoya zacharycmontoya self-assigned this Dec 10, 2019
@@ -72,6 +74,13 @@ private void OnBeginRequest(object sender, EventArgs eventArgs)
return;
}

// Only one HttpModule will respond to ASP.NET lifecycle events
// Do nothing if another HttpModule has already been run
if (httpContext.Items[_httpContextOwningModuleKey] is TracingHttpModule)
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume this accounts for it not being in the Items list?

Copy link
Contributor Author

@zacharycmontoya zacharycmontoya Dec 10, 2019

Choose a reason for hiding this comment

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

That's correct. If the key is not present, then the Items lookup will return null, and a null value will cause the is check to return false. I thought to follow the previous convention with this style of check, but would there be another way to express this concisely?

…t handlers for one TracingHttpModule per HttpApplication object.
lucaspimentel
lucaspimentel previously approved these changes Dec 10, 2019
@zacharycmontoya zacharycmontoya merged commit 0de9f4f into master Dec 10, 2019
@zacharycmontoya zacharycmontoya deleted the zach/feature/aspnet-httpmodule-resiliency-final branch December 10, 2019 23:19
MikeGoldsmith pushed a commit to lightstep/ls-trace-dotnet that referenced this pull request Mar 20, 2020
The previous implementation of the HttpModule did not behave correctly when it was invoked more than once in one ASP.NET request. Multiple scopes would be created and only the innermost one would be closed, so the root span would not be closed and the Datadog UI would show no traces. The new implementation registers the first HttpModule to hit the event handlers as the only one allowed to create/modify Scope objects, so only one scope is created and closed.

This fix is important to support customers who have other HttpModules in their system web.config that subscribe to the BeginRequest and would like our Tracer to start spans before hitting them. As a result, the customer will enter our HttpModule before the other HttpModule's in their web.config which causes our the ASP.NET application to create and invoke two instances of our HttpModule: once from the web.config declaration and once from the dynamic module after referencing our NuGet package.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:automatic-instrumentation Automatic instrumentation managed C# code (Datadog.Trace.ClrProfiler.Managed) type:bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants