o
    i!                     @   s$  d dl Z d dlmZ d dlmZ d dlmZ d dlm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 d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ eeZejdkrtdee
j fddZn
dee
j fddZG dd deZ G dd dZ!e! Z"dS )    N)defaultdict)deque)entry_points)chain)Protocol)forksafe)
get_logger)DDConfig)report_configuration)telemetry_writer)check_uwsgi)uWSGIConfigDeprecationWarning)uWSGIConfigError)uWSGIMasterProcess)   
   returnc                   C   s   t tddS )Nddtrace.productsgroup)listr    r   r   M/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/products.pyget_product_entry_points   s   r   c                   C   s   dd t   D S )Nc                 S   s(   g | ]\}}|D ]	}|j d kr|qqS )r   r   ).0_epsepr   r   r   
<listcomp>    s   ( z,get_product_entry_points.<locals>.<listcomp>)r   itemsr   r   r   r   r      s   c                   @   sj   e Zd ZU ee ed< dddZdddZdd	eddfd
dZ	dd	eddfddZ
dd	eddfddZdS )Productrequiresr   Nc                 C      d S Nr   selfr   r   r   post_preload&       zProduct.post_preloadc                 C   r"   r#   r   r$   r   r   r   start(   r'   zProduct.startFjoinc                 C   r"   r#   r   r%   r)   r   r   r   restart*   r'   zProduct.restartc                 C   r"   r#   r   r*   r   r   r   stop,   r'   zProduct.stopc                 C   r"   r#   r   r*   r   r   r   at_exit.   r'   zProduct.at_exitr   NF)__name__
__module____qualname__r   str__annotations__r&   r(   boolr+   r,   r-   r   r   r   r   r    #   s   
 

r    c                   @   s   e Zd ZU i Zeeef ed< d#ddZd#ddZ	de
eeef  fdd	Zede
eeef  fd
dZd#ddZd#ddZd$deddfddZd$deddfddZd$deddfddZd#ddZd#ddZd#ddZd%ded edefd!d"ZdS )&ProductManager__products__r   Nc                 C   s   d | _ t | _d S r#   )	_productsset_failedr$   r   r   r   __init__4   s   zProductManager.__init__c              	   C   s   t  D ]A}|j}td| z| }W n ty)   td| | j| Y qw t	t
|dd  }tr9t| td| || j|< qd S )NzDiscovered product plugin '%s'z"Failed to load product plugin '%s'configz'Product plugin '%s' loaded successfully)r   namelogdebugload	Exception	exceptionr:   add
isinstancegetattrr	   r
   r7   )r%   product_pluginr=   productr<   r   r   r   _load_products8   s   
zProductManager._load_productsc           
         s   t  }tt}i  tj dd jD D ]$\}}t|dg }|s)|| qt	| |< |D ]	}|| | q1qg }|re|
 }|| || D ]}	 |	 |  |	 sb||	  |	= qM|s@ rqtdt    fdd|D S )Nc                 s   s    | ]}|d fV  qd S r#   r   )r   pr   r   r   	<genexpr>U   s    z0ProductManager._sort_products.<locals>.<genexpr>r!   zSCircular dependencies among products detected. These products won't be enabled: %s.c                    s,   g | ]}| vr|j v r|j | fqS r   )r7   )r   r=   fr%   r   r   r   n   s   , z1ProductManager._sort_products.<locals>.<listcomp>)r   r   r   r   r7   r   r:   rE   appendr9   popleftremover>   errorkeys)
r%   qgr=   rG   product_requiresrorderingnrI   r   rK   r   _sort_productsM   s6   $

	zProductManager._sort_productsc                 C   s   | j d u r
|  | _ | j S r#   )r8   rX   r$   r   r   r   productsp   s   

