
    gE                        d 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 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j(                  e      Z G d
 de      Zdej0                  e   fdZej4                  fdej6                  j8                  defdZdej6                  j8                  fdZddZ d Z!dejD                  e#   fdZ$ejJ                  defd       Z&d Z'y)zp
This library provides functionality to enrich HTTP client spans with IPs. It does
not create spans on its own.
    N)
Collection)context)BaseInstrumentor)unwrap)SpanAttributes)Spanhttpbase_instrumentation_statec                   *    e Zd Zdee   fdZd Zd Zy)HttpClientInstrumentorreturnc                      y)N r   )selfs    T/var/www/openai/venv/lib/python3.12/site-packages/opentelemetry/util/http/httplib.pyinstrumentation_dependenciesz3HttpClientInstrumentor.instrumentation_dependencies)   s        c                     t                y)zBInstruments the http.client module (not creating spans on its own)N)_instrumentr   kwargss     r   r   z"HttpClientInstrumentor._instrument,   s    r   c                     t                y N)_uninstrumentr   s     r   r   z$HttpClientInstrumentor._uninstrument0   s    r   N)__name__
__module____qualname__r   strr   r   r   r   r   r   r   r   (   s    jo r   r   spanlistc                 "   t        |       dz
  }|dk\  r|| |   j                         s^t        j                  d| |          |dz   t        |       k(  }|s| t        |       dz
     | |<   | j	                          |r|dk(  ry|dz  }n|dz  }|dk\  r|y)N   r   zSpan is not recording: %sFT)lenis_recordingloggerdebugpop)r   idxislasts      r   _remove_nonrecordingr(   4   s    
h-!
C
(}))+LL4hsmD1WH-F (X): ;LLN!8 q1HC ( r   connr   c                    t               }|sy|j                  d      }|syt        |      syd}	 | j                  }t        j                  d|       |y|j                         }|r
|d   r|d   }|D ]"  }|j                  t        j                         $ y# t        $ r t        j                  |d|dd       Y yw xY w)	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: %sFr   z"Failed to get peer address from %s)exc_info
stack_info)	_getstategetr(   sockr#   r$   getpeernameset_attributer   NET_PEER_IP	Exceptionlog)r)   loglevelstater   r0   addripspans           r   trysetipr;   F   s     KE"'))I"6H  )$D?/3yy%t,<!DGaB D~992>   


0 	 	
 
s   $B B #C ?C instancec                 L     | |i |}t        |t        j                         |S N)r6   r;   loggingWARNING)wrappedr<   argsr   results        r   _instrumented_connectrE   p   s&     d%f%FX0Mr   c                 :    t        j                  | |t               y)zBInstrument additional connect() methods, e.g. for derived classes.N)wraptwrap_function_wrapperrE   )modulenames     r   instrument_connectrK   x   s     
r   c                      dt         j                  j                  fd} t        j                  t         j                  j                  d|        t        t         j                  j                         y )Nr<   c                 f    t        |      } | |i |}|st        |t        j                         |S r>   r?   )rB   r<   rC   r   donerD   s         r   instrumented_sendz&_instrument.<locals>.instrumented_send   s3     !$)&)X8r   send)httpclientHTTPConnectionrG   rH   rK   )rO   s    r   r   r      sP    ;;55 
"" t{{112r   c                  4    t        j                  t              S r   )r   	get_value
_STATE_KEYr   r   r   r.   r.      s    Z((r   r:   c              #     K   t               }|sKt        j                  t        j                  t        d| gi            }	 d  t        j
                  |       y |d   }|j                  |        	 d  	 |j                  |        y # t        j
                  |       w xY w# t        $ r Y y w xY w# 	 |j                  |        w # t        $ r Y w w xY wxY ww)Nr+   )	r.   r   attach	set_valuerV   detachappendremove
ValueError)r:   r7   tokenspanss       r   set_ip_on_next_http_connectionr`      s     KEj9tf*=>
	"NN5!#(#3T	T" NN5!  T" sw   =C B ,C1B1 6B" CBC"	B.+C-B..C1C3CC	CCCCCc                      t        t        j                  j                  d       t        t        j                  j                  d       y )NrP   connect)r   rQ   rR   rS   r   r   r   r   r      s*    
4;;%%v.
4;;%%y1r   )rb   )(__doc__
contextlibhttp.clientrQ   r@   sockettypingr   rG   opentelemetryr   *opentelemetry.instrumentation.instrumentorr   #opentelemetry.instrumentation.utilsr   opentelemetry.semconv.tracer   opentelemetry.trace.spanr   rV   	getLoggerr   r#   r   Listr(   DEBUGrR   rS   boolr;   rE   rK   r   Optionaldictr.   contextmanagerr`   r   r   r   r   <module>rt      s   
        ! G 6 6 )-
			8	$	- 	6;;t#4 $ 9@ '4;;-- '$ 'T{{113()6??4( )   ,2r   