o
    Á¿ip  ã                   @   s²   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 deƒ ¡ defd	d
„Zdeeef fdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )é    N)Úconfig)Úunwrap)Úwrap)Úget_argument_value)Úset_argument_valueÚ	structlogÚreturnc                   C   s   t tddƒS )NÚ__version__Ú )Úgetattrr   © r   r   ú\/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/structlog/patch.pyÚget_version   s   r   c                   C   s   ddiS )Nr   z>=20.2.0r   r   r   r   r   Ú_supported_versions   s   r   c                 C   s   t jr| tj ¡ ¡ |S )N)r   Ú_logs_injectionÚupdateÚddtraceÚtracerÚget_log_correlation_context)Ú_Ú__Ú
event_dictr   r   r   Ú_tracer_injection   s   r   c                 C   sH   t g}t ttjjjƒvrtjjjr|ttjjjƒ tjj_| |i |¤ŽS )a´  
    Append the tracer injection processor to the ``default_processors`` list used by the logger
    Ensures that the tracer injection processor is the first processor in the chain and only injected once
    The ``default_processors`` list has built in defaults which protects against a user configured ``None`` value.
    The argument to configure ``default_processors`` accepts an iterable type:
        - List: default use case which has been accounted for
        - Tuple: patched via list conversion
        - Set: ignored because structlog processors care about order notably the last value to be a Renderer
        - dict: because keys are ignored, this essentially becomes a List
    ©r   Úlistr   Ú_configÚ_CONFIGÚdefault_processors©ÚfuncÚinstanceÚargsÚkwargsÚdd_processorr   r   r   Ú_w_get_logger   s   ÿr$   c                 C   sL   t g}t||dddƒ}|rt|ƒdkrt||dd|t|ƒ ƒ | |i |¤ŽS )aI  
    Injects the tracer injection processor to the ``processors`` list parameter when configuring a logger
    Ensures that the tracer injection processor is the first processor in the chain and only injected once
    In addition, the tracer injection processor is only injected if there is a renderer processor in the chain
    r   Ú
processorsT)r   r   Úlenr   r   )r   r    r!   r"   r#   Úarg_processorsr   r   r   Ú_w_configure5   s
   r(   c                 C   sL   | |i |¤Ž t g}t ttjjjƒvr$tjjjr$|ttjjjƒ tjj_dS )z¥
    Reset the default_processors list to the original defaults
    Ensures that the tracer injection processor is injected after to the default_processors list
    Nr   r   r   r   r   Ú_w_reset_defaultsD   s   ÿr)   c                   C   sv   t tddƒrdS dt_ttdƒrttdtƒ ttdƒr!ttdtƒ ttdƒr,ttdtƒ ttdƒr9ttdtƒ dS dS )	z›
    Patch ``structlog`` module for injection of tracer information
    by appending a processor before creating a logger via ``structlog.get_logger``
    Ú_datadog_patchFNTÚ
get_loggerÚ	getLoggerÚ	configureÚreset_defaults)r   r   r*   ÚhasattrÚ_wr$   r(   r)   r   r   r   r   ÚpatchU   s   



ÿr1   c                   C   sn   t tddƒr3dt_ttdƒrttdƒ ttdƒrttdƒ ttdƒr'ttdƒ ttdƒr5ttdƒ d S d S d S )Nr*   Fr+   r,   r-   r.   )r   r   r*   r/   Ú_ur   r   r   r   Úunpatchl   s   






ö	r3   )r   r   r   Ú$ddtrace.contrib.internal.trace_utilsr   r2   r   r0   Úddtrace.internal.utilsr   r   Ú_addÚdictÚstrr   r   r   r$   r(   r)   r1   r3   r   r   r   r   Ú<module>   s&    þ