zProductManager.productsc              	   C   s   t  }| jD ]G\}}|t t|dg @ }|r#td|| || qz|  td| t	|
ddd W q tyM   td| || Y qw d S )Nr!   zGProduct '%s' won't start because these dependencies failed to start: %szStarted product '%s'-r   TzFailed to start product '%s')r9   rY   rE   r>   rP   rC   r(   r?   r   product_activatedreplacerA   rB   )r%   failedr=   rG   failed_requirementsr   r   r   start_productsv   s$   
zProductManager.start_productsc              	   C   s`   | j D ]*\}}zt|dd  }d u rW q|  td| W q ty-   td| Y qw d S )Nbefore_forkz*Before-fork hook for product '%s' executedz3Failed to execute before-fork hook for product '%s')rY   rE   r>   r?   rA   rB   )r%   r=   rG   hookr   r   r   r`      s   zProductManager.before_forkFr)   c              	   C   s   t  }| jD ]:\}}|t t|dg @ }|r#td|| || qz|j|d td| W q ty@   t	d| Y qw d S )Nr!   zKProduct '%s' won't restart because these dependencies failed to restart: %sr)   zRestarted product '%s'zFailed to restart product '%s')
r9   rY   rE   r>   rP   rC   r+   r?   rA   rB   )r%   r)   r]   r=   rG   r^   r   r   r   restart_products   s$   
zProductManager.restart_productsc              	   C   sf   t | jD ]+\}}z|j|d td| t|ddd W q ty0   t	d| Y qw d S )Nrb   zStopped product '%s'rZ   r   FzFailed to stop product '%s')
reversedrY   r,   r>   r?   r   r[   r\   rA   rB   r%   r)   r=   rG   r   r   r   stop_products   s   zProductManager.stop_productsc              	   C   sR   t | jD ]!\}}ztd| |j|d W q ty&   td| Y qw d S )NzExiting product '%s'rb   zFailed to exit product '%s')rd   rY   r>   r?   r-   rA   rB   re   r   r   r   exit_products   s   zProductManager.exit_productsc              	   C   sJ   | j D ]\}}z|  td| W q ty"   td| Y qw d S )NzPost-preload product '%s' donez#Failed to post_preload product '%s')rY   r&   r>   r?   rA   rB   )r%   r=   rG   r   r   r   post_preload_products   s   z$ProductManager.post_preload_productsc                 C   s0   |    t| j t| j t| j d S r#   )r_   r   register_before_forkr`   registerrc   atexitrg   r$   r   r   r   _do_products   s   zProductManager._do_productsc                    s      zttjd W nG ty!   tjd
 fdd Y d S  ty1   tjddd Y d S  t	yE   tj
ddd   Y d S  tyS   td	 Y d S w   d S )N)worker_callbackr   c                      s      t  d S r#   )rl   r   
unregisterr   r   r%   r   r   r      s   z&ProductManager.run_protocol.<locals>._zuWSGI configuration errorT)exc_infoz'uWSGI configuration deprecation warningz#Failed to check uWSGI configurationr.   )rH   r   r   ddtrace_after_in_childr   rj   r   r>   rP   r   warningrl   rA   rB   r$   r   ro   r   run_protocol   s   zProductManager.run_protocolenabledproduct_nameenabled_attributec                 C   s<   | j | }d u rdS t|dd  }d u rdS t||dS )NFr<   )r7   getrE   )r%   ru   rv   rG   r<   r   r   r   
is_enabled   s
   zProductManager.is_enabledr.   r/   )rt   )r0   r1   r2   r7   dictr3   r    r4   r;   rH   r   tuplerX   propertyrY   r_   r`   r5   rc   rf   rg   rh   rl   rs   rx   r   r   r   r   r6   1   s    
 

#


	


r6   )#rk   collectionsr   r   importlib.metadatar   	itertoolsr   systypingtr   ddtrace.internalr   ddtrace.internal.loggerr   ddtrace.internal.settings._corer	   ddtrace.internal.telemetryr
   r   ddtrace.internal.uwsgir   r   r   r   r0   r>   version_infor   Anyr   r    r6   managerr   r   r   r   <module>   s2    
 
E