o
    i                     @  s  d dl mZ d dlZd dlmZmZmZmZ d dlZd dl	m
Z
 d dlmZmZmZ d dlmZmZmZ d dlmZ zd dlmZ W n eyM   ed	w d d
lmZmZ d dlmZ d dlmZm Z m!Z!m"Z" ertd dlm#Z# e#dZ$dBddZ%G dd dZ&ej'd d d d!G d"d# d#ee&Z(ej'd d d$G d%d& d&e&Z)dCd(d)Z*dDd*d+Z+dEd0d1Z,dFd4d5Z-dGd:d;Z.dHd@dAZ/dS )I    )annotationsN)TYPE_CHECKINGAnyCallableLiteral)ClientResponse)TraceRequestEndParamsTraceRequestExceptionParamsTraceRequestStartParams)NonRecordingSpanSpanuse_span)URL)AioHttpClientInstrumentorz`logfire.instrument_aiohttp_client()` requires the `opentelemetry-instrumentation-aiohttp-client` package.
You can install this with:
    pip install 'logfire[aiohttp-client]')LogfireLogfireSpan)handle_internal_errors)AioHttpRequestHeadersAioHttpResponseHeadersRequestHookResponseHook)	ParamSpecPlogfire_instancer   capture_response_bodyboolcapture_headersrequest_hookRequestHook | Noneresponse_hookResponseHook | Nonekwargsr   returnNonec              
   K  sL   | j dd} t jdi | j t||t|| ||| j d| dS )zInstrument the `aiohttp` module so that spans are automatically created for each client request.

    See the `Logfire.instrument_aiohttp_client` method for details.
    aiohttp_client)custom_scope_suffix)tracer_providerr   r   meter_providerN )with_settingsr   
instrumentconfigget_tracer_providermake_request_hookmake_response_hookget_meter_provider)r   r   r   r   r   r!   r(   r(   s/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/logfire/_internal/integrations/aiohttp_client.pyinstrument_aiohttp_client   s   


r1   c                   @  s   e Zd ZU ded< dS )LogfireClientInfoMixinr   headersN)__name__
__module____qualname____annotations__r(   r(   r(   r0   r2   =   s   
 r2   T)auto_attribsfrozenslotsc                   @  s   e Zd ZU ded< dd ZdS )LogfireAioHttpRequestInfor   spanc                 C  s   t | j| jd d S )Nrequest)#capture_request_or_response_headersr<   r3   selfr(   r(   r0   r   E   s   z)LogfireAioHttpRequestInfo.capture_headersN)r4   r5   r6   r7   r   r(   r(   r(   r0   r;   A   s   
 r;   )r8   r:   c                   @  s~   e Zd ZU ded< ded< ded< ded< d	ed
< ded< ded< dZded< dd Zd#d$ddZd%ddZed&d d!Z	d"S )'LogfireAioHttpResponseInfor   r<   strmethodr   urlr   r3   zClientResponse | NoneresponsezBaseException | None	exceptionr   r   Fr   body_capturedc                 C  s    | j rt| j| j jd d S d S )NrE   )rE   r>   r<   r3   r?   r(   r(   r0   r   T   s   z*LogfireAioHttpResponseInfo.capture_headershttp.response.body.text	attr_namer"   r#   c                   s@   j d u r	d S jtd fdd}|_d S )Nr"   bytesc                    s   j r
 I d H S ttj c jdL}  I d H }z }||}W n tt	fyJ   d_ | Y W  d    W  d    S w j
