o
    i*                  
   @   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
 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dedee deee ddd defddZdeeef fddZdd Zdd ZdejfddZ dd  Z!d!d" Z"d&d$d%Z#dS )'    N)config)Pin)ROW_RETURNING_COMMANDS)_instrument_redis_cmd)"_instrument_redis_execute_pipeline)determine_row_count)unwrap)core)schematize_service_name)CMD_MAX_LEN)asbool)stringify_cache_argsredisDD_REDIS_CMD_MAX_LENGTHDD_REDIS_RESOURCE_ONLY_COMMANDT)_default_servicecmd_max_lengthresource_only_commandreturnc                   C   s   t tddS )N__version__ )getattrr    r   r   X/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/redis/patch.pyget_version   s   r   c                   C   s   ddiS )Nr   *r   r   r   r   r   _supported_versions"   s   r   c                  C   s  t tddrdS dt_tj} tjdk r<| ddttj | ddt | dd	t | d
dt	tjd | d
dttj n| ddttj | dd	t | d
dt	tjd | d
dttj tjdkr| ddttj | ddt | ddt	tjd t
ddtjj tjdkrddlm} ddlm} | dd| | dd	t | dd| | dd| t
ddtjj tjdkrddlm} | dd| tjdkrddlm} | ddt | dd| t
ddtjj t
ddtj dS )zPatch the instrumented methods

    This duplicated doesn't look nice. The nicer alternative is to use an ObjectProxy on top
    of Redis and StrictRedis. However, it means that any "import redis.Redis" won't be instrumented.
    _datadog_patchFNT   r   r   r   zStrictRedis.execute_commandzStrictRedis.pipelinezRedis.pipelinezredis.clientzBasePipeline.executez&BasePipeline.immediate_execute_commandzRedis.execute_commandzPipeline.executez"Pipeline.immediate_execute_command)      zredis.clusterzRedisCluster.execute_commandzRedisCluster.pipelinezClusterPipeline.execute)servicer       r   r!   )"instrumented_async_execute_command)#instrumented_async_execute_pipelinezredis.asyncio.clientr    r   r   zredis.asyncio.clusterr    r   r$   )+instrumented_async_execute_cluster_pipeline)r   r   r   wraptwrap_function_wrapperVERSIONinstrumented_execute_commandr   instrumented_pipelineinstrumented_execute_pipeliner   ontoclusterRedisClusterasyncio_patchr%   r&   asyncioRedisr)   StrictRedis)_wr%   r&   r)   r   r   r   patch&   sH   




r8   c                   C   sb  t tddrdt_tjdk r0ttjd ttjd ttjd ttjjd ttjjd d S ttjd ttjd ttjj	d ttjj	d tjdkrdttj
jd ttj
jd ttj
jd tjd	krttjjjd ttjjjd ttjjj	d ttjjj	d tjd
krttjj
jd tjdkrttjj
jd ttjj
jd d S d S d S )Nr   Fr   execute_commandpipelineexecuteimmediate_execute_command)r    r!   r   r#   r'   r(   )r   r   r   r,   r   r6   r5   clientBasePipelinePipeliner1   r2   ClusterPipeliner4   r   r   r   r   unpatch_   s8   




rA   ctxc                 C   s   t |}|dd }d }d }z.z#||i |}|W W |d u r%t||d}|tvr+d }td| |g S  ty=   d} w |d u rHt||d}|tvrNd }td| |g w )N r   )redis_commandresultzredis.command.post)r   splitr   r   r	   dispatch	Exception)rB   funcargskwargsparsed_commandrD   rowcountrE   r   r   r   _run_redis_command~   s*   rN   c                    s    fdd}|S )Nc                    sf   t |}|r| s| |i |S t| ||}t|| ||dW  d    S 1 s,w   Y  d S )N)rB   rI   rJ   rK   )r   get_fromenabledr   rN   )rI   instancerJ   rK   pinrB   integration_configr   r   _instrumented_execute_command   s   
$zCinstrumented_execute_command.<locals>._instrumented_execute_commandr   )rT   rU   r   rS   r   r-      s   r-   c                 C   s*   | |i |}t |}|r|| |S )N)r   rO   r0   )rI   rQ   rJ   rK   r:   rR   r   r   r   r.      s
   

r.   Fc                    s    fdd}|S )Nc                    s   t |}|r| s| |i |S r fdd|jD }n
 fdd|jD }t| || | |i |W  d    S 1 sBw   Y  d S )Nc                    s   g | ]
}t |j jd qS )cmd_max_len)r   rJ   r   ).0crS   r   r   
<listcomp>   s    zYinstrumented_execute_pipeline.<locals>._instrumented_execute_pipeline.<locals>.<listcomp>c                    s   g | ]\}}t | jd qS rV   )r   r   )rX   rY   _rS   r   r   rZ      s    )r   rO   rP   command_stackr   )rI   rQ   rJ   rK   rR   cmdsrT   
is_clusterr   r   _instrumented_execute_pipeline   s   


$zEinstrumented_execute_pipeline.<locals>._instrumented_execute_pipeliner   )rT   r_   r`   r   r^   r   r/      s   r/   )F)$osr   r*   ddtracer   ddtrace._trace.pinr   $ddtrace.contrib.internal.redis_utilsr   r   r   r   $ddtrace.contrib.internal.trace_utilsr   ddtrace.internalr	   ddtrace.internal.schemar
   ddtrace.internal.utils.formatsr   r   r   _addintgetenvstrr   dictr   r8   rA   ExecutionContextrN   r-   r.   r/   r   r   r   r   <module>   s<    
9