o
    h~                  	   @   s  d dl mZmZ d dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
m
Z
mZmZ d dlmZ d dlmZ zd dlZW n eyK   dZY nw d dlZd dlZd dlZd dlmZ d dlmZmZmZ d dlmZ d d	lmZm Z m!Z! d d
l"m#Z#m$Z$m%Z%m&Z& e#rd dl"m'Z' d dl"m(Z( d dl"m)Z) d dl"m*Z* d dl"m+Z+ d dl"m,Z, d dl"m-Z- d dl"m.Z. d dl"m/Z/ d dl"m0Z0 d dl1m2Z2 d dl1m3Z3 d dl4m5Z5m6Z6 g Z7ej8dd dfej9dd dfej9dd dfej9dd d ffD ]Z:ze7;e:d  e:d  e:d! f W q e<y   Y qw G d"d# d#eZ=d1d$d%Z>G d&d' d'e=Z?G d(d) d)e?Z@z
d dlAZAd dlBZBW n eyM   G d*d+ d+e@ZCY n	w G d,d+ d+e?ZCG d-d. d.e=ZDd/d0 ZEdS )2    )ABCabstractmethodN)datetime	timedeltatimezone)defaultdict)
getproxies)EndpointType)Dsnloggercapture_internal_exceptions)BackgroundWorker)EnvelopeItem
PayloadRef)TYPE_CHECKINGcastListDict)Any)Callable)DefaultDict)Iterable)Mapping)Optional)Self)Tuple)Type)Union)PoolManager)ProxyManager)EventEventDataCategoryc                   C   
   t tdS )NSO_KEEPALIVEgetattrsocket r(   r(   V/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/sentry_sdk/transport.py<lambda>0      
 r*      c                   C   r#   )NTCP_KEEPIDLEr%   r(   r(   r(   r)   r*   1   r+   -   c                   C   r#   )NTCP_KEEPINTVLr%   r(   r(   r(   r)   r*   2   r+   
   c                   C   r#   )NTCP_KEEPCNTr%   r(   r(   r(   r)   r*   3   r+         c                   @   sb   e Zd ZdZdZdddZdd Zedd Z	dd	d
Z	dd Z
		dddddZdd ZdS )	TransportzWBaseclass for all transports.

    A transport is used to send an event to sentry.
    Nc                 C   s:   || _ |r|d d ur|d rt|d | _d S d | _d S )Ndsn)optionsr
   
