o
    i6                     @   sh  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	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 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&G dd de'Z(G dd dej)Z*dS )    N)Any)Callable)Mapping)Optional)Union)cast)configatexit)process_tags)service)uwsgi)ddup)ModuleWatchdog)
config_str)telemetry_writer)TELEMETRY_APM_PRODUCT)	collector)	scheduler)asyncio)memalloc)pytorch)stack)	threadingc                   @   s`   e Zd ZdZdededdfddZddd	ZddeddfddZdddZ	de
defddZdS )ProfilerzRun profiling while code is executed.

    Note that the whole Python process is profiled, not only the code executed. Data from all running threads are
    caught.

    argskwargsreturnNc                 O   s   t |i || _d S N)_ProfilerInstance	_profiler)selfr   r    r"   N/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/profiling/profiler.py__init__+   s   zProfiler.__init__c                 C   sx   zt j| j| jd W n t jy   Y dS  t jy'   tjddd Y dS w | j	  t
| j ttjd dS )Start the profiler.r	   Nz'uWSGI configuration deprecation warningTexc_info)r   check_uwsgi_start_on_forkstopuWSGIMasterProcessuWSGIConfigDeprecationWarningLOGwarningr    startr
   registerr   product_activatedr   PROFILERr!   r"   r"   r#   r/   .   s   
zProfiler.startTflushc                 C   sD   t | j z| j| ttjd W dS  tj	y!   Y dS w )zFStop the profiler.

        :param flush: Flush last profile.
        FN)
r
   
unregisterr*   r    r   r1   r   r2   r   ServiceStatusError)r!   r4   r"   r"   r#   r*   G   s   zProfiler.stopc                 C   s   | j   dS )zUStart a fresh profiler in child process after fork. This is needed for uWSGI support.N)r    r/   r3   r"   r"   r#   r)   T   s   zProfiler._start_on_forkkeyc                 C   s   t | j|S r   )getattrr    )r!   r7   r"   r"   r#   __getattr__Y   s   zProfiler.__getattr__r   N)T)__name__
__module____qualname____doc__r   r$   r/   boolr*   r)   strr9   r"   r"   r"   r#   r   #   s    

r   c                       s  e Zd ZdZddddejdejjej	jej
jejjejejfdee deeeef  dee dee dedee d	ed
ededededef fddZdedefddZd&ddZd&ddZd&ddZddhZd'ddZd&dd Zd(d"ed#eddfd$d%Z  ZS ))r   zQA instance of the profiler.

    Each process must manage its own instance.

    Nr   tagsenvversiontracerapi_key_memory_collector_enabled_stack_collector_enabled_lock_collector_enabled_pytorch_collector_enabledenable_code_provenanceendpoint_collection_enabledc                    s   t    |d ur|ntj| _|d ur|ntj| _|d ur|ntj| _|d ur)|ntj| _|| _|d ur6|ntj	| _
|| _|| _|	| _|
| _|| _|| _g | _d | _d | _tjd| _tjp`d | _|   d S )NAWS_LAMBDA_FUNCTION_NAME)superr$   r   r   profiling_configrA   rB   rC   rD   _dd_api_keyrE   rF   rG   rH   rI   rJ   rK   _collectors_collectors_on_import
_schedulerosenvironget_lambda_function_namer   __post_init__)r!   r   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   	__class__r"   r#   r$   d   s&   
z_ProfilerInstance.__init__otherr   c                 C   sF   t |  D ]\}}|ds|| jv rq|t||d kr  dS qdS )N_FT)varsitems
startswith_COPY_IGNORE_ATTRIBUTESr8   )r!   rZ   kvr"   r"   r#   __eq__   s   z_ProfilerInstance.__eq__c                 C   s   | j d ur| jd| j i tt}| jd|i | jj}| jr%|  t	j
| j| j| jtttttf tttf f | jtjtjtjtjtj| jd
 t	  d S )Nfunctionnameprofiler_config)
