o
    0 is                     @   s4  d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZ d dlm	Z	 d dl
mZmZmZmZmZ d dlZd dlZd dlmZmZ d dlmZ d dlmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z' d d	l(m)Z) d d
l*m+Z+ d dl,m-Z-m.Z. d dl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 d dl6m7Z7m8Z8 d dl9m:Z: d dl;m<Z< d dl=m>Z> d dl?m@Z@ d dlAmBZBmCZCmDZD d dlEmFZF d dlGmHZH er3d dl
mIZI d dl
mJZJ d dl
mKZK d dl
mLZL d dl
mMZM d dl
mNZN d dl
mOZO d dlPmQZQmRZRmSZSmTZTmUZUmVZV d dl6mWZW d dlXmYZY d dlZm[Z[ d d l\m]Z] d d!l,m^Z^ d d"l_m`Z` d dlmZ eOd#eWd$Zaed%Zbd&e4d'e4d(gd)Zcd*d+ ZdzeeZfW n egyU   ehZfY nw G d,d- d-ZiG d.d/ d/eiZjG d0d1 d1eiZkd d2l
mZ erG d3d4 d4e5eeleIf ZmG d5d6 d6e5ekZndS d7d8  Zmd9d8  ZndS ):    N)Mapping)datetimetimezone)import_module)TYPE_CHECKINGListDictcastoverload)PY37check_uwsgi_thread_support)MetricsBatcher)AnnotatedValue
ContextVarcapture_internal_exceptionscurrent_stacktraceenv_to_boolformat_timestampget_sdk_nameget_type_nameget_default_releasehandle_in_app	is_geventloggerget_before_send_logget_before_send_metrichas_logs_enabledhas_metrics_enabled)	serialize)trace)BaseHttpTransportmake_transport)SPANDATADEFAULT_MAX_VALUE_LENGTHDEFAULT_OPTIONSINSTRUMENTERVERSIONClientConstructor)_DEFAULT_INTEGRATIONSsetup_integrations)DedupeIntegration)SessionFlusher)Envelope)setup_continuous_profiler)has_profiling_enabledProfilesetup_profiler)EventScrubber)Monitor)Any)Callable)Optional)Sequence)Type)Union)TypeVar)EventHintSDKInfoLogMetricEventDataCategory)Integration)Scope)Session)SpotlightClient)	Transport
LogBatcherI)boundZclient_init_debugzsentry.pythonzpypi:sentry-sdk)nameversion)rI   rJ   packagesc                  O   sf  | rt | d ttfs| d d u r| d }| dd  } nd }t| dkr(tdtt}t| i |}|d urB|dd u rB||d< | D ]\}}||vrUtd|f |||< qF|d d u rht	j
d|d< |d d u rst |d< |d d u rt	j
d	pd
|d< |d d u rtt	j
dddpd|d< |d d u rttdrt |d< |d d u rtj|d< |d d u rzt	 }W n ty   d }Y nw ||d< |d du r|d d u rd|d< |d d u rt|d d u rdn|d d|d< |d rt |d tstd d |d< |d d u r"tt	j
dddpd|d< |d d ur1tjdtdd  |S )!Nr      z+Only single positional argument is expecteddsnzUnknown option %rZ
SENTRY_DSNreleaseenvironmentZSENTRY_ENVIRONMENT
productiondebugZSENTRY_DEBUGTstrictFserver_namegethostnameinstrumenterproject_rootZenable_tracingZtraces_sample_rate      ?event_scrubbersend_default_pii)rZ   socket_optionszxIgnoring socket_options because of unexpected format. See urllib3.HTTPConnection.socket_options for the expected format.
keep_aliveZSENTRY_KEEP_ALIVEzVThe `enable_tracing` parameter is deprecated. Please use `traces_sample_rate` instead.   )
stacklevel)
isinstancebytesstrlen	TypeErrordictr$   getitemsosenvironr   r   hasattrsocketrU   r%   ZSENTRYgetcwd	Exceptionr1   listr   warningwarningswarnDeprecationWarning)argskwargsrM   rvoptionskeyvaluerW    rx   ]/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/sentry_sdk/client.py_get_optionsV   sl   "