parsed_dsnselfr6   r(   r(   r)   __init__E   s   
zTransport.__init__c                 C   s.   t jdtdd t }|| | | dS )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!r3   
stacklevelN)warningswarnDeprecationWarningr   	add_eventcapture_envelope)r9   eventenveloper(   r(   r)   capture_eventM   s   	
zTransport.capture_eventc                 C      dS )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(   )r9   rC   r(   r(   r)   rA   `   s   
zTransport.capture_envelopec                 C   rE   )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(   r9   timeoutcallbackr(   r(   r)   flushl   s   zTransport.flushc                 C   rE   )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(   r9   r(   r(   r)   killz   s   zTransport.killr,   quantityc                C   rE   )a  This 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(   )r9   reasondata_categoryitemrM   r(   r(   r)   record_lost_event   s   zTransport.record_lost_eventc                 C   rE   )NTr(   rJ   r(   r(   r)   
is_healthy      zTransport.is_healthyNNN)__name__
__module____qualname____doc__r7   r:   rD   r   rA   rI   rK   rQ   rR   r(   r(   r(   r)   r4   =   s     


r4   c           	   
   c   s    |d u rt tj}| dD ]]}zP| d}|d d \}}|tt|d }|r2|dp3dD ],}|dkr[z	|d d}W n tyN   g }Y nw |rUd	|v rZ||fV  q4||fV  q4W q t	t
fym   Y qw d S )
N,:r3   seconds;rT   metric_bucket   custom)r   nowr   utcsplitstripr   int
IndexErrorLookupError
ValueError)	headerrb   limit
parametersretry_after_val
categoriesretry_aftercategory
namespacesr(   r(   r)   _parse_rate_limits   s0   
rr   c                   @   s   e Zd ZdZdZdd Z		d7dddd	Zd
d Zdd Ze	j
dfddZdd Zd8ddZd9ddZdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Z	d:d.d/Zd0d1 Zed2d3 Zed4d5 Zej d6d5 ZdS );BaseHttpTransportzThe base HTTP transport.   c                 C   sL  ddl m} t| | | jd usJ || _t|d d| _| jd| | _	i | _
tj | _tt| _t | _|  | _tj| _|di }|d|d}|d	|d us]td u r_d
nd}|dkrstd u rstd d
}d }|dvrtd| d| _d | _n|| _|d ur|| _d S | jd
krd| _d S | jdkrd| _d S d S )Nr   )VERSIONtransport_queue_size)
queue_sizezsentry.python/%s_experimentstransport_compression_level transport_zlib_compression_leveltransport_compression_algogzipbrzSYou asked for brotli compression without the Brotli module, falling back to gzip -9)r}   r|   z2Unknown compression algo %s, disabling compression	   r`   )sentry_sdk.constsru   r4   r:   r7   r6   r   _workerto_auth_auth_disabled_untilurllib3utilRetry_retryr   rf   _discarded_eventstime_last_client_report_sent
_make_pool_pool
sentry_sdkHub_hub_clsgetbrotlir   warning_compression_level_compression_algo)r9   r6   ru   experimentscompression_levelcompression_algor(   r(   r)   r:      sZ   






zBaseHttpTransport.__init__Nr,   rL   c                C   s   | j d sd S |d urD|j}d}|dkr7| pi }ttttttf  |	dp)g d }| j
|d|d n|dkrCt| pBd}n|d u rLtd| j||f  |7  < d S )	Nsend_client_reportsr,   transactionspansspanrL   
attachmentzdata category not provided)r6   rO   get_transaction_eventlenr   r   r   strobjectr   rQ   	get_bytes	TypeErrorr   )r9   rN   rO   rP   rM   rB   
span_countr(   r(   r)   rQ      s    
	&z#BaseHttpTransport.record_lost_eventc                 C   s   |j |S rT   )headersr   r9   responserj   r(   r(   r)   _get_header_value#  s   z#BaseHttpTransport._get_header_valuec                 C   s   |  |d}|rtd | jt| d S |jdkrEtd |  |d}|d ur1| j|nd p4d}t	
tjt|d | jd < d S d S )Nzx-sentry-rate-limitsz%Rate-limited via x-sentry-rate-limits  zRate-limited via 429zRetry-After<   r\   )r   r   r   r   updaterr   statusr   parse_retry_afterr   rb   r   rc   r   )r9   r   rj   retry_after_valuero   r(   r(   r)   _update_rate_limits'  s"   


z%BaseHttpTransport._update_rate_limitsc                    s   fdd}| tjjtj d z
d|||}W n ty3   d |d  w zL| |j	dkrEd n.|j	d	ksO|j	d
k rzt
d|j	t|dt|dd  d|j	 |d W |  d S W |  d S W |  d S |  w )Nc                    s8    d u rj | dd d S  jD ]	}j | |d qd S )Nerror)rO   rP   )rQ   items)rN   rP   rC   r9   r(   r)   record_lossI  s
   
z4BaseHttpTransport._send_request.<locals>.record_loss)z
User-AgentzX-Sentry-AuthPOSTnetworknetwork_errorr   
status_429i,     z%Unexpected status code: %s (body: %s)datacontentz	status_{})r   r   r   client	to_header_request	Exceptionon_dropped_eventr   r   r   r   r&   formatclose)r9   bodyr   endpoint_typerC   r   r   r(   r   r)   _send_request@  sH   	





	zBaseHttpTransport._send_requestc                 C   s   d S rT   r(   )r9   _reasonr(   r(   r)   r   y  rS   z"BaseHttpTransport.on_dropped_eventFr   c                 C   st   | j d sd S |s| jt | k sd S | j}tt| _t | _|s%d S ttt dd | D ddddS )Nr   c                 S   s    g | ]\\}}}|||d qS ))rN   rp   rM   r(   ).0rp   rN   rM   r(   r(   r)   
<listcomp>  s    
zBBaseHttpTransport._fetch_pending_client_report.<locals>.<listcomp>)	timestampdiscarded_events)jsonclient_report)type)	r6   r   r   r   r   rf   r   r   r   )r9   forceintervalr   r(   r(   r)   _fetch_pending_client_report}  s&   


