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
Merged
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions samples-aspnet/Samples.WebForms/DDCustomLoggingModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;
using Datadog.Trace;

namespace Samples.WebForms
{
public class DDCustomLoggingModule : IHttpModule
{
public void Dispose()
{
// Nothing to do
}

public void Init(HttpApplication context)
{
context.BeginRequest += OnBeginRequest;
context.EndRequest += OnEndRequest;
}

private void OnBeginRequest(object sender, EventArgs eventArgs)
{
Debug.Write("\nDDCustomLoggingModule: OnBeginRequest: call from: " + HttpContext.Current.Request.Path + ". TraceID:" + CorrelationIdentifier.TraceId + "\n");
}

private void OnEndRequest(object sender, EventArgs eventArgs)
{
Debug.Write("\nDDCustomLoggingModule: OnEndRequest: call from: " + HttpContext.Current.Request.Path + ". TraceID:" + CorrelationIdentifier.TraceId + "\n");
}
}
}
5 changes: 5 additions & 0 deletions samples-aspnet/Samples.WebForms/Samples.WebForms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<Content Include="Web.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="DDCustomLoggingModule.cs" />
<Compile Include="Database\Elasticsearch.aspx.cs">
<DependentUpon>Elasticsearch.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType>
Expand Down Expand Up @@ -196,6 +197,10 @@
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Datadog.Trace.AspNet\Datadog.Trace.AspNet.csproj">
<Project>{f2780421-295d-4187-912c-71d34ec35c27}</Project>
<Name>Datadog.Trace.AspNet</Name>
</ProjectReference>
<ProjectReference Include="..\..\src\Datadog.Trace.ClrProfiler.Managed\Datadog.Trace.ClrProfiler.Managed.csproj">
<Project>{85f35aaf-d102-4960-8b41-3bd9cbd0e77f}</Project>
<Name>Datadog.Trace.ClrProfiler.Managed</Name>
Expand Down
2 changes: 2 additions & 0 deletions samples-aspnet/Samples.WebForms/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<remove name="FormsAuthentication" />
<remove name="TelemetryCorrelationHttpModule" />
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
<add name="DatadogModule" type="Datadog.Trace.AspNet.TracingHttpModule, Datadog.Trace.AspNet"/>
<add name="DDArbitrarynameForCustomLoggingModule" type="Samples.WebForms.DDCustomLoggingModule, Samples.WebForms"/>
</modules>
</system.webServer>
<runtime>
Expand Down
20 changes: 16 additions & 4 deletions src/Datadog.Trace.AspNet/TracingHttpModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Web;
using Datadog.Trace.ExtensionMethods;
using Datadog.Trace.Logging;
Expand All @@ -15,8 +16,11 @@ public class TracingHttpModule : IHttpModule

private static readonly Vendors.Serilog.ILogger Log = DatadogLogging.GetLogger(typeof(TracingHttpModule));

private static HashSet<HttpApplication> registeredEventHandlers = new HashSet<HttpApplication>();

private readonly string _httpContextScopeKey;
private readonly string _requestOperationName;
private HttpApplication _httpApplication;

/// <summary>
/// Initializes a new instance of the <see cref="TracingHttpModule" /> class.
Expand All @@ -40,15 +44,23 @@ public TracingHttpModule(string operationName)
/// <inheritdoc />
public void Init(HttpApplication httpApplication)
{
httpApplication.BeginRequest += OnBeginRequest;
httpApplication.EndRequest += OnEndRequest;
httpApplication.Error += OnError;
_httpApplication = httpApplication;

// The first HttpModule to run Init for this HttpApplication will register for events
if (registeredEventHandlers.Add(httpApplication))
{
httpApplication.BeginRequest += OnBeginRequest;
httpApplication.EndRequest += OnEndRequest;
httpApplication.Error += OnError;
}
}

/// <inheritdoc />
public void Dispose()
{
// Nothing to do...
// Remove the HttpApplication mapping so we don't keep the object alive
registeredEventHandlers.Remove(_httpApplication);
_httpApplication = null;
}

private void OnBeginRequest(object sender, EventArgs eventArgs)
Expand Down