o
    ip3                     @  sl  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 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	dCdDddZdd Zdd Z dEddZ!dFd!d"Z"d#d$ Z#dGd&d'Z$G d(d) d)eZ%G d*d+ d+eZ&G d,d- d-eZ'G d.d/ d/eZ(G d0d1 d1eZ)G d2d3 d3eZ*e%j+e&d4d5 e%j+e'd6d5 e%j+e(d7d5 e%j+e)d8d5 e%j+e*d9d5 e% Z,ee, e \Z-Z.e.se-pd:Z/e0d;e/ e1ej2d<e- d= d>e,_3e  \Z4Z5e,j6j3r)e5s)e4pd?Z/e0d@e/ e1ej2d@e/  d>e,j6_3e$e,j7e,_7dHdAdBZ8dS )I    )annotationsN)Env)
COMMIT_SHA)MAIN_PACKAGE)REPOSITORY_URL)compat)gitmetadata)
get_logger)DDConfig)ValueSource)report_configuration)telemetry_writer)TELEMETRY_LOG_LEVEL)parse_tags_str   heap_configr   default_heap_sample_sizeintreturnc                 C  s   t | tsJ | j}|d urtt|S | jsdS zddlm} |	 j
| j
 }W n ty@   tjd|d dd | Y S w d}ttt|| |S )Nr   )psutilzBUnable to get total memory available, using default value of %d KBi   T)exc_infoi   )
isinstanceProfilingConfigHeap_sample_sizetcastr   enabledddtrace.vendorr   swap_memorytotalvirtual_memory	Exceptionloggerwarningmaxmathceil)r   r   heap_sample_sizer   	total_memmax_samples r*   W/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/internal/settings/profiling.py _derive_default_heap_sample_size   s&   	r,   c                  C     ddl m}  | j| jfS )Nr   ddup)"ddtrace.internal.datadog.profilingr/   failure_msgis_availabler.   r*   r*   r+   _check_for_ddup_available9   s   r3   c                  C  r-   )Nr   stack)r0   r5   r1   r2   r4   r*   r*   r+   _check_for_stack_availableC   s   r6   boolc                  C  s0   t jd} | du rdS tdd | dD S )zBReturn True if DD_INJECTION_ENABLED contains the 'profiler' token.DD_INJECTION_ENABLEDNFc                 s  s     | ]}|   d kV  qdS )profilerN)striplower).0tokr*   r*   r+   	<genexpr>P   s    z2_injection_enabled_has_profiler.<locals>.<genexpr>,)osenvirongetanysplit)injection_enabledr*   r*   r+   _injection_enabled_has_profilerJ   s   rF   rawstrc                 C  s   t  rdS |  }|dv S )NT)1trueyesonauto)rF   r;   )rG   raw_lcr*   r*   r+   _parse_profiling_enabledS   s   rO   c                 C  s@   t |  t  \}}}|r|| t< |r|| t< |r|| t< | S )z0
    Update profiler tags with git metadata
    )r   
