request context for Sentry

106 views
Skip to first unread message

Zsolt Ero

unread,
Mar 16, 2016, 9:36:44 PM3/16/16
to pylons-devel
I'm using Sentry with Pyramid, as per their official .ini config notes:
https://docs.getsentry.com/hosted/clients/python/integrations/pyramid/

This way, if there is ever an Internal Server Error page, I get a full stack-trace submitted to Sentry as well as a request which can be replayed or converted to curl command line. Super life-saver when trying to debug hard-to-reproduce bugs.

My problem is that obviously I don't want the user to see an Internal Server Error page, so I put all critical functions in try-except blocks.

When using a try-except block Sentry has a captureException method, which submits that stack-trace to Sentry.
https://docs.getsentry.com/hosted/clients/python/usage/

My problem is that when using this captureException, the request parameters are not submitted, only the stack-trace, so the exception cannot be replayed / curl-ed this way.

David Cramer from Sentry replied to me that if Sentry is used with the middleware, then it should automatically receive the WSGI context:
from sentry.middleware import Sentry

application
= Sentry(application, client=Client(dsn, ...))

My problem is that in Pyramid I have no idea where could I get an application, or if this would work at all.

There is a pyramid-raven library, which I believe does what I'd need to attach the http context, but it's not using an application but a manually created http data dict.
https://github.com/thruflo/pyramid_raven/blob/bb4bb5227b9c40efec71238b4ee452db01921d45/src/pyramid_raven/client.py#L103

What is the recommended way to Pyramid to get this context? Are any of you using Sentry with a wrapper for captureException?  

Jonathan Vanasco

unread,
Mar 16, 2016, 10:40:28 PM3/16/16
to pylons-devel


On Wednesday, March 16, 2016 at 9:36:44 PM UTC-4, Zsolt Ero wrote:
David Cramer from Sentry replied to me that if Sentry is used with the middleware, then it should automatically receive the WSGI context:
from sentry.middleware import Sentry

application
= Sentry(application, client=Client(dsn, ...))

My problem is that in Pyramid I have no idea where could I get an application, or if this would work at all.

That looks like the initial app setup in your `project/__init__.py`

`application` would be what is returned from config.make_wsgi_app()

Some people do "return config.make_wsgi_app()"

Others prefer "app = config.make_wsgi_app()", then wrap it in middleware


Zsolt Ero

unread,
Mar 17, 2016, 8:26:39 PM3/17/16
to pylons-devel
Thanks, I see. I believe this way I do not need the .ini part, do I?

Zsolt Ero

unread,
Mar 19, 2016, 6:44:51 PM3/19/16
to pylons-devel
Thanks, I was able to make a working client, but not using the pipeline + filter part in the ini, but by wrapping app by:

from raven import Client

from raven.middleware import Sentry


client = Client('https://...:...@app.getsentry.com/...')

app = Sentry(app, client=client)


This way client is configured on init and captureException works automatically. 


Michael Merickel

unread,
Mar 19, 2016, 8:01:07 PM3/19/16
to pylons-devel
I'm missing something here probably but the raven docs have pretty straightforward support for configuring it via the ini file. Have you tried this?


--
You received this message because you are subscribed to the Google Groups "pylons-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-devel...@googlegroups.com.
To post to this group, send email to pylons...@googlegroups.com.
Visit this group at https://groups.google.com/group/pylons-devel.
For more options, visit https://groups.google.com/d/optout.

Zsolt Ero

unread,
Mar 19, 2016, 8:49:30 PM3/19/16
to pylons...@googlegroups.com
Yes, that is how I started. The problem is with client.captureException().

The problem is that there is no way to get the Client when it's
configured via ini (or at least I have no idea how to get it).

So when configured via ini, Sentry can only provide HTTP context for
uncaught exceptions which results in an Internal Server Error.

Obviously, your web app shouldn't result in Internal Server Errors,
but would try to give a reasonable error message, and internally log
the problem, so critical parts are in try-except blocks, and this is
where client.captureException() is used.

try:
...
except Exception:
client.captureException()

Now this client needs to be configured via a Sentry __call__, or else,
the http context is missing. That's why I needed to use the python
config vay.
> You received this message because you are subscribed to a topic in the
> Google Groups "pylons-devel" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/pylons-devel/ou3XQ6OC4OQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

Mikko Ohtamaa

unread,
Jun 3, 2016, 12:04:22 PM6/3/16
to pylons-devel
Reply all
Reply to author
Forward
0 new messages