o
    i                  	   @   s  d 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 ddlmZ ddlmZ ddlmZ ddlmZ ejryddlmZ ededejdddd dZ dZ!dZ"dZ#dd Z$dd Z%d d! Z&d"d# Zej'd$d% Z(ej)d&d'd(d) Z*ej)d*d'd+d, Z+ej)d*d-d.d/d0 Z,dS )1a;  
This custom pytest plugin implements tracing for pytest by using pytest hooks. The plugin registers tracing code
to be run at specific points during pytest execution. The most important hooks used are:

    * pytest_sessionstart: during pytest session startup, a custom trace filter is configured to the global tracer to
        only send test spans, which are generated by the plugin.
    * pytest_runtest_protocol: this wraps around the execution of a pytest test function, which we trace. Most span
        tags are generated and added in this function. We also store the span on the underlying pytest test item to
        retrieve later when we need to report test status/result.
    * pytest_runtest_makereport: this hook is used to set the test status/result tag, including skipped tests and
        expected failures.

    Nconfig)pytest_collection_finish)pytest_configure)#pytest_ddtrace_get_item_module_name)"pytest_ddtrace_get_item_suite_name)!pytest_ddtrace_get_item_test_name)pytest_load_initial_conftests)pytest_report_teststatus)pytest_runtest_makereport)pytest_runtest_protocol)pytest_runtest_protocol_wrapper)pytest_sessionfinish)pytest_sessionstart)pytest_terminal_summary)_extract_span)ddtrace_iastpytestDD_PYTEST_OPERATION_NAMEzpytest.test)default)_default_serviceoperation_namez#Enable tracing of pytest functions.z$Disable tracing of pytest functions.z3Prepend 'ClassName.' to names of class-based tests.z-Call ddtrace._patch_all before running tests.c                   C   s
   dt _d S )NF)telemetry_configDEPENDENCY_COLLECTION r   r   Z/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/pytest/plugin.py(_disable_telemetry_dependency_collection:   s   
r   c                 C   s    |  ds
| do|  d S )z'Check if the ddtrace plugin is enabled.ddtrace
no-ddtrace)	getoptiongetinir   r   r   r   
is_enabled>   s    r!   c                 C   s   |  d}|jddddtd |jddddtd |jddd	dtd |jd
dddtd |jddddtd | jdtdd | jdtdd | jd	tdd | jdtdd tjreddl	m
} |  dS dS )zAdd ddtrace options.r   z	--ddtrace
store_trueF)actiondestr   helpz--no-ddtracer   z--ddtrace-patch-allddtrace-patch-allz--ddtrace-include-class-namezddtrace-include-class-namez--ddtrace-iast-fail-testszddtrace-iast-fail-testsbool)typer   )_iast_pytest_activationN)getgroup
_addoptionDDTRACE_HELP_MSGNO_DDTRACE_HELP_MSGPATCH_ALL_HELP_MSGDDTRACE_INCLUDE_CLASS_HELP_MSGaddini
asm_config_iast_enabledddtrace.appsec._iastr)   )parsergroupr)   r   r   r   pytest_addoptionC   sX   

r6   c                 C   s*   |  dd t| rt  t|  d S d S )Nmarkersz+dd_tags(**kwargs): add tags to current span)addinivalue_liner!   r   _versioned_pytest_configurer   r   r   r   r   y   s
   r   c                 C   s   ddl m} | | d S )Nr   )newhooks)ddtrace.contrib.internal.pytestr:   add_hookspecs)pluginmanagerr:   r   r   r   pytest_addhooks   s   r>   function)scopec                 C   s    ddl m} |jrt| jS dS )zReturn the :class:`ddtrace.trace.Span` instance associated with the
    current test when Datadog CI Visibility is enabled.
    r   CIVisibilityN)ddtrace.internal.ci_visibilityrB   enabledr   node)request_CIVisibilityr   r   r   ddspan   s   
rH   sessionc                  C   s(   ddl } ddlm} |jr|jjS | jS )zReturn the :class:`ddtrace.tracer.Tracer` instance for Datadog CI
    visibility if it is enabled, otherwise return the default Datadog tracer.
    r   NrA   )r   rC   rB   rD   	_instancetracer)r   rG   r   r   r   ddtracer   s
   rL   T)r@   autousec                 C   s2   | j ds| j drddlm} |  dS dS )zpPatch all available modules for Datadog tracing when ddtrace-patch-all
    is specified in command or .ini.
    r&   r   )
_patch_allN)r   r   r    ddtrace._monkeyrN   )rF   rN   r   r   r   	patch_all   s   
rP   )-__doc__osr   r   r   *ddtrace.contrib.internal.pytest._plugin_v2r   r   r9   r   r   r   r	   r
   r   r   r   r   r   r   &ddtrace.contrib.internal.pytest._utilsr   $ddtrace.internal.settings._telemetryr   ddtrace.internal.settings.asmr1   r2   #ddtrace.appsec._iast._pytest_pluginr   _adddictgetenvr,   r-   r/   r.   r   r!   r6   hookimplr>   fixturerH   rL   rP   r   r   r   r   <module>   sZ    	6