rz   c                   @   s   e Zd ZdZdZd#ddZdd Zdd Zed	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd ZerAedd Zedd Zdd Zdd Zdd Zdd  Zd!d" ZdS )$
BaseClientzp
    .. versionadded:: 2.0.0

    The basic definition of a client that is used for sending data to Sentry.
    Nc                 C   s.   |d ur|nt | _d | _d | _d | _d | _d S N)r$   ru   	transportmonitorlog_batchermetrics_batcher)selfru   rx   rx   ry   __init__   s
   
zBaseClient.__init__c                 O   s   di iS Nru   rx   r   rr   rs   rx   rx   ry   __getstate__   s   zBaseClient.__getstate__c                 O      d S r|   rx   r   rx   rx   ry   __setstate__      zBaseClient.__setstate__c                 C   r   r|   rx   r   rx   rx   ry   rM         zBaseClient.dsnc                 C      dS )NFrx   r   rx   rx   ry   should_send_default_pii   r   z"BaseClient.should_send_default_piic                 C   r   )u
        .. versionadded:: 2.0.0

        Returns whether the client is active (able to send data to Sentry)
        Frx   r   rx   rx   ry   	is_active      zBaseClient.is_activec                 O   r   r|   rx   r   rx   rx   ry   capture_event   r   zBaseClient.capture_eventc                 C   r   r|   rx   )r   logrx   rx   ry   _capture_log   r   zBaseClient._capture_logc                 C   r   r|   rx   )r   metricrx   rx   ry   _capture_metric   r   zBaseClient._capture_metricc                 O   r   r|   rx   r   rx   rx   ry   capture_session   r   zBaseClient.capture_sessionc                 C   r   r|   rx   r   name_or_classrx   rx   ry   get_integration   r   zBaseClient.get_integrationc                 C   r   r|   rx   r   rx   rx   ry   r      r   c                 C   r   r|   rx   r   rx   rx   ry   r      r   c                 O   r   r|   rx   r   rx   rx   ry   close   r   zBaseClient.closec                 O   r   r|   rx   r   rx   rx   ry   flush   r   zBaseClient.flushc                 C      | S r|   rx   r   rx   rx   ry   	__enter__  r   zBaseClient.__enter__c                 C   r   r|   rx   r   exc_type	exc_valuetbrx   rx   ry   __exit__  r   zBaseClient.__exit__r|   )__name__
__module____qualname____doc__	spotlightr   r   r   propertyrM   r   r   r   r   r   r   r   r
   r   r   r   r   r   rx   rx   rx   ry   r{      s0    
	
	

r{   c                   @   s   e Zd ZdZdS )NonRecordingClientz
    .. versionadded:: 2.0.0

    A client that does not send any events to Sentry. This is used as a fallback when the Sentry SDK is not yet initialized.
    N)r   r   r   r   rx   rx   rx   ry   r   
  s    r   c                       s   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
edd Zdd Zdd Z	d1ddZdd Zdd Z		d2ddZdd  Zd!d" Zd#d$ Zeraed%d& Zed'd& Zd(d& Z		d2d)d*Z		d2d+d,Zd-d. Zd/d0 Z  ZS )3_ClientaI  
    The client is internally responsible for capturing the events and
    forwarding them to sentry through the configured transport.  It takes
    the client options as keyword arguments and optionally the DSN as first
    argument.

    Alias of :py:class:`sentry_sdk.Client`. (Was created for better intelisense support)
    c                    s(   t t| jt|i |d |   d S )Nru   )superr   r   get_options
_init_implr   	__class__rx   ry   r     s   z_Client.__init__c                 C   s
   d| j iS r   r   r   rx   rx   ry   r   #  s   
