-
Notifications
You must be signed in to change notification settings - Fork 516
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
Provide a means for capturing stack trace for messages that are not errors #338
Comments
We (or at least I) are trying to move people away from instrumenting our SDK explicitly and instead use logging mechanisms that are native to the platform (in this case Python). In my view there's potential to make Sentry error reporting through logging a better experience than using So you say the logging integration is not a good substitute, but I want to try making it one. Given your constraints, perhaps your actual problem is that the integration logger level is not configurable per-logger? We still expose logging handlers that you can explicitly register: https://docs.sentry.io/platforms/python/logging/ So I wonder if in your case it's appropriate to register those handlers explicitly (with individual handler levels) and disable the logging integration altogether. The part about |
So about threads vs stacktrace: We are trying to deprecate |
This feels highly incongruous, because I still have to call Sentry to add tags to events. with sentry_sdk.configure_scope() as s:
s.set_tag("blah", 1)
log.error("something happened") |
Yes. However, you'll find that tags are a thing we never set in any integration, because indexing by them is a serverside cost we only want to bear if the user is explicitly asking for it. There are multiple requirements we have to serve here, and if those requirements are in conflict we have to pick one for lack of a compromise. As far as I understand tags are rather an exception to the rule. Another idea is that you use |
Another use case that's also popped up recently: structlogs & errors/non-exception events with stack traces I've been trying to integrate the new SDK with another Python project that uses structlog instead of stdlib logging. Due to grouping issues (similar to the discussions in #228), I wound up writing my own processor for Sentry. The code is very similar to hynek's SentryProcessor in https://gist.github.com/hynek/a1f3f92d57071ebc5b91, but I have a branch for error-level logs and have been trying to preserve the option to include stack traces. The old SDK let me use class SentryProcessor:
def __call_(self, log, method, event):
# In the future I might want to expand this to support arbitrary events with stack traces
if event['level'] not in {'error', 'exception'}:
return event
stack = event.pop('stack', False)
# This came from log.exception
if 'exc_info' in event:
self._sentry.captureException(...)
# This came from log.error
else:
self._sentry.captureMessage(..., stack=stack) # The stack kwarg is key here
return event
def some_other_code():
log.error('oh no', stack=True)
log.exception('oh no') # exc_info=1 gets added by stdlib, which then calls "error"
log.error('oh no', exc_info=True) # Thus this will look exactly like an exception log to structlog's processors |
So to summarize, I see three distinct issues here:
Do you think this is correct? |
For the third, point, it's more like:
Basically, decoupling stack traces from exceptions. |
This is kind of a stupid workaround, but would the following work for you? sys.exc_clear()
log.error(..., exc_info=True) Not suggesting that this is great, just trying to understand. |
It would work for stdlib logging cases, but not for structlog, where I can't distinguish between exception or error calls. |
@goodspark I want to close this issue because the structlog support is already discussed/tracked elsewhere and I see the rest more as feedback on the API rather than actual features. I gave you a few workarounds for your problems with the logging integration, but that aspect of the discussion never really went anywhere.
I don't quite understand how this is relevant. If you want to funnel this through structlog then this would indeed take some effort, but you don't have to either. As far as I understand you are saying that structlog does not have a format for attaching a caller stack and an exception stack, but I don't see how the SDK can help with that. |
|
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 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. |
Yeah the workarounds will suffice. Thanks for your time |
@goodspark I hope this didn't come across as harsh. I do value your feedback but I think we need to push back a bit on features as well to avoid feature creep. |
@untitaker how come I can't just do |
@mecampbellsoup This issue is old, things have changed since then. |
Ah @untitaker thank you so much! I don't think this is well documented at all FWIW. |
Yeah, I just noticed: #1086 |
Another thing I would mention @untitaker is that |
|
@untitaker not quite - I am saying that when calling |
Right, you don't have a stacktrace attached here, that's why you also get no locals. We only attach local variables in the context of a stacktrace. |
@untitaker hmmm is this not supported API?
|
@mecampbellsoup |
It would be useful if you could override this on single calls though. |
From all this I wasn't able to understand how to attach a stack trace to capture_message(...) |
It's not possible in the current SDK, and it's not planned either |
I created an ad-hoc workaround to attach stacktrace to As in the blog: if this is more generally useful, I can upgrade to an actual library. |
... 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:Ideally, the Sentry SDK could add an additional argument to the and
capture_message
APIs to make this simple from a user perspective: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.
This was more or less copied from the code I see for adding stack traces in
Client._prepare_event
.The text was updated successfully, but these errors were encountered: