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

Add handled exceptions to error tracking #9528

Open
lminer opened this issue Jun 12, 2024 · 4 comments
Open

Add handled exceptions to error tracking #9528

lminer opened this issue Jun 12, 2024 · 4 comments
Labels

Comments

@lminer
Copy link

lminer commented Jun 12, 2024

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?

@emmettbutler
Copy link
Collaborator

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!

@lminer
Copy link
Author

lminer commented Jun 14, 2024

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
Copy link
Author

lminer commented Jun 19, 2024

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

@emmettbutler
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants