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

Fix ASP.NET Core ExceptionFilter prevents recording exception #3475

Merged
merged 14 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
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 @@ -45,6 +45,7 @@ internal class HttpInListener : ListenerHandler
private readonly PropertyFetcher<HttpContext> startContextFetcher = new("HttpContext");
private readonly PropertyFetcher<HttpContext> stopContextFetcher = new("HttpContext");
private readonly PropertyFetcher<Exception> stopExceptionFetcher = new("Exception");
private readonly PropertyFetcher<Exception> stopExceptionFilterFetcher = new("ExceptionContext.Exception");
alanwest marked this conversation as resolved.
Show resolved Hide resolved
private readonly PropertyFetcher<object> beforeActionActionDescriptorFetcher = new("actionDescriptor");
private readonly PropertyFetcher<object> beforeActionAttributeRouteInfoFetcher = new("AttributeRouteInfo");
private readonly PropertyFetcher<string> beforeActionTemplateFetcher = new("Template");
Expand Down Expand Up @@ -273,30 +274,49 @@ public override void OnCustom(string name, Activity activity, object payload)

public override void OnException(Activity activity, object payload)
{
if (activity.IsAllDataRequested)
if (!activity.IsAllDataRequested)
{
if (!this.stopExceptionFetcher.TryFetch(payload, out Exception exc) || exc == null)
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnException));
return;
}
return;
}

if (this.options.RecordException)
{
activity.RecordException(exc);
}
var exc = this.TryFetchException(payload);
if (exc == null)
{
AspNetCoreInstrumentationEventSource.Log.NullPayload(nameof(HttpInListener), nameof(this.OnException));
return;
}

activity.SetStatus(Status.Error.WithDescription(exc.Message));
if (this.options.RecordException)
{
activity.RecordException(exc);
}

try
{
this.options.Enrich?.Invoke(activity, "OnException", exc);
}
catch (Exception ex)
{
AspNetCoreInstrumentationEventSource.Log.EnrichmentException(ex);
}
activity.SetStatus(Status.Error.WithDescription(exc.Message));

try
{
this.options.Enrich?.Invoke(activity, "OnException", exc);
}
catch (Exception ex)
{
AspNetCoreInstrumentationEventSource.Log.EnrichmentException(ex);
}
}

private Exception TryFetchException(object payload)
{
Exception exc;
if (this.stopExceptionFetcher.TryFetch(payload, out exc) && exc != null)
{
return exc;
}

if (this.stopExceptionFilterFetcher.TryFetch(payload, out exc) && exc != null)
{
return exc;
}

return null;
}

private static string GetUri(HttpRequest request)
Expand Down
4 changes: 4 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Fix issue where when an application has an ExceptionFilter, the exception data
wouldn't be collected.
([#3475](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3475))
alanwest marked this conversation as resolved.
Show resolved Hide resolved

* `TracerProviderSDK` modified for spans with remote parent. For such spans
activity will be created irrespective of SamplingResult, to maintain context
propagation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,30 @@ public bool TryFetch(object obj, out T value, bool skipObjNullCheck = false)
return false;
}

object cur = obj;
if (this.innerFetcher == null)
{
this.innerFetcher = PropertyFetch.Create(obj.GetType().GetTypeInfo(), this.propertyName);
if (this.propertyName.Contains("."))
{
var parts = this.propertyName.Split('.');
for (var i = 0; i < parts.Length; i++)
{
this.innerFetcher = PropertyFetch.Create(cur.GetType().GetTypeInfo(), parts[i]);
if (i == parts.Length - 1)
{
break;
}

cur = cur.GetType().GetProperty(parts[i]).GetValue(cur);
}
}
else
{
this.innerFetcher = PropertyFetch.Create(obj.GetType().GetTypeInfo(), this.propertyName);
}
}

return this.innerFetcher.TryFetch(obj, out value);
return this.innerFetcher.TryFetch(cur, out value);
}

// see https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceEventSource.cs
Expand Down