o
    i2                     @   s  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 d dlm	Z	 erDd 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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 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( d dl'm)Z) d dl*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1 e$e2Z3e/Z4e5de6ddd  d!e7fd"d#Z8d!e6e7e7f fd$d%Z9G d&d' d'e:Z;d(d) Z<d*ee7e7f d!ee7e7f fd+d,Z=d-d. Z>G d/d0 d0e;Z?dS )1    )Mapping)TYPE_CHECKING)Any)Callable)Iterable)Optional)SpanDirection)Pin)Config)Span)Tracer)quoteN)config)	SPAN_KIND)trace_utils)SpanKind)	SpanTypes)core)BlockingException)	COMPONENT)
get_logger)schematize_url_operation)get_blocked)set_blocked)DDTraceDeprecationWarning)from_wsgi_header)HTTPPropagator)	deprecatewsgiT)_default_servicedistributed_tracingreturnc                   C      dS )N  r$   r$   r$   V/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/wsgi/wsgi.pyget_version4   s   r&   c                   C   s   ddiS )Nr   *r$   r$   r$   r$   r%   _supported_versions8   s   r(   c                   @   s  e Zd ZdZ	d+deded dddd	d
eddfddZede	fddZ
ede	fddZede	fddZdededejfddZ	d,deddddde	dededdfddZd,d dded!ee ddfd"d#Zddded$eddfd%d&Zd'dd(eddfd)d*ZdS )-_DDWSGIMiddlewareBasea  Base WSGI middleware class.

    :param application: The WSGI application to apply the middleware to.
    :param tracer: [Deprecated] Global tracer will be used instead.
    :param int_config: Integration specific configuration object.
    :param pin: Set tracing metadata on a particular traced connection
    :param app_is_iterator: Boolean indicating whether the wrapped app is a Python iterator
    Fapplicationtracerr   
int_configr
   pinr	   app_is_iteratorr!   Nc                 C   s4   |d urt ddtdd || _|| _|| _|| _d S )Nz"The tracer parameter is deprecatedz'The global tracer will be used instead.z5.0.0)messagecategoryremoval_version)r   r   app_config_pinr.   )selfr*   r+   r,   r-   r.   r$   r$   r%   __init__F   s   
z_DDWSGIMiddlewareBase.__init__c                 C      t )z<Returns the name of a request span. Example: `flask.request`NotImplementedErrorr5   r$   r$   r%   _request_span_nameZ      z(_DDWSGIMiddlewareBase._request_span_namec                 C   r7   )zEReturns the name of an application span. Example: `flask.application`r8   r:   r$   r$   r%   _application_span_name_   r<   z,_DDWSGIMiddlewareBase._application_span_namec                 C   r7   )z>Returns the name of a response span. Example: `flask.response`r8   r:   r$   r$   r%   _response_span_named   r<   z)_DDWSGIMiddlewareBase._response_span_nameenvironstart_responsec                    s2  t |}d}d}tjd|d|dt| j| jtj	t
| dr"| jn| j| j| j||| dddމ  dt  fd	d
}t rR| \}}}|t|| |g}d}td|f d }	|rtd |f z| | d}W nR ty }
 z$t|
jd  | \}}}|t|| |g}td f W Y d }
~
nDd }
~
w ty }
 z		 |
}	W Y d }
~
n0d }
~
w ty   td f  w t rtd tfjjpd\}}}|g}td |f td || jfj}|	r|jr|j  td |f |	|r|jng W  d    S 1 sw   Y  d S )Nr$   Tzwsgi.__call__REMOTE_ADDR_request_call_namereq_span)remote_addrheadersheaders_case_sensitiveservice	span_type	span_namemiddleware_configintegration_configdistributed_headersr?   
middlewarespan_keyactivate_distributed_headerszwsgi.construct_urlc                     s>   t d tfj} | r| j\}}}ndg d}}}|||fS )Nwsgi.block.startedi  r#   )r   dispatch_with_resultsconstruct_urlstatus_headers_contentvalue)resultstatusrE   contentctxr$   r%   blocked_view   s   
z4_DDWSGIMiddlewareBase.__call__.<locals>.blocked_viewFzwsgi.block_decidedzwsgi.request.prepareintercept_start_responser   zwsgi.app.exceptionrP   )NNr#   zwsgi.app.successzwsgi.request.complete)get_request_headersr   context_with_datagetr   int_servicer4   r3   r   WEBhasattrrB   r;   set_itemrR   r   strdispatchr2   	find_itemr   r   argsStopIterationBaseExceptionrQ   rS   rT   r.   traced_iterable_finish_spans)r5   r?   r@   rE   closing_iterablenot_blockedrZ   rW   rV   stop_iteration_exceptione_rU   r$   rX   r%   __call__i   s   
	
&z_DDWSGIMiddlewareBase.__call__request_spanr   app_spanrV   exc_infoc                 C   sh   t jd| ||||tjtd| jdt| jjt	t
jid
 ||||W  d   S 1 s-w   Y  dS )zDsets the status code on a request span when start_response is calledwsgi.responseNF)	rM   rq   parent_callrV   r?   rH   rG   
