Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Hello,

If I wanted to test code that’s been instrumented by Sentry, or if I wanted to test that my own filters work as expected, then I would want to run tests without Sentry actually sending off anything. (Related discussion #4499.)

There’s some documentation here on passing a custom Transport object when initializing Sentry, but there’s not much information on subclassing Transports. Reading the code it seems the capture_envelope() methods needs to be overridden:

>>> import sentry_sdk
>>> 
>>> class PrintTransport(sentry_sdk.Transport):
...     def capture_envelope(self, envelope):
...         print("SENTRY", envelope)
...         

However, when I initialize Sentry

>>> sentry_sdk.init(dsn=None, transport=PrintTransport(), transport_queue_size=0)
<sentry_sdk._init_implementation._InitGuard object at 0x10847b770>

and then trigger an error

>>> 1 / 0
Traceback (most recent call last):
  File "<python-input-7>", line 1, in <module>
    1 / 0
    ~~^~~
ZeroDivisionError: division by zero

I had expected the print() output but that didn’t happen.

What am I missing here? Thanks!

You must be logged in to vote

Replies: 1 comment · 1 reply

Comment options

I tried your custom transport with explicitly calling sentry.capture_exception and it seems to be working. Maybe the sdk just doesn't capture exceptions in the python console.

>>> import sentry_sdk
>>> class PrintTransport(sentry_sdk.Transport):
...     def capture_envelope(self, envelope):
...         print("SENTRY", envelope)
...
>>> sentry_sdk.init(dsn=None, transport=PrintTransport(), transport_queue_size=0)
<sentry_sdk._init_implementation._InitGuard object at 0x7f4046857b60>
>>> try:
...     a=1/0
... except:
...     sentry_sdk.capture_exception()
...
SENTRY <Envelope headers={'event_id': '669b18c429e54f32b0abbd64da600635', 'sent_at': '2025-12-11T15:28:44.422663Z', 'trace': {'trace_id': '1a507934c3fb496f9fce0847c37c2d79', 'environment': 'production', 'release': '2e9a36cdc11139eff27e126331f1a8c7cf2d148f'}} items=[<Item headers={'type': 'event', 'content_type': 'application/json'} payload=<Payload 'application/json'> data_category='error'>]>
'669b18c429e54f32b0abbd64da600635'
You must be logged in to vote
1 reply
@jenstroeger
Comment options

Thanks @eduardocardoso, and I think you’re right: if I run the test code above in a script then I get the same output:

~ > python ./test.py 
SENTRY <Envelope headers={'event_id': '187bd27e917a404fa8be4a325bcd63e6', 'sent_at': '2025-12-11T15:33:18.498558Z', 'trace': {'trace_id': 'c4ba3232935d4decb4a7105d26ec6bd2', 'environment': 'production', 'release': 'a2fd3233cd0616427a094029d355293491f4e665'}} items=[<Item headers={'type': 'event', 'content_type': 'application/json'} payload=<Payload 'application/json'> data_category='error'>]>
Traceback (most recent call last):
  File "/path/to/./test.py", line 9, in <module>
    1/0
    ~^~
ZeroDivisionError: division by zero

The Envelope class is implemented here so we can poke around and understand what’s being passed to the Transport. Now changing the PrintTransport to using

print("SENTRY", envelope.serialize()) 

returns the JSON serialized compete envelope information for testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.