o
    Á¿iu  ã                   @   s’   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 eeƒZG d
d„ dƒZG dd„ deƒZdS )é    N)ÚOptional)Úurlparse)ÚSpanProcessor)ÚSpan)Ú	SpanTypes)Úhttp)Ú
get_logger)Úconfigc                   @   s<   e Zd Zdd„ Zdedefdd„Zdee defdd	„Zd
S )ÚSimplifiedEndpointComputerc                 C   s@   t  d¡| _t  d¡| _t  d¡| _t  d¡| _t  d¡| _d S )Nz^[1-9][0-9]+$z^(?=.*[0-9].*)[0-9._-]{3,}$z^(?=.*[0-9].*)[A-Fa-f0-9]{6,}$z!^(?=.*[0-9].*)[A-Fa-f0-9._-]{6,}$z^(.{20,}|.*[%&'()*+,:=@].*)$)ÚreÚcompileÚ_INT_REÚ
_INT_ID_REÚ_HEX_REÚ
_HEX_ID_REÚ_STR_RE©Úself© r   ú^/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/_trace/processor/resource_renaming.pyÚ__init__   s
   z#SimplifiedEndpointComputer.__init__ÚelemÚreturnc                 C   sT   | j  |¡rdS | j |¡rdS | j |¡rdS | j |¡r dS | j |¡r(dS |S )zAApplies the parameter replacement rules to a single path element.z{param:int}z{param:int_id}z{param:hex}z{param:hex_id}z{param:str})r   Ú	fullmatchr   r   r   r   )r   r   r   r   r   Ú)_compute_simplified_endpoint_path_element   s   zDSimplifiedEndpointComputer._compute_simplified_endpoint_path_elementÚurlc              
      s¸   |sdS zt |ƒ}W n ty# } zt d|¡ W Y d}~dS d}~ww |j}|r-|dkr/dS g }| d¡D ]}|rG| |¡ t|ƒdkrG nq6|sLdS ‡ fdd„|D ƒ}dd |¡ S )z2Extracts and simplifies the path from an HTTP URL.ú/zKFailed to parse http.url tag when processing span for resource renaming: %sNé   c                    s   g | ]}ˆ   |¡‘qS r   )r   )Ú.0r   r   r   r   Ú
<listcomp>>   s    z7SimplifiedEndpointComputer.from_url.<locals>.<listcomp>)	r   Ú
ValueErrorÚlogÚerrorÚpathÚsplitÚappendÚlenÚjoin)r   r   Ú
parsed_urlÚer#   ÚelementsÚpartr   r   r   Úfrom_url&   s.   €þ
€z#SimplifiedEndpointComputer.from_urlN)Ú__name__Ú
__module__Ú__qualname__r   Ústrr   r   r,   r   r   r   r   r
      s    r
   c                   @   s0   e Zd Zdd„ Zdefdd„Zdefdd„ZdS )	ÚResourceRenamingProcessorc                 C   s   t ƒ | _d S ©N)r
   Úsimplified_endpoint_computerr   r   r   r   r   C   s   z"ResourceRenamingProcessor.__init__Úspanc                 C   s   d S r2   r   )r   r4   r   r   r   Úon_span_startF   s   z'ResourceRenamingProcessor.on_span_startc                 C   sˆ   |j r|jtjtjtjfvrd S | tj¡}|dkp|dk}| tj	¡}|s@|r+t
jrB| tj¡}| j |¡}| tj|¡ d S d S d S )NÚ404i”  )Ú_is_top_levelÚ	span_typer   ÚWEBÚHTTPÚ
SERVERLESSÚget_tagr   ÚSTATUS_CODEÚROUTEr	   Ú3_trace_resource_renaming_always_simplified_endpointÚURLr3   r,   Ú_set_tag_strÚENDPOINT)r   r4   ÚstatusÚis_404Úrouter   Úendpointr   r   r   Úon_span_finishI   s   ýz(ResourceRenamingProcessor.on_span_finishN)r-   r.   r/   r   r   r5   rG   r   r   r   r   r1   B   s    r1   )r   Útypingr   Úurllib.parser   Úddtrace._trace.processorr   Úddtrace._trace.spanr   Úddtrace.extr   r   Úddtrace.internal.loggerr   Ú!ddtrace.internal.settings._configr	   r-   r!   r
   r1   r   r   r   r   Ú<module>   s    2