Skip to content

Commit 352e75d

Browse files
committed
Address PR feedback
1 parent 21dbf7c commit 352e75d

1 file changed

Lines changed: 37 additions & 20 deletions

File tree

src/Datadog.Trace.AspNet/TracingHttpModule.cs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ private void OnBeginRequest(object sender, EventArgs eventArgs)
118118

119119
tags.SetAnalyticsSampleRate(IntegrationId, tracer.Settings, enabledWithGlobalSetting: true);
120120

121-
httpContext.Items[_httpContextScopeKey] = scope;
122-
123121
// Decorate the incoming HTTP Request with distributed tracing headers
124122
// in case the next processor cannot access the stored Scope
125123
// (e.g. WCF being hosted in IIS)
126124
if (HttpRuntime.UsingIntegratedPipeline)
127125
{
128126
SpanContextPropagator.Instance.Inject(scope.Span.Context, httpRequest.Headers.Wrap());
129127
}
128+
129+
httpContext.Items[_httpContextScopeKey] = scope;
130130
}
131131
catch (Exception ex)
132132
{
@@ -149,28 +149,33 @@ private void OnEndRequest(object sender, EventArgs eventArgs)
149149
if (sender is HttpApplication app &&
150150
app.Context.Items[_httpContextScopeKey] is Scope scope)
151151
{
152-
if (HttpRuntime.UsingIntegratedPipeline)
153-
{
154-
scope.Span.SetHeaderTags<IHeadersCollection>(app.Context.Response.Headers.Wrap(), Tracer.Instance.Settings.HeaderTags, defaultTagPrefix: SpanContextPropagator.HttpResponseHeadersTagPrefix);
155-
}
156-
157-
scope.Span.SetHttpStatusCode(app.Context.Response.StatusCode, isServer: true);
158-
159-
if (app.Context.Items[SharedConstants.HttpContextPropagatedResourceNameKey] is string resourceName
160-
&& !string.IsNullOrEmpty(resourceName))
152+
try
161153
{
162-
scope.Span.ResourceName = resourceName;
154+
if (HttpRuntime.UsingIntegratedPipeline)
155+
{
156+
scope.Span.SetHeaderTags<IHeadersCollection>(app.Context.Response.Headers.Wrap(), Tracer.Instance.Settings.HeaderTags, defaultTagPrefix: SpanContextPropagator.HttpResponseHeadersTagPrefix);
157+
}
158+
159+
scope.Span.SetHttpStatusCode(app.Context.Response.StatusCode, isServer: true);
160+
161+
if (app.Context.Items[SharedConstants.HttpContextPropagatedResourceNameKey] is string resourceName
162+
&& !string.IsNullOrEmpty(resourceName))
163+
{
164+
scope.Span.ResourceName = resourceName;
165+
}
166+
else
167+
{
168+
string path = UriHelpers.GetCleanUriPath(app.Request.Url);
169+
scope.Span.ResourceName = $"{app.Request.HttpMethod.ToUpperInvariant()} {path.ToLowerInvariant()}";
170+
}
171+
172+
scope.Dispose();
163173
}
164-
else
174+
finally
165175
{
166-
string path = UriHelpers.GetCleanUriPath(app.Request.Url);
167-
scope.Span.ResourceName = $"{app.Request.HttpMethod.ToUpperInvariant()} {path.ToLowerInvariant()}";
176+
// Clear the context to make sure another TracingHttpModule doesn't try to close the same scope
177+
TryClearContext(app.Context);
168178
}
169-
170-
scope.Dispose();
171-
172-
// Clear the context to make sure another TracingHttpModule doesn't try to close the same scope
173-
app.Context.Items.Remove(_httpContextScopeKey);
174179
}
175180
}
176181
catch (Exception ex)
@@ -208,5 +213,17 @@ private void OnError(object sender, EventArgs eventArgs)
208213
Log.Error(ex, "Datadog ASP.NET HttpModule instrumentation error");
209214
}
210215
}
216+
217+
private void TryClearContext(HttpContext context)
218+
{
219+
try
220+
{
221+
context.Items.Remove(_httpContextScopeKey);
222+
}
223+
catch (Exception ex)
224+
{
225+
Log.Error(ex, "Error while clearing the HttpContext");
226+
}
227+
}
211228
}
212229
}

0 commit comments

Comments
 (0)