o
    iLD                     @  s  d dl mZ d dlZd dlZd dlZd dlmZm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mZ d dlZd dlmZmZmZ d d	lmZ d d
lmZ zd dlmZ d dlm Z m!Z!m"Z"m#Z#m$Z$m%Z% W n e&yy   e'dw d dl(m)Z)m*Z* d dl+m,Z, d dl-m.Z. erd dlm/Z/ e/dZ0dgd)d*Z1G d+d, d,Z2G d-d. d.e#e2Z3G d/d0 d0e%e2Z4dhd4d5Z5did9d:Z6djd<d=Z7dkd>d?Z8dldDdEZ9dmdJdKZ:dndPdQZ;dndRdSZ<dodXdYZ=dZd[ ej>j?ej>j@ejAj?ejAj@fD ZBedpd_d`ZCdqdcddZDedrdedfZEdS )s    )annotationsN)	AwaitableMapping)ContentTypeHeader)EmailPolicy)cached_property	lru_cache)TYPE_CHECKINGAnyCallableLiteralcast)NonRecordingSpanSpanuse_span)GLOBAL_CONFIG)warn_at_user_stacklevel)HTTPXClientInstrumentor)AsyncRequestHookAsyncResponseHookRequestHookRequestInfoResponseHookResponseInfoz`logfire.instrument_httpx()` requires the `opentelemetry-instrumentation-httpx` package.
You can install this with:
    pip install 'logfire[httpx]')LogfireLogfireSpan)set_user_attributes_on_raw_span)handle_internal_errors)	ParamSpecPlogfire_instancer   client'httpx.Client | httpx.AsyncClient | Nonecapture_allbool | Nonecapture_headersboolcapture_request_bodycapture_response_bodyrequest_hook%RequestHook | AsyncRequestHook | Noneresponse_hook'ResponseHook | AsyncResponseHook | Noneasync_request_hookAsyncRequestHook | Noneasync_response_hookAsyncResponseHook | Nonekwargsr
   returnNonec
                 K  s  |r|s|s|rt dt |
d}|
d}|dur t dt |dur)t dt tttjd|}|p8|p8|}|p>|p>|}|pB|}|pF|}~~~~~~| j	
 | j	 d|
}~
t }| jd	d
} |du rtd|}td|}t||||d< t|||| |d< t||||d< t|	||| |d< |jdi | dS t|tjrt|||}t|||| }ntd|}td|}t|||}t|||| }|d }|d }t|||d}z|j|fd|i| W dS  ty   |j|fi | Y dS w )zInstrument the `httpx` module so that spans are automatically created for each request.

    See the `Logfire.instrument_httpx` method for details.
    zQYou should use either `capture_all` or the specific capture parameters, not both.capture_request_headerscapture_response_headersNzUThe `capture_request_headers` parameter is deprecated. Use `capture_headers` instead.zVThe `capture_response_headers` parameter is deprecated. Use `capture_headers` instead.httpx_capture_all)tracer_providermeter_providerhttpx)custom_scope_suffixRequestHook | NoneResponseHook | Noner)   r+   r-   r/   r7   r8   )r7   r)   r+    )r   UserWarninggetDeprecationWarningr   r&   r   param_manager
load_paramconfigget_tracer_providerget_meter_providerr   with_settingsmake_request_hookmake_response_hookmake_async_request_hookmake_async_response_hook
instrument
isinstancer9   AsyncClientdictinstrument_client	TypeError)r    r!   r#   r%   r'   r(   r)   r+   r-   r/   r1   r4   r5   should_capture_request_headersshould_capture_response_headersshould_capture_request_bodyshould_capture_response_bodyfinal_kwargsinstrumentorr7   r8   client_kwargsr=   r=   j/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/logfire/_internal/integrations/httpx.pyinstrument_httpx.   s   





rY   c                   @  s2   e Zd ZU ded< edddZeddd	Zd
S )LogfireHttpxInfoMixinhttpx.Headersheadersr2   r   c                 C  s
   t | jS N)content_type_header_from_stringcontent_type_header_stringselfr=   r=   rX   content_type_header_object   s   