rB   r   rC   rA   max_nframestimeline_enabledoutput_filenamesample_pool_capacitytimeoutr   )rV   rA   updater   rN   rD   %_endpoint_call_counter_span_processorrK   enabler   r   rB   r   rC   r   r   r   r@   bytes
max_framesrf   output_pprofrh   api_timeout_msr   r/   )r!   rd   $endpoint_call_counter_span_processorr"   r"   r#   _build_default_exporters   s(   
"z*_ProfilerInstance._build_default_exportersc                    s   j r+td z jtj jd td W n ty*   tj	ddd Y nw  j
rdttj dd f fd	d
dfddfdfddfdfddfdfddfdfddfdfddfdfddfdfddfdfddfg	 _ jD ]
\}}t|| q jrdttj dd f fdd
dfddfg _ jD ]
\}}t|| q jr jt      jrtjntj}| j jd _d S )Nz#Profiling collector (stack) enabledrD   z'Profiling collector (stack) initializedz+Failed to start stack collector, disabling.Tr&   collector_classr   c              	      s    j W |  jd} jtjjkrLz|  td| W n. t	j
y4   td| Y W d    d S  tyK   tjd|dd Y W d    d S w  j| W d    d S 1 s]w   Y  d S )Nrs   zStarted collector %r&Collector %r is unavailable, disabling(Failed to start collector %r, disabling.Tr&   )_service_lockrD   statusr   ServiceStatusRUNNINGr/   r-   debugr   CollectorUnavailable	ExceptionerrorrP   appendrt   colr3   r"   r#   start_collector   s"   "z8_ProfilerInstance.__post_init__.<locals>.start_collectorr   c                    
    t jS r   )r   ThreadingLockCollectorr[   r   r"   r#   <lambda>      
 z1_ProfilerInstance.__post_init__.<locals>.<lambda>c                    r   r   )r   ThreadingRLockCollectorr   r   r"   r#   r      r   c                    r   r   )r   ThreadingSemaphoreCollectorr   r   r"   r#   r      r   c                    r   r   )r   "ThreadingBoundedSemaphoreCollectorr   r   r"   r#   r      r   c                    r   r   )r   ThreadingConditionCollectorr   r   r"   r#   r      r   r   c                    r   r   )r   AsyncioLockCollectorr   r   r"   r#   r      r   c                    r   r   )r   AsyncioSemaphoreCollectorr   r   r"   r#   r      r   c                    r   r   )r    AsyncioBoundedSemaphoreCollectorr   r   r"   r#   r      r   c                    r   r   )r   AsyncioConditionCollectorr   r   r"   r#   r      r   c              	      s    j T |  } jtjjkrIz|  td| W n. tj	y1   td| Y W d    d S  t
yH   tjd|dd Y W d    d S w  j| W d    d S 1 sZw   Y  d S )NzStarted pytorch collector %rz.Collector %r pytorch is unavailable, disablingz0Failed to start collector %r pytorch, disabling.Tr&   )rw   rx   r   ry   rz   r/   r-   r{   r   r|   r}   r~   rP   r   r   r3   r"   r#   r      s"   "torchc                    r   r   )r   TorchProfilerCollectorr   r   r"   r#   r      r   )before_flushrD   )rG   r-   r{   rP   r   r   StackCollectorrD   r}   r~   rH   typer   	CollectorrQ   r   register_module_hookrI   rF   r   MemoryCollectorrr   rV   r   ServerlessScheduler	Scheduler_collectors_snapshotrR   )r!   modulehookscheduler_classr"   )r!   r   r#   rW      sJ   
z_ProfilerInstance.__post_init__c              	   C   s>   | j D ]}z|  W q ty   tjd|dd Y qw d S )Nz%Error while snapshotting collector %rTr&   )rP   snapshotr}   r-   r~   )r!   cr"   r"   r#   r     s   
z&_ProfilerInstance._collectors_snapshotrx   r   c                    s&    j di  fddt  D S )Nc                    s*   i | ]\}}| d s| jvr||qS r   )r^   r_   ).0r7   valuer3   r"   r#   
<dictcomp>  s    
z*_ProfilerInstance.copy.<locals>.<dictcomp>r"   )rY   r\   r]   r3   r"   r3   r#   copy  s
   

z_ProfilerInstance.copyc              	   C   s   g }| j D ].}z|  W n  tjy   td| Y q ty-   tjd|dd Y qw || q|| _ | j	durC| j	  dS dS )r%   ru   rv   Tr&   N)
rP   r/   r   r|   r-   r{   r}   r~   r   rR   )r!   
collectorsr   r"   r"   r#   _start_service  s   

z _ProfilerInstance._start_serviceTr4   joinc              	   C   s   t d | jr$| jr$| jD ]\}}zt|| W q ty#   Y qw | jdur<| j  |r5| j	  |r<| j
  t| jD ]}z|  W qA tjyS   Y qAw |rbt| jD ]}|	  q[dS dS )zHStop the profiler.

        :param flush: Flush a last profile.
        zStopping profilerN)r-   r{   rH   rQ   r   unregister_module_hook
ValueErrorrR   r*   r   r4   reversedrP   r   r6   )r!   r4   r   r   r   r   r"   r"   r#   _stop_service)  s2   





z_ProfilerInstance._stop_servicer:   )r   r   )TT)r;   r<   r=   r>   ddtracerD   rN   memoryenabledr   lockr   code_provenanceendpoint_collectionr   r@   dictr   r?   r$   rb   rr   rW   r   r_   r   r   r   __classcell__r"   r"   rX   r#   r   ]   s^    	
*


V

	 r   )+loggingrS   typingr   r   r   r   r   r   r   r   ddtrace.internalr
   r   r   r   "ddtrace.internal.datadog.profilingr   ddtrace.internal.moduler   #ddtrace.internal.settings.profilingrN   r   ddtrace.internal.telemetryr   $ddtrace.internal.telemetry.constantsr   ddtrace.profilingr   r   ddtrace.profiling.collectorr   r   r   r   r   	getLoggerr;   r-   objectr   Servicer   r"   r"   r"   r#   <module>   s<   
: