σ ΏφYc@@sœdZddlmZddlmZddlmZddlmZm Z ddl m Z ddl m Z de fd „ƒYZd efd „ƒYZd S( s  raven.contrib.tornado ~~~~~~~~~~~~~~~~~~~~~ :copyright: (c) 2012 by the Sentry Team, see AUTHORS for more details :license: BSD, see LICENSE for more details i(tabsolute_import(tpartial(tioloop(tAsyncHTTPClientt HTTPError(R(tClienttAsyncSentryClientcB@sVeZdZd„Zd„Zddd„Zddd„Zd„Zddd„Z RS(sΊ A mixin class that could be used along with request handlers to asynchronously send errors to sentry. The client also captures the information from the request handlers cO@s2|jdtƒ|_tt|ƒj||ŽdS(Nt validate_cert(tpoptTrueRtsuperRt__init__(tselftargstkwargs((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pyR scO@sQ|jƒsdS|j||Ž}|jd|jddƒ|}|d|fS(s  Takes the same arguments as the super function in :py:class:`Client` and extracts the keyword argument callback which will be called on asynchronous sending of the request :return: a 32-length string identifying this event Ntcallbacktevent_id(t is_enabledt build_msgtsendtgettNone(R R Rtdatatfuture((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pytcaptures  !cK@s(|j|ƒ}|j|d|d|ƒS(sV Serializes the message and passes the payload onto ``send_encoded``. t auth_headerR(tencodet send_encoded(R RRRtmessage((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pyR/sc C@s”|dkri}n|jjƒsD|j|ƒ}|j|ƒdS|jd|d|d|d|ƒ}tjjƒj |t |j ||ƒƒ|S(NturlRtheadersR( Rtstatet should_trytdecodet_log_failed_submissiont _send_remoteRtIOLooptcurrentt add_futureRt_handle_result(R RRRRR((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pyt send_remote7s    (cC@s‹y|jƒWnitk rE}|j|ƒ}|j|||ƒnBtk ry}|j|ƒ}|j|||ƒnX|jjƒdS(N(tresultRR!t _failed_sendt ExceptionRt set_success(R RRRte((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pyR'Fsc C@sC|dkri}ntƒj||ddd|d|d|jƒS(s± Initialise a Tornado AsyncClient and send the reuqest to the sentry server. If the callback is a callable, it will be called with the response. tmethodtPOSTtbodyRRN(RRtfetchR(R RRRR((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pyR#Rs    N( t__name__t __module__t__doc__R RRRR(R'R#(((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pyRs   t SentryMixincB@sqeZdZd„Zd„Zd„Zd„Zd„Zd d„Z d d„Z d„Z d „Z d d „Z RS( s€ A mixin class that extracts information from the Request in a Request Handler to capture and send to sentry. This mixin class is designed to be used along with `tornado.web.RequestHandler` .. code-block:: python :emphasize-lines: 6 class MyRequestHandler(SentryMixin, tornado.web.RequestHandler): def get(self): try: fail() except Exception as e: self.captureException() While the above example would result in sequential execution, an example for asynchronous use would be .. code-block:: python :emphasize-lines: 6 class MyRequestHandler(SentryMixin, tornado.web.RequestHandler): @tornado.web.asynchronous @tornado.gen.engine def get(self): # Do something and record a message in sentry response = yield tornado.gen.Task( self.captureMessage, "Did something really important" ) self.write("Your request to do something important is done") self.finish() The mixin assumes that the application will have an attribute called `sentry_client`, which should be an instance of :py:class:`AsyncSentryClient`. This can be changed by implementing your own get_sentry_client method on your request handler. cC@s |jjS(sΐ Returns the sentry client configured in the application. If you need to change the behaviour to do something else to get the client, then subclass this method (t applicationt sentry_client(R ((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pytget_sentry_client‹scC@snii|jjƒd6|jjd6|jjd6|jjd6|jjjdd ƒd6t|jjƒd6d6S( s΅ Extracts the data required for 'sentry.interfaces.Http' from the current request being handled by the request handler :param return: A dictionary. RR.Rt query_stringtCookietcookiesRtrequestN( R<tfull_urlR.R0tqueryRRRtdict(R ((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pytget_sentry_data_from_request“s   cC@sCy|jƒ}Wntk r$iSXii|r7tntd6d6S(sι Data for sentry.interfaces.User Default implementation only sends `is_authenticated` by checking if `tornado.web.RequestHandler.get_current_user` tests postitively for on Truth calue testing tis_authenticatedtuser(tget_current_userR+R tFalse(R RB((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pytget_sentry_user_info₯s cC@s iid6S(sf Subclass and implement this method if you need to send any extra information textra((R ((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pytget_sentry_extra_info·scC@sCi}|j|jƒƒ|j|jƒƒ|j|jƒƒ|S(N(tupdateR@RERG(R R((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pytget_default_contextΑs cK@s|dkr|jƒ}n?|jƒ}t|tƒrF|j|ƒn||dd<|}|jƒ}t||ƒd||S(NRFt extra_dataR(RRIt isinstanceR?RHR8tgetattr(R t call_nameRRtdefault_contexttclient((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pyt_captureΟs   cK@s|jdd||S(NtcaptureExceptiontexc_info(RP(R RRR((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pyRQήscK@s|jdd||S(NtcaptureMessageR(RP(R RR((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pyRSαscC@sltt|ƒj|||ƒ}t|tƒrO|jdksK|jdkrO|S|jd|||fƒ|S(svOverride implementation to report all exceptions to sentry. log_exception() is added in Tornado v3.1. iτiWRR(R R5t log_exceptionRKt WebHTTPErrort status_codeRQ(R ttyptvaluettbtrv((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pyRTδs -iτcK@sŒttt|ƒdƒr1tt|ƒj||Stt|ƒj||}d|kocdknr„|jd|jdƒƒn|SdS(s–Override implementation to report all exceptions to sentry, even after self.flush() or self.finish() is called, for pre-v3.1 Tornado. RTiτiWRRN(thasattrR R5t send_errorRQR(R RVRRZ((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pyR\οs N(R2R3R4R8R@RERGRIRRPRQRSRTR\(((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pyR5as(        N(R4t __future__Rt functoolsRttornadoRttornado.httpclientRRt tornado.webRUt raven.baseRRtobjectR5(((sO/opt/alt/python27/lib/python2.7/site-packages/raven/contrib/tornado/__init__.pytsN