z_Client.__getstate__c                 C   s   |d | _ |   d S r   )ru   r   )r   staterx   rx   ry   r   '  s   
z_Client.__setstate__c                 C   sP  |D ]}d}|d }| dd\}}zt|}t||}t||t| td| W q ty   z?| dd\}}t|}t||}	t|	|}t|	j	| }
t|}|
t
tfv r^t
|}t|	|| t|||	 td| W n ty } ztd|| W Y d}~nd}~ww Y q ty } ztd|| W Y d}~qd}~ww dS )z
        Instruments the functions given in the list `functions_to_trace` with the `@sentry_sdk.tracing.trace` decorator.
        NZqualified_name.rL   zEnabled tracing for %szWCan not enable tracing for '%s'. (%s) Please check your `functions_to_trace` parameter.)rsplitr   getattrsetattrr   r   rQ   module_not_found_errortype__dict__staticmethodclassmethodrl   rn   )r   functions_to_tracefunction
class_nameZfunction_qualnamemodule_nameZfunction_nameZ
module_objZfunction_objZ	class_objZfunction_typeZtraced_functionerx   rx   ry   _setup_instrumentation,  sP   


z_Client._setup_instrumentationc              
      s  t d} fdd}	d, fdd	}zFt  jd  t j _d  _ jr4 jd r4t j _t|d	 _	d  _
t jrOd
dlm} |||d _
d  _t jr^t||d _d} jd |vrntd| jd ddrtd tj jd< dtvrtd t jd  jd  jd  jd  jd _ jd}|d u rdtjv rtjd }t|dd}|d ur|n| jd<  jdrd
dlm} | j _  jd sd d! }	d jd"< |	 jd#< |	 jd$< |	 jd%< t!t" j# }
|
t$d&< td'|
 t% jr0zt& j W n= t'y/ } ztd(| W Y d }~n)d }~ww z
t( jt$|d) W n t'yS } ztd*| W Y d }~nd }~ww W t | nt | w  ) jd+g   js j
st% jst* jt+rt,  d S d S )-NFc                    s    j d ur j |  d S d S r|   )r}   capture_envelope)enveloper   rx   ry   _capture_envelopec  s   
z-_Client._init_impl.<locals>._capture_enveloperL   c                    s$    j d ur j j| ||d d S d S )Nreasondata_categoryquantity)r}   record_lost_eventr   r   rx   ry   _record_lost_eventh  s   

z._Client._init_impl.<locals>._record_lost_eventrQ   Zenable_backpressure_handling)capture_funcr   rE   )r   Zrecord_lost_func)alwaysneverZsmallZmediummax_request_body_sizez:Invalid value for max_request_body_size. Must be one of {}Z_experimentsZotel_powered_performancezA[OTel] Enabling experimental OTel-powered performance monitoring.rV   zJsentry_sdk.integrations.opentelemetry.integration.OpenTelemetryIntegrationintegrationsZdefault_integrationsZauto_enabling_integrationsdisabled_integrations)Zwith_defaultsZwith_auto_enabling_integrationsr   ru   r   ZSENTRY_SPOTLIGHTTrR   )setup_spotlightrM   c                  _   r   )NrX   rx   )_args_kwargsrx   rx   ry   <lambda>      z$_Client._init_impl.<locals>.<lambda>rZ   error_samplerZtraces_samplerZprofiles_samplerrI   zSetting SDK name to '%s'zCan not set up profiler. (%s))sdk_infor   z(Can not set up continuous profiler. (%s)r   )rL   )-_client_init_debugre   setru   r!   r}   r~   r2   r+   session_flusherr   r   sentry_sdk._log_batcherrF   r   r   r   
ValueErrorformatr   rQ   r%   ZOTELr(   appendr)   r   rg   rh   r   sentry_sdk.spotlightr   r   r   rm   keysSDK_INFOr.   r0   rl   r-   r   r_   r    r   )r   Z	old_debugr   r   rF   r   Zspotlight_configZspotlight_env_valuer   Z
sample_allZsdk_namer   rx   r   ry   r   _  s   













z_Client._init_implc                 C   r   )r   Trx   r   rx   rx   ry   r     r   z_Client.is_activec                 C   s   | j dpdS )z
        .. versionadded:: 2.0.0

        Returns whether the client should send default PII (Personally Identifiable Information) data to Sentry.
        rZ   F)ru   re   r   rx   rx   ry   r     s   z_Client.should_send_default_piic                 C   s
   | j d S )z%Returns the configured DSN as string.rM   r   r   rx   rx   ry   rM     s   