| | d d_ |W  d    W  d    S 1 siw   Y  W d    d S 1 syw   Y  d S )NzReading response bodyT)textrI   )rG   r   r   r<   get_span_contextr   get_encodingdecodeUnicodeDecodeErrorLookupErrorcapture_text_as_json)r<   bodyencodingrK   rI   original_readrE   r@   r(   r0   read_   s*   
Rz=LogfireAioHttpResponseInfo.capture_body_if_text.<locals>.read)r"   rJ   )rE   rV   	functoolswraps)r@   rI   rV   r(   rT   r0   capture_body_if_textX   s   
z/LogfireAioHttpResponseInfo.capture_body_if_textr   rK   c                C  s   | |i  |j || d S N)set_attribute_span)r@   r<   rK   rI   r(   r(   r0   rQ   u   s   z/LogfireAioHttpResponseInfo.capture_text_as_jsonparams3TraceRequestEndParams | TraceRequestExceptionParamsc              
   C  s,   | ||j |j|jt|dd t|dd |dS )NrE   rF   )r<   rC   rD   r3   rE   rF   r   )rC   rD   r3   getattr)clsr<   r]   r   r(   r(   r0   create_from_trace_paramsy   s   

z3LogfireAioHttpResponseInfo.create_from_trace_paramsN)rH   )rI   rB   r"   r#   )r<   r   rK   rB   rI   rB   r"   r#   )r<   r   r]   r^   r   r   r"   rA   )
r4   r5   r6   r7   rG   r   rY   rQ   classmethodra   r(   r(   r(   r0   rA   I   s   
 
rA   hookc                   s     ssd S d	 fdd}|S )
Nr<   r   r=   r
   r"   r#   c                   sB   t  t| |  t| | W d    d S 1 sw   Y  d S rZ   )r   capture_requestrun_hook)r<   r=   r   rc   r(   r0   new_hook   s   "z#make_request_hook.<locals>.new_hook)r<   r   r=   r
   r"   r#   r(   )rc   r   rg   r(   rf   r0   r-      s   r-   c                   s(    sssd S d	 fdd}|S )
Nr<   r   rE   r^   r"   r#   c                   sF   t  t| |  t| | W d    d S 1 sw   Y  d S rZ   )r   capture_responsere   )r<   rE   r   r   rc   r   r(   r0   rg      s   "z$make_response_hook.<locals>.new_hook)r<   r   rE   r^   r"   r#   r(   )rc   r   r   r   rg   r(   ri   r0   r.      s   r.   r<   r   r=   r
   c                 C  s&   t |j|j|j| d}|r|  |S )N)rC   rD   r3   r<   )r;   rC   rD   r3   r   )r<   r=   r   request_infor(   r(   r0   rd      s   rd   rE   r^   c                 C  s,   t j| ||d}|r|  |r|  |S )N)r<   r]   r   )rA   ra   r   rY   )r<   rE   r   r   r   response_infor(   r(   r0   rh      s   rh   Callable[P, Any] | NoneargsP.argsP.kwargsc                 O  s   | r| |i | d S d S rZ   r(   )rc   rm   r!   r(   r(   r0   re      s   re   r3   .AioHttpRequestHeaders | AioHttpResponseHeaders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.)getall).0header_namer3   rq   r(   r0   
<dictcomp>   s    z7capture_request_or_response_headers.<locals>.<dictcomp>)set_attributeskeys)r<   r3   rq   r(   rv   r0   r>      s
   r>   )r   r   r   r   r   r   r   r   r   r    r!   r   r"   r#   )rc   r   r   r   r"   r   )
rc   r    r   r   r   r   r   r   r"   r    )r<   r   r=   r
   r   r   r"   r;   )r<   r   rE   r^   r   r   r   r   r   r   r"   rA   )rc   rl   rm   rn   r!   ro   r"   r#   )r<   r   r3   rp   rq   rr   r"   r#   )0
__future__r   rW   typingr   r   r   r   attraiohttp.client_reqrepr   aiohttp.tracingr   r	   r
   opentelemetry.tracer   r   r   yarlr   ,opentelemetry.instrumentation.aiohttp_clientr   ImportErrorRuntimeErrorlogfirer   r   logfire._internal.utilsr   #logfire.integrations.aiohttp_clientr   r   r   r   r   r   r1   r2   sr;   rA   r-   r.   rd   rh   re   r>   r(   r(   r(   r0   <module>   sB    

A



