o
    hj                     @   sf  d dl m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	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 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#m$Z$m%Z% d d
lm&Z& d dl'm(Z( d dl)m*Z* d dl+m,Z,m-Z- d dl.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5 e5rd dl6m7Z7 d dl6m8Z8 d dl6m9Z9 d dl6m:Z: d dl6m;Z; d dl6m<Z< d dl6m=Z= d dl#m>Z> d dl?m@Z@ d dl4mAZAmBZB d dlCmDZD e&dZEde!de!d gd!ZFd"d# ZGzeHZIW n eJy   eKZIY nw G d$d% d%eLZMd dl4m5Z5 e5r'G d&d' d'e"e9eNe7f ZOG d(d) d)e"eMZPdS d*d+  ZOd,d+  ZPdS )-    )import_moduleN)datetime_utcnowstring_types	text_type	iteritems)
capture_internal_exceptionscurrent_stacktracedisable_capture_eventformat_timestampget_sdk_nameget_type_nameget_default_releasehandle_in_applogger	is_gevent)	serialize)tracehas_tracing_enabled)make_transport)DEFAULT_MAX_VALUE_LENGTHDEFAULT_OPTIONSINSTRUMENTERVERSIONClientConstructor)_DEFAULT_INTEGRATIONSsetup_integrations)
ContextVar)SessionFlusher)Envelope)has_profiling_enabledsetup_profiler)EventScrubber)Monitor)setup_spotlight)TYPE_CHECKING)Any)Callable)Dict)Optional)Sequence)Type)Union)Integration)Scope)EventHint)Sessionclient_init_debugzsentry.pythonzpypi:sentry-sdk)nameversion)r2   r3   packagesc            	      O   s  | rt | d t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rC|dd u rC||d< t	|D ]3\}}||vrv|dkr_d}t
| ||d< qG|dkrod	}t
| ||d
< qGtd|f |||< qG|d d u rtjd|d< |d d u rt |d< |d d u rtjdpd|d< |d d u rtjdd dv |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 rt |d< |S )Nr      z+Only single positional argument is expecteddsnwith_localszDeprecated: The option 'with_locals' was renamed to 'include_local_variables'. Please use 'include_local_variables'. The option 'with_locals' will be removed in the future.include_local_variablesrequest_bodieszDeprecated: The option 'request_bodies' was renamed to 'max_request_body_size'. Please use 'max_request_body_size'. The option 'request_bodies' will be removed in the future.max_request_body_sizezUnknown option %r
SENTRY_DSNreleaseenvironmentSENTRY_ENVIRONMENT
productiondebugSENTRY_DEBUGFalse)true1tserver_namegethostnameinstrumenterproject_rootenable_tracingTtraces_sample_rate      ?event_scrubber)
isinstancer   bytesstrlen	TypeErrordictr   getr   r   warningosenvironr   lowerhasattrsocketrG   r   SENTRYgetcwd	Exceptionr!   )	argskwargsr6   rvoptionskeyvaluemsgrI    re   T/var/www/html/aiguide_backend/venv/lib/python3.10/site-packages/sentry_sdk/client.py_get_optionsB   sd   $





rg   c                   @   s   e Zd Z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	d'ddZdd Zdd Z		d(ddZdd Zdd Z		d(dd Z		d(d!d"Zd#d$ Zd%d& ZdS ))_Clienta9  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:`Client`. (Was created for better intelisense support)
    c                 O   s   t |i || _|   d S N)get_optionsra   
_init_impl)selfr^   r_   re   re   rf   __init__   s   z_Client.__init__c                 C   s
   d| j iS Nra   ra   rl   re   re   rf   __getstate__   s   
z_Client.__getstate__c                 C   s   |d | _ |   d S rn   )ra   rk   )rl   statere   re   rf   __setstate__   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.
        Nqualified_name.r5   zEnabled tracing for %szWCan not enable tracing for '%s'. (%s) Please check your `functions_to_trace` parameter.)rsplitr   getattrsetattrr   r   r@   module_not_found_errortype__dict__staticmethodclassmethodr]   rU   )rl   functions_to_tracefunction