z_Client.dsnc              	   C   s  d }d }| dd u rttj|d< | ddk}|d urttttt	t
f  | dg }|||| j}|d u rY| jrW| jjd|rFdndd |rW| jjdd|d	 d
 d S |}|ttttt	t
f  | dg  }	|r|	dkr| jd ur| jjdd|	d
 |dd|	 }
|
dkr||
 }|jdkr| di }t|ts| dg ng }t||j }|s| jd rd|vrd|vrd|vrt ! dt| j dd| j dtddddgi|d< W d    n1 sw   Y  dD ]}| |d u r| j| d urt	| j|  ||< q| dd u r)tt}t| j |d< ||d< | dd u r5d|d< t|| jd | jd | jd  }|d urW| jd! }|rW|| |d ur|jr| dg }t|tr|D ].}| d"d }| d#i }|r||jv rt j!|v rt|t j! d$|j| i|t j!< qn|d urt| dg d$|i|d< |d urt| ddg id$|i|d< |d urtd%t"td&|| j d'| j d| j d(d)}| jd* }|d ur9|d ur9| ddkr9d }t  |||pi }W d    n	1 sw   Y  |d u r7t#$d+ | jr-| jjd*dd | dr7t%&  |}| jd, }|d ur|d ur| ddkrd }ttttt	t
f  | dg }t  |||pmi }W d    n	1 szw   Y  |d u rt#$d- | jr| jjd*dd. | jjd*d|d	 d/ n|t| dg  }	|	dkr| jd ur| jjd*d|	d/ |}|S )0N	timestampr   transactionspansZevent_processorerrorr   spanrL   )r   r   r   Z_dropped_spansbreadcrumbsvaluesZattach_stacktrace	exception
stacktracethreadsinclude_local_variablesTmax_value_length)r   r   F)r   crashedcurrent)rN   rO   rT   distZsdkr   platformpythonZin_app_excludeZin_app_includerW   rY   span_iddatarb   r:   zDict[str, Any]r   custom_repr)r   r   r   before_sendzbefore send dropped eventbefore_send_transactionz%before send transaction dropped event)r   r   r   )'re   r   nowr   utcrb   r	   r   r   ra   objectZapply_to_eventru   r}   r   popZ_n_breadcrumbs_truncatedr_   r   r   r   r#   striprd   r   sortedr   r   r   Zscrub_eventZ_gen_ai_original_message_countrm   r"   ZGEN_AI_REQUEST_MESSAGESr   r   infor*   Zreset_last_seen)r   eventhintscopeZprevious_total_spansZprevious_total_breadcrumbsis_transactionZspans_beforeZevent_Zspans_deltaZdropped_spansr   r   rv   r   rY   r   r   r   Z	span_datar   Z	new_eventr   rx   rx   ry   _prepare_event  sD  "


 


















"

z_Client._prepare_eventc                 C   s   | d}|d u rdS |d }t|d }d|d j|f }| jd D ]}t|tr6||ks2||kr5 dS q#t||r> dS q#dS )Nexc_infoFr   z%s.%signore_errorsT)re   r   r   ru   r_   ra   
issubclass)r   r   r   r  r   Zerror_type_nameZerror_full_nameZignored_errorrx   rx   ry   _is_ignored_error  s   


z_Client._is_ignored_errorNc                 C   sF   | ddk}|rdS |d uo|j }|rdS | ||}|r!dS dS )Nr   r   TF)re   _should_capturer  )r   r   r   r   r  Zignoring_prevents_recursionZignored_by_config_optionrx   rx   ry   r    s   z_Client._should_capturec              
   C   s   | j dd }t|r$t  |||}W d    n1 sw   Y  n| j d }z|dk o3t |k}W n0 tyE   td d}Y n! tye   t|rQdnd\}}td||t	|f  d}Y nw |ru| j
rs| j
jdd	d
 dS dS )Nr   sample_raterX   zMThe provided error_sampler raised an error. Defaulting to sampling the event.F)r   returned)r  containszsThe provided %s %s an invalid value of %s. The value should be a float or a bool. Defaulting to sampling the event.r   r   T)ru   re   callabler   random	NameErrorr   rn   rc   reprr}   r   )r   r   r   r   r  Znot_in_sample_rateZ	parameterverbrx   rx   ry   _should_sample_error  s>   
