o
    0 i|                  	   @   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(   `/home/app/PaddleOCR-VL-test/.venv_paddleocr/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   Z	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   rB   r(   r(   r)   r@   `   s   
zTransport.capture_envelopec                 C   rD   )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   rD   )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   rD   )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itemrL   r(   r(   r)   record_lost_event   s   zTransport.record_lost_eventc                 C   rD   )NTr(   rI   r(   r(   r)   
is_healthy      zTransport.is_healthyNNN)__name__
__module____qualname____doc__r7   r:   rC   r   r@   rH   rJ   rP   rQ   r(   r(   r(   r)   r4   =   s     


r4   c              
   c   s    |d u rt tj}| dD ]8}z+| d}|d d \}}|tt|d }|r2|dp3dD ]}||fV  q4W q tt	fyH   Y qw d S )N,:r3   seconds;rS   )
r   nowr   utcsplitstripr   intLookupError
ValueError)headerr^   limit
parametersZretry_after_val
categoriesretry_aftercategoryr(   r(   r)   _parse_rate_limits   s   rk   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   )VERSIONZtransport_queue_size)Z
queue_sizezsentry.python/%s_experimentsZtransport_compression_levelZ transport_zlib_compression_levelZtransport_compression_algogzipbrzSYou asked for brotli compression without the Brotli module, falling back to gzip -9)rq   rp   z2Unknown compression algo %s, disabling compression	      )sentry_sdk.constsrn   r4   r:   r7   r6   r   _workerZto_auth_auth_disabled_untilurllib3utilRetry_retryr   rb   _discarded_eventstime_last_client_report_sent
_make_pool_pool
sentry_sdkZHub_hub_clsgetbrotlir   warning_compression_level_compression_algo)r9   r6   rn   ZexperimentsZcompression_levelZcompression_algor(   r(   r)   r:      sV   







zBaseHttpTransport.__init__Nr,   rK   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spanrK   
attachmentzdata category not provided)r6   rN   Zget_transaction_eventlenr   r   r   strobjectr   rP   	get_bytes	TypeErrorr|   )r9   rM   rN   rO   rL   rA   Z
span_countr(   r(   r)   rP      s    
	&z#BaseHttpTransport.record_lost_eventc                 C   s   |j |S rS   )headersr   r9   responsere   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   rw   updaterk   statusr{   parse_retry_afterr   r^   r   r_   r   )r9   r   re   Zretry_after_valueri   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)rN   rO   )rP   items)rM   rO   rB   r9   r(   r)   record_loss=  s
   
z4BaseHttpTransport._send_request.<locals>.record_loss)z
User-AgentzX-Sentry-AuthPOSTnetworkZnetwork_errorr   Z
status_429i,     z%Unexpected status code: %s (body: %s)datacontentz	status_{})r   r   rv   clientZ	to_header_request	Exceptionon_dropped_eventr   r   r   r   r&   formatclose)r9   bodyr   endpoint_typerB   r   r   r(   r   r)   _send_request4  sH   	





	zBaseHttpTransport._send_requestc                 C   s   d S rS   r(   )r9   Z_reasonr(   r(   r)   r   m  rR   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 ))rM   rj   rL   r(   ).0rj   rM   rL   r(   r(   r)   
<listcomp>  s    
zBBaseHttpTransport._fetch_pending_client_report.<locals>.<listcomp>)	timestampdiscarded_events)jsonclient_report)type)	r6   r~   r}   r|   r   rb   r   r   r   )r9   forceintervalr   r(   r(   r)   _fetch_pending_client_reportq  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   r@   r   )r9   r   r   r(   r(   r)   _flush_client_reports  s   z'BaseHttpTransport._flush_client_reportsc                    s    fdd}||p|d S )Nc                    s$    j | }|d uo|ttjkS rS   )rw   r   r   r^   r   r_   )ZbuckettsrI   r(   r)   	_disabled  s   z4BaseHttpTransport._check_disabled.<locals>._disabledr(   )r9   rj   r   r(   rI   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 rS   )r   r^   r   r_   )r   r   r(   r(   r)   	<genexpr>  s    
z5BaseHttpTransport._is_rate_limited.<locals>.<genexpr>)anyrw   valuesrI   r(   r(   r)   _is_rate_limited  s   z"BaseHttpTransport._is_rate_limitedc                 C   s
   | j  S rS   )ru   fullrI   r(   r(   r)   _is_worker_full  s   
z!BaseHttpTransport._is_worker_fullc                 C   s   |   p|   S rS   )r   r   rI   r(   r(   r)   rQ     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   defaultZstatsdZself_rate_limitsZratelimit_backoffr   )r   r   rm   )r   z(Sending envelope [%s] project:%s host:%szContent-Typezapplication/x-sentry-envelopezContent-Encoding)r   r   rB   )r   r   rN   r   rP   appendr   r   r   _serialize_enveloper7   r   debugdescriptionZ
project_idhostr   getvaluer	   ENVELOPE)r9   rB   Z	new_itemsrO   Z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   rq   )Zqualityw)fileobjmodecompresslevel)ioBytesIOr   r   Zserialize_intor   writecompress	serializerp   GzipFile)r9   rB   r   r   fr(   r(   r)   r     s,   


z%BaseHttpTransport._serialize_envelopec                 C      t  rS   NotImplementedErrorrI   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noFrY   T)r   r   r`   ra   r   endswithnetloc)r9   r7   no_proxyr   r(   r(   r)   _in_no_proxy  s   zBaseHttpTransport._in_no_proxyc                 C   r   rS   r   rI   r(   r(   r)   r     r   zBaseHttpTransport._make_poolc                 C   r   rS   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 rS   )r   r   r   r(   r   r(   r)   send_envelope_wrapper  s   

"zABaseHttpTransport.capture_envelope.<locals>.send_envelope_wrapperZ
full_queueZqueue_overflowr   )ru   submitr   r   rP   )r9   rB   r   rO   r(   r   r)   r@     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(   rI   r(   r)   r*   .  s    z)BaseHttpTransport.flush.<locals>.<lambda>)r   r   ru   r   rH   rE   r(   rI   r)   rH   %  s
   
zBaseHttpTransport.flushc                 C   s   t d | j  d S )NzKilling HTTP transport)r   r   ru   rJ   rI   r(   r(   r)   rJ   1  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_cls6  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   rI   r(   r(   r)   hub_cls@  s   zBaseHttpTransport.hub_clsc                 C   s   t   || _dS )r   Nr   )r9   valuer(   r(   r)   r   G  s   
rT   )Fr   )FrS   )!rU   rV   rW   rX   TIMEOUTr:   rP   r   r   r	   r   r   r   r   r   r   r   r   rQ   r   r   r   r   r   r   r@   rH   rJ   staticmethodr   propertyr   setterr(   r(   r(   r)   rl      sJ    >$
9

	5


	
rl   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 )Nro   Ztransport_num_poolsr3   CERT_REQUIRED)total)	num_pools	cert_reqsrF   socket_options
keep_alivec                 S      h | ]
}|d  |d fqS r   r,   r(   r   or(   r(   r)   	<setcomp>f      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   rb   rx   Timeoutr   KEEP_ALIVE_SOCKET_OPTIONSr   osenvironcertifiwhere)r9   r   r6   r   used_optionsdefault_optionr(   r(   r)   r   S  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   rd   r   r6   schemer   r   r   
startswithZurllib3.contrib.socksr  ImportErrorr   r   rx   r   r    )	r9   proxyr   r  r  optsr  Zuse_socks_proxyr  r(   r(   r)   r   ~  s>   




zHttpTransport._make_poolc                 C   s   | j j|| j|||dS )N)r   r   )r   requestrv   get_api_urlr   r(   r(   r)   r     s   
zHttpTransport._requestN)rU   rV   rW   r   r   r   r    __annotations__r   r   r   r(   r(   r(   r)   r   O  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__)rU   rV   rW   r:   __classcell__r(   r(   r  r)   r    s    r  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 )r  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re   r(   r)   r     s    z3Http2Transport._get_header_value.<locals>.<genexpr>)nextr   r   r(   r(  r)   r     s   
z Http2Transport._get_header_valuec                 C   s8   | j j|| j|||d| j| j| j| jdid}|S )NrF   )poolconnectr   read)r   r   
extensions)r   r  rv   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 )Nr  r   )Z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   r  r6   r  r   sslcreate_default_contextload_verify_locationsr  r  r   r  r	  load_cert_chain)r9   r6   r   r
  r  r/  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  r  r  r  r  r  r  r  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   rd   r   r6   r  r   r   r   r  popr   r   httpcore
SOCKSProxyRuntimeError	HTTPProxyConnectionPool)r9   r  r   r  r  r  r  r   r(   r(   r)   r     s@   





zHttp2Transport._make_poolN)rU   rV   rW   rX   r   r   r   r6  r7  r9  r:  r  r   r   r   r   r(   r(   r(   r)   r    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 rS   )r4   r:   _func)r9   funcr(   r(   r)   r:   O  s   

z_FunctionTransport.__init__c                 C   s   |  | d S rS   )r<  )r9   rA   r(   r(   r)   rC   W  s   
z _FunctionTransport.capture_eventrB   returnNc                 C   s"   |  }|d ur| | d S d S rS   )Z	get_eventrC   )r9   rB   rA   r(   r(   r)   r@   _  s   z#_FunctionTransport.capture_envelope)rU   rV   rW   rX   r:   rC   r   r@   r(   r(   r(   r)   r;  I  s
    r;  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	transportro   Z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   r  r   
isinstancer4   r   
issubclasscallabler=   r>   r?   r;  )r6   Zref_transportZuse_http2_transportZtransport_clsr(   r(   r)   make_transporth  s"   
rC  rS   )Fabcr   r   r   r  rp   r'   r0  r}   r=   r   r   r   collectionsr   urllib.requestr   r   r  rx   r  r   rt   r	   Zsentry_sdk.utilsr
   r   r   Zsentry_sdk.workerr   Zsentry_sdk.enveloper   r   r   typingr   r   r   r   r   r   r   r   r   r   r   r   r   r   Zurllib3.poolmanagerr   r    Zsentry_sdk._typesr!   r"   r  
SOL_SOCKETSOL_TCPoptionr   AttributeErrorr4   rk   rl   r   r6  Zh2r  r;  rC  r(   r(   r(   r)   <module>   s    "
e   m~