z.BaseHttpTransport._fetch_pending_client_reportc                 C   s0   | j |dd}|d ur| t|gd d S d S )Nr   )r   r   )r   )r   rA   r   )r9   r   r   r(   r(   r)   _flush_client_reports  s   z'BaseHttpTransport._flush_client_reportsc                    s    fdd}||p|d S )Nc                    s0   | dkrd}  j | }|d uo|ttjkS )Nstatsdr_   )r   r   r   rb   r   rc   )buckettsrJ   r(   r)   	_disabled  s   z4BaseHttpTransport._check_disabled.<locals>._disabledr(   )r9   rp   r   r(   rJ   r)   _check_disabled  s   z!BaseHttpTransport._check_disabledc                 C   s   t dd | j D S )Nc                 s   s     | ]}|t tjkV  qd S rT   )r   rb   r   rc   )r   r   r(   r(   r)   	<genexpr>  s    
z5BaseHttpTransport._is_rate_limited.<locals>.<genexpr>)anyr   valuesrJ   r(   r(   r)   _is_rate_limited  s   z"BaseHttpTransport._is_rate_limitedc                 C   s
   | j  S rT   )r   fullrJ   r(   r(   r)   _is_worker_full  s   
z!BaseHttpTransport._is_worker_fullc                 C   s   |   p|   S rT   )r   r   rJ   r(   r(   r)   rR     s   zBaseHttpTransport.is_healthyc                 C   s   g }|j D ]}| |jr|jdv r| d | jd|d q|| qt|j|d}|j s1d S | jdd}|d urA|j | | 	|\}}| j
d usOJ td|j| j
j| j
j d	d
i}|rf||d< | j| |tj|d d S )N)r   r   defaultr   self_rate_limitsratelimit_backoffr   )r   r   rt   )r   z(Sending envelope [%s] project:%s host:%szContent-Typezapplication/x-sentry-envelopezContent-Encoding)r   r   rC   )r   r   rO   r   rQ   appendr   r   r   _serialize_enveloper7   r   debugdescription
project_idhostr   getvaluer	   ENVELOPE)r9   rC   	new_itemsrP   client_report_itemcontent_encodingr   r   r(   r(   r)   _send_envelope  s@   


z BaseHttpTransport._send_envelopec                 C   s   d }t  }| jdks| jd u r|| ||fS | j}| jdkr6td ur6|tj| | jd ||fS t	j
|d| jd}|| W d    ||fS 1 sRw   Y  ||fS )Nr   r}   )qualityw)fileobjmodecompresslevel)ioBytesIOr   r   serialize_intor   writecompress	serializer|   GzipFile)r9   rC   r   r   fr(   r(   r)   r     s,   


z%BaseHttpTransport._serialize_envelopec                 C      t  rT   NotImplementedErrorrJ   r(   r(   r)   _get_pool_options
     z#BaseHttpTransport._get_pool_optionsc                 C   sN   t  d}|s
dS |dD ]}| }|j|s!|j|r$ dS qdS )NnoFrZ   T)r   r   rd   re   r   endswithnetloc)r9   r7   no_proxyr   r(   r(   r)   _in_no_proxy  s   zBaseHttpTransport._in_no_proxyc                 C   r   rT   r   rJ   r(   r(   r)   r     r   zBaseHttpTransport._make_poolc                 C   r   rT   r   r9   methodr   r   r   r(   r(   r)   r     s   zBaseHttpTransport._requestc                    sF    fdd}j |sd  jD ]}jd|d qd S d S )Nc                      s>   t       W d    d S 1 sw   Y  d S rT   )r   r   r   r(   r   r(   r)   send_envelope_wrapper+  s   

"zABaseHttpTransport.capture_envelope.<locals>.send_envelope_wrapper
full_queuequeue_overflowr   )r   submitr   r   rQ   )r9   rC   r  rP   r(   r   r)   rA   '  s   

z"BaseHttpTransport.capture_envelopec                    s<   t d |dkr j fdd  j|| d S d S )NzFlushing HTTP transportr   c                      s    j ddS )NT)r   )r   r(   rJ   r(   r)   r*   ?  s    z)BaseHttpTransport.flush.<locals>.<lambda>)r   r   r   r  rI   rF   r(   rJ   r)   rI   6  s
   