z_Client._should_sample_errorc                 C   s   d}d}d }| dpi  d}|r8d}|D ]!}t|tr"|jp!i }| d}t|tr7| ddu r7d} nq| d}	|jd u rh| dpHi  d	}
t|
trS|
ni }| D ]\}}| d
krg|} nqY|j	|rndnd |	||j
|pv| d d S )NFr   r   T	mechanismZhandleduserrequestheadersz
user-agentr   )statusr  
user_agenterrors)re   r_   r   rw   r   r  rd   rf   lowerupdater  )r   sessionr   r   Zerroredr  
exceptionsr   r  r  r  Zheaders_dictkvrx   rx   ry   _update_session_from_event&  s:   






z"_Client._update_session_from_eventc                 C   s  t |pd}| |||sdS |dd}|d}|du r't j |d< }| |||}|du r4dS |r9|jnd}|rC| 	|| |ddk}|ddk}	|s]|	s]| 
||s]dS |d}
|d	i d
pli }|di }|d tttjd}|r||d
< t|d}|rt|tr|||| j || n|	r|| n|| |
pdD ]	}||  qd}| jr| j| |}| jdur| j| |}|S )a  Captures an event.

        :param event: A ready-made event that can be directly sent to Sentry.

        :param hint: Contains metadata about the event that can be read from `before_send`, such as the original exception object or a HTTP request object.

        :param scope: An optional :py:class:`sentry_sdk.Scope` to apply to events.

        :returns: An event ID. May be `None` if there is no DSN set or of if the SDK decided to discard the event for other reasons. In such situations setting `debug=True` on `init()` may help.
        rx   Nprofileevent_idr   r   Zcheck_inattachmentsZcontextsr   dynamic_sampling_context)r   Zsent_at)r  )rd   r  r   re   uuiduuid4hexr  _sessionr  r  r   r   r   r   r   r,   r_   r/   Zadd_profileto_jsonru   Zadd_transactionZadd_checkinZ	add_eventZadd_itemZto_envelope_itemr   r   r}   )r   r   r   r   r  r   Z	event_optr  r  Z
