U 췀g} @sddlmZmZddlZddlZddlZddlZddlZddlZddl Z ddl m Z m Z m Z ddl mZddlmZz ddlZWnek rdZYnXddlZddlZddlZddlmZddlmZmZmZddlmZdd lmZm Z m!Z!dd l"m#Z#e#rdd l"m$Z$dd l"m%Z%dd l"m&Z&ddl"m'Z'ddl"m(Z(ddl"m)Z)ddl"m*Z*ddl"m+Z+ddl"m,Z,ddl"m-Z-ddl"m.Z.ddl"m/Z/ddl0m1Z1ddl0m2Z2ddl3m4Z4m5Z5gZ6ej7dddfej8dddfej8ddd fej8d!dd"ffD]@Z9z"e6:e9de9de9d#fWne;k r8YnXqGd$d%d%eZGd*d+d+e>Z?zddl@Z@ddlAZAWn&ek rGd,d-d-e?ZBYnXGd.d-d-e>ZBGd/d0d0e<ZCd1d2ZDdS)4)ABCabstractmethodN)datetime timedeltatimezone) defaultdict) getproxies) EndpointType)Dsnloggercapture_internal_exceptions)BackgroundWorker)EnvelopeItem PayloadRef) TYPE_CHECKING)Any)Callable)Dict) DefaultDict)Iterable)List)Mapping)Optional)Self)Tuple)Type)Union) PoolManager) ProxyManager)EventEventDataCategorycCs ttdS)N SO_KEEPALIVEgetattrsocketr&r&B/opt/hc_python/lib/python3.8/site-packages/sentry_sdk/transport.py2r(cCs ttdS)N TCP_KEEPIDLEr#r&r&r&r'r(3r)-cCs ttdS)N TCP_KEEPINTVLr#r&r&r&r'r(4r) cCs ttdS)N TCP_KEEPCNTr#r&r&r&r'r(5r)c@sdeZdZdZdZdddZddZeddZdd d Z d d Z dd dddZ ddZ ddZ dS) TransportzWBaseclass for all transports. A transport is used to send an event to sentry. NcCs8||_|r.|ddk r.|dr.t|d|_nd|_dS)Ndsn)optionsr parsed_dsnselfr4r&r&r'__init__GszTransport.__init__cCs.tjdtddt}||||dS)z DEPRECATED: Please use capture_envelope instead. This gets invoked with the event dictionary when an event should be sent to sentry. zAcapture_event is deprecated, please use capture_envelope instead!r1 stacklevelN)warningswarnDeprecationWarningrZ add_eventcapture_envelope)r7eventenveloper&r&r' capture_eventOs  zTransport.capture_eventcCsdS)a Send an envelope to Sentry. Envelopes are a data container format that can hold any type of data submitted to Sentry. We use it to send all event data (including errors, transactions, crons check-ins, etc.) to Sentry. Nr&)r7r@r&r&r'r>bs zTransport.capture_envelopecCsdS)z Wait `timeout` seconds for the current events to be sent out. The default implementation is a no-op, since this method may only be relevant to some transports. Subclasses should override this method if necessary. Nr&r7timeoutcallbackr&r&r'flushns zTransport.flushcCsdS)z Forcefully kills the transport. The default implementation is a no-op, since this method may only be relevant to some transports. Subclasses should override this method if necessary. Nr&r7r&r&r'kill|szTransport.killr*quantitycCsdS)aThis increments a counter for event loss by reason and data category by the given positive-int quantity (default 1). If an item is provided, the data category and quantity are extracted from the item, and the values passed for data_category and quantity are ignored. When recording a lost transaction via data_category="transaction", the calling code should also record the lost spans via this method. When recording lost spans, `quantity` should be set to the number of contained spans, plus one for the transaction itself. When passing an Item containing a transaction via the `item` parameter, this method automatically records the lost spans. Nr&)r7reason data_categoryitemrIr&r&r'record_lost_eventszTransport.record_lost_eventcCsdS)NTr&rFr&r&r' is_healthyszTransport.is_healthycCs&z |Wntk r YnXdSN)rG ExceptionrFr&r&r'__del__s zTransport.__del__)N)N)NN)__name__ __module__ __qualname____doc__r5r8rArr>rErGrMrNrQr&r&r&r'r2?s      r2c cs|dkrttj}|dD]}z|d}|dd\}}|tt|d}|rb|dpddD]Z}|dkrz|dd}Wntk rg}YnX|rd |kr||fVqf||fVqfWqt t fk rYqYqXqdS) N,:r1seconds;rO metric_bucketZcustom) rnowrutcsplitstriprint IndexError LookupError ValueError) headerr]limit parametersZretry_after_val categories retry_aftercategory namespacesr&r&r'_parse_rate_limitss$    rlc@seZdZdZddZd6ddddZd d Zd d Zej dfd dZ ddZ d7ddZ d8ddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd9d-d.Zd/d0Zed1d2Zed3d4Zejd5d4ZdS):BaseHttpTransportzThe base HTTP transport.cCsLddlm}t|||jdk s&t||_t|dd|_|j d||_ i|_ t j |_tt|_t|_||_tj|_|di}|d|d}|d |dk stdkrd nd }|d krtdkrtd d }d}|d kr td|d|_d|_n||_|dk r"||_n&|jd kr6d|_n|jd krHd|_dS)Nr)VERSIONZtransport_queue_size)Z queue_sizezsentry.python/%s _experimentsZtransport_compression_levelZ transport_zlib_compression_levelZtransport_compression_algogzipbrzSYou asked for brotli compression without the Brotli module, falling back to gzip -9)rqrpz2Unknown compression algo %s, disabling compression r\) sentry_sdk.constsrnr2r8r5AssertionErrorr4r _workerZto_auth_auth_disabled_untilurllib3utilRetry_retryrra_discarded_eventstime_last_client_report_sent _make_pool_pool sentry_sdkZHub_hub_clsgetbrotlir warning_compression_level_compression_algo)r7r4rnZ experimentsZcompression_levelZcompression_algor&r&r'r8s^           zBaseHttpTransport.__init__Nr*rHcCs|jdsdS|dk rv|j}d}|dkr\|p2i}t|dpBgd}|j|d|dq|dkrt|prd}n|dkrtd|j||f|7<dS) Nsend_client_reportsr* transactionspansspanrH attachmentzdata category not provided) r4rKZget_transaction_eventlenrrM get_bytes TypeErrorr|)r7rJrKrLrIr?Z span_countr&r&r'rMs  z#BaseHttpTransport.record_lost_eventcCs |j|SrO)headersrr7responserer&r&r'_get_header_value(sz#BaseHttpTransport._get_header_valuecCs||d}|r,td|jt|nX|jdkrtd||d}|dk r`|j|ndpfd}t t j t |d|jd<dS)Nzx-sentry-rate-limitsz%Rate-limited via x-sentry-rate-limitszRate-limited via 429z Retry-After<rX)rr rrwupdaterlstatusr{parse_retry_afterrr]rr^r)r7rreZretry_after_valuerir&r&r'_update_rate_limits,s       z%BaseHttpTransport._update_rate_limitsc sfdd}|tjjtjdzd|||}Wn(tk rld|dYnXzr ||j dkrdnN|j d ks|j d krt d |j t |d t |d dd|j |dW5|XdS)Ncs6dkrj|ddnjD]}j||dqdS)Nerror)rKrL)rMitems)rJrLr@r7r&r' record_lossNs z4BaseHttpTransport._send_request..record_loss)z User-Agentz X-Sentry-AuthPOSTnetworkZ network_errorrZ status_429i,z%Unexpected status code: %s (body: %s)datacontentz status_{})rstrrvclientZ to_header_requestrPon_dropped_eventcloserrr rr$format)r7bodyr endpoint_typer@rrr&rr' _send_requestEs>        zBaseHttpTransport._send_requestcCsdSrOr&)r7Z_reasonr&r&r'r~sz"BaseHttpTransport.on_dropped_eventFrcCst|jdsdS|s(|jt|ks(dS|j}tt|_t|_|sJdStttdd|DddddS)NrcSs g|]\\}}}|||dqS))rJrjrIr&).0rjrJrIr&r&r' szBBaseHttpTransport._fetch_pending_client_report..) timestampdiscarded_events)json client_report)type) r4r~r}r|rrarrr)r7forceintervalrr&r&r'_fetch_pending_client_reports&    z.BaseHttpTransport._fetch_pending_client_reportcCs,|j|dd}|dk r(|t|gddS)Nr)rr)r)rr>r)r7rrr&r&r'_flush_client_reportssz'BaseHttpTransport._flush_client_reportscsfdd}||p|dS)Ncs0|dkr d}j|}|dk o.|ttjkS)Nstatsdr[)rwrrr]rr^)ZbuckettsrFr&r' _disableds z4BaseHttpTransport._check_disabled.._disabledr&)r7rjrr&rFr'_check_disableds z!BaseHttpTransport._check_disabledcCstdd|jDS)Ncss|]}|ttjkVqdSrO)rr]rr^)rrr&r&r' sz5BaseHttpTransport._is_rate_limited..)anyrwvaluesrFr&r&r'_is_rate_limitedsz"BaseHttpTransport._is_rate_limitedcCs |jSrO)rufullrFr&r&r'_is_worker_fullsz!BaseHttpTransport._is_worker_fullcCs|p| SrO)rrrFr&r&r'rNszBaseHttpTransport.is_healthycCsg}|jD]>}||jr>|jdkr.|d|jd|dq ||q t|j|d}|jsbdS|jdd}|dk r|j|| |\}}|j dk st t d|j|j j|j jd d i}|r||d <|j||tj|d dS) N)rrdefaultrZself_rate_limitsZratelimit_backoffr)rr)rz(Sending envelope [%s] project:%s host:%sz Content-Typezapplication/x-sentry-envelopezContent-Encoding)rrr@)rrrKrrMappendrrr_serialize_enveloper5rtr debug descriptionZ project_idhostrgetvaluer ENVELOPE)r7r@Z new_itemsrLZclient_report_itemcontent_encodingrrr&r&r'_send_envelopesB       z BaseHttpTransport._send_envelopec Csd}t}|jdks |jdkr,||n\|j}|jdkr`tdk r`|tj||jdn(t j |d|jd}||W5QRX||fS)Nrrq)Zqualityw)fileobjmode compresslevel) ioBytesIOrrZserialize_intorwritecompress serializerpGzipFile)r7r@rrfr&r&r'rs( z%BaseHttpTransport._serialize_envelopecCs tdSrONotImplementedErrorrFr&r&r'_get_pool_optionssz#BaseHttpTransport._get_pool_optionscCsNtd}|sdS|dD]*}|}|j|sB|j|rdSqdS)NnoFrVT)rrr_r`rendswithnetloc)r7r5no_proxyrr&r&r' _in_no_proxys zBaseHttpTransport._in_no_proxycCs tdSrOrrFr&r&r'rszBaseHttpTransport._make_poolcCs tdSrOrr7methodrrrr&r&r'r"szBaseHttpTransport._requestcsBfdd}j|s>djD]}jd|dq*dS)Nc s(tW5QRXdSrO)r rrr&rr&r'send_envelope_wrapper0s zABaseHttpTransport.capture_envelope..send_envelope_wrapperZ full_queueZqueue_overflowr)rusubmitrrrM)r7r@rrLr&rr'r>,s    z"BaseHttpTransport.capture_envelopecs8td|dkr4jfddj||dS)NzFlushing HTTP transportrcs jddS)NT)r)rr&rFr&r'r(Dr)z)BaseHttpTransport.flush..)r rrurrErBr&rFr'rE;s zBaseHttpTransport.flushcCstd|jdS)NzKilling HTTP transport)r rrurGrFr&r&r'rGGs zBaseHttpTransport.killcCstjdtdddS)zRConvenience method to warn users about the deprecation of the `hub_cls` attribute.zNThe `hub_cls` attribute is deprecated and will be removed in a future release.r9N)r;r<r=r&r&r&r' _warn_hub_clsLs zBaseHttpTransport._warn_hub_clscCst|jS)QDEPRECATED: This attribute is deprecated and will be removed in a future release. HttpTransportrrrFr&r&r'hub_clsVszBaseHttpTransport.hub_clscCst||_dS)rNr)r7valuer&r&r'r]s)NN)Fr)F)N) rRrSrTrUr8rMrrr rrrrrrrrrNrrrrrrr>rErG staticmethodrpropertyrsetterr&r&r&r'rmsF@ " 9  5     rmc@s:eZdZUereeefed<ddZddZ ddZ dS) rrcCs|jdid}|dkr dnt|dd}d}|jddk rJ|jd}|jdr|dkr`g}dd |D}tD]"}|d |d f|krr||qr|dk r||d<|jd ptjd ptjdpt|d <|jdptjd|d<|jdptjd|d<|S)NroZtransport_num_poolsr1 CERT_REQUIRED) num_pools cert_reqssocket_options keep_alivecSsh|]}|d|dfqSrr*r&ror&r&r' {sz2HttpTransport._get_pool_options..rr*ca_certs SSL_CERT_FILEREQUESTS_CA_BUNDLE cert_fileCLIENT_CERT_FILEkey_fileCLIENT_KEY_FILE) r4rraKEEP_ALIVE_SOCKET_OPTIONSrosenvironcertifiwhere)r7rr4r used_optionsdefault_optionr&r&r'ris<      zHttpTransport._get_pool_optionsc Cs*|jdkrtdd}||j}|jd}|jjdkrV|dkrV|pT| oTtd}|jd}|s|dkr|p| otd}|}|r|jd}|r||d<|dr d }zd d l m }Wn$t k rd }t d |YnX|r||f|Stjf|Sntj|f|Sn tjf|SdS)N4Cannot create HTTP-based transport without valid DSN https_proxyhttps http_proxyhttp proxy_headerssocksTr)SOCKSProxyManagerFzYou have configured a SOCKS proxy (%s) but support for SOCKS proxies is not installed. Disabling proxy support. Please add `PySocks` (or `urllib3` with the `[socks]` extra) to your dependencies.)r5rdrr4schemerrr startswithZurllib3.contrib.socksr  ImportErrorr rrxrr) r7proxyrrr optsr Zuse_socks_proxyr r&r&r'rs<         zHttpTransport._make_poolcCs|jj||j|||dS)N)rrrrequestrvZ get_api_urlrr&r&r'rs  zHttpTransport._requestN) rRrSrTrrrr__annotations__rrrr&r&r&r'res *.rcseZdZfddZZS)Http2Transportcst|tddS)NzhYou tried to use HTTP2Transport but don't have httpcore[http2] installed. Falling back to HTTPTransport.)superr8r rr6 __class__r&r'r8s zHttp2Transport.__init__)rRrSrTr8 __classcell__r&r&rr'rsrc@sNeZdZUdZer*eejejej fe d<ddZ ddZ ddZ d d Zd S) rz&The HTTP2 transport based on httpcore.rcstfdd|jDdS)Nc3s.|]&\}}|dkr|dVqdS)asciiN)decodelower)rkeyvalrer&r'rsz3Http2Transport._get_header_value..)nextrrr&r r'rs  z Http2Transport._get_header_valuecCs |jj||j|||d}|S)N)rrr)r7rrrrrr&r&r'rs zHttp2Transport._requestcCs|jdk o|jjdkdd}|jddk r4|jdng}dd|D}tD]"}|d|df|krJ||qJ||d<t}||jd ptj d ptj d pt |jd ptj d }|jdptj d}|dk r| ||||d<|S)Nrr)Zhttp2retriesrcSsh|]}|d|dfqSrr&rr&r&r'rsz3Http2Transport._get_pool_options..rr*rrrrrrr ssl_context)r5rr4rrsslcreate_default_contextload_verify_locationsrrrrrload_cert_chain)r7r4rrrr#rrr&r&r'rs8        z Http2Transport._get_pool_optionscCs.|jdkrtdd}||j}|jd}|jjdkrV|dkrV|pT| oTtd}|jd}|s|dkr|p| otd}|}|r"|jd}|r||d<|drz6d |kr| d }|rt d t j fd |i|WStk r t d |YnXnt jfd |i|St jf|S) Nrrrrr r r r rzqYou have defined socket_options but using a SOCKS proxy which doesn't support these. We'll ignore socket_options. proxy_urlzoYou have configured a SOCKS proxy (%s) but support for SOCKS proxies is not installed. Disabling proxy support.)r5rdrr4rrrrrpopr rhttpcore SOCKSProxy RuntimeError HTTPProxyConnectionPool)r7rrrr rr rr&r&r'r(s>         zHttp2Transport._make_poolN)rRrSrTrUrrr*r+r-r.rrrrrr&r&r&r'rs  %c@s0eZdZdZddZddZedddd ZdS) _FunctionTransportz DEPRECATED: Users wishing to provide a custom transport should subclass the Transport class, rather than providing a function. cCst|||_dSrO)r2r8_func)r7funcr&r&r'r8Zs z_FunctionTransport.__init__cCs||dSrO)r0)r7r?r&r&r'rAas z _FunctionTransport.capture_eventN)r@returncCs|}|dk r||dSrO)Z get_eventrA)r7r@r?r&r&r'r>hsz#_FunctionTransport.capture_envelope)rRrSrTrUr8rArr>r&r&r&r'r/Tsr/cCs|d}|didd}|r$tnt}t|tr6|St|trPt|trP|}n t|rptj dt ddt |S|dr||SdS) N transportroZtransport_http2FzFunction transports are deprecated and will be removed in a future release.Please provide a Transport instance or subclass, instead.r1r9r3) rrr isinstancer2r issubclasscallabler;r<r=r/)r4Z ref_transportZuse_http2_transportZ transport_clsr&r&r'make_transportqs$  r7)N)Eabcrrrrrpr%r$r}r;rrr collectionsrurllib.requestrrrrxrrrsr Zsentry_sdk.utilsr r r Zsentry_sdk.workerr Zsentry_sdk.enveloperrrtypingrrrrrrrrrrrrrZurllib3.poolmanagerrrZsentry_sdk._typesr r!r SOL_SOCKETSOL_TCPoptionrAttributeErrorr2rlrmrr*Zh2rr/r7r&r&r&r's~                     " l "l  t