o
    i{                     @   s|  d dl 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lm Z  d dl!m"Z# d dl$m%Z% d dl&m'Z' dZ(e)dede e j*dddej+e e j*dddd de,fddZ-de.e,e,f fd d!Z/d"d# Z0d$d% Z1d&d' Z2dS )(    N)parse)wrap_function_wrapper)config)Pin)	SPAN_KIND)trace_utils)SpanKind)	SpanTypes)net)	COMPONENT)schematize_service_name)schematize_url_operation)SpanDirection)ArgumentError)get_argument_value)asbool)unwrap)HTTPPropagator)tracer)P   i  urllib3DD_URLLIB3_DISTRIBUTED_TRACINGT)defaultDD_URLLIB3_SPLIT_BY_DOMAINF)_default_servicedistributed_tracingdefault_http_tag_query_stringsplit_by_domainreturnc                   C   s   t tddS )N__version__ )getattrr    r"   r"   Z/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/urllib3/patch.pyget_version*   s   r$   c                   C   s   ddiS )Nr   z>=1.25.0r"   r"   r"   r"   r#   _supported_versions.   s   r%   c                  C   s   t tddrdS dt_tddt tjr8ddlm}  dd	lm	} td
d| t
tdr2tdd|  ntdd|  t tjj dS )z'Enable tracing for all urllib3 requests__datadog_patchFNTr   z)connectionpool.HTTPConnectionPool.urlopenr   ) wrapped_request_D8CB81E472AF98A2)wrapped_urllib3_make_requestzurllib3.connectionpoolz HTTPConnectionPool._make_request_request_methodszurllib3._request_methodszRequestMethods.requesturllib3.request)r!   r   r&   _w_wrap_urlopen
asm_config_load_modules%ddtrace.appsec._common_module_patchesr'   r(   hasattrr   ontoconnectionpoolHTTPConnectionPool)_wrap_request_make_requestr"   r"   r#   patch2   s   
r6   c                   C   s(   t tddrdt_ttjjd dS dS )z&Disable trace for all urllib3 requestsr&   FurlopenN)r!   r   r&   _ur2   r3   r"   r"   r"   r#   unpatchF   s   r9   c                 C   st  t ||dd}t ||dd}z	t ||dd}W n ty"   d}Y nw z	t ||dd	}W n ty7   d}Y nw |d
r\t|j|jrQ|jtvrQd|j	|jnt
|j	|dddf}t|}|j}	t|}
|
ro|
 sv| |i |S tjtddtjdt|
tjtjd}|ttjj |ttj  tjj!r|	|_"tjj#r|du ri }||d< t$%|j&| t'|tj(j)j*r|j+nd}d}z3| |i |}W tj,|tj|||j	|du rdn|j-|j.||du ri nt/|j0|d
 |t1j2|j	 n-tj,|tj|||j	|du r	dn|j-|j.||du ri nt/|j0|d
 |t1j2|j	 w |W  d   S 1 s3w   Y  dS )av  
    Wrapper function for the lower-level urlopen in urllib3

    :param func: The original target function "urlopen"
    :param instance: The patched instance of ``HTTPConnectionPool``
    :param args: Positional arguments from the target function
    :param kwargs: Keyword arguments from the target function
    :return: The ``HTTPResponse`` from the target function
    r   method   url   headersN   retries/z{}:{}r*   http)protocol	direction)service	span_type)	integration_configr:   r<   target_hoststatus_codequeryrequest_headersresponse_headersretries_remain)3r   r   
startswithr   
urlunparseschemeport
DROP_PORTSformathoststrurlparsenetlocr   get_fromenabledr   tracer   r   OUTBOUNDr   ext_servicer   r   r	   HTTP_set_tag_strr   integration_namer   r   CLIENTr   rE   r   r   injectcontext
isinstanceutilretryRetrytotalset_http_metastatusrJ   dictr>   r
   SERVER_ADDRESS)funcinstanceargskwargsrequest_methodrequest_urlrK   request_retries
parsed_urihostnamepinspanr@   responser"   r"   r#   r,   N   s   




&r,   )3osurllibr   r   wraptr   r+   ddtracer   ddtrace._trace.pinr   ddtrace.constantsr   ddtrace.contribr   ddtrace.extr   r	   r
   ddtrace.internal.constantsr   ddtrace.internal.schemar   r   -ddtrace.internal.schema.span_attribute_schemar   ddtrace.internal.settings.asmr-   ddtrace.internal.utilsr   r   ddtrace.internal.utils.formatsr   ddtrace.internal.utils.wrappersr   r8   ddtrace.propagation.httpr   ddtrace.tracer   rR   _addgetenv_http_client_tag_query_stringrU   r$   rj   r%   r6   r9   r,   r"   r"   r"   r#   <module>   sH    