o
    ip                  
   @   s8  d Z 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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 )(a  
The valkey integration traces valkey requests.


Enabling
~~~~~~~~

The valkey integration is enabled automatically when using
:ref:`ddtrace-run<ddtracerun>` or :ref:`import ddtrace.auto<ddtraceauto>`.

Or use :func:`patch()<ddtrace.patch>` to manually enable the integration::

    from ddtrace import patch
    patch(valkey=True)


Configuration
~~~~~~~~~~~~~

.. py:data:: ddtrace.config.valkey["service"]

   The service name reported by default for valkey traces.

   This option can also be set with the ``DD_VALKEY_SERVICE`` environment
   variable.

   Default: ``"valkey"``


.. py:data:: ddtrace.config.valkey["cmd_max_length"]

   Max allowable size for the valkey command span tag.
   Anything beyond the max length will be replaced with ``"..."``.

   This option can also be set with the ``DD_VALKEY_CMD_MAX_LENGTH`` environment
   variable.

   Default: ``1000``


.. py:data:: ddtrace.config.valkey["resource_only_command"]

   The span resource will only include the command executed. To include all
   arguments in the span resource, set this value to ``False``.

   This option can also be set with the ``DD_VALKEY_RESOURCE_ONLY_COMMAND`` environment
   variable.

   Default: ``True``
    N)config)Pin)_instrument_valkey_cmd)#_instrument_valkey_execute_pipeline)ROW_RETURNING_COMMANDS)determine_row_count)unwrap)core)schematize_service_name)CMD_MAX_LEN)asbool)stringify_cache_argsvalkeyDD_VALKEY_CMD_MAX_LENGTHDD_VALKEY_RESOURCE_ONLY_COMMANDT)_default_servicecmd_max_lengthresource_only_commandc                   C   s   t tddS )N__version__ )getattrr    r   r   Y/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/valkey/patch.pyget_versionQ   s   r   returnc                   C   s   ddiS )Nr   z>=6.0.0r   r   r   r   r   _supported_versionsV   s   r   c                  C   s\  t tddrdS dt_tj} ddlm} ddlm} ddlm} | d	d
t	t
j | d	dt | ddtt
jd | ddt	t
j | ddt	t
j | ddt | ddtt
jd tddtjj | dd
| | ddt | dd| | dd| tddtjj | dd| | 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 Valkey and StrictValkey. However, it means that any "import valkey.Valkey" won't be instrumented.
    _datadog_patchFNT   )+instrumented_async_execute_cluster_pipeline)"instrumented_async_execute_command)#instrumented_async_execute_pipeliner   zValkey.execute_commandzValkey.pipelinezvalkey.clientzPipeline.executez"Pipeline.immediate_execute_commandzvalkey.clusterzValkeyCluster.execute_commandzValkeyCluster.pipelinezClusterPipeline.execute)servicezvalkey.asyncio.clientzvalkey.asyncio.cluster)r   r   r   wraptwrap_function_wrapperasyncio_patchr   r   r    instrumented_execute_commandr   instrumented_pipelineinstrumented_execute_pipeliner   ontoclusterValkeyClusterasyncioValkeyStrictValkey)_wr   r   r    r   r   r   patchZ   s2   r/   c                   C   s   t tddrrdt_ttjd ttjd ttjjd ttjjd ttjjd ttjjd ttjj	d ttj
jjd ttj
jjd ttj
jjd ttj
jjd ttj
jjd ttj
jjd ttj
jj	d d S d S )Nr   Fexecute_commandpipelineexecuteimmediate_execute_command)r   r   r   r   r,   clientPipeliner)   r*   ClusterPipeliner+   r   r   r   r   unpatch   s"   r7   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   )valkey_commandresultzvalkey.command.post)r   splitr   r   r	   dispatch	Exception)r8   funcargskwargsparsed_commandr:   rowcountr;   r   r   r   _run_valkey_command   s*   rD   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)r8   r?   r@   rA   )r   get_fromenabledr   rD   )r?   instancer@   rA   pinr8   integration_configr   r   _instrumented_execute_command   s   
$zCinstrumented_execute_command.<locals>._instrumented_execute_commandr   )rJ   rK   r   rI   r   r%      s   r%   c                 C   s*   | |i |}t |}|r|| |S )N)r   rE   r(   )r?   rG   r@   rA   r1   rH   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 sCw   Y  d S )Nc                    s   g | ]
}t |j jd qS )cmd_max_len)r   r@   r   ).0crI   r   r   
<listcomp>   s    zYinstrumented_execute_pipeline.<locals>._instrumented_execute_pipeline.<locals>.<listcomp>c                    s   g | ]\}}t | jd qS rL   )r   r   )rN   rO   _rI   r   r   rP      s    )r   rE   rF   command_stackr   )r?   rG   r@   rA   rH   cmdsrJ   
is_clusterr   r   _instrumented_execute_pipeline   s   


$zEinstrumented_execute_pipeline.<locals>._instrumented_execute_pipeliner   )rJ   rU   rV   r   rT   r   r'      s   r'   )F)&__doc__osr   r"   ddtracer   ddtrace._trace.pinr   ddtrace._trace.utils_valkeyr   r   %ddtrace.contrib.internal.valkey_utilsr   r   ddtrace.contrib.trace_utilsr   ddtrace.internalr	   ddtrace.internal.schemar
   ddtrace.internal.utils.formatsr   r   r   _addintgetenvr   dictstrr   r/   r7   ExecutionContextrD   r%   r&   r'   r   r   r   r   <module>   s>    3
'