
    g}                        d dl Zd dlmZ d dlmZ d dlmZmZ d dl	m
Z
mZmZmZ d dlmZ d dlmZmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZ  e       ZdZ	 d"dedee   de
eef   de
eef   fdZ 	 	 d#de!de"de"defdZ#dee$ef   defdZ%	 d"dZ&defdZ'de(fdZ)d Z*de"fdZ+de"fdZ,ededed   fd       Z-eded   fd        Z.eded   fd!       Z/y)$    N)contextmanager)import_module)escapesub)DictIterableSequenceUnion)ObjectProxy)contexttrace)"_SUPPRESS_HTTP_INSTRUMENTATION_KEY_SUPPRESS_INSTRUMENTATION_KEY)extract)
StatusCode)TraceContextTextMapPropagatorsuppress_instrumentationobj
attributesexistingreturnc                 z    i }|r|j                  |       |D ]   }t        | |d       }|t        |      ||<   " |S N)updategetattrstr)r   r   r   	extractedattrvalues         X/var/www/openai/venv/lib/python3.12/site-packages/opentelemetry/instrumentation/utils.pyextract_attributes_from_objectr!   .   sM     I"T4(!%jIdO      statusallow_redirectserver_spanc                    t        | t              st        j                  S | dk  rt        j                  S | dk  rt        j                  S | dk  r|rt        j                  S | dk  r|rt        j                  S t        j                  S )z}Converts an HTTP status code to an OpenTelemetry canonical status code

    Args:
        status (int): HTTP status code
    d   i+  i  i  )
isinstanceintr   UNSETERROR)r#   r$   r%   s      r    http_status_to_status_coder,   ;   sv     fc"|}}}r"   r   c                    t        | t              r.	 | j                  dd      \  }}t        |      }	 t        ||      } t        | |d      }|r6t        |t              r%t        |d	      rt        | ||j                         yyyy# t        $ r}t	        d|  d      |d}~ww xY w# t        $ r}t	        d| d| d      |d}~ww xY w)
aM  Given a function that was wrapped by wrapt.wrap_function_wrapper, unwrap it

    The object containing the function to unwrap may be passed as dotted module path string.

    Args:
        obj: Object that holds a reference to the wrapped function or dotted import path as string
        attr (str): Name of the wrapped function
    .   zCannot parse 'z' as dotted import pathNzCannot import 'z' from ''__wrapped__)r(   r   rsplit
ValueErrorImportErrorr   r   AttributeErrorr   hasattrsetattrr1   )r   r   module_path
class_nameexcmodulefuncs          r    unwrapr=   T   s     #s	&)jja&8#K
 {+	&*-C 3d#D
4-'$2NT4++, 3O-t  	 %<=	  	!*XfXQ?	s.   B B& 	B#BB#&	C/CCc                 `   dx}x}}t        j                         t         j                  u r=t        ||      }t	        j
                  |      }t         j                  j                  }n.t	        j                         }t         j                  j                  }| j                  |||||      }	|	|fS )a  Returns internal or server span along with the token which can be used by caller to reset context


    Args:
        tracer : tracer in use by given instrumentation library
        span_name (string): name of the span
        start_time : start time of the span
        context_carrier : object which contains values that are
            used to construct a Context. This object
            must be paired with an appropriate getter
            which understands how to extract a value from it.
        context_getter : an object which contains a get function that can retrieve zero
            or more values from the carrier and a keys function that can get all the keys
            from carrier.
    N)getter)namer   kind
start_timer   )r   get_current_spanINVALID_SPANr   r   attachSpanKindSERVERget_currentINTERNAL
start_span)
tracer	span_namerB   context_carriercontext_getterr   tokenctx	span_kindspans
             r    _start_internal_or_server_spanrS   q   s    0 #"E"C)5#5#55on=s#NN))	!!#NN++	  D ;r"   c                     t        | t        t        f      s| S t        j                  j                  |       }|j                  dd      S )N%z%%)r(   r   bytesurllibparsequotereplace)squoteds     r    
_url_quoter]      s<    a#u&\\"F
 >>#t$$r"   c                  4    i } t         j                  |        | S )zs
    Return the OpenTelemetry Trace and Span IDs if Span ID is set in the
    OpenTelemetry execution context.
    )
propagatorinject)_headerss    r    _get_opentelemetry_valuesrb      s     HhOr"   c                 8    t        t        |       | dd|       S )Nz(?!$) )r   r   )python_path	directorypath_separators      r    _python_path_without_directoryrh      s*    9~.e4
 r"   c                  l    t        j                  t              xs t        j                  t               S r   )r   	get_valuer   #_SUPPRESS_INSTRUMENTATION_KEY_PLAIN r"   r    is_instrumentation_enabledrm      s0    78 	B@A r"   c                  N    t               xr t        j                  t               S r   )rm   r   rj   r   rl   r"   r    is_http_instrumentation_enabledro      s'    %' 0A0A*1 - r"   keysc               '     K   t        j                         }| D ]  }t        j                  |d|      } t        j                  |      }	 d t        j                  |       y# t        j                  |       w xY ww),Suppress instrumentation within the context.TN)r   rH   	set_valuerE   detach)rp   rP   keyrO   s       r    _suppress_instrumentationrv      sb      


CT3/ NN3Euus   AA?A% A?%A<<A?c               #   f   K   t        t        t              5  d ddd       y# 1 sw Y   yxY wwrr   N)rv   r   rk   rl   r"   r    r   r      s*      
#%'J
 	
 
 
s   1%	1.1c               #   \   K   t        t              5  d ddd       y# 1 sw Y   yxY wwrx   )rv   r   rl   r"   r    suppress_http_instrumentationrz      s       
##E	F 
G	F	Fs   , 	,),r   )TF)0urllib.parserW   
contextlibr   	importlibr   rer   r   typingr   r   r	   r
   wraptr   opentelemetryr   r   opentelemetry.contextr   r   opentelemetry.propagater   opentelemetry.tracer   ,opentelemetry.trace.propagation.tracecontextr   r_   rk   anyr   r!   r)   boolr,   objectr=   rS   r]   dictrb   rh   rm   ro   rv   r   rz   rl   r"   r    <module>r      sw    % #  2 2  ( , * +,
  $ EI
	
"3-
37S>
	#s(^
    	2-fck" -# -F 'T%S %4 D   	S 	Xd^ 	 	 (4.   x~  r"   