o
    h0)                     @   s  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	 d dl
mZmZ d dlmZmZmZ z4d dlmZmZ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mZm Z  d dl!m"Z" W n e#yo   edw d dl$m%Z% e%rd dl$m&Z&m'Z'm(Z( d dl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z2m3Z3m4Z4 d dlm5Z5 d dl6m7Z7 dZ8G dd deZ9G dd deZ:dd Z;dd Z<dd Z=d d! Z>d"d# Z?ee9d$d% Z@dS )&    N)OP)DidNotEnableIntegration)SentryAsgiMiddleware)should_send_default_pii)SOURCE_FOR_STYLETransactionSource)ensure_integration_enabledevent_from_exceptiontransaction_from_function)RequestStarliteState)BaseRouteHandler)DefineMiddleware)get_plugin_for_value)	HTTPRoute)ConnectionDataExtractoris_async_callableRef)	BaseModelzStarlite is not installed)TYPE_CHECKING)AnyOptionalUnion)
ASGIAppHintHTTPReceiveMessage	HTTPScopeMessage
MiddlewareReceiveScopeSendWebSocketReceiveMessage)MiddlewareProtocol)Eventzgeneric Starlite requestc                   @   s&   e Zd ZdZde Zedd ZdS )StarliteIntegrationstarlitez
auto.http.c                   C   s   t   t  t  d S )N)patch_app_initpatch_middlewarespatch_http_route_handle r,   r,   b/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/sentry_sdk/integrations/starlite.py
setup_once3   s   
zStarliteIntegration.setup_onceN)__name__
__module____qualname__
identifieroriginstaticmethodr.   r,   r,   r,   r-   r'   /   s
    
r'   c                       s"   e Zd Zejf fdd	Z  ZS )SentryStarliteASGIMiddlewarec                    s   t  j|ddd|dd d S )NFendpointasgi   )appunsafe_context_datatransaction_stylemechanism_typespan_originasgi_version)super__init__)selfr9   r=   	__class__r,   r-   r@   <   s   
z%SentryStarliteASGIMiddleware.__init__)r/   r0   r1   r'   r3   r@   __classcell__r,   r,   rB   r-   r5   ;   s    r5   c                     s&   t j tt  fdd} | t _dS )a  
    Replaces the Starlite class's `__init__` function in order to inject `after_exception` handlers and set the
    `SentryStarliteASGIMiddleware` as the outmost middleware in the stack.
    See:
    - https://starlite-api.github.io/starlite/usage/0-the-starlite-app/5-application-hooks/#after-exception
    - https://starlite-api.github.io/starlite/usage/7-middleware/0-middleware-intro/
    c                    sd   | dg }|jtgt|tr|n|gd |dpg }tg||d<  | g|R i | d S )Nafter_exception)rE   
