
    g                    6   d dl m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m	Z	 d dl
mZ d dlmZmZmZ ddlmZ dd	lmZmZmZ dd
lmZ erddlmZ  e e	ej6                        j8                  j;                               ZddZ G d ded      Z ddZ!ddZ"y)    )annotationsN)contextmanager)Path)CoroutineType)TYPE_CHECKINGAnyContextManager   )ONE_SECOND_IN_NANOSECONDS)	StackInfoget_code_object_infoget_stack_info_from_frame)	safe_repr)Logfirec                B    t         j                  j                  j                   j	                  d        j
                  j                  j                  t        z  d fd}|t         j                  j                  _        t        fd       } |       S )zLog a warning whenever a function running in the asyncio event loop blocks for too long.

    See Logfire.log_slow_async_callbacks.
    Inspired by https://gitlab.com/quantlane/libs/aiodebug.
    asyncio)custom_scope_suffixc                           } |       }        |z
  }|k\  r6	 |t         z  }| j                  } j                  	 dd|it        |       |S |S # t        $ r' 	 j                  d       Y |S # t        $ r Y Y |S w xY ww xY w)NdurationzError in log_slow_callbacks)z/Async {name} blocked for {duration:.3f} seconds)r   	_callbackwarn_callback_attributes	Exception	exception)	self
start_timereturn_valuer   callbacklogfireoriginal_runslow_durationtimers	        M/var/www/openai/venv/lib/python3.12/site-packages/logfire/_internal/async_.pypatched_runz'log_slow_callbacks.<locals>.patched_run!   s    W
#D)7Z'}$55 $E% +84 |  %%&CD  ! 	s)   3A 	B"A66	B?BBBc               3     K   	 d   t         j                  j                  _        y #  t         j                  j                  _        w xY wwN)r   eventsHandle_run)r    s   r#   patch_contextz)log_slow_callbacks.<locals>.patch_context9   s3     
	6)5GNN!!&GNN!!&s   A)  A!A

A)r   zasyncio.events.Handlereturnr   )
r   r'   r(   r)   with_settingsconfigadvancedns_timestamp_generatorr   r   )r   r!   r$   r*   r    r"   s   ``  @@r#   log_slow_callbacksr0      s     >>((--L##	#BGNN##::E..M , "-GNN6 6 ?    c                  "    e Zd ZU ded<   ded<   y)_CallbackAttributesstrnamezlist[StackInfo]stackN)__name__
__module____qualname____annotations__ r1   r#   r3   r3   F   s    
Ir1   r3   F)totalc                ^    | j                   x}rt        |      S t        | j                        S r&   )cr_framer   r   cr_code)coroframes     r#   stack_info_from_coroutinerB   K   s-    u(// $DLL11r1   c                <   t        | dd       }t        |t        j                  j                        r|j                         }dd|j                          i}t        |t              s|S t        |      }i ||}|j                  d      x}r|dxx   d| dz  cc<   |g}t        |j                  x}t              r^t        |      }|j                  dd      j                  t              s|j                  |       t        |j                  x}t              r^||d	<   |S t        j                  |       } i }t        | d
d       }|ri t!        |      }t        | dd      xs! t        | dd      xs |j                  dd      }|xs t#        |       }d| |d<   |S )N__self__r5   ztask zcode.functionz ()zcode.filepath r6   __code__r9   r7   z	callback )getattr
isinstancer   tasksTaskget_coroget_namer   rB   getcr_await
startswithASYNCIO_PATHappendinspectunwrapr   r   )	r   taskr@   result
stack_infofunction_namer6   coder5   s	            r#   r   r   T   s   8Z.D$**+ MMO'-t}}6G/H&I$.M.t4
)F)j)&NN?;;=;6N=/33N .>248J>>/26AA,OZ(	 .>
  w ~~h'H"$F8Z.D/(./."-u:r1RuV\V`V`aprtVu 	 &9X&D 'F6NMr1   )r   r   r!   floatr+   zContextManager[None])r@   zCoroutineType[Any, Any, Any]r+   r   )r   r   r+   r3   )#
__future__r   r   asyncio.eventsasyncio.tasksrS   
contextlibr   pathlibr   typesr   typingr   r   r	   	constantsr   rW   r   r   r   utilsr   mainr   r4   __file__parentabsoluterQ   r0   r3   rB   r   r;   r1   r#   <module>rh      sw    "     %   5 5 0 R R 4(()0099;<-`)5 
2&r1   