is_checkinr!  trace_contextr"  r  r   
attachmentreturn_valuerx   rx   ry   r   M  s`   






z_Client.capture_eventc                 C   s  t | jr	|d u rd S t }t }td |d d< td |d d< | jd}|d ur;tj|d vr;||d tj< | jd}|d urQd|d vrQ||d d< | jd	}|d urgd
|d vrg||d d
< |	 }|d}|d}	|d ur|dd u r||d< |	d urd|d vr|	|d d< |j
d urdD ]\}
}||j
v r|
|d vr|j
| |d |
< q| jdd}|rtd|d d|d  t| j}|d ur||i }|d u rd S | jr| j| d S d S )NrI   
attributessentry.sdk.namerJ   sentry.sdk.versionrT   rO   sentry.environmentrN   sentry.releasetrace_idr   zsentry.trace.parent_span_id)zuser.idid)z	user.nameusername)z
user.emailemailrQ   Fz[Sentry Logs] [Zseverity_text] body)r   ru   
sentry_sdkget_current_scopeget_isolation_scoper   re   r"   SERVER_ADDRESSget_trace_context_userr   rQ   r   r   add)r   r   current_scopeisolation_scoperT   rO   rN   r(  r0  r   Zlog_attributeuser_attributerQ   Zbefore_send_logrx   rx   ry   r     sX   





z_Client._capture_logc              
   C   s  t | jr	|d u rd S t }t }td |d d< td |d d< | jd}|d ur;tj|d vr;||d tj< | jd}|d urQd|d vrQ||d d< | jd	}|d urgd
|d vrg||d d
< |	 }|d}|d}	|pxd|d< |	d ur|	|d< |j
d urdD ]\}
}||j
v r|
|d vr|j
| |d |
< q| jdd}|rtd|d d|d d|d  t| j}|d ur||i }|d u rd S | jr| j| d S d S )NrI   r+  r,  rJ   r-  rT   rO   r.  rN   r/  r0  r   z$00000000-0000-0000-0000-000000000000r1  rQ   Fz[Sentry Metrics] [r   r5  z: rw   )r   ru   r7  r8  r9  r   re   r"   r:  r;  r<  r   rQ   r   r   r=  )r   r   r>  r?  rT   rO   rN   r(  r0  r   Zmetric_attributer@  rQ   Zbefore_send_metricrx   rx   ry   r     sV   



&

z_Client._capture_metricc                 C   s$   |j s
td d S | j| d S )Nz3Discarded session update because of missing release)rN   r   r   r   Zadd_session)r   r  rx   rx   ry   r   &  s   z_Client.capture_sessionc                 C   r   r|   rx   r   rx   rx   ry   r   2  r   z_Client.get_integrationc                 C   r   r|   rx   r   rx   rx   ry   r   7  r   c                 C   s6   t |tr|}n|jdur|j}ntd| j|S )zReturns the integration for this client by name or class.
        If the client does not have that integration then `None` is returned.
        NzIntegration has no name)r_   ra   
identifierr   r   re   )r   r   Zintegration_namerx   rx   ry   r   <  s   

c                 C   sr   | j dur7| j||d | j  | jdur| j  | jdur%| j  | jr-| j  | j   d| _ dS dS )z
        Close the client and shut down the transport. Arguments have the same
        semantics as :py:meth:`Client.flush`.
        Ntimeoutcallback)r}   r   r   killr   r   r~   r   rC  rD  rx   rx   ry   r   M  s   









z_Client.closec                 C   sf   | j dur1|du r| jd }| j  | jdur| j  | jdur'| j  | j j||d dS dS )a#  
        Wait for the current events to be sent.

        :param timeout: Wait for at most `timeout` seconds. If no `timeout` is provided, the `shutdown_timeout` option value is used.

        :param callback: Is invoked with the number of pending events and the configured timeout.
        NZshutdown_timeoutrB  )r}   ru   r   r   r   r   rF  rx   rx   ry   r   c  s   






z_Client.flushc                 C   r   r|   rx   r   rx   rx   ry   r   z  r   z_Client.__enter__c                 C   s   |    d S r|   )r   r   rx   rx   ry   r   ~  s   z_Client.__exit__r|   )NN)r   r   r   r   r   r   r   r   r   r   r   r   rM   r  r  r  r  r  r   r   r   r   r   r
   r   r   r   r   r   __classcell__rx   rx   r   ry   r     sN    	3 		
 J
0*
UD@




r   )r   c                   @      e Zd ZdS )r   Nr   r   r   rx   rx   rx   ry   r         r   c                   @   rH  )ClientNrI  rx   rx   rx   ry   rK    rJ  rK  c                   C      t S r|   )rz   rx   rx   rx   ry   r     r   r   c                   C   rL  r|   )r   rx   rx   rx   ry   r     r   )org   r#  r  rj   collections.abcr   r   r   	importlibr   typingr   r   r   r	   r
   ro   r7  Zsentry_sdk._compatr   r   Zsentry_sdk._metrics_batcherr   Zsentry_sdk.utilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zsentry_sdk.serializerr   Zsentry_sdk.tracingr   Zsentry_sdk.transportr    r!   Zsentry_sdk.constsr"   r#   r$   r%   r&   r'   Zsentry_sdk.integrationsr(   r)   Zsentry_sdk.integrations.deduper*   Zsentry_sdk.sessionsr+   Zsentry_sdk.enveloper,   Z'sentry_sdk.profiler.continuous_profilerr-   Z(sentry_sdk.profiler.transaction_profilerr.   r/   r0   Zsentry_sdk.scrubberr1   Zsentry_sdk.monitorr2   r3   r4   r5   r6   r7   r8   r9   Zsentry_sdk._typesr:   r;   r<   r=   r>   r?   r@   Zsentry_sdk.scoperA   Zsentry_sdk.sessionrB   r   rC   rD   r   rF   rG   r   r   rz   ModuleNotFoundErrorr   rl   ImportErrorr{   r   r   ra   r   rK  rx   rx   rx   ry   <module>   s    H  
O]
      u
	