
    Dj1                     b   d Z ddlZddlZddlmZ ddlmZ ddl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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mZmZm Z m!Z!m"Z" ddlm#Z# ddl$m%Z% e%r&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* ddl+m,Z,m-Z-  ed      Z.dZ/dZ0ddZ1d Z2 G d d      Z3y)ze
An ASGI middleware.

Based on Tom Christie's `sentry-asgi <https://github.com/encode/sentry-asgi>`.
    N)deepcopy)partial)continue_trace)OP)_get_headers_get_request_data_get_url)DEFAULT_HTTP_METHODS_TO_CAPTUREnullcontext)track_session)SOURCE_FOR_STYLETRANSACTION_SOURCE_ROUTETRANSACTION_SOURCE_URLTRANSACTION_SOURCE_COMPONENTTRANSACTION_SOURCE_CUSTOM)
ContextVarevent_from_exceptionHAS_REAL_CONTEXTVARSCONTEXTVARS_ERROR_MESSAGEloggertransaction_from_function_get_installed_modules)Transaction)TYPE_CHECKING)Any)Callable)Dict)Optional)Tuple)EventHintsentry_asgi_middleware_appliedzgeneric ASGI request)endpointurlc                     t        | t        j                         j                  |dd      \  }}t        j                  ||       y )NF)typehandled)client_options	mechanism)hint)r   
sentry_sdk
get_clientoptionscapture_event)excmechanism_typeeventr*   s       `C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\sentry_sdk/integrations/asgi.py_capture_exceptionr3   ?   sA     '!,,.66)e<KE4
 U.    c                     t        j                  |       rt        | d      S t        j                  |       rt	        j
                  |       S t        | dd      }t	        j
                  |      S )z
    Try to figure out if an application object supports ASGI3.

    This is how uvicorn figures out the application version as well.
    	__await____call__N)inspectisclasshasattr
isfunctionasyncioiscoroutinefunctiongetattr)appcalls     r2   _looks_like_asgi3rA   J   s[     ssK((			C	 **3//sJ-**400r4   c                   @    e Zd ZdZddddefdZd Zd Zd	 Zd
 Z	d Z
y)SentryAsgiMiddleware)r?   r7   transaction_styler0   span_originhttp_methods_to_captureFr#   asgimanualc                 j   |st         st        dt        z         |t        vrt	        d|dt        d      |dk(  xr dt               v }|rt        j                  d       || _        || _	        || _
        || _        || _        t        |      r| j                  | _        y| j                   | _        y)	ab  
        Instrument an ASGI application with Sentry. Provides HTTP/websocket
        data to sent events and basic handling for exceptions bubbling up
        through the middleware.

        :param unsafe_context_data: Disable errors when a proper contextvars installation could not be found. We do not recommend changing this from the default.
        zRThe ASGI middleware for Sentry requires Python 3.7+ or the aiocontextvars package.z%Invalid value for transaction_style: z (must be in )rG   	starlettezThe Sentry Python SDK can now automatically support ASGI frameworks like Starlette and FastAPI. Please remove 'SentryAsgiMiddleware' from your project. See https://docs.sentry.io/platforms/python/guides/asgi/ for more information.N)r   RuntimeErrorr   TRANSACTION_STYLE_VALUES
ValueErrorr   r   warningrD   r0   rE   r?   rF   rA   
_run_asgi3r7   
_run_asgi2)selfr?   unsafe_context_datarD   r0   rE   rF   0asgi_middleware_while_using_starlette_or_fastapis           r2   __init__zSentryAsgiMiddleware.__init__d   s    " #+? 13LM  $<<$&>@  f$P8N8P)P 	9 <NNa "3,&'>$S! OODM OODMr4   c                       fd}|S )Nc                 J   K   j                  | |d       d {   S 7 w)N   asgi_version_run_app)receivesendscoperR   s     r2   innerz.SentryAsgiMiddleware._run_asgi2.<locals>.inner   s$     ugt!LLLLs   #!# )rR   r_   r`   s   `` r2   rQ   zSentryAsgiMiddleware._run_asgi2   s    	M r4   c                 H   K   | j                  |||d       d {   S 7 w)N   rY   r[   )rR   r_   r]   r^   s       r2   rP   zSentryAsgiMiddleware._run_asgi3   s#     ]]5'4a]HHHHs   " "c           	      L  K   t         j                  d      }|d   dk(  }|s|rA	 |dk(  r  | j                  |      |       d {   S | j                  ||       d {   S t         j                  d       	 t        j                         5 }t        |d      5  |j                          d	|_        t        | j                  |
      }	|j                  |	       |d   }