start_spantags)r   r]   r   r`   r   r_   r3   r   integration_namer   r   SERVER)r5   r@   rq   rr   rV   r?   rs   r$   r$   r%   _traced_start_response   s   

$z,_DDWSGIMiddlewareBase._traced_start_responserC   parsed_headersc                 C   r"   z7Implement to modify span attributes on the request_spanNr$   )r5   rC   r?   r{   r$   r$   r%   _request_span_modifier       z,_DDWSGIMiddlewareBase._request_span_modifierrU   c                 C   r"   )z;Implement to modify span attributes on the application_spanNr$   )r5   rr   r?   rU   r$   r$   r%   _application_span_modifier   r~   z0_DDWSGIMiddlewareBase._application_span_modifier	resp_spanresponsec                 C   r"   r|   r$   r5   r   r   r$   r$   r%   _response_span_modifier   r~   z-_DDWSGIMiddlewareBase._response_span_modifier)FN)__name__
__module____qualname____doc__r   r   boolr6   propertyrc   r;   r=   r>   r   wraptObjectProxyrp   dictr   rz   r}   r   r   r$   r$   r$   r%   r)   <   sT    
\
 r)   c                 C   s<  | d d }|  dr|| d 7 }n)|| d 7 }| d dkr-| d dkr,|d| d  7 }n| d d	kr;|d| d  7 }|t|  d
d7 }|  dre|| d 7 }|  drcd| d vrc|d| d  7 }|S |  dr|| d 7 }|  drd| d vr|d| d  7 }|S |t|  dd7 }|  dr|d| d  7 }|S )zF
    https://www.python.org/dev/peps/pep-3333/#url-reconstruction
    zwsgi.url_schemez://	HTTP_HOSTSERVER_NAMEhttpsSERVER_PORT443:80SCRIPT_NAMEr#   RAW_URIQUERY_STRING?REQUEST_URI	PATH_INFO)r^   r   )r?   urlr$   r$   r%   rR      s2   



rR   r?   c                 C   s8   i }|   D ]}|drt|}|r| | ||< q|S )zH
    Manually grab the request headers from the environ dictionary.
    HTTP_)keys
startswithr   )r?   request_headerskeynamer$   r$   r%   r\     s   
r\   c                 C   s   d |d |d | _d S )Nz{} {}REQUEST_METHODr   )formatresource)spanr?   r$   r$   r%   default_wsgi_span_modifier  s   r   c                       s   e Zd ZdZeddejdZdZdZ	de
dfd	ed
ed dedeeef gdf deddf
 fddZdddZdddZdd Z  ZS )DDWSGIMiddlewarea  WSGI middleware providing tracing around an application.

    :param application: The WSGI application to apply the middleware to.
    :param tracer: [Deprecated] Global tracer will be used.
    :param span_modifier: "Span" modifier that can add tags to the root span.
                            Defaults to using the request method and url in the resource.
    :param app_is_iterator: Boolean indicating whether the wrapped WSGI app is a Python iterator
    zwsgi.requesthttp)protocol	directionzwsgi.applicationrt   NFr*   r+   r   span_modifierr   r.   r!   c                    s&   t t| j||tjd |d || _d S )N)r.   )superr   r6   r   r   r   )r5   r*   r+   r   r.   	__class__r$   r%   r6   %  s   
zDDWSGIMiddleware.__init__c                 C   s   t jd| ||||tjdtd | jdt| jjt	t
jid)}|j ||||W  d    W  d    S 1 s9w   Y  W d    d S 1 sIw   Y  d S )Nrt   zwsgi.start_responseT)
rM   rq   ru   rV   r?   rH   rI   rG   rv   rw   )r   r]   r   r`   r   r_   r3   r   rx   r   r   ry   r   )r5   r@   rq   rr   rV   r?   rs   rY   r$   r$   r%   rz   /  s&   
Rz'DDWSGIMiddleware._traced_start_responsec                 C   s6   t |}|d ur
|nt|}td| ||||f d S )Nzwsgi.request.prepared)rR   r\   r   rd   )r5   rC   r?   r{   r   r   r$   r$   r%   r}   B  s   z'DDWSGIMiddleware._request_span_modifierc                 C   s   t d||f d S )Nzwsgi.response.prepared)r   rd   r   r$   r$   r%   r   G  s   z(DDWSGIMiddleware._response_span_modifierr   )r   r   r   r   r   r   INBOUNDr;   r=   r>   r   r   r   r   r   rc   r   r6   rz   r}   r   __classcell__r$   r$   r   r%   r     s,    	


r   )@collections.abcr   typingr   r   r   r   r   -ddtrace.internal.schema.span_attribute_schemar   ddtrace._trace.pinr	   !ddtrace.internal.settings._configr
   ddtrace.tracer   r   urllib.parser   r   ddtracer   ddtrace.constantsr   ddtrace.contribr   ddtrace.extr   r   ddtrace.internalr   ddtrace.internal._exceptionsr   ddtrace.internal.constantsr   ddtrace.internal.loggerr   ddtrace.internal.schemar   ddtrace.internal.utilsr   r   #ddtrace.internal.utils.deprecationsr   ddtrace.propagation._utilsr   ddtrace.propagation.httpr   ddtrace.vendor.debtcollectorr   r   log
propagator_addr   rc   r&   r(   objectr)   rR   r\   r   r   r$   r$   r$   r%   <module>   s^    	 %"&