o
    ;i+                     @   s   d dl Zd dlZd dl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
mZ d dlmZ d dlmZ dZdZdZdZG d	d
 d
ejjZG dd dejjZdefddZdefddZdd ZdS )    N)	find_specmodule_from_spec)pack)loggermodule_load_startmodule_load_endz<I   c                   @   sD   e Zd Zdd Zdd Zdd Zdedefd	d
ZdefddZ	dS )InterceptedModuleLoaderc                 C   s   || _ || _|| _d S N)nameloaderinterceptor)selfr   r   r    r   L/home/ubuntu/.local/lib/python3.10/site-packages/modal/_runtime/telemetry.py__init__   s   
z InterceptedModuleLoader.__init__c              	   C   s@   | j d u rd S z| j | W | j| j d S | j| j w r
   )r   exec_moduler   load_endr   )r   moduler   r   r   r      s
   
"z#InterceptedModuleLoader.exec_modulec                 C   s   | j |_ t|}| |_ |S r
   )r   r   )r   specr   r   r   r   create_module%   s   z%InterceptedModuleLoader.create_modulepathreturnc                 C      | j |S )a$  
        Implementation is required to support pkgutil.get_data.

        > If the package cannot be located or loaded, or it uses a loader which does
        > not support get_data, then None is returned.

        ref: https://docs.python.org/3/library/pkgutil.html#pkgutil.get_data
        )r   get_data)r   r   r   r   r   r   +      	z InterceptedModuleLoader.get_datafullnamec                 C   r   )u  
        Support reading a binary artifact that is shipped within a package.

        > Loaders that wish to support resource reading are expected to provide a method called
        > get_resource_reader(fullname) which returns an object implementing this ABC’s interface.

        ref: docs.python.org/3.10/library/importlib.html?highlight=traversableresources#importlib.abc.ResourceReader
        )r   get_resource_reader)r   r   r   r   r   r   6   r   z+InterceptedModuleLoader.get_resource_readerN)
__name__
__module____qualname__r   r   r   strbytesr   r   r   r   r   r   r	      s    r	   c                   @   s   e Zd ZU eeeeef f ed< ejed< e	j
ed< ededd fddZdejfdd	ZdddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd
S )ImportInterceptorloadingtracing_socketeventssocket_filenamer   c                 C   s"   t  t jt j}|| | |S r
   )socketAF_UNIXSOCK_STREAMconnect)clsr'   r%   r   r   r   r+   G   s   
zImportInterceptor.connectc                 C   s6   i | _ || _tjdd| _tj| jdd}|  d S )Ni @  )maxsizeT)targetdaemon)	r$   r%   queueQueuer&   	threadingThread_sendstart)r   r%   senderr   r   r   r   M   s
   zImportInterceptor.__init__Nc                 C   sJ   || j v rd S | | t|}|d u r| | d S t||j| |_|S r
   )r$   
load_startr   r   r	   r   )r   r   r   r.   r   r   r   r   r   T   s   


zImportInterceptor.find_specc                 C   sB   t  }tt }| |t   td|id ||f| j|< d S )Nr   span_id	timestampevent
attributes)time	monotonicr!   uuiduuid4emitMODULE_LOAD_STARTr$   )r   r   t0r9   r   r   r   r7   _   s   zImportInterceptor.load_startc                 C   sL   | j |d\}}|d u rd S t | }| |t t||dd d S )N)NN)r   latencyr8   )r$   popr=   r>   rA   MODULE_LOAD_END)r   r   r9   rC   rD   r   r   r   r   g   s   zImportInterceptor.load_endc                 C   s4   z	| j | W d S  tjy   td Y d S w )Nz failed to emit event: queue full)r&   
put_nowaitr0   Fullr   debug)r   r;   r   r   r   rA   x   s
   zImportInterceptor.emitc              
   C   s   	 | j  }z
t|d}W n ty* } ztd|  W Y d }~q d }~ww ztt	t
|}| j||  W n tyV } ztd|  W Y d }~nd }~ww q)NTzutf-8zfailed to serialize event: zfailed to send event: )r&   getjsondumpsencodeBaseExceptionr   rI   r   MESSAGE_HEADER_FORMATlenr%   sendOSError)r   r;   msgeencoded_lenr   r   r   r4   ~   s"   
zImportInterceptor._sendc                 C   s   | gt j t _d S r
   )sys	meta_pathr   r   r   r   install   s   zImportInterceptor.installc                 C   s   t j|  d S r
   )rV   rW   removerX   r   r   r   rZ      s   zImportInterceptor.removec                 C      |    d S r
   )rY   rX   r   r   r   	__enter__      zImportInterceptor.__enter__c                 C   r[   r
   )rZ   )r   exc_typeexc_valexc_tbr   r   r   __exit__   r]   zImportInterceptor.__exit__r
   )r   r   r    dictr!   tuplefloat__annotations__r(   r0   r1   classmethodr+   r   r   r7   r   rA   r4   rY   rZ   r\   ra   r   r   r   r   r#   B   s    
 


r#   r'   c                 C   s*   t  s
td d S t| }|  d S )Nz/unsupported platform, not instrumenting imports)supported_platformr   rI   r#   r+   rY   )r'   r   r   r   r   _instrument_imports   s
   

rh   c              
   C   sF   zt |  W d S  ty" } ztd|  W Y d }~d S d }~ww )Nzfailed to instrument imports: )rh   rN   r   warning)r'   rT   r   r   r   instrument_imports   s   rj   c                   C   s
   t jdv S )N)linuxdarwin)rV   platformr   r   r   r   rg      s   
rg   )importlib.abc	importlibrK   r0   r(   rV   r2   r=   r?   importlib.utilr   r   structr   modal.configr   rB   rF   rO   MESSAGE_HEADER_LENabcLoaderr	   MetaPathFinderr#   r!   rh   rj   rg   r   r   r   r   <module>   s(   +W