class_namefunction_qualnamemodule_namefunction_name
module_objfunction_obj	class_objfunction_typetraced_functionere   re   rf   _setup_instrumentation   sP   


z_Client._setup_instrumentationc              
      s  t d} fdd}zt  jd  t j _d  _ jr+ jd r+t j _t|d _	d  _
 jdi }|dd	r]t rJtd
 nddlm} ||t|dd	d _
d} jd |vrmtd| jd ddrtd tj jd< td t jd  jd  jd d _d  _ jdrt j _tt j }|td< td| t  jrzt! j W n t"y } ztd| W Y d }~nd }~ww W t | nt | w  # jdg  d S )NFc                    s    j d ur j |  d S d S ri   )	transportcapture_envelope)enveloperp   re   rf   _capture_envelope   s   
z-_Client._init_impl.<locals>._capture_enveloper@   enable_backpressure_handling)capture_func_experimentsenable_metricsTz,Metrics currently not supported with gevent.r   )MetricsAggregatormetric_code_locations)r   enable_code_locations)alwaysneversmallmediumr:   z:Invalid value for max_request_body_size. Must be one of {}otel_powered_performancezA[OTel] Enabling experimental OTel-powered performance monitoring.rH   zJsentry_sdk.integrations.opentelemetry.integration.OpenTelemetryIntegrationintegrationsdefault_integrationsauto_enabling_integrations)with_defaultswith_auto_enabling_integrations	spotlightr2   zSetting SDK name to '%s'zCan not set up profiler. (%s)r~   )$_client_init_debugrT   setra   r   r   monitorr"   r   session_flushermetrics_aggregatorr   r   rU   sentry_sdk.metricsr   bool
ValueErrorformatr@   r   OTELr   appendr   r   r   r#   r   listkeysSDK_INFOr   r    r]   r   )rl   	old_debugr   experimentsr   r:   sdk_namer   re   rp   rf   rk      sx   



z_Client._init_implc                 C   s
   | j d S )z%Returns the configured DSN as string.r6   ro   rp   re   re   rf   r6   7  s   
z_Client.dsnc                 C   s  | dd u rt |d< |d ur6| ddk}|||| j}|d u r4| jr2| jjd|r.dndd d S |}| jd rsd|vrsd	|vrsd
|vrst ! dt| j dd| j dtddddgi|d
< W d    n1 snw   Y  dD ]}| |d u r| j| d urt	| j| 
 ||< qu| dd u rtt}t| j |d< ||d< | dd u rd|d< t|| jd | jd | jd }|d ur| jd }|r| jd s|| |d urt|| 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r7| jjddd |
}| jd  }|d ur|d ur| ddkrd }
t  |||p\i }
W d    n	1 siw   Y  |
d u rtd! | jr| jjddd |
}|S )"N	timestamprz   transactionevent_processorerrordata_categoryattach_stacktrace	exception
stacktracethreadsvaluesr8   Tmax_value_length)r8   r   F)r   crashedcurrent)r<   r=   rF   distsdkr   platformpythonin_app_excludein_app_includerI   rM   send_default_piir:   )r:   r   before_sendzbefore send dropped eventbefore_send_transactionz%before send transaction dropped event)rT   r   apply_to_eventra   r   record_lost_eventr   r   r   r   striprS   r   sortedr   r   r   scrub_eventr   r   info)rl   eventhintscopeis_transactionevent_rb   sdk_inforM   r   	new_eventr   re   re   rf   _prepare_event=  s   
















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)rT   r   
__module__ra   rN   r   
issubclass)rl   r   r   r   r   error_type_nameerror_full_nameignored_errorre   re   rf   _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 )Nrz   r   TF)rT   _should_capturer   )rl   r   r   r   r   ignoring_prevents_recursionignored_by_config_optionre   re   rf   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 )Nerror_samplersample_raterL   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)ra   rT   callabler   random	NameErrorr   rU   rR   reprr   r   )rl   r   r   r   r   not_in_sample_rate	parameterverbre   re   rf   _should_sample_error  s>   
z_Client._should_sample_errorc                 C   s   d}d}d }| dpi  d}|r+d}|D ]}| d}|r*| ddu r*d} nq| d}	|jd u rT| dp;i  d	}
t|
pCi D ]\}}| d
krS|} nqE|j|rZdnd |	||j|pb| d d S )NFr   r   T	mechanismhandleduserrequestheadersz
user-agentr   )statusr   
user_agenterrors)rT   r   r   rX   updater   )rl   sessionr   r   erroredr   
exceptionsr   r   r   r   kvre   re   rf   _update_session_from_event  s4   




