o
    hy'                     @   s  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
mZ d dlmZ d dlmZmZ d dlmZmZ z`d 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# erd dl m$Z$m%Z%m&Z&m'Z'm(Z( d dl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 d dlm3Z3 d dl4m5Z5 W n e6y   e
dw dZ7G dd deZ8G dd deZ9d/ddZ:d/ddZ;d0d!d"Z<d/d#d$Z=d1d(d)Z>d*e?d%d&d+d,ddfd-d.Z@dS )2    )TYPE_CHECKING)	BaseModel)OP)Hub_should_send_default_pii)DidNotEnableIntegration)SentryAsgiMiddleware)SOURCE_FOR_STYLETRANSACTION_SOURCE_ROUTE)event_from_exceptiontransaction_from_function)RequestStarliteState)BaseRouteHandler)DefineMiddleware)get_plugin_for_value)	HTTPRoute)ConnectionDataExtractoris_async_callableRef)AnyDictListOptionalUnion)	ASGIAppHTTPReceiveMessage	HTTPScopeMessage
MiddlewareReceiveScopeSendWebSocketReceiveMessage)MiddlewareProtocol)EventzStarlite is not installedzgeneric Starlite requestc                       s   e Zd Zd fddZ  ZS )SentryStarliteASGIMiddlewareappr   c                    s   t  j|dddd d S )NFendpointasgi)r)   unsafe_context_datatransaction_stylemechanism_type)super__init__)selfr)   	__class__ c/var/www/html/aiguide_backend/venv/lib/python3.10/site-packages/sentry_sdk/integrations/starlite.pyr0   *   s   
z%SentryStarliteASGIMiddleware.__init__)r)   r   )__name__
__module____qualname__r0   __classcell__r4   r4   r2   r5   r(   )   s    r(   c                   @   s   e Zd ZdZedddZdS )StarliteIntegrationstarlitereturnNc                   C   s   t   t  t  d S )N)patch_app_initpatch_middlewarespatch_http_route_handler4   r4   r4   r5   
setup_once6   s   
zStarliteIntegration.setup_oncer<   N)r6   r7   r8   
identifierstaticmethodr@   r4   r4   r4   r5   r:   3   s    r:   r<   Nc                     s   t j d
 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/
    r1   r   argsr   kwargsr<   Nc                    sn   | dg }|jtgt|tr|n|gd tjt_| dd p"g }tg||d<  | g|R i | d S )Nafter_exception)rF   
middleware)popupdateexception_handler
isinstancelistr(   
_run_asgi3__call__)r1   rD   rE   rF   rG   old__init__r4   r5   injection_wrapperG   s   z)patch_app_init.<locals>.injection_wrapper)r1   r   rD   r   rE   r   r<   N)r   r0   )rQ   r4   rO   r5   r=   =   s   
r=   c                     s   t j d fdd} | t _d S )Nr1   r   r<   List[Middleware]c                    s   dd  | D S )Nc                 S   s   g | ]}t |qS r4   )enable_span_for_middleware).0rG   r4   r4   r5   
<listcomp>`   s    zIpatch_middlewares.<locals>.resolve_middleware_wrapper.<locals>.<listcomp>r4   )r1   old__resolve_middleware_stackr4   r5   resolve_middleware_wrapper_   s   z5patch_middlewares.<locals>.resolve_middleware_wrapper)r1   r   r<   rR   )r   resolve_middleware)rX   r4   rV   r5   r>   \   s   
r>   rG   r!   c                    s   t | dr	| tu r| S t| tr| jj n| j 										
	 d fdd} jdv}|r>t| tr;|| j_| S || _| S )NrN   r1   r&   scoper#   receiver"   sendr$   r<   c                    s   t j}|t}|d uru| jj |jtj dR}|	d  						d fdd	}t
