Yc@@sdZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z ddl mZddlmZddlmZejd d fkrddlZn ddlZydd lmZWn!ek r!dd lmZnXddlZdd lmZdd lmZddl m!Z!m"Z"ddl#m$Z$m%Z%m&Z&m'Z'ddl(m)Z)m*Z*ddl+m,Z,ddl-m.Z.ddl/m0Z0m1Z1ddl2m3Z3ddl4m5Z5m6Z6ddl7ZdfZ8e9a:dZ;idd6ej<d6Z=e9a>ejd d fkrTe?Z@ndZAdeBfdYZCdeDfdYZEdeDfd YZFd!eFfd"YZGdS(#s raven.base ~~~~~~~~~~ :copyright: (c) 2010-2012 by the Sentry Team, see AUTHORS for more details. :license: BSD, see LICENSE for more details. i(tabsolute_importN(tdatetime(tisclass(tRandom(t FunctionType(tlocalii(t get_ident(tdefaults(t RemoteConfig(tAPIErrort RateLimited(tjsont get_versionstget_auth_headert merge_dicts(t text_typet iteritems(t to_unicode(t transform(tget_stack_infotiter_stack_frames(tTransactionStack(tTransportRegistrytdefault_transportstClienttpythons raven-pythontnametversioncC@s/tj}t|dd}|dk r+|SdS(Nt raven_client(tsyst excepthooktgetattrtNone(thooktclient((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pytget_excepthook_clientEs  tModuleProxyCachecB@seZdZRS(cC@sG|jdd\}}tt|ii|g|}|||<|S(Nt.i(trsplitRt __import__(tselftkeytmodulet class_namethandler((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt __missing__Ms  (t__name__t __module__R-(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyR$Lst ClientStatecB@sDeZdZdZdZdZddZdZdZRS(iicC@s+|j|_d|_d|_d|_dS(Ni(tONLINEtstatusR t last_checkt retry_numbert retry_after(R(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt__init__\s   cC@sV|j|jkrtS|jp2t|jdd}tj|j|krRtStS(Nii( R2R1tTrueR5tminR4ttimeR3tFalse(R(tinterval((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt should_trybs cC@s7|j|_|jd7_tj|_||_dS(Ni(tERRORR2R4R9R3R5(R(R5((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pytset_failms cC@s+|j|_d|_d|_d|_dS(Ni(R1R2R R3R4R5(R(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt set_successss   cC@s|j|jkS(N(R2R=(R(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pytdid_failys( R.R/R1R=R6R<R>R?R@(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyR0Xs   c B@s9eZdZejdZdZedeZ d/e d/e e d/e d/dZ dZd/d/dZdZdZd Zed Zd Zd Zd ZdZd/dZdZdZdZd/d/d/d/d/d/d/d/dZdZedZ dZ!dZ"dZ#dZ$d/d/d/d/d/d/d/dZ%dZ&dZ'dZ(dZ)dZ*d/d Z+d/d!Z,d/d"Z-d#Z.d$Z/d%Z0d&Z1d/d'Z2d(Z3d/d)Z4d0d/d*Z5d+Z6d,Z7e7Z8ed-Z9e9j:d.Z9RS(1sU The base Raven client. Will read default configuration from the environment variable ``SENTRY_DSN`` if available. >>> from raven import Client >>> # Read configuration from ``os.environ['SENTRY_DSN']`` >>> client = Client() >>> # Specify a DSN explicitly >>> client = Client(dsn='https://public_key:secret_key@sentry.local/project_id') >>> # Record an exception >>> try: >>> 1/0 >>> except ZeroDivisionError: >>> ident = client.get_ident(client.captureException()) >>> print "Exception caught; reference is %s" % ident travent6t transportsc K@s| } t|_||_|j} t|_tjd| j| j f|_ tjd|_ tjd|_ i|_ |j||t| jdpg|_t| jdpg|_t| jdp| jdptj|_t| jdptj|_t| jd tj|_t| jd pRtj|_t| jd pstj|_ | jd |_!| jd t"|_#| jd|_$|j$dkrtj&|_$n| jd} | dkr it't(dgd6} n| |_)| jdp&i|_*| jdp>d|_+| jdpbt,j-jd|_.|j/| jd|_0| jddk r| jdnd|_1t2|_3t| jdpd|_4t5|_6t7||_8|j9s|j j:dnt;dkr)|a;nt<|_=||_>ddl?m@} | ||_A|rp|jBn|r|jCn|jD|dS(Ns%s.%ss sentry.errorsssentry.errors.uncaughtt include_pathst exclude_pathsRtmachinetauto_log_stackstcapture_localststring_max_lengthtlist_max_lengthtsitetinclude_versionst processorstcontexttargvssys.argvttagst environmenttreleasetHEROKU_SLUG_COMMITtrepost sample_rateitignore_exceptionssaRaven is not configured (logging is disabled). Please see the documentation for more information.i(tContext((ERt _local_statetraise_send_errorst __class__R0tstatetloggingt getLoggerR/R.tloggert error_loggertuncaught_loggert_transport_cachetset_dsntsettgetRDRERRtNAMERtbooltAUTO_LOG_STACKSRGtCAPTURE_LOCALSRHtinttMAX_LENGTH_STRINGRItMAX_LENGTH_LISTRJRKR7RLRMR t PROCESSORSRRtextraRPRQtostenvironRRt _format_reposRTRURt transactionRVR$t module_cacheRt_randomt is_enabledtinfotRaventget_thread_identtmain_thread_idtenable_breadcrumbst raven.contextRWt_contexttinstall_sys_hooktinstall_logging_hookthook_libraries(R(tdsnRYt transportR|R}R~Ryt _random_seedtoptionstotclsRNRW((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyR6sn     0  $$            cC@si|s iSi}xRt|D]D\}}|ddkrWtjjt|j}n|||>> result = client.capture(**kwargs) >>> ident = client.get_ident(result) sVClient.get_ident is deprecated. The event ID is now returned as the result of capture.(twarningstwarntDeprecationWarning(R(R((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyR7s cC@s|j||S(N(Rr(R(R((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt get_handlerCscC@s7|jsdS|jj}|s)|Sd||fS(s Returns a public DSN which is consumable by raven-js >>> # Return scheme-less DSN >>> print client.get_public_dsn() >>> # Specify a scheme to use (http or https) >>> print client.get_public_dsn('https') Ns%s:%s(RtRtget_public_dsn(R(Rturl((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRFs cC@sd}d}yAt|do)|djj}|drF|djpId}Wnttfk rfnX|dt|d|t|d|fS(Niii(tidttb_frametf_codettb_lastitAttributeErrort IndexError(R(Rtcode_idtlast_id((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt_get_exception_keyWs !  cC@s|j|}||jjkS(N(RRNtexceptions_to_skip(R(RR)((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pytskip_error_for_loggingiscC@s&|j|}|jjj|dS(N(RRNRtadd(R(RR)((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pytrecord_exception_seenmsc  @stjj} t|jj|}|jdi|jdid|kr]d|}n|j|} | j| } | j dd }|j dr|d}nx3t | D]%\}}||kr|||sc3@s|]}j|VqdS(N(R(RR(R(s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pys sRt server_nameRRRRQRKt fingerprinttmessagetprojectt timestampt time_spenttevent_idtplatformtsdkRTt breadcrumbstvaluesN(3tuuidtuuid4thexRRNtdatat setdefaultRtcapturetpopR RdRRGR7RRRRHtupdateRDt _iter_framesRR:tanyRERqtpeekR\R=RRRRRQRPRmRKRtprocesst to_stringRRRRtutcnowt PLATFORM_NAMEt SDK_VALUERTRyRt get_buffer(R(t event_typeRtdateRRmtstackt public_keyRPRtkwargsRR,RRtktvtframest stack_infotframeRKRR)Rtcrumbs((Rs;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt build_msgqs             +   " cC@st|d|jd|jS(NRJRI(RRJRI(R(R((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRscC@s|jS(sT Updates this clients thread-local context for future events. >>> def view_handler(view_func, *args, **kwargs): >>> client.context.merge(tags={'key': 'value'}) >>> try: >>> return view_func(*args, **kwargs) >>> finally: >>> client.context.clear() (R{(R(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRNs cC@s|jji|d6S(s{ Update the user context for future events. >>> client.user_context({'email': 'foo@example.com'}) tuser(RNtmerge(R(R((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt user_contexts cK@s|jji|d6S(s| Update the http context for future events. >>> client.http_context({'url': 'http://example.com'}) trequest(RNR(R(RR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt http_contexts cK@s|jji|d6S(so Update the extra context for future events. >>> client.extra_context({'foo': 'bar'}) Rm(RNR(R(RR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt extra_context$s cK@s|jji|d6S(sr Update the tags context for future events. >>> client.tags_context({'version': '1.0'}) RP(RNR(R(RR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt tags_context.s c K@s|jsdS| jd} | dk r|j| r>dS|j| st|jjd| ddtjdS|j | n|j ||||||d|| }|dkr|j }n|j j |kr|j|n|d|j_|dS(s Captures and processes an event and pipes it off to SentryClient.send. To use structured data (interfaces) with capture: >>> capture('raven.events.Message', message='foo', data={ >>> 'request': { >>> 'url': '...', >>> 'data': {}, >>> 'query_string': '...', >>> 'method': 'POST', >>> }, >>> 'logger': 'logger.name', >>> }, extra={ >>> 'key': 'value', >>> }) The finalized ``data`` structure contains the following (some optional) builtin values: >>> { >>> # the culprit and version information >>> 'culprit': 'full.module.name', # or /arbitrary/path >>> >>> # all detectable installed modules >>> 'modules': { >>> 'full.module.name': 'version string', >>> }, >>> >>> # arbitrary data provided by user >>> 'extra': { >>> 'key': 'value', >>> } >>> } :param event_type: the module path to the Event class. Builtins can use shorthand class notation and exclude the full module path. :param data: the data base, useful for specifying structured data interfaces. Any key which contains a '.' will be assumed to be a data interface. :param date: the datetime of this event :param time_spent: a integer value representing the duration of the event (in milliseconds) :param extra: a dictionary of additional standard metadata :param stack: a stacktrace for the event :param tags: dict of extra tags :param sample_rate: a float in the range [0, 1] to sample this message :return: a tuple with a 32-length string identifying this event NRs*Not capturing exception due to filters: %siRPR(RtRdR Rtshould_captureR^RuRRRRRURstrandomtsendRXt last_event_id( R(RRRRRmRRPRURR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyR9s*5        cC@s |jjS(sg Return a boolean describing whether the client should attempt to send events. (Rt is_active(R(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRtscc@srd|kr-x|ddD] }|VqWnd|krnx2|ddddjdgD] }|Vq\WndS(NRRRRi(Rd(R(RR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRs    )cC@s|jjdS(N(R[R?(R(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt_successful_sendsc C@sd}t|trUt|tr0|j}n|jjdt|j|jn)|jjd||dt di|d6|j ||j j d|dS(Nis*Sentry responded with an API error: %s(%s)s,Sentry responded with an error: %s (url: %s)RRmRR5( t isinstanceR R R5R_terrorttypeR.RR7t_log_failed_submissionR[R>(R(texcRRR5((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt _failed_sends     cC@s|jdd}|g}d|krd|dddkrxw|ddddjdgD]P}|jdi|jd d d 6|jd dd 6|jd dd6qbWn|jj|dS(sj Log a reasonable representation of an event that should have been sent to Sentry RsRRRiRs- File "%(fn)s", line %(lineno)s, in %(func)stfilenametunknown_filenametfntlinenotfunctiontunknown_functiontfuncN(RRdtappendR`R(R(RRtoutputR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRs $) c@s |dkri}nj rOjj rOjjdSjjdtfd}yXj j }|j r|j |j |n|j|j Wn,tk r}jrn||nXdS(Ns"Sending message of length %d to %sc@s j|jdS(N(Rtdecode(te(RR(R(s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt failed_sends(R RYR[R<RRR^RtlenRt get_transporttis_asynct async_sendRRt Exception(R(RRtheadersRRR((RR(Rs;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt send_remotes&      cK@s"|j|}|j|d|S(sV Serializes the message and passes the payload onto ``send_encoded``. t auth_header(tencodet send_encoded(R(R RR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRsc K@sdtjf}|s[tj}td|jd|d|d|jjd|jj}ni|d6|d6|jd 6d d 6}|j d |jj d |d||S(s Given an already serialized message, signs the message and passes the payload off to ``send_remote``. sraven-python/%stprotocolRR"tapi_keyt api_secrets User-Agents X-Sentry-AuthsContent-Encodingsapplication/octet-streams Content-TypeRRR ( RAtVERSIONR9R tprotocol_versionRRt secret_keytget_content_encodingR tstore_endpoint(R(RR Rt client_stringRR ((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRs&       cC@sdS(Ntdeflate((R(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRscC@stjtj|jdS(s8 Serializes ``data`` into a raw string. tutf8(tzlibtcompressR tdumpsR(R(R((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyR scC@stjtj|jdS(s2 Unserializes a string, ``data``. R(R tloadsRt decompressR(R(R((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRscK@s|jdd||S(sr Creates an event from ``message``. >>> client.captureMessage('My event just happened!') sraven.events.MessageR(R(R(RR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pytcaptureMessagescK@s=|dks|tkr'tj}n|jdd||S(s Creates an event from an exception. >>> try: >>> exc_info = sys.exc_info() >>> client.captureException(exc_info) >>> finally: >>> del exc_info If exc_info is not provided, or is set to True, then this method will perform the ``exc_info = sys.exc_info()`` and the requisite clean-up for you. ``kwargs`` are passed through to ``.capture``. sraven.events.ExceptionRN(R R7RRR(R(RR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRsc@s|ddjjf|j}d|D}d|D}d|D}|kritSj|kr|tS|krtStfd|DrtStfd|DrtStS(Nis%s.%scs@s$|]}t|tr|VqdS(N(Rt basestring(RR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pys 8scs@s$|]}|jdr|VqdS(t*N(tendswith(RR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pys 9scs@s!|]}t|r|VqdS(N(R(RR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pys :sc3@s|]}t|VqdS(N(t issubclass(RR(texc_type(s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pys Bsc3@s"|]}j|d VqdS(iN(R(RR(texc_name(s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pys Ds(R/R.RVR:RR7(R(Rt exclusionststring_exclusionstwildcard_exclusionstclass_exclusions((R&R%s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyR4s"    c @swd}tf}t|tr'|}n|dk r<|}ntjfd}||}|rs||S|S(s Wrap a function or code block in try/except and automatically call ``.captureException`` if it raises an exception, then the exception is reraised. By default, it will capture ``Exception`` >>> @client.capture_exceptions >>> def foo(): >>> raise Exception() >>> with client.capture_exceptions(): >>> raise Exception() You can also specify exceptions to be caught specifically >>> @client.capture_exceptions((IOError, LookupError)) >>> def bar(): >>> ... >>> with client.capture_exceptions((IOError, LookupError)): >>> ... ``kwargs`` are passed through to ``.captureException``. c3@s1y dVWn!|k r,jnXdS(N(R(t exceptions(RR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pytmake_decoratorjs    N(R R RRt contextlibtcontextmanager(R(tfunction_or_exceptionsRRR+R,t decorator((RR(s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pytcapture_exceptionsHs      c K@s"|jdd|d|d||S(si Creates an event for a SQL query. >>> client.captureQuery('SELECT * FROM foo') sraven.events.Querytquerytparamstengine(R(R(R2R3R4R((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyt captureQueryxscK@stjdt|j|S(Ns8captureExceptions is deprecated, used context() instead.(RRRRN(R(R((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pytcaptureExceptionsscO@s|jjj||dS(sp Records a breadcrumb with the current context. They will be sent with the next event. N(RNRtrecord(R(targsR((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pytcaptureBreadcrumbscC@st|jddS(NR(RRXR (R(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRscC@s||j_dS(N(RXR(R(R((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRsN((;R.R/t__doc__R\R]R^RRRRR R:R7R6RpRbR|R}R~t classmethodRRRRRRRRRRRtpropertyRNRRRRRRtRRRRR RRRRRR RRR1R5R6R9tcapture_breadcrumbRtsetter(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyR}sl  Q            Q               0  t DummyClientcB@seZdZdZRS(s"Sends messages into an empty void.cK@sdS(N(R (R(R((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyRs(R.R/R:R(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pyR?s(HR:t __future__RRR\RnRR9RRRtinspectRRRttypesRt threadingRRR-t contextlib2tthreadRRwt ImportErrort_threadRAt raven.confRtraven.conf.remoteRtraven.exceptionsR R t raven.utilsR R R Rtraven.utils.compatRRtraven.utils.encodingRtraven.utils.serializerRtraven.utils.stacksRRtraven.utils.transactionRtraven.transport.registryRRt raven.eventst__all__R RRRRRvtstrR!R#tdictR$tobjectR0RR?(((s;/opt/alt/python27/lib/python2.7/site-packages/raven/base.pytsb          "      %#