o
    BDi|)                     @   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,   Y/var/www/Datamplify/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 S )NFendpointasgi)appunsafe_context_datatransaction_stylemechanism_typespan_origin)super__init__)selfr8   r<   	__class__r,   r-   r>   <   s   
z%SentryStarliteASGIMiddleware.__init__)r/   r0   r1   r'   r3   r>   __classcell__r,   r,   r@   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                    sl   | dg }|jtgt|tr|n|gd tjt_|dp!g }tg||d<  | g|R i | d S )Nafter_exception)rC   
middleware)	popupdateexception_handler
isinstancelistr5   
_run_asgi3__call__get)r?   argskwargsrC   rD   old__init__r,   r-   injection_wrapperR   s   z)patch_app_init.<locals>.injection_wrapperN)r   r>   r	   r'   )rQ   r,   rO   r-   r)   G   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).0rD   r,   r,   r-   
<listcomp>p   s    zIpatch_middlewares.<locals>.resolve_middleware_wrapper.<locals>.<listcomp>r,   )r?   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 )NrK   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   rY   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/   ri   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_   r`   r'   ra   r   MIDDLEWARE_STARLITE_SENDrc   rd   r3   re   )messagerf   )rg   sendr,   r-   _sentry_send   s   $zKenable_span_for_middleware.<locals>._create_span_call.<locals>._sentry_sendrm   )r^   r_   r`   r'   rA   r/   ra   r   MIDDLEWARE_STARLITEr3   re   rc   rd   )r?   scoperh   rl   middleware_spanri   receive_namereceive_patchednew_receiverm   	send_namesend_patchednew_sendold_call)rg   rh   rl   r-   _create_span_call   s*   $z5enable_span_for_middleware.<locals>._create_span_call)ry   )hasattrr5   rH   r   rD   rK   r/   )rD   ry   not_yet_patchedr,   rw   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 )Nr8   )ro   rh   rl   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[   rH   fnr   valuer   r   _DEFAULT_TRANSACTION_NAMEr   ROUTErF   )event_r   request_infofunctx_nametx_infoextracted_request_datarequest_dataro   r,   r-   event_processor   s4   



zHpatch_http_route_handle.<locals>.handle_wrapper.<locals>.event_processor)r^   r_   r`   r'   get_isolation_scoperequest_classr   rE   r2   _nameadd_event_processor)r?   ro   rh   rl   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   rH   dictr   rz   r   r   r   to_dict)ro   
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   rH   r   r^   r   set_userr
   r_   optionsr'   r2   capture_event)excro   r   	user_infor   r   r   r,   r,   r-   rG     s   


rG   )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   rG   r,   r,   r,   r-   <module>   sD    0"I@