| j                  | j                   |      \  }}|j                  dd      j#                         }d || j$                  v r|
dv rIt'        t)        |      dj+                  |
      ||| j,                        t/        j0                  d       n=t3        t4        j6                  ||| j,                        t/        j0                  d       j9                  d|
       t/        j0                  dj:                  j<                         t        j>                  d|i      n	tA               5  t/        j0                  d       	 fd}|dk(  rP | j                  |      ||       d {   cd d d        cd d d        cd d d        t         j                  d       S | j                  |||       d {   cd d d        cd d d        cd d d        t         j                  d       S 7 7 ~# t        $ r}t	        || j
                         |d d }~ww xY w7 7 g# t        $ r}t	        || j
                         |d d }~ww xY w# 1 sw Y   nxY w	 d d d        n# 1 sw Y   nxY wd d d        n# 1 sw Y   nxY wt         j                  d       y # t         j                  d       w xY ww)NFr&   lifespanrX   )r0   Trequest)session_moderG   )
asgi_scopemethod )http	websocketz	{}.server)opnamesourceoriginz1[ASGI] Created transaction (continuing trace): %sz$[ASGI] Created transaction (new): %sz	asgi.typezB[ASGI] Set transaction name and source on transaction: '%s' / '%s'rh   )custom_sampling_contextz[ASGI] Started transaction: %sc                    K   0| j                  d      dk(  xr d| v }|rj                  | d           |        d {   S 7 w)Nr&   zhttp.response.startstatus)getset_http_status)r1   is_http_responser^   transactions     r2   _sentry_wrapped_sendz;SentryAsgiMiddleware._run_app.<locals>._sentry_wrapped_send   sX     #.#:(-		&(9=R(R )>,4,= %5 (8(3(C(CE(O(T-1%['8 8'8s   >AAA)!_asgi_middleware_appliedrt   r?   	Exceptionr3   r0   setr+   isolation_scoper   clear_breadcrumbs_namer   event_processoradd_event_processor _get_transaction_name_and_sourcerD   upperrF   r   r   formatrE   r   debugr   r   HTTP_SERVERset_tagrn   ro   start_transactionr   )rR   r_   r]   r^   rZ   is_recursive_asgi_middlewareis_lifespanr/   sentry_scope	processortytransaction_nametransaction_sourceri   rx   rw   s      `           @r2   r\   zSentryAsgiMiddleware._run_app   s    '?'C'CE'J$Fmz1';$1$!0%$!???!%%$!??? 	!$$T*U	0++- R0"<iH Q0 224)/L& '(<(< OI 44Y?vB ==..(* #YYx4::<F"&K!=!==!66*8 ,U 3#.#5#5b#9%5'9'+'7'7+K #LL S +
 +6#%>>%5'9'+'7'7	+K #LL F $++K<`',,'.. '2	 #44'5A54I
 )]!0 %E{S0
9  ,q0-<TXXe_$+-A." ("1!0 !0aQ0 Q0R0 R0h %((/ .2XX$)74H." ("9!0 !0aQ0 Q0R0 R0h %((/} @? $"3t7J7JKt#$^("("  ) 0.s4CVCVW"%4/0?!0 !0 !0aQ0 Q0 Q0R0 R0 R0h %((/$((/s+  "N$ K# KK# N$K# "K #K# &N$=N
 M(F ML=7&LL
L!	M*	M(3	N
 <N$L(L
)L,	M5	M(>	N
 N$K#  K# #	L,LLN$LL	L:	L5	5L:	:L==MM
	M(M	M(	N
 (M1-N
 4N$
N!!N$c                 z   |j                  di       }|j                  t        |             t        |      |d<   |d   t        k7  xr% |d   j                  d      t
        t        t        fv }|sK| j                  | j                  |      \  }}||d<   d|i|d<   t        j                  d|d   |d   d          |S )Nrf   rw   transaction_inforo   zF[ASGI] Set transaction name and source in event_processor: '%s' / '%s')rt   updater   r   _DEFAULT_TRANSACTION_NAMEr   r   r   r   rD   r   r   )rR   r1   r*   rh   request_dataalready_setrn   ro   s           r2   r   z$SentryAsgiMiddleware.event_processor
  s    yyB/-j9:#L1i M*.GG 
EM

#h-($%
M
 @@&&
LD& $(E- )16(:E$%LLXm$()(3 r4   c                 x   d }t         |   }|j                  d      }|dk(  r?|j                  d      }|rt        |      xs d}nat        ||dk(  rdndd       }t        }nE|dk(  r@|j                  d      }|rt        |d	d       }||}nt        ||dk(  rdndd       }t        }|t        }t        }||fS ||fS )
Nr&   r#   rj   rk   ws)hostr$   routepath)r   rt   r   r	   r   r>   r   r   )	rR   rD   rh   rn   ro   r   r#   r   r   s	            r2   r   z5SentryAsgiMiddleware._get_transaction_name_and_source-  s    !"34^^F#
*!~~j1H 0:@b
bFlFSWX/%' NN7+Eufd3#D
bFlFSWX/<,D-F<V|r4   N)__name__
__module____qualname__	__slots__r
   rU   rQ   rP   r\   r   r   ra   r4   r2   rC   rC   Z   s=    I "$ ?1,fIe0NF"r4   rC   )rG   )4__doc__r<   r8   copyr   	functoolsr   r+   sentry_sdk.apir   sentry_sdk.constsr   $sentry_sdk.integrations._asgi_commonr   r   r	   $sentry_sdk.integrations._wsgi_commonr
   r   sentry_sdk.sessionsr   sentry_sdk.tracingr   r   r   r   r   sentry_sdk.utilsr   r   r   r   r   r   r   r   typingr   r   r   r   r   r   sentry_sdk._typesr    r!   ry   r   rM   r3   rA   rC   ra   r4   r2   <module>r      s         )   
 .    +  - &&FG 2 . /1 u ur4   