middleware)popupdateexception_handler
isinstancelistgetr5   )rA   argskwargsrE   rF   old__init__r,   r-   injection_wrapperS   s   z)patch_app_init.<locals>.injection_wrapperN)r   r@   r	   r'   )rQ   r,   rO   r-   r)   H   s   	
r)   c                     s&   t j tt  fdd} | t _d S )Nc                    s   dd  | D S )Nc                 S   s   g | ]}t |qS r,   )enable_span_for_middleware).0rF   r,   r,   r-   
<listcomp>p   s    zIpatch_middlewares.<locals>.resolve_middleware_wrapper.<locals>.<listcomp>r,   )rA   old_resolve_middleware_stackr,   r-   resolve_middleware_wrapperm   s   z5patch_middlewares.<locals>.resolve_middleware_wrapper)r   resolve_middlewarer	   r'   )rW   r,   rU   r-   r*   i   s   
r*   c                    sj   t | dr	| tu r| S t| tr| jj n| j  fdd} jdv}|r3t| tr0|| j_| S || _| S )N__call__c                    s   t  td u r| |I d H S | jj t jtj tj	dJ}|
d   fdd}tdt}|dk}|s@|n} fdd}	tdt}
|
d	k}|sY|	n}| |||I d H W  d    S 1 sow   Y  d S )
Nopnamer3   starlite.middleware_namec                     s   t  td u r| i |I d H S t jtjtdttj	d}|
d  | i |I d H W  d    S 1 s?w   Y  d S Nr1   rZ   r]   )
sentry_sdk
get_clientget_integrationr'   
start_spanr   MIDDLEWARE_STARLITE_RECEIVEgetattrstrr3   set_tag)rM   rN   span)middleware_namereceiver,   r-   _sentry_receive   s   $zNenable_span_for_middleware.<locals>._create_span_call.<locals>._sentry_receiver/   rj   c                    s   t  td u r| I d H S t jtjtdttj	d}|
d  | I d H W  d    S 1 s9w   Y  d S r^   )r_   r`   ra   r'   rb   r   MIDDLEWARE_STARLITE_SENDrd   re   r3   rf   )messagerg   )rh   sendr,   r-   _sentry_send   s   $zKenable_span_for_middleware.<locals>._create_span_call.<locals>._sentry_sendrn   )r_   r`   ra   r'   rC   r/   rb   r   MIDDLEWARE_STARLITEr3   rf   rd   re   )rA   scoperi   rm   middleware_spanrj   receive_namereceive_patchednew_receivern   	send_namesend_patchednew_sendold_call)rh   ri   rm   r-   _create_span_call   s*   $z5enable_span_for_middleware.<locals>._create_span_call)rz   )hasattrr5   rJ   r   rF   rY   r/   )rF   rz   not_yet_patchedr,   rx   r-   rR   x   s   


1
rR   c                     s   t j  fdd} | t _d S )Nc                    s   t  td u r| ||I d H S t  }d j||d}tddd|  d}|I d H  fdd}tj|_	|
| | ||I d H S )Nr9   )rp   ri   rm   T)
parse_bodyparse_querybodyc                    s    d}|  di }t dd|d< t r d |d< d ur&|d< d }|jd ur1|j}nt|jtr<|jj}n|j}|d urGt|}dt	d	 i}|sVt
}dtji}| |||d
 | S )Nroute_handlerrequest_body    content_lengthcookiesdatasourcer6   )r   transactiontransaction_info)rL   lenr   r\   rJ   fnr   valuer   r   _DEFAULT_TRANSACTION_NAMEr   ROUTErH   )event_r   request_infofunctx_nametx_infoextracted_request_datarequest_datarp   r,   r-   event_processor   s4   



zHpatch_http_route_handle.<locals>.handle_wrapper.<locals>.event_processor)r_   r`   ra   r'   get_isolation_scoperequest_classr   rG   r2   _nameadd_event_processor)rA   rp   ri   rm   sentry_scoper   r   r   
old_handler   r-   handle_wrapper   s$   

$
z/patch_http_route_handle.<locals>.handle_wrapper)r   handle)r   r,   r   r-   r+      s   
9r+   c                 C   sh   |  d}|s	d S t|tr|S t|tr| S t|dr"| S t|}|r2t|js2||S d S )Nuserasdict)	rL   rJ   dictr   r{   r   r   r   to_dict)rp   
scope_userpluginr,   r,   r-   retrieve_user_from_scope  s   




r   c                 C   sd   d }t  r	t|}|rt|trt }|| t| t j	t
jddd\}}tj||d d S )NF)typehandled)client_options	mechanism)hint)r   r   rJ   r   r_   r   set_userr
   r`   optionsr'   r2   capture_event)excrp   r   	user_infor   r   r   r,   r,   r-   rI     s   


rI   )Ar_   sentry_sdk.constsr   sentry_sdk.integrationsr   r   sentry_sdk.integrations.asgir   sentry_sdk.scoper   sentry_sdk.tracingr   r   sentry_sdk.utilsr	   r
   r   r(   r   r   r   starlite.handlers.baser   starlite.middlewarer   starlite.plugins.baser   starlite.routes.httpr   starlite.utilsr   r   r   pydanticr   ImportErrortypingr   r   r   r   starlite.typesr   r   r   r   r   r    r!   r"   StarliteScoper#   r$   r%   sentry_sdk._typesr&   r   r'   r5   r)   r*   rR   r+   r   rI   r,   r,   r,   r-   <module>   sD    0!I@