d
t}|dk}	|	s>|n}
d fdd}t
d
t}|dk}|sX|n}| ||
|I d H W  d    S 1 snw   Y  d S | |I d H S )Nopdescriptionstarlite.middleware_namerD   r   rE   r<   2Union[HTTPReceiveMessage, WebSocketReceiveMessage]c                     sh   t j}|jtjtdtd}|d  | i |I d H W  d    S 1 s-w   Y  d S Nr8   r]   r`   )r   current
start_spanr   MIDDLEWARE_STARLITE_RECEIVEgetattrstrset_tag)rD   rE   hubspan)middleware_namer[   r4   r5   _sentry_receive   s   $zNenable_span_for_middleware.<locals>._create_span_call.<locals>._sentry_receiver6   rl   messager    c                    sb   t j}|jtjtdtd}|d  | I d H W  d    S 1 s*w   Y  d S rb   )r   rc   rd   r   MIDDLEWARE_STARLITE_SENDrf   rg   rh   )rm   ri   rj   )rk   r\   r4   r5   _sentry_send   s   $zKenable_span_for_middleware.<locals>._create_span_call.<locals>._sentry_sendro   )rD   r   rE   r   r<   ra   )rm   r    r<   N)r   rc   get_integrationr:   r3   r6   rd   r   MIDDLEWARE_STARLITErh   rf   rg   )r1   rZ   r[   r\   ri   integrationmiddleware_spanrl   receive_namereceive_patchednew_receivero   	send_namesend_patchednew_sendold_call)rk   r[   r\   r5   _create_span_callt   s6   
	$%z5enable_span_for_middleware.<locals>._create_span_call)r|   )
r1   r&   rZ   r#   r[   r"   r\   r$   r<   N)hasattrr(   rK   r   rG   rN   r6   )rG   r|   not_yet_patchedr4   rz   r5   rS   h   s2   


.
rS   c                     s2   t j 											 d fd
d} | t _d S )Nr1   r   rZ   r   r[   r"   r\   r$   r<   c           
         s   t j}|t}|d u r| ||I d H S | @}d j||d}tddd|  d}|I d H d fdd}	tj|_	|
|	 | ||I d H W  d    S 1 s^w   Y  d S )Nr)   )rZ   r[   r\   T)
parse_bodyparse_querybodyeventr'   _Dict[str, Any]r<   c                    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}|sUt
}dti}| j|||d
 | S )Nroute_handlerrequest_body    content_lengthcookiesdatasourcer*   )r   transactiontransaction_info)getlenr   namerK   fnr   valuer   r
   _DEFAULT_TRANSACTION_NAMEr   rI   )r   r   r   request_infofunctx_nametx_infoextracted_request_datarequest_datarZ   r4   r5   event_processor   s.   


zHpatch_http_route_handle.<locals>.handle_wrapper.<locals>.event_processor)r   r'   r   r   r<   r'   )r   rc   rp   r:   configure_scoperequest_classr   rH   rB   _nameadd_event_processor)
r1   rZ   r[   r\   ri   rr   sentry_scoper   r   r   
old_handler   r5   handle_wrapper   s*   




$z/patch_http_route_handle.<locals>.handle_wrapper)
r1   r   rZ   r   r[   r"   r\   r$   r<   N)r   handle)r   r4   r   r5   r?      s   
7r?   rZ   r#   Optional[Dict[str, Any]]c                 C   sj   |  di }|s
d S t|tr|S t|tr| S t|dr#| S t|}|r3t|js3||S d S )Nuserasdict)	r   rK   dictr   r}   r   r   r   to_dict)rZ   
scope_userpluginr4   r4   r5   retrieve_user_from_scope   s   



r   excr   r   c                 C   s   t j}|td u rd S d }t rt|}|r5t|tr5| }|	| W d    n1 s0w   Y  t
| |jr>|jjnd tjddd\}}|j||d d S )NF)typehandled)client_options	mechanism)hint)r   rc   rp   r:   r   r   rK   r   r   set_userr   clientoptionsrB   capture_event)r   rZ   r   ri   	user_infor   r   r   r4   r4   r5   rJ      s    


rJ   rA   )rG   r!   r<   r!   )rZ   r#   r<   r   )Atypingr   pydanticr   sentry_sdk.constsr   sentry_sdk.hubr   r   sentry_sdk.integrationsr   r   sentry_sdk.integrations.asgir	   sentry_sdk.tracingr
   r   sentry_sdk.utilsr   r   r;   r   r   r   starlite.handlers.baser   starlite.middlewarer   starlite.plugins.baser   starlite.routes.httpr   starlite.utilsr   r   r   r   r   r   r   r   starlite.typesr   r   r   r    r!   r"   r#   r$   r%   r&   sentry_sdk._typesr'   ImportErrorr   r(   r:   r=   r>   rS   r?   r   	ExceptionrJ   r4   r4   r4   r5   <module>   sB    ,





E
=