o
    i(                     @   sb  U 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 ddlmZ eeZeee
j Z!ee"d< e dee# dee# fddZ$e de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d!fd"d#Z)d!S )$    )Iterable)FunctionType)
ModuleType)Any)Optional)cast)config)	SpanTypes)core)	COMPONENT)
get_loggerschematize_service_name)IntegrationConfig)cached)is_wrapped_with)wrap   )utilsconfig_djangonamereturnc                 C   s   t | S )Nr   )r    r   Y/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/django/cache.pyget_service_name   s   r   funcc                 C   s"   t | d| j}|d\}}}|S )z
    Returns the cache operation name for the given function.
    This is used to set the resource name for the cache span.
    __qualname__.)getattr__name__
rpartition)r   fname_r   r   r   func_cache_operation!   s   r#   args.kwargsc                 C   s  t js
| |i |S |d }d|j|jj}tt jd|i}t|dkr/t	
|d }||d< t| }| j d| }t|dd }	|	rJ| d|	 }| }tjd	d	tjtt j||d
Y}
| |i |}d}|dkr~|r{t|tr{tdd |D nd}n)|dkrz|d u st|ts|t|dd krd}nd}W n tttfy   Y nw |
d| |W  d    S 1 sw   Y  d S )Nr   z{}.{}zdjango.cache.backendr   zdjango.cache.keyr   
key_prefix zdjango.cache)	span_name	span_typeserviceresourcetagsget_manyc                 s   s    | ]}|rd V  qdS )r   Nr   ).0docr   r   r   	<genexpr>N   s    ztraced_cache.<locals>.<genexpr>get_missing_keyrowcount)r   instrument_cachesformat
__module__	__class__r   r   integration_namelenr   quantize_key_valuesr#   r   lowerr
   context_with_datar	   CACHEr   cache_service_name
isinstancer   sumAttributeErrorNotImplementedError
ValueErrorset_item)r   r$   r%   instancecache_backendr,   keyscache_operationr+   r&   ctxresultr3   r   r   r   traced_cache.   sT   &$rK   djangoNc              
   C   s   t dd | jjj D }|D ]3}dD ].}z| jj|}t||d }|r0t	|t
s0t|t
 W q tyB   tjd|dd Y qw qd S )Nc                 S   s   g | ]	}t t|d  qS )BACKEND)r   str)r.   cacher   r   r   
<listcomp>d   s    z%instrument_caches.<locals>.<listcomp>)	r1   setadddeleteincrdecrr-   set_manydelete_manyzError instrumenting cache %rT)exc_info)rQ   confsettingsCACHESvaluesr   module_loadingimport_stringr   r   rK   r   	Exceptionlogdebug)rL   cache_backends
cache_pathmethod_nameclsmethodr   r   r   r4   c   s   
r4   )*collections.abcr   typesr   r   typingr   r   r   ddtracer   ddtrace.extr	   ddtrace.internalr
   ddtrace.internal.constantsr   ddtrace.internal.loggerr   ddtrace.internal.schemar   %ddtrace.internal.settings.integrationr   ddtrace.internal.utils.cacher   ddtrace.internal.wrappingr   r    r   r   r`   rL   r   __annotations__rN   r   r#   tupledictrK   r4   r   r   r   r   <module>   s2    *5