o
    ih                     @   s   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
 eeZde
ddfd	d
Zdd ZdddZdedeje fddZe jdejeddf fddZe jdejeddf fddZdS )    N)DDTESTOPT_ROOT_SPAN_RESOURCE)DDTraceTestContextPlainTestContext)TestContext)TestOptWriterwriterreturnc                 C   sT   zddl }W n ty   td Y dS w ddlm} || }|jj|gd dS )zz
    Install a trace filter in the global ddtrace tracer that forwards spans generated by ddtrace to ddtrace.testing.
    r   Nz4ddrace is not available, not installing trace filter   )TestOptSpanProcessortrace_processors)ddtraceImportErrorlogdebugspan_processorr   tracer	configure)r   r   r   r    r   _/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/testing/internal/tracer_api/context.pyinstall_global_trace_filter   s   
r   c                  C   s8   zddl m}  |   W d S  ty   td Y d S w )Nr   
_patch_allz#Error enabling ddtrace integrations)ddtrace._monkeyr   	Exceptionr   	exceptionr   r   r   r   enable_all_ddtrace_integrations    s   r   c                  C   s4   zddl } W n
 ty   Y dS w | jjg d dS )zA
    Uninstall trace filters from the global ddtrace tracer.
    r   Nr   )r   r   r   r   )r   r   r   r   uninstall_global_trace_filter)   s   r   ddtrace_enabledc                 C   s<   | rzddl }t W S  ty   td Y t S w t S )a  
    Create a trace context for a test to run.

    If ddtrace is enabled, a ddtrace context will be started; any spans created inside the test (e.g., instrumented HTTP
    requests) will be children of this context. This context manager yields a `TestContext` object containing the
    trace_id and span_id of created context.

    If ddtrace is not enabled, yields a dummy context with a freshly generated trace_id and span_id.
    r   Nz<ddrace is not available, falling back to non-ddtrace context)r   _ddtrace_contextr   r   r   _plain_context)r   r   r   r   r   trace_context5   s   
r"   c                  c   sl    dd l } | jjd  | jt}|dd |dd t|V  W d    d S 1 s/w   Y  d S )Nr   typetestz	span.kind)r   r   context_provideractivatetracer   set_tagr   )r   	root_spanr   r   r   r    J   s   "r    c                   c   s    t  V  d S )Nr   r   r   r   r   r!   Y   s   r!   )r	   N)
contextlibloggingtypingtddtrace.testing.internal.utilsr   r   r   r   ddtrace.testing.internal.writerr   	getLogger__name__r   r   r   r   boolContextManagerr"   contextmanager	Generatorr    r!   r   r   r   r   <module>   s"    

	 