question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Send call stack and context without triggering an exception

See original GitHub issue

I’m trying to catch a bug in my code, that is very hard to reproduce. I can detect it, but I’d need a bit more context to reliably reproduce it.

I think the data that is reported to sentry on exception (call stack, values of local variables) will be very useful to track it. Is there any way to trigger such an event? I don’t want to interrupt my program, so I can’t raise an exception here. I’ve tried capture_event() and capture_message() but none of them captures the data I need.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jimmy927commented, Feb 15, 2021

Found this when googeling, EXCATLY what i needed.

Perfected it a little bit by popping the last element of the stack and added a proper name to the message if possible:

from sentry_sdk.utils import capture_internal_exceptions, current_stacktrace
import sys
import sentry_sdk

def send_stacktrace_to_sentry():
    client_options = sentry_sdk.client.get_options()
    event = {}
    with capture_internal_exceptions():
        stacktrace = current_stacktrace(client_options["with_locals"])
        stacktrace["frames"].pop()
        event["threads"] = {
            "values": [
                {
                    "stacktrace": stacktrace,
                    "crashed": False,
                    "current": True,
                }
            ]
        }

    event["level"] = "error"
    event["logentry"] = {"message": sys.exc_info()[1] or "No Exception Found"}
    sentry_sdk.capture_event(event)

1reaction
aklajnertcommented, Feb 8, 2021

OK, I think I’ve figured out from the logging EventHandler how to do what I want - the code is not pretty, but works:

from sentry_sdk.utils import capture_internal_exceptions, current_stacktrace

client_options = sentry_sdk.client.get_options()

event = {}
with capture_internal_exceptions():
    event["threads"] = {
        "values": [
            {
                "stacktrace": current_stacktrace(client_options["with_locals"]),
                "crashed": False,
                "current": True,
            }
        ]
    }

event["level"] = "error"
event["logentry"] = {"message": "message I want to send"}

sentry_sdk.capture_event(event)

I think this could be inside some utility function, but it is good enough for me.

One question though - the last frame of the stacktrace points at "stacktrace": current_stacktrace(client_options["with_locals"]),, which makes sense, but I’d like to remove it. I’ve figured out that I can remove the last element from event['threads']['values'][0]['stacktrace']['frames'], and my stacktrace looks much better. The question is if event['threads']['values'][0] is guaranteed to be the current thread, or it may change when multiple threads are used?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Supress/filter exception context when raised within a trigger
In a Python application I'm catching exceptions thrown from stored procedures on my database as psycopg2.InternalError . I've noticed that when ...
Read more >
Best Practices for exceptions - .NET - Microsoft Learn
Learn best practices for exceptions, such as using try/catch/finally, handling common conditions without exceptions, and using predefined .
Read more >
Can I store information even if the trigger throws an exception?
EXCEPTION - To summarize the edits from subsequent contact with salesforce: this will not work if every record in the trigger has errors....
Read more >
contextlib — Utilities for with-statement contexts ... - Python Docs
For any context managers and exit callbacks registered, the arguments passed in will indicate that no exception occurred. class contextlib.AsyncExitStack¶. An ...
Read more >
Java Stack Trace: How to Read and Understand to Debug Code
Usually, a stack trace is shown when an Exception is not handled correctly in code. (An exception is what a runtime environment uses...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found