o
    3Ih                     @  s.  d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
mZmZ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
ZeeZedZG dd deZd7ddZ ej!fd8ddZ"d9d!d"Z#d:d;d(d)Z$d*d+ Z%G d,d- d-eZ&d<d/d0Z'ej(d=d3d4Z)d5d6 Z*dS )>zp
This library provides functionality to enrich HTTP client spans with IPs. It does
not create spans on its own.
    )annotationsN)AnyCallable
Collection	TypedDictcast)context)BaseInstrumentor)unwrap)SpanAttributes)Spanhttpbase_instrumentation_stateRc                   @  s*   e Zd ZdddZdddZdd	d
ZdS )HttpClientInstrumentorreturnCollection[str]c                 C  s   dS )N r   )selfr   r   _/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/opentelemetry/util/http/httplib.pyinstrumentation_dependencies-   s   z3HttpClientInstrumentor.instrumentation_dependencieskwargsr   c                 K  s
   t   dS )zBInstruments the http.client module (not creating spans on its own)N)_instrumentr   r   r   r   r   r   0   s   
z"HttpClientInstrumentor._instrumentc                 K  s
   t   d S N)_uninstrumentr   r   r   r   r   4   s   
z$HttpClientInstrumentor._uninstrumentN)r   r   )r   r   )__name__
__module____qualname__r   r   r   r   r   r   r   r   ,   s    

r   spanlist
list[Span]r   boolc                 C  s   t | d }|dkrE| |  s=td| |  |d t | k}|s,| t | d  | |< |   |r<|dkr8dS |d8 }n|d8 }|dks
dS )N   r   zSpan is not recording: %sFT)lenis_recordingloggerdebugpop)r   idxislastr   r   r   _remove_nonrecording8   s    r)   connhttp.client.HTTPConnectionloglevelintc              
   C  s   t  }|sdS |d}|sdS t|sdS d}z | j}td| |du r)W dS | }|r7|d r7|d }W n tyL   tj|d|ddd	 Y dS w |D ]	}|	t
j| qOdS )
zTries to set the net.peer.ip semantic attribute on the current span from the given
    HttpConnection.

    Returns False if the connection is not yet established, False if the IP was captured
    or there is no need to capture it.
    Tneed_ipz<property not accessed>zGot socket: %sNFr   z"Failed to get peer address from %s)exc_info
stack_info)	_getstategetr)   sockr$   r%   getpeername	Exceptionlogset_attributer   NET_PEER_IP)r*   r,   stater   r3   addripspanr   r   r   trysetipJ   s>   


	r=   wrappedCallable[..., R]instanceargstuple[Any, ...]r   dict[str, Any]c                 C  s    | |i |}t |tjd |S N)r,   r=   loggingWARNING)r>   r@   rA   r   resultr   r   r   _instrumented_connectv   s   rI   connectmodule	type[Any]namestrc                 C  s   t | |t dS )zBInstrument additional connect() methods, e.g. for derived classes.N)wraptwrap_function_wrapperrI   )rK   rM   r   r   r   instrument_connect   s
   rQ   c                  C  s,   ddd} t tjjd|  ttjj d S )Nr>   r?   r@   r+   rA   rB   r   rC   r   r   c                 S  s,   t |}| |i |}|st |tjd |S rD   rE   )r>   r@   rA   r   donerH   r   r   r   instrumented_send   s
   z&_instrument.<locals>.instrumented_sendsend
r>   r?   r@   r+   rA   rB   r   rC   r   r   )rO   rP   httpclientHTTPConnectionrQ   )rS   r   r   r   r      s   
r   c                   @  s   e Zd ZU ded< dS )_ConnectionStater   r.   N)r   r   r   __annotations__r   r   r   r   rY      s   
 rY   _ConnectionState | Nonec                   C  s   t tttS r   )r   rY   r   	get_value
_STATE_KEYr   r   r   r   r1      s   r1   r<   r   c                 c  s    t  }|s$tttd| gi}zd V  W t| d S t| w |d }||  zd V  W z||  W d S  tyD   Y d S w z||  W w  tyU   Y w w )Nr.   )	r1   r   attach	set_valuer]   detachappendremove
ValueError)r<   r9   tokenspansr   r   r   set_ip_on_next_http_connection   s.   
rf   c                   C  s    t tjjd t tjjd d S )NrT   rJ   )r
   rV   rW   rX   r   r   r   r   r      s   r   )r   r   r   r    )r*   r+   r,   r-   r   r    rU   )rJ   )rK   rL   rM   rN   )r   r[   )r<   r   )+__doc__
__future__r   
contextlibhttp.clientrV   rF   sockettypingr   r   r   r   r   rO   opentelemetryr   *opentelemetry.instrumentation.instrumentorr	   #opentelemetry.instrumentation.utilsr
   opentelemetry.semconv.tracer   opentelemetry.trace.spanr   r]   	getLoggerr   r$   TypeVarr   r   r)   DEBUGr=   rI   rQ   r   rY   r1   contextmanagerrf   r   r   r   r   r   <module>   s:   



,