z"_Client._update_session_from_eventc                 C   s  t drdS |du ri }|d}t|pd}|du r%t j |d< }| |||s.dS |dd}| |||}|du rAdS |rF|j	nd}|rP| 
|| |ddk}|ddk}	|sj|	sj| ||sjdS t| j}
|d	}|d
i dp~i }|di }|
p|p|	pt|pt| j}|r|d tt d}|r||d< t|d}|r|dur|||| j || n|	r|| n|| |pdD ]	}||  q| jr| j| | jdu rdS | j| |S | jdu rdS | j| |S )am  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.
            The `scope` and `scope_kwargs` parameters are mutually exclusive.

        :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.
        FNevent_idre   profilerz   r   check_inattachmentscontextsr   dynamic_sampling_context)r   sent_at)r   )r	   rT   rS   uuiduuid4hexr   popr   _sessionr   r   r   ra   r   r   r
   r   r   add_profileto_jsonadd_transactionadd_checkin	add_eventadd_itemto_envelope_itemr   r   capture_event)rl   r   r   r   r   r   	event_optr   r   
is_checkintracing_enabledr   trace_contextr  should_use_envelope_endpointr   r   
attachmentre   re   rf   r  3  s   







	
z_Client.capture_eventc                 C   s$   |j s
td d S | j| d S )Nz3Discarded session update because of missing release)r<   r   r   r   add_session)rl   r   re   re   rf   capture_session  s   z_Client.capture_sessionc                 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)rN   rP   
identifierr   r   rT   )rl   name_or_classintegration_namere   re   rf   get_integration  s   

z_Client.get_integrationc                 C   s^   | j dur-| j||d | 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   flushr   killr   r   rl   r  r  re   re   rf   close  s   







z_Client.closec                 C   sR   | j dur'|du r| jd }| 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.
        Nshutdown_timeoutr  )r   ra   r   r   r   r"  re   re   rf   r     s   




z_Client.flushc                 C   s   | S ri   re   rp   re   re   rf   	__enter__  s   z_Client.__enter__c                 C   s   |    d S ri   )r#  )rl   exc_type	exc_valuetbre   re   rf   __exit__  s   z_Client.__exit__ri   )NN)__name__r   __qualname____doc__rm   rq   rs   r   rk   propertyr6   r   r   r   r   r   r  r  r  r#  r   r%  r)  re   re   re   rf   rh      s8    4P
v
0'
l	

rh   c                   @      e Zd ZdS )rj   Nr*  r   r+  re   re   re   rf   rj         rj   c                   @   r.  )ClientNr/  re   re   re   rf   r1    r0  r1  c                   C      t S ri   )rg   re   re   re   rf   <lambda>      r3  c                   C   r2  ri   )rh   re   re   re   rf   r3     r4  )Q	importlibr   rV   r  r   rZ   sentry_sdk._compatr   r   r   r   sentry_sdk.utilsr   r   r	   r
   r   r   r   r   r   r   sentry_sdk.serializerr   sentry_sdk.tracingr   r   sentry_sdk.transportr   sentry_sdk.constsr   r   r   r   r   sentry_sdk.integrationsr   r   r   sentry_sdk.sessionsr   sentry_sdk.enveloper   sentry_sdk.profilerr   r    sentry_sdk.scrubberr!   sentry_sdk.monitorr"   sentry_sdk.spotlightr#   sentry_sdk._typesr$   typingr%   r&   r'   r(   r)   r*   r+   r,   sentry_sdk.scoper-   r.   r/   sentry_sdk.sessionr0   r   r   rg   ModuleNotFoundErrorry   r]   ImportErrorobjectrh   rP   rj   r1  re   re   re   rf   <module>   sl    0
Q    S
	