z0LogfireHttpxInfoMixin.content_type_header_objectstrc                 C  s   | j ddS )Ncontent-type )r\   r?   r`   r=   r=   rX   r_      s   z0LogfireHttpxInfoMixin.content_type_header_stringN)r2   r   )r2   rc   )__name__
__module____qualname____annotations__propertyrb   r_   r=   r=   r=   rX   rZ      s   
 rZ   c                   @  s   e Zd ZU ded< dd Zdd Zd#d$d
dZd%d&ddZd'ddZe	dd Z
e	dd Ze	d(ddZed)ddZd*d d!Zd"S )+LogfireHttpxRequestInfor   spanc                 C     t | j| jd d S )Nrequest#capture_request_or_response_headersrl   r\   r`   r=   r=   rX   r%         z'LogfireHttpxRequestInfo.capture_headersc                 C  s   |   }|s|   d S d S r]   )capture_body_if_formcapture_body_if_text)ra   captured_formr=   r=   rX   capture_body   s   z$LogfireHttpxRequestInfo.capture_bodyhttp.request.body.text	attr_namerc   c              	   C  sV   | j s'| j}|r)z	| j| j}W n ttfy   Y d S w | j||d d S d S d S )Nrw   text)body_is_streamingcontentdecodecontent_type_charsetUnicodeDecodeErrorLookupErrorcapture_text_as_json)ra   rw   r{   ry   r=   r=   rX   rs      s   z,LogfireHttpxRequestInfo.capture_body_if_texthttp.request.body.formr2   r&   c                 C  s8   | j dksdS | j}|rt|tsdS | ||i dS )Nz!application/x-www-form-urlencodedFT)r_   	form_datarL   r   set_complex_span_attributes)ra   rw   datar=   r=   rX   rr      s   
z,LogfireHttpxRequestInfo.capture_body_if_formry   c                 C  s    |  |i i | j|| d S r]   )r   rl   set_attribute)ra   rw   ry   r=   r=   rX   r      s   z,LogfireHttpxRequestInfo.capture_text_as_jsonc                 C  s   t | jtj S r]   )rL   streamr9   
ByteStreamr`   r=   r=   rX   rz         z)LogfireHttpxRequestInfo.body_is_streamingc                 C  s   | j jddS )Ncharsetutf-8)rb   paramsr?   r`   r=   r=   rX   r}      r   z,LogfireHttpxRequestInfo.content_type_charsetbytesc                 C  s   | j rtdt| jd S )Nz)Cannot read content from a streaming bodyr   )rz   
ValueErrorlistr   r`   r=   r=   rX   r{      s   zLogfireHttpxRequestInfo.contentMapping[str, Any] | Nonec                 C  s8   t  jjj}|r|jtv rn|j}|s	d S |jdS )Nr   )inspectcurrentframef_backf_code!CODES_FOR_METHODS_WITH_DATA_PARAMf_localsr?   )ra   framer=   r=   rX   r      s   
z!LogfireHttpxRequestInfo.form_data
attributesdict[str, Any]c                 C  s   t | j| d S r]   )r   rl   )ra   r   r=   r=   rX   r      s   z3LogfireHttpxRequestInfo.set_complex_span_attributesN)rv   rw   rc   )r   )rw   rc   r2   r&   )rw   rc   ry   rc   r2   r   )r2   r   )r   r   )rf   rg   rh   ri   r%   ru   rs   rr   r   rj   rz   r}   r{   r   r   r   r=   r=   r=   rX   rk      s    
 




rk   c                   @  s~   e Zd ZU ded< ded< ded< dd Zd#d$ddZed%ddZd&ddZd'ddZ	d(ddZ
ejdd Zd)d d!Zd"S )*LogfireHttpxResponseInfor   rl   r   r    r&   is_asyncc                 C  rm   )Nresponsero   r`   r=   r=   rX   r%      rq   z(LogfireHttpxResponseInfo.capture_headershttp.response.body.textrw   rc   c                   s   d fdd} | d S )Nrl   r   c              	     sH   zj jj jp
d}W n ttfy   Y d S w j|  |d d S )Nr   rx   )r   r{   r|   encodingr~   r   r   )rl   ry   rw   ra   r=   rX   hook   s   z;LogfireHttpxResponseInfo.capture_body_if_text.<locals>.hook)rl   r   )on_response_read)ra   rw   r   r=   r   rX   rs      s   	z-LogfireHttpxResponseInfo.capture_body_if_textr2   httpx.Responsec                 C  s>   t  jj}|r|jd}|j}t|tjr|S |std)Nr   z"Could not find the response object)	r   r   r   r   r?   rL   r9   ResponseRuntimeError)ra   r   r   r=   r=   rX   r   	  s   z!LogfireHttpxResponseInfo.responser   Callable[[LogfireSpan], None]c                   sB   j rd fdd}| d S d fd	d
}| d S )Noriginal_areadCallable[[], Awaitable[bytes]]r2   r   c              	     s     1 jd}|  I d H } | W d    n1 s!w   Y  W d    |S W d    |S 1 s9w   Y  |S NzReading response bodyattach_original_span_contextr    rl   )r   rl   r{   r   ra   r=   rX   aread  s   
(z8LogfireHttpxResponseInfo.on_response_read.<locals>.areadoriginal_readCallable[[], bytes]c              	     sx     . jd}|  } | W d    n1 sw   Y  W d    |S W d    |S 1 s5w   Y  |S r   r   )r   rl   r{   r   r=   rX   read  s   
(z7LogfireHttpxResponseInfo.on_response_read.<locals>.read)r   r   r2   r   )r   r   r2   r   )r   wrap_response_areadwrap_response_read)ra   r   r   r   r=   r   rX   r     s
   z)LogfireHttpxResponseInfo.on_response_read&Callable[[Callable[[], bytes]], bytes]c                   2   | j jtd fdd}|_d S )Nr2   r   c                     s&   zj W S  tjy     Y S w r]   r{   r9   ResponseNotReadr=   r   r   r   r=   rX   r   +  s
   z9LogfireHttpxResponseInfo.wrap_response_read.<locals>.readr   )r   r   	functoolswraps)ra   r   r   r=   r   rX   r   '  
   
