o
    i                     @   s  d dl 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! ee"Z#ej$Z%dede&edf de'e(ef defddZ)dede&edf de'e(ef defddZ*dede&edf de'e(ef defddZ+e j,d d! Z-dede&edf de'e(ef defd"d#Z.d$Z/d%d& Z0d'd( Z1d)d* Z2dS )+    N)FunctionType)Any)AsyncConnection)Server)db)
get_logger)get_argument_value)unwrap)wrap)tracer   )datadog_trace_operation)parse_socket_command_spec)parse_socket_write_command_msg	trace_cmd)create_checkout_span)dbm_dispatch)process_server_operation_result)setup_checkout_span_tagsfuncargs.kwargsreturnc                    s   t ||dd}t ||dd}t||}|du r"| |i |I dH S |! t|||\}}}| |i |I dH }t|||W  d   S 1 sHw   Y  dS )z:Wrapper for AsyncServer.run_operation to trace operations.r   self   	operationN)r   r   r   r   )r   r   r   server_instancer   spanresult r    a/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/pymongo/async_client.py trace_async_server_run_operation"   s   

$r"   c                    sH   t ||dd| |i |I dH  tjs S tj fdd}| S )zWrapper for AsyncServer.checkout to trace socket checkout.

    AsyncServer.checkout() returns an async context manager. We wrap it to add tracing.
    r   r   Nc               
     s   t  6}  4 I d H }t| | |V  W d   I d H  n1 I d H s%w   Y  W d    d S W d    d S 1 s=w   Y  d S )N)r   r   )r   	sock_infocminstancer    r!   	traced_cm>   s   *"z.trace_async_server_checkout.<locals>.traced_cm)r   r   enabled
contextlibasynccontextmanager)r   r   r   r'   r    r$   r!   trace_async_server_checkout0   s   r+   c              	      s   t ||}|du r| |i |I dH S |\}}}t|||j4 I dH }t|||\}}}| |i |I dH W  d  I dH  S 1 I dH sHw   Y  dS )z@Wrapper for AsyncConnection.command to trace command operations.N)r   async_trace_cmdaddressr   )r   r   r   parsedsocket_instancedbnamecmdsr    r    r!   trace_async_socket_commandH   s   

0r3   c                 C  s:   t | ||}|V  W d   dS 1 sw   Y  dS )zPAsync context manager wrapper for trace_cmd that properly handles GeneratorExit.Nr   )r1   r/   r-   r   r    r    r!   r,   T   s   "r,   c              	      s   t ||}|du r| |i |I dH S |\}}t|||j4 I dH $}| |i |I dH }|r;|tj|dd |W  d  I dH  S 1 I dH sMw   Y  dS )zLWrapper for AsyncConnection.write_command to trace write command operations.Nn)r   r,   r-   
set_metricr   ROWCOUNTget)r   r   r   r.   r/   r1   r2   r   r    r    r!    trace_async_socket_write_command[   s   
0r9   )      c                   C   s(   t tk rtddttt dS dS )z,Check if async pymongo support is available.z,Async pymongo support requires pymongo >= %s.FT)VERSION_ASYNC_MIN_VERSIONlogwarningjoinmapstrr    r    r    r!   _check_async_supportl   s   rD   c                   C   >   t  sdS ttjt ttjt ttjt	 ttj
t dS )z#Patch asynchronous pymongo modules.N)rD   _wAsyncServerrun_operationr"   checkoutr+   r   commandr3   write_commandr9   r    r    r    r!   patch_pymongo_async_modulest      rL   c                   C   rE   )z%Unpatch asynchronous pymongo modules.N)rD   _urG   rH   r"   rI   r+   r   rJ   r3   rK   r9   r    r    r    r!   unpatch_pymongo_async_modules~   rM   rO   )3r)   typesr   typingr   pymongopymongo.asynchronous.poolr   pymongo.asynchronous.serverr   rG   ddtrace.extr   ddtrace.internal.loggerr   ddtrace.internal.utilsr   ddtrace.internal.wrappingr	   rN   r
   rF   ddtrace.tracer   clientr   r   r   r   utilsr   r   r   r   __name__r?   version_tupler=   tupledictrC   r"   r+   r3   r*   r,   r9   r>   rD   rL   rO   r    r    r    r!   <module>   s@   ***
*
