123456 |
- {
- "html": "<div class=\"section\" id=\"installation\">\n<h2>Installation</h2>\n<p>If you haven’t already, start by downloading Raven. The easiest way is\nwith <em>pip</em>:</p>\n<div class=\"highlight-python\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span>pip install raven --upgrade\n</pre></div>\n</div>\n</div>\n\n\n<div class=\"section\" id=\"setup\">\n<h2>Setup</h2>\n<p>The first thing you’ll need to do is to initialize sentry client under\nyour application</p>\n<div class=\"highlight-python\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #2eb0f7\">import</span> <span style=\"color: #111111\">tornado.web</span>\n<span style=\"color: #2eb0f7\">from</span> <span style=\"color: #111111\">raven.contrib.tornado</span> <span style=\"color: #2eb0f7\">import</span> <span style=\"color: #111111\">AsyncSentryClient</span>\n\n<span style=\"color: #2eb0f7\">class</span> <span style=\"color: #111111\">MainHandler(tornado</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">web</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">RequestHandler):</span>\n <span style=\"color: #2eb0f7\">def</span> <span style=\"color: #111111\">get(</span><span style=\"color: #45c2c9\">self</span><span style=\"color: #111111\">):</span>\n <span style=\"color: #45c2c9\">self</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">write(</span><span style=\"color: #e8535a\">"Hello, world"</span><span style=\"color: #111111\">)</span>\n\n<span style=\"color: #111111\">application</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #111111\">tornado</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">web</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">Application([</span>\n <span style=\"color: #111111\">(</span><span style=\"color: #e8535a\">r"/"</span><span style=\"color: #111111\">,</span> <span style=\"color: #111111\">MainHandler),</span>\n<span style=\"color: #111111\">])</span>\n<span style=\"color: #111111\">application</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">sentry_client</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #111111\">AsyncSentryClient(</span>\n <span style=\"color: #e8535a\">'___DSN___'</span>\n<span style=\"color: #111111\">)</span>\n</pre></div>\n</div>\n</div>\n\n\n<div class=\"section\" id=\"usage\">\n<h2>Usage</h2>\n<p>Once the sentry client is attached to the application, request handlers\ncan automatically capture uncaught exceptions by inheriting the <cite>SentryMixin</cite> class.</p>\n<div class=\"highlight-python\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #2eb0f7\">import</span> <span style=\"color: #111111\">tornado.web</span>\n<span style=\"color: #2eb0f7\">from</span> <span style=\"color: #111111\">raven.contrib.tornado</span> <span style=\"color: #2eb0f7\">import</span> <span style=\"color: #111111\">SentryMixin</span>\n\n<span style=\"color: #2eb0f7\">class</span> <span style=\"color: #111111\">UncaughtExceptionExampleHandler(</span>\n <span style=\"color: #111111\">SentryMixin,</span> <span style=\"color: #111111\">tornado</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">web</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">RequestHandler):</span>\n <span style=\"color: #2eb0f7\">def</span> <span style=\"color: #111111\">get(</span><span style=\"color: #45c2c9\">self</span><span style=\"color: #111111\">):</span>\n <span style=\"color: #45c2c9\">1</span><span style=\"color: #4b4f5c\">/</span><span style=\"color: #45c2c9\">0</span>\n</pre></div>\n</div>\n<p>You can also send events manually using the shortcuts defined in <cite>SentryMixin</cite>.\nThe shortcuts can be used for both asynchronous and synchronous usage.</p>\n<div class=\"section\" id=\"asynchronous\">\n<h3>Asynchronous</h3>\n<div class=\"highlight-python\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #2eb0f7\">import</span> <span style=\"color: #111111\">tornado.web</span>\n<span style=\"color: #2eb0f7\">import</span> <span style=\"color: #111111\">tornado.gen</span>\n<span style=\"color: #2eb0f7\">from</span> <span style=\"color: #111111\">raven.contrib.tornado</span> <span style=\"color: #2eb0f7\">import</span> <span style=\"color: #111111\">SentryMixin</span>\n\n<span style=\"color: #2eb0f7\">class</span> <span style=\"color: #111111\">AsyncMessageHandler(SentryMixin,</span> <span style=\"color: #111111\">tornado</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">web</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">RequestHandler):</span>\n <span style=\"color: #696D80\">@tornado.web.asynchronous</span>\n <span style=\"color: #696D80\">@tornado.gen.engine</span>\n <span style=\"color: #2eb0f7\">def</span> <span style=\"color: #111111\">get(</span><span style=\"color: #45c2c9\">self</span><span style=\"color: #111111\">):</span>\n <span style=\"color: #45c2c9\">self</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">write(</span><span style=\"color: #e8535a\">"You requested the main page"</span><span style=\"color: #111111\">)</span>\n <span style=\"color: #2eb0f7\">yield</span> <span style=\"color: #111111\">tornado</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">gen</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">Task(</span>\n <span style=\"color: #45c2c9\">self</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">captureMessage,</span> <span style=\"color: #e8535a\">"Request for main page served"</span>\n <span style=\"color: #111111\">)</span>\n <span style=\"color: #45c2c9\">self</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">finish()</span>\n\n<span style=\"color: #2eb0f7\">class</span> <span style=\"color: #111111\">AsyncExceptionHandler(SentryMixin,</span> <span style=\"color: #111111\">tornado</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">web</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">RequestHandler):</span>\n <span style=\"color: #696D80\">@tornado.web.asynchronous</span>\n <span style=\"color: #696D80\">@tornado.gen.engine</span>\n <span style=\"color: #2eb0f7\">def</span> <span style=\"color: #111111\">get(</span><span style=\"color: #45c2c9\">self</span><span style=\"color: #111111\">):</span>\n <span style=\"color: #2eb0f7\">try</span><span style=\"color: #111111\">:</span>\n <span style=\"color: #2eb0f7\">raise</span> <span style=\"color: #e8535a\">ValueError</span><span style=\"color: #111111\">()</span>\n <span style=\"color: #2eb0f7\">except</span> <span style=\"color: #e8535a\">Exception</span> <span style=\"color: #2eb0f7\">as</span> <span style=\"color: #111111\">e:</span>\n <span style=\"color: #111111\">response</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #2eb0f7\">yield</span> <span style=\"color: #111111\">tornado</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">gen</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">Task(</span>\n <span style=\"color: #45c2c9\">self</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">captureException,</span> <span style=\"color: #111111\">exc_info</span><span style=\"color: #4b4f5c\">=</span><span style=\"color: #45c2c9\">True</span>\n <span style=\"color: #111111\">)</span>\n <span style=\"color: #45c2c9\">self</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">finish()</span>\n</pre></div>\n</div>\n<div class=\"admonition tip\">\n<p class=\"first admonition-title\">Tip</p>\n<p>The value returned by the yield is a <code class=\"docutils literal\"><span class=\"pre\">HTTPResponse</span></code> object.</p>\n<p>To dynamically use Python if configuration DSN available, change your base handler on fly and make sure all concrete handlers are imported after this.</p>\n<blockquote class=\"last\">\n<div><div class=\"highlight-python\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #2eb0f7\">from</span> <span style=\"color: #111111\">raven.contrib.tornado</span> <span style=\"color: #2eb0f7\">import</span> <span style=\"color: #111111\">SentryMixin</span>\n<span style=\"color: #111111\">app</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">sentry_client</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #111111\">AsyncSentryClient(dsn)</span>\n<span style=\"color: #111111\">BaseHandler</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">__bases__</span> <span style=\"color: #4b4f5c\">=</span> <span style=\"color: #111111\">(SentryMixin,</span> <span style=\"color: #111111\">)</span> <span style=\"color: #4b4f5c\">+</span> <span style=\"color: #111111\">BaseHandler</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">__bases__</span>\n</pre></div>\n</div>\n</div></blockquote>\n</div>\n</div>\n<div class=\"section\" id=\"synchronous\">\n<h3>Synchronous</h3>\n<div class=\"highlight-python\"><div class=\"highlight\" style=\"background: #ffffff\"><pre style=\"line-height: 125%\"><span></span><span style=\"color: #2eb0f7\">import</span> <span style=\"color: #111111\">tornado.web</span>\n<span style=\"color: #2eb0f7\">from</span> <span style=\"color: #111111\">raven.contrib.tornado</span> <span style=\"color: #2eb0f7\">import</span> <span style=\"color: #111111\">SentryMixin</span>\n\n<span style=\"color: #2eb0f7\">class</span> <span style=\"color: #111111\">AsyncExampleHandler(SentryMixin,</span> <span style=\"color: #111111\">tornado</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">web</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">RequestHandler):</span>\n <span style=\"color: #2eb0f7\">def</span> <span style=\"color: #111111\">get(</span><span style=\"color: #45c2c9\">self</span><span style=\"color: #111111\">):</span>\n <span style=\"color: #45c2c9\">self</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">write(</span><span style=\"color: #e8535a\">"You requested the main page"</span><span style=\"color: #111111\">)</span>\n <span style=\"color: #45c2c9\">self</span><span style=\"color: #4b4f5c\">.</span><span style=\"color: #111111\">captureMessage(</span><span style=\"color: #e8535a\">"Request for main page served"</span><span style=\"color: #111111\">)</span>\n</pre></div>\n</div>\n</div>\n</div>\n",
- "link": "https://docs.getsentry.com/clients/python/integrations/tornado/",
- "id": "python-tornado",
- "name": "Tornado"
- }
|