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

Handle case where SetExceptionTags() throws #5291

Merged
merged 1 commit into from
Mar 13, 2024
Merged
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
26 changes: 17 additions & 9 deletions tracer/src/Datadog.Trace/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,17 +405,25 @@ internal void SetExceptionTags(Exception exception)
{
if (exception != null)
{
// for AggregateException, use the first inner exception until we can support multiple errors.
// there will be only one error in most cases, and even if there are more and we lose
// the other ones, it's still better than the generic "one or more errors occurred" message.
if (exception is AggregateException aggregateException && aggregateException.InnerExceptions.Count > 0)
try
{
exception = aggregateException.InnerExceptions[0];
}
// for AggregateException, use the first inner exception until we can support multiple errors.
// there will be only one error in most cases, and even if there are more and we lose
// the other ones, it's still better than the generic "one or more errors occurred" message.
if (exception is AggregateException aggregateException && aggregateException.InnerExceptions.Count > 0)
{
exception = aggregateException.InnerExceptions[0];
}

SetTag(Trace.Tags.ErrorMsg, exception.Message);
SetTag(Trace.Tags.ErrorStack, exception.ToString());
SetTag(Trace.Tags.ErrorType, exception.GetType().ToString());
SetTag(Trace.Tags.ErrorMsg, exception.Message);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Small nit: It might be easier to move the exception null-check until after the AggregateException if statement, since that if statement also functions as a null-check

Copy link
Member Author

Choose a reason for hiding this comment

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

Does it make any difference 🤔 The current order is what we had previously, and I think you always need both checks, but with the current order, if it's null you can bail out straight away. With the other order, you would always do 2 checks I think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah I suppose you're right, this is fine as-is 👍🏼

SetTag(Trace.Tags.ErrorType, exception.GetType().ToString());
SetTag(Trace.Tags.ErrorStack, exception.ToString());
}
catch (Exception ex)
{
// We have found rare cases where exception.ToString() throws an exception, such as in a FileNotFoundException
Log.Warning(ex, "Error setting exception tags on span {SpanId} in trace {TraceId}", SpanId, TraceId);
}
}
}

Expand Down
Loading