dd-trace-py icon indicating copy to clipboard operation
dd-trace-py copied to clipboard

Add handled exceptions to error tracking

Open lminer opened this issue 1 year ago • 4 comments

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?

lminer avatar Jun 12 '24 16:06 lminer

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!

emmettbutler avatar Jun 14 '24 16:06 emmettbutler

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))

lminer avatar Jun 14 '24 18:06 lminer

@emmettbutler any sense of whether this would work as a solution?

lminer avatar Jun 19 '24 15:06 lminer

@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.

emmettbutler avatar Jun 21 '24 17:06 emmettbutler

I've also been looking for such a feature and I ran into this as I was about to write a similar issue.

guyzmo avatar Sep 30 '24 09:09 guyzmo

@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())

P403n1x87 avatar Sep 30 '24 12:09 P403n1x87

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.

github-actions[bot] avatar Mar 02 '25 00:03 github-actions[bot]