z+LogfireHttpxResponseInfo.wrap_response_read<Callable[[Callable[[], Awaitable[bytes]]], Awaitable[bytes]]c                   r   )Nr2   r   c                     s.   zj W S  tjy    I d H  Y S w r]   r   r=   r   r   r   r=   rX   r   9  s   z;LogfireHttpxResponseInfo.wrap_response_aread.<locals>.areadr   )r   r   r   r   )ra   r   r   r=   r   rX   r   5  r   z,LogfireHttpxResponseInfo.wrap_response_areadc                 c  s@    t t| j  d V  W d    d S 1 sw   Y  d S r]   )r   r   rl   get_span_contextr`   r=   r=   rX   r   C  s   "z5LogfireHttpxResponseInfo.attach_original_span_contextr   ry   c                C  s   | |i  |j || d S r]   )r   _span)ra   rl   ry   rw   r=   r=   rX   r   H  s   z-LogfireHttpxResponseInfo.capture_text_as_jsonN)r   r   )r2   r   )r   r   )r   r   )r   r   )rl   r   ry   rc   rw   rc   )rf   rg   rh   ri   r%   rs   r   r   r   r   r   
contextlibcontextmanagerr   r   r=   r=   r=   rX   r      s   
 
	


r   r   r;   ru   c                   s&   s ssd S d	 fdd}|S )
Nrl   r   rn   r   r2   r3   c                   sD   t  t| | }t| | W d    d S 1 sw   Y  d S r]   )r   capture_requestrun_hookrl   rn   ru   r%   r   r=   rX   new_hookR  s   "z#make_request_hook.<locals>.new_hookrl   r   rn   r   r2   r3   r=   )r   r%   ru   r   r=   r   rX   rG   N  s   rG   %AsyncRequestHook | RequestHook | Noneshould_capture_headersshould_capture_bodyc                   s&   ss sd S d	 fdd}|S )
Nrl   r   rn   r   r2   r3   c                   sL   t  t| |}t | |I d H  W d    d S 1 sw   Y  d S r]   )r   r   run_async_hookr   r   r   r   r=   rX   r   b  s
   "z)make_async_request_hook.<locals>.new_hookr   r=   )r   r   r   r   r=   r   rX   rI   Z  s   rI   r<   c                   s(   s ssd S d fd	d
}|S )Nrl   r   rn   r   r   r   r2   r3   c              
     sR   t  t| || dd\}}t| || W d    d S 1 s"w   Y  d S )NFr   )r   capture_responser   rl   rn   r   ru   r%   r   r    r=   rX   r   s  s   
	"z$make_response_hook.<locals>.new_hookrl   r   rn   r   r   r   r2   r3   r=   )r   r%   ru   r    r   r=   r   rX   rH   j     rH   c                   s(   ss sd S d fd	d
}|S )Nrl   r   rn   r   r   r   r2   r3   c              
     sZ   t   t| ||dd\}}t | ||I d H  W d    d S 1 s&w   Y  d S )NTr   )r   r   r   r   r   r    r   r   r=   rX   r     s   
	"z*make_async_response_hook.<locals>.new_hookr   r=   )r   r   r   r    r   r=   r   rX   rJ     r   rJ   rl   r   rn   r   c                 C  s*   t | }| |_|r|  |r|  |S r]   )rk   rl   r%   ru   )rl   rn   r   r   r=   r=   rX   r     s   r   r   r   r   8tuple[LogfireHttpxRequestInfo, LogfireHttpxResponseInfo]c                C  sH   t | }| |_t| }| |_||_||_|r|  |r |  ||fS r]   )rk   rl   r   r    r   r%   rs   )rl   rn   r   r    r%   ru   r   r=   r=   rX   r     s   
r   Callable[P, Any] | NoneargsP.argsP.kwargsc                   s>   | r| |i |}t |r|I d H }t |sd S d S d S r]   )r   isawaitable)r   r   r1   resultr=   r=   rX   r     s   

r   c                 O  s   | r| |i | d S d S r]   r=   )r   r   r1   r=   r=   rX   r     s   r   r\   r[   request_or_responseLiteral['request', 'response']c                   s"   |   fdd  D  d S )Nc                   s$   i | ]}d  d|   |qS )zhttp.z.header.)get_list).0header_namer\   r   r=   rX   
<dictcomp>  s    z7capture_request_or_response_headers.<locals>.<dictcomp>)set_attributeskeys)rl   r\   r   r=   r   rX   rp     s
   rp   c                 C  s   g | ]}t |jqS r=   )r   unwrap__code__)r   methodr=   r=   rX   
<listcomp>  s    
r   content_typerc   r   c                 C  s   t d| S )Nrd   )r   header_factory)r   r=   r=   rX   r^     s   r^   subtypeset[str]c                 C  s$   |  dr| dd  } t| dS )Nzx-   +)
startswithsetsplit)r   r=   r=   rX   content_type_subtypes  s   
r   c                 C  s    t | }|jdkodt|jv S )Napplicationjson)r^   maintyper   r   )r   headerr=   r=   rX   is_json_type  s   r   )r    r   r!   r"   r#   r$   r%   r&   r'   r&   r(   r&   r)   r*   r+   r,   r-   r.   r/   r0   r1   r
   r2   r3   )r   r;   r%   r&   ru   r&   r2   r;   )r   r   r   r&   r   r&   r2   r.   )
r   r<   r%   r&   ru   r&   r    r   r2   r<   )
r   r,   r   r&   r   r&   r    r   r2   r0   )
rl   r   rn   r   r   r&   r   r&   r2   rk   )rl   r   rn   r   r   r   r    r   r%   r&   ru   r&   r   r&   r2   r   )r   r   r   r   r1   r   r2   r3   )rl   r   r\   r[   r   r   r2   r3   )r   rc   r2   r   )r   rc   r2   r   )r   rc   r2   r&   )F
__future__r   r   r   r   collections.abcr   r   email.headerregistryr   email.policyr   r   r   typingr	   r
   r   r   r   r9   opentelemetry.tracer   r   r   logfire._internal.configr   logfire._internal.stack_infor   #opentelemetry.instrumentation.httpxr   logfire.integrations.httpxr   r   r   r   r   r   ImportErrorr   logfirer   r   logfire._internal.mainr   logfire._internal.utilsr   r   r   rY   rZ   rk   r   rG   rI   rH   rJ   r   r   r   r   rp   Clientrn   r   rM   r   r^   r   r   r=   r=   r=   rX   <module>   sh    $
zA
Y








