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.

Provide a means for capturing stack trace for messages that are not errors

See original GitHub issue

… without relying on the logging integration.

SDK v0.7.10

Currently, the only way to do this is via the logging integration and issuing a log at the configured level or higher with exc_info=True. I don’t think this is a good substitute because:

  • I don’t want to classify these events as errors (the end goal being errors should drive alerts)
  • Sometimes there are interesting or unexpected events that I want to debug more and leveraging Sentry’s stack traces with locals helps in this regard
  • I don’t want to lower our logging integration level to warning or similar, as that may spam our project with a lot of warnings/events that are not interesting or useful

Ideally, the Sentry SDK could add an additional argument to the and capture_message APIs to make this simple from a user perspective:

sentry_sdk.capture_message("Unexpected event", level="warning", stack_trace=True)

I’m trying to get this working locally with the SDK, and it’s leading to incomplete event data (see picture) and also leading to pretty hairy code.

sentry_sdk.capture_event({"message": "oops 2", "level": "warning", "threads": [{"stacktrace": sentry_sdk.utils.current_stacktrace(with_locals=True), "crashed": False, "current": True}])

This was more or less copied from the code I see for adding stack traces in Client._prepare_event.

Screenshot from 2019-04-13 14-39-24

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:25 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
untitakercommented, Apr 22, 2019

As it stands you’re the first one I know who both has a usecase like this and is sufficiently dissatisfied with the UX around it to want additional API surface. So you have to understand that this is not something we want to change right now, especially considering that this thread contains multiple workarounds that are not even fragile or rely on unstable API, instead they are just verbose.

The simplest suggestion with before_send, that I think I didn’t elaborate enough on, would look like this (untested):

import logging, sys

from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk import *

def before_send(event, hint):
    if event.get('level') not in ('debug', 'info', 'warning') or event['extra'].get('sentry_force_send_event'):
        return event

init(DSN, integrations=[LoggingIntegration(event_level=logging.WARN)], before_send=before_send)

#sys.exc_clear()
logging.warn("Hi! I am an event", sentry_force_send_event=True, exc_info=True)

Admittedly I don’t really understand whether you can’t or don’t want to use the logging integration to do this. It seems to me like the above would work perfectly fine regardless of whether you use structlog besides it.

1reaction
blueyedcommented, Jun 3, 2021

It would be useful if you could override this on single calls though. The use case I have is to have it enabled by default, because it provides more info (with logging in particular), but would like to avoid it with a custom sentry_sdk.capture_message("Initialized Sentry") (where/when logging is not configured yet, but also there it would not be possible to turn it off for a particular logging message anyway).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Understanding Error.captureStackTrace and stack trace ...
captureStackTrace returns a string that represents the location of that particular error in the call. It gives us a ...
Read more >
What is a Java Stack Trace? How to Read & Analyze Traces
The stack trace includes information about program subroutines and can be used to debug or troubleshoot and is often used to create log...
Read more >
JavaScript Errors and Stack Traces in Depth - Lucas F. Costa
Manipulating stack traces lets you clean up unuseful data and focus on what matters. Also, when understanding what exactly are Errors and their ......
Read more >
Stack Trace: How to Debug Your Application With a Stack Trace
A stack trace is a valuable piece of information that can be used for debugging purposes. Whenever you encounter an error in your...
Read more >
Understanding the Python Traceback
Python prints a traceback when an exception is raised in your code. The traceback output can be a bit overwhelming if you're seeing...
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