getsentry/sentry-python

sentry_sdk.capture_exception is ignored when called after logging.exception in the same except-block

Closed this issue · 3 comments

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.5.12

Steps to Reproduce

import sentry_sdk
import logging


SENTRY_DSN = "<YOUR_DSN>"

logger = logging.getLogger("test")
sentry_sdk.init(dsn=SENTRY_DSN, environment="staging")

def foo():
	try:
		raise Exception("just fail")
	except Exception as err:
		logger.exception("An exception occurred")
		sentry_sdk.capture_exception(err, user={"id": 1, "username": "admin"}, tags={"foo": 1})

foo()

Expected Result

sentry_sdk.capture_exception should capture the event, with specified user & tags.

Actual Result

Only 1 event is created, from logger.exception():

06:56:18 DEBUG <YOUR_DSN>:443 "POST /api/5/store/ HTTP/1.1" 200 41 - urllib3.connectionpool - connectionpool - /usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py:456

Is that expected? My use case is that I want to see the error logs in my console. At the same time, I want to capture more contextual data for that exception (by capturing user & tags) on Sentry. How would I do so, if sentry_sdk only registers the event produced by logger.exception() and completely ignores the sentry_sdk.capture_exception() call below it?

Hey @teddyhartanto !

About adding user and tags:
If you use one of our integrations (we have them for 24 Python web frameworks I guess) the user should be added automatically to each error that shows up in Sentry.

To add tags to errors see this documentation page: https://docs.sentry.io/platforms/python/enriching-events/tags/
(so there is no need for you to catch exceptions and then re-raise them with more data, just call set_tag at the top of your function, and you are good to go. If you want to limit the tags to a certain area of your code have a look at this documentation: https://docs.sentry.io/platforms/python/enriching-events/scopes/)

Why your error is not showing up: Sentry is only showing "unhandled" Errors on Sentry.io and therefore you need to set a flag when calling capture_event. You can see how our integrations do this as an example here in the Flask integration: https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/flask.py#L201-L218

Hope this helps!

Hi @antonpirker,

Thank you for the reply. For context, the error we wanted to catch doesn't happen in the context of our web framework. Rather, it happens in the context of our job worker. When a user hit a particular endpoint, a job is queued using Django RQ, which is then picked up by a worker. It is in that worker that we want to catch the error.

Anyways, I've tried calling set_tag and set_user before calling logger.exception(). That works.

Why your error is not showing up: Sentry is only showing "unhandled" Errors on Sentry.io and therefore you need to set a flag when calling capture_event.

I guess that makes more sense now. IIUC, because Sentry integrates with logging module by default, when logger.exception() is called, Sentry deems that exception as "handled", and therefore the subsequent sentry_sdk.capture_exception() is ignored? Is that right?

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