o
    i                     @   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 d dlmZ d d	lmZ d d
lmZ dZdZdZdZdd Zdd Zdd ZdddZdS )    )web)SystemRoute)config)	SpanTypes)http)core)schematize_url_operation)SpanDirection)DDTraceDeprecationWarning)	deprecatedatadog_tracedatadog_context__datadog_trace_config__datadog_request_spanc                    s    fdd}|S )a}  
    ``aiohttp`` middleware that traces the handler execution.
    Because handlers are run in different tasks for each request, we attach the Context
    instance both to the Task and to the Request objects. In this way:

    * the Task is used by the internal automatic instrumentation
    * the ``Context`` attached to the request can be freely used in the application code
    c                    s  t  d }tjdtddtjdtj|i  jt	j
dt  d dd
V}|j}|d| td	|t	j
f |j t< | t< t   t< z' I d H t	j
d
 scttjrc j fdd W W  d    S  tyx   |   w 1 s|w   Y  d S )Nservicezaiohttp.requestr   )protocol	directionTdistributed_tracing_enabled)		span_name	span_typer   tagsdistributed_headersintegration_configactivate_distributed_headers#distributed_headers_config_overrideheaders_case_sensitivereq_spanzweb.request.start"disable_stream_timing_for_mem_leakc                    s
   t  S )N)finish_request_span)_requestresponse `/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/aiohttp/middlewares.py<lambda>=   s   
 z:trace_middleware.<locals>.attach_context.<locals>.<lambda>)
CONFIG_KEYr   context_with_datar   r	   INBOUNDr   WEBheadersr   aiohttpspanset_itemdispatchcontextREQUEST_CONTEXT_KEYREQUEST_SPAN_KEYREQUEST_CONFIG_KEY
isinstancer   StreamResponsetaskadd_done_callback	Exceptionset_traceback)r!   r   ctxr   apphandlerr    r$   attach_context   sB   


z(trace_middleware.<locals>.attach_contextr#   )r;   r<   r=   r#   r:   r$   trace_middleware   s   
%r>   c                 C   s  |  td }|s
d S t|j}| jjjrB| jjj }| dr&| d}n| dr1| d}n
| dr;| d}d| j	|}||_| t
  d}|d u rTtjj}|r^|tj| j d }t| jdrs| jj}t|tss|jj}td|tj| j	t| j|jd | j|j|df
 d S )	Npath	formatterprefixz{} {}trace_query_stringroutezweb.request.finishT)getr1   strstatus
match_inforC   resourceget_infoformatmethodr2   r   _httprB   _set_tag_strr   QUERY_STRINGquery_stringhasattrr3   r   	canonicalr   r.   r+   urlr*   )r!   r"   request_spanrH   res_inforB   rC   aiohttp_router#   r#   r$   r   F   sL   






r   c                    s6   t jd st|tju r|j sdS t| | dS )z|
    The on_prepare signal is used to close the request span that is created during
    the trace middleware execution.
    r   N)r   r+   typer   r4   r5   doner   r    r#   r#   r$   
on_prepare   s
   

rX   Naiohttp-webc                 C   sb   t | ddrdS d| _|durtddtdd tj|d	dd
| t< | jdt	 | j
t dS )z
    Tracing function that patches the ``aiohttp`` application so that it will be
    traced using the global tracer.

    :param app: aiohttp application to trace
    :param service: service name of tracer
    __datadog_traceFNTz"The tracer parameter is deprecatedz'The global tracer will be used instead.z5.0.0)messagecategoryremoval_version)default)r   r   r   )getattrrZ   r   r
   r   _get_servicer&   middlewaresinsertr>   on_response_prepareappendrX   )r;   tracerr   r#   r#   r$   	trace_app   s   


rf   )NrY   )r+   r   aiohttp.web_urldispatcherr   ddtracer   ddtrace.extr   r   ddtrace.internalr   ddtrace.internal.schemar   -ddtrace.internal.schema.span_attribute_schemar	   #ddtrace.internal.utils.deprecationsr
   ddtrace.vendor.debtcollectorr   r&   r0   r2   r1   r>   r   rX   rf   r#   r#   r#   r$   <module>   s$    2;