Add handled exceptions to error tracking
There are certain exceptions that we catch, but that we would nonetheless like to have included in error tracking. Can this only be done by manually adding it to the span? And if so, is there any way to manually add the exception replay functionality?
Hi @lminer, and thanks for getting in touch. I'll see if I can help.
The Span class includes a method set_exc_info, which you can call with an exception tuple to add that exception's details to the span as a tag.
I think this might be "manually adding it to the span" as you mentioned. Could you say more about how you'd like caught exceptions to behave with spans if add_exc_info isn't called?
Thanks again!
We were using sentry before and in sentry there was the ability to run sentry_sdk.capture_exception(err) on a caught exception. This would give you not only the stack trace, but also all the locals that were in memory at the time that the exception was thrown. There doesn't appear to be an equivalent in datadog. Am I correct that in order to get the same functionality, I would need to do something like this, where I store the locals in the exception at the time that it is raised?
def trace_exception(ex: MyExceptionWithLocals):
span = tracer.current_span()
if span is None:
return
span.set_traceback()
span.set_tag("error", True)
span.set_tag("error.type", type(ex).__name__)
span.set_tag("error.message", str(ex))
span.set_tag("error.stack", "".join(traceback.format_tb(ex.__traceback__)))
locals_dict = getattr(ex, "locals", {})
for var_name, var_value in locals_dict.items():
span.set_tag(f"local.{var_name}", str(var_value))
@emmettbutler any sense of whether this would work as a solution?
@lminer yes, I believe that solution would work if your goal is to capture all of the locals. It's also interesting for us to consider adding locals capture to set_traceback.
I've also been looking for such a feature and I ran into this as I was about to write a similar issue.
@lminer In order to capture variables with Exception Reply you would need ddtrace>=2.13 and the following snippet
span = tracer.current_span()
span.set_exc_info(*sys.exc_info())
This issue has been automatically closed after a period of inactivity. If it's a feature request, it has been added to the maintainers' internal backlog and will be included in an upcoming round of feature prioritization. Please comment or reopen if you think this issue was closed in error.