clean_tagsget_git_tagsr   r   r   )tagsrepository_url
commit_shamain_packager*   r*   r+   _update_git_metadata_tags\   s   
rV   dict[str, str]c                 C  s2   dd t tttjd |  D } | S )Nc                 S  s   i | ]\}}|t |d qS )zutf-8)r   ensure_text)r<   kvr*   r*   r+   
<dictcomp>m   s    z _enrich_tags.<locals>.<dictcomp>DD_TAGS)	itertoolschainrV   r   r@   rA   rB   items)rR   r*   r*   r+   _enrich_tagsl   s   r`   c                      sX  e Zd ZdZejededdddZd5 fddZ	ejedddddZ
ejedddddZejedddddZejeje dddddZejedddddZejedddddZejed d!d"d#dZejed$ddd%dZejed&ddd'dZejed(d)d"d*dZejed+ddd,dZejed-ei d.d/dZejed0ddd1dZejed2d3d"d4dZ  ZS )6ProfilingConfigzdd.profilingr   FBooleanz3Enable Datadog profiling when using ``ddtrace-run``)parserdefault	help_typehelpargst.Anykwargsr   Nonec                   s<   t  j|i | | jst rd| _tj| jd< d S d S d S )NTDD_PROFILING_ENABLED)super__init__r   rF   r   ENV_VAR_value_source)selfrg   ri   	__class__r*   r+   rm      s
   zProfilingConfig.__init__	agentless rd   re   rf   enable_code_provenanceTz!Whether to enable code provenanceendpoint_collection_enabledz:Whether to enable the endpoint data collection in profilesoutput_pprofNStringupload_intervalg      N@FloatzCThe interval in seconds to wait before flushing out recorded eventscapture_pctg      ?zThe percentage of events that should be captured (e.g. memory allocation). Greater values reduce the program execution speed. Must be greater than 0 lesser or equal to 100
max_frames@   IntegerzBThe maximum number of frames to capture in stack execution tracingignore_profilerzD**Deprecated**: whether to ignore the profiler in the generated datamax_time_usage_pctzThe percentage of maximum time the stack profiler can use when computing statistics. Must be greater than 0 and lesser or equal to 100api_timeout_msi'  zQThe timeout in milliseconds before dropping events if the HTTP API does not replytimeline_enabledzWhether to add timestamp information to captured samples.  Adds a small amount of overhead to the profiler, but enables the use of the Timeline view in the UI.rR   Mappingz^The tags to apply to uploaded profile. Must be a list in the ``key1:value,key2:value2`` formatenable_assertsz7Whether to enable debug assertions in the profiler codesample_pool_capacity   zThe number of Sample objects to keep in the pool for reuse. Increasing this can reduce the overhead from frequently allocating and deallocating Sample objects.)rg   rh   ri   rh   r   rj   ) __name__
__module____qualname__
__prefix__r
   rZ   r7   rO   r   rm   rs   code_provenanceendpoint_collectionr   OptionalrH   rx   floatrz   r|   r   r}   r   r   r   r   dictr   rR   r   r   __classcell__r*   r*   rq   r+   ra   x   s    	

			ra   c                   @  sP   e Zd Zd ZZejedddddZejedddddZ	ejed	ddd
dZ
dS )ProfilingConfigStackr5   r   Trb   z$Whether to enable the stack profilerru   zadaptive_sampling.enabled)rd   re   privateuvloopz4Whether to enable uvloop support for async profilingN)r   r   r   __item__r   r
   rZ   r7   r   adaptive_samplingr   r*   r*   r*   r+   r     s.    
r   c                   @  s<   e Zd Zd ZZejedddddZejedddddZ	d	S )
ProfilingConfigLocklockr   Trb   z#Whether to enable the lock profilerru   name_inspect_dirzWhether to inspect the ``dir()`` of local and global variables to find the name of the lock. With this enabled, the profiler finds the name of locks that are attributes of an object.N)
r   r   r   r   r   r
   rZ   r7   r   r   r*   r*   r*   r+   r   $       
r   c                   @  <   e Zd Zd ZZejedddddZeje	ddd	d
dZ
dS )ProfilingConfigMemorymemoryr   Trb   z%Whether to enable the memory profilerru   events_buffer   r   rt   N)r   r   r   r   r   r
   rZ   r7   r   r   r   r*   r*   r*   r+   r   9  r   r   c                   @  sN   e Zd Zd ZZejedddddZeje	j
e ddd	d
dZeeeZdS )r   heapr   Trb   z*Whether to enable the heap memory profilerru   sample_sizeNr   zAAverage number of bytes allocated between memory profiler samples)r   r   r   r   r   r
   rZ   r7   r   r   r   r   r   dr,   r   r*   r*   r*   r+   r   M  s"    r   c                   @  r   )ProfilingConfigPytorchpytorchr   Frb   z&Whether to enable the PyTorch profilerru   events_limiti@B r   z<How many events the PyTorch profiler records each collectionN)r   r   r   r   r   r
   rZ   r7   r   r   r   r*   r*   r*   r+   r   b  r   r   r5   )	namespacer   r   r   r   zlibdd not availablez4Failed to load ddup module (%s), disabling profilingzFailed to load ddup module (z), disabling profilingFzstack not availablez;Failed to load stack module (%s), disabling stack profilingc                 C  s   g }| j jr|d | jjr|d | jjr|d | jjdkr(|d | jjr1|d |d |dt| j	  |d	t| j
  d
|S )Nstack_v2r   memr   r   r   exp_ddCAPMAXF_)r5   r   appendr   r   r   r   r   rH   r|   r}   join)configconfigured_featuresr*   r*   r+   
config_str  s   






r   )r   )r   r   r   r   r   r   )r   r7   )rG   rH   r   r7   )r   rW   )r   rH   )9
__future__r   r]   r%   r@   typingr   envierr   ddtrace.ext.gitr   r   r   ddtrace.internalr   r   ddtrace.internal.loggerr	   ddtrace.internal.settings._corer
   r   ddtrace.internal.telemetryr   r   $ddtrace.internal.telemetry.constantsr   ddtrace.internal.utils.formatsr   r   r"   r,   r3   r6   rF   rO   rV   r`   ra   r   r   r   r   r   includer   ddup_failure_msgddup_is_availablemsgr#   add_logERRORr   stack_failure_msgstack_is_availabler5   rR   r   r*   r*   r*   r+   <module>   sz    


		
 