zBaseHttpTransport.flushc                 C   s   t d | j  d S )NzKilling HTTP transport)r   r   r   rK   rJ   r(   r(   r)   rK   B  s   
zBaseHttpTransport.killc                   C   s   t jdtdd dS )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.   r;   N)r=   r>   r?   r(   r(   r(   r)   _warn_hub_clsG  s
   
zBaseHttpTransport._warn_hub_clsc                 C   s   t   | jS )QDEPRECATED: This attribute is deprecated and will be removed in a future release.HttpTransportr	  r   rJ   r(   r(   r)   hub_clsQ  s   zBaseHttpTransport.hub_clsc                 C   s   t   || _dS )r
  Nr  )r9   valuer(   r(   r)   r  X  s   
rU   )Fr   )FrT   )!rV   rW   rX   rY   TIMEOUTr:   rQ   r   r   r	   r   r   r   r   r   r   r   r   rR   r   r   r   r  r   r   rA   rI   rK   staticmethodr	  propertyr  setterr(   r(   r(   r)   rs      sJ    @$
9

5


	
rs   c                   @   s:   e Zd ZU ereeef ed< dd Zdd Z	dd Z
dS )	r  r   c                 C   s  | j di d}|d u rdnt|dtj| jdd}d }| j d d ur+| j d }| j d rQ|d u r6g }d	d
 |D }tD ]}|d |d f|vrP|| q?|d urY||d< | j d pmtj	dpmtj	dpmt
 |d< | j d pztj	d|d< | j d ptj	d|d< |S )Nrx   transport_num_poolsr3   CERT_REQUIRED)total)	num_pools	cert_reqsrG   socket_options
keep_alivec                 S      h | ]
}|d  |d fqS r   r,   r(   r   or(   r(   r)   	<setcomp>w      z2HttpTransport._get_pool_options.<locals>.<setcomp>r   r,   ca_certsSSL_CERT_FILEREQUESTS_CA_BUNDLE	cert_fileCLIENT_CERT_FILEkey_fileCLIENT_KEY_FILE)r6   r   rf   r   Timeoutr  KEEP_ALIVE_SOCKET_OPTIONSr   osenvironcertifiwhere)r9   r  r6   r  used_optionsdefault_optionr(   r(   r)   r   d  s@   





zHttpTransport._get_pool_optionsc           	      C   s,  | j d u r	tdd }| | j }| jd }| j jdkr+|dkr+|p*| o*t d}| jd }|sA|dkrA|p@| o@t d}|  }|r| jd }|rR||d< |drd	}zd
dl	m
} W n tyr   d}td| Y nw |r}||fi |S tjdi |S tj|fi |S tjdi |S )N4Cannot create HTTP-based transport without valid DSNhttps_proxyhttps 
http_proxyhttpproxy_headerssocksTr   )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.r(   )r7   ri   r  r6   schemer   r   r   
startswithurllib3.contrib.socksr7  ImportErrorr   r   r   r   r    )	r9   proxyr   r0  r3  optsr5  use_socks_proxyr7  r(   r(   r)   r     s>   




zHttpTransport._make_poolc                 C   s   | j j|| j|||dS )N)r   r   )r   requestr   get_api_urlr  r(   r(   r)   r     s   
zHttpTransport._requestN)rV   rW   rX   r   r   r   r    __annotations__r   r   r   r(   r(   r(   r)   r  `  s   
 +.r  c                       s   e Zd Z fddZ  ZS )Http2Transportc                    s   t  | td d S )NzhYou tried to use HTTP2Transport but don't have httpcore[http2] installed. Falling back to HTTPTransport.)superr:   r   r   r8   	__class__r(   r)   r:     s   zHttp2Transport.__init__)rV   rW   rX   r:   __classcell__r(   r(   rD  r)   rB    s    rB  c                   @   sR   e Zd ZU dZdZereejej	ej
f ed< dd Zdd Zdd	 Zd
d ZdS )rB  z&The HTTP2 transport based on httpcore.   r   c                    s   t  fdd|jD d S )Nc                 3   s0    | ]\}}| d   kr| d V  qdS )asciiN)decodelower)r   keyvalrj   r(   r)   r     s    z3Http2Transport._get_header_value.<locals>.<genexpr>)nextr   r   r(   rM  r)   r     s   
z Http2Transport._get_header_valuec                 C   s8   | j j|| j|||d| j| j| j| jdid}|S )NrG   )poolconnectr   read)r   r   
extensions)r   r?  r   r@  r  )r9   r  r   r   r   r   r(   r(   r)   r     s   
zHttp2Transport._requestc                 C   s   | j d uo
| j jdkdd}| jd d ur| jd ng }dd |D }tD ]}|d |d f|vr6|| q%||d< t }|| jd	 pUtj	
d
pUtj	
dpUt  | jd patj	
d}| jd pltj	
d}|d urw||| ||d< |S )Nr1  r  )http2retriesr  c                 S   r  r  r(   r  r(   r(   r)   r    r  z3Http2Transport._get_pool_options.<locals>.<setcomp>r   r,   r   r!  r"  r#  r$  r%  r&  ssl_context)r7   r8  r6   r(  r   sslcreate_default_contextload_verify_locationsr)  r*  r   r+  r,  load_cert_chain)r9   r6   r  r-  r.  rU  r#  r%  r(   r(   r)   r   	  s:   






z Http2Transport._get_pool_optionsc                 C   s(  | j d u r	tdd }| | j }| jd }| j jdkr+|dkr+|p*| o*t d}| jd }|sA|dkrA|p@| o@t d}|  }|r| jd }|rR||d< |drzd	|v rh|	d	}|rht
d
 tjdd|i|W S  ty   t
d| Y nw tjdd|i|S tjdi |S )Nr/  r0  r1  r2  r3  r4  r5  r6  r  zqYou 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.r(   )r7   ri   r  r6   r8  r   r   r   r9  popr   r   httpcore
SOCKSProxyRuntimeError	HTTPProxyConnectionPool)r9   r<  r   r0  r3  r=  r5  r  r(   r(   r)   r   .  s@   





zHttp2Transport._make_poolN)rV   rW   rX   rY   r  r   r   r\  r]  r_  r`  rA  r   r   r   r   r(   r(   r(   r)   rB    s   
 %c                   @   s2   e Zd ZdZdd Zdd Zdeddfd	d
ZdS )_FunctionTransportz
    DEPRECATED: Users wishing to provide a custom transport should subclass
    the Transport class, rather than providing a function.
    c                 C   s   t |  || _d S rT   )r4   r:   _func)r9   funcr(   r(   r)   r:   `  s   

z_FunctionTransport.__init__c                 C   s   |  | d S rT   )rb  )r9   rB   r(   r(   r)   rD   g  s   
z _FunctionTransport.capture_eventrC   returnNc                 C   s"   |  }|d ur| | d S d S rT   )	get_eventrD   )r9   rC   rB   r(   r(   r)   rA   n  s   z#_FunctionTransport.capture_envelope)rV   rW   rX   rY   r:   rD   r   rA   r(   r(   r(   r)   ra  Z  s
    ra  c                 C   s   | d }|  di  dd}|rtnt}t|tr|S t|tr(t|tr(|}nt|r8tj	dt
dd t|S | d r@|| S d S )	N	transportrx   transport_http2FzFunction transports are deprecated and will be removed in a future release.Please provide a Transport instance or subclass, instead.r3   r;   r5   )r   rB  r  
isinstancer4   r   
issubclasscallabler=   r>   r?   ra  )r6   ref_transportuse_http2_transporttransport_clsr(   r(   r)   make_transportw  s$   

rn  rT   )Fabcr   r   r   r)  r|   r'   rV  r   r=   r   r   r   collectionsr   urllib.requestr   r   r;  r   r+  r   r   r	   sentry_sdk.utilsr
   r   r   sentry_sdk.workerr   sentry_sdk.enveloper   r   r   typingr   r   r   r   r   r   r   r   r   r   r   r   r   r   urllib3.poolmanagerr   r    sentry_sdk._typesr!   r"   r(  
SOL_SOCKETSOL_TCPoptionr   AttributeErrorr4   rr   rs   r  r\  h2rB  ra  rn  r(   r(   r(   r)   <module>   s    "
e   &m~