o
    iY                     @   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 eeZdddd	d
ddddddddZdZedZG dd deZdd Zdd Zd ddZdd Zdd ZdS )!    N)CodecOptions)SON)net)
get_loggerreplymsgupdateinsertreservedqueryget_moredeletekill_cursorscommandcommand_reply)   i  i  i  i  i  i  i  i  i  i  i  i   z<iiiic                   @   s(   e Zd ZdZg dZdd Zdd ZdS )Commandz;Command stores information about a pymongo network command,namecolldbtagsmetricsr   c                 C   s(   || _ || _|| _i | _i | _d | _d S Nr   )selfr   r   r    r   Z/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/pymongo/parse.py__init__-   s   
zCommand.__init__c                 C   s   d| j | j| jf S )NzCommand(name=%s,db=%s,coll=%s))r   r   r   )r   r   r   r   __repr__5   s   zCommand.__repr__N)__name__
__module____qualname____doc__	__slots__r   r   r   r   r   r   r   (   s
    r   c                 C   s  t | }|dkr
dS t| d}|\}}}}t|}|s%td| dS d}d}	tj}
d}|dkrx|
d7 }
t| |
d }|
t |d 7 }
t	|\}}	|
d7 }
|t
krktt}ttj| |
d |d}t||}ntd	|d
}|jsw|	|_nB|dkr|
d7 }
t| |
|
d  }|
d7 }
|dkr|t
krtt}ttj| |
d |d}t||}ntd	|d
}ntd	|d}|r||jtj< |S )zReturn a command from a binary mongo db message or None if we shouldn't
    trace it. The protocol is documented here:
    http://docs.mongodb.com/manual/reference/mongodb-wire-protocol
    r   Nzunknown op code: %sr      r      )codec_optionsr   untraced_message_too_larger   unsupported_msg_kind)lenheader_structunpack_fromOP_CODESgetlogdebugsize_cstring_split_namespaceMAX_MSG_PARSE_LENr   r   nextbsondecode_iter
parse_specr   r   ordr   netx	BYTES_OUT)	msg_bytesmsg_lenheaderlengthreq_idresponse_toop_codeopr   r   offsetcmdnscodecspeckindr   r   r   	parse_msg9   sR   
rI   c                 C   s^   d\}}t | dd}|rt|\}}nt | dd}t | dd}tt | dd||}| j|_|S )z6Return a command parsed from the given mongo db query.NNrE   Nr   r   r   r   )getattrr2   r   rG   r   )r   r   r   rE   rD   r   r   r   parse_query   s   rL   c                 C   s   t |  }|s
dS |d \}}t||p| d|}d| v r&| d |jd< |jdkr:d| v r8t| d |jd< |S |jd	krP| d
}|rN|d d|_|S |jdkrd| d}|rd|d d|_|S )z]Return a Command that has parsed the relevant detail for the given
    pymongo SON spec.
    Nr   z$dborderedzmongodb.orderedr	   	documentszmongodb.documentsr   updatesqr   deletes)	listitemsr   r-   r   r   r)   r   r   )rG   r   rS   r   r   rD   rO   delsr   r   r   r7      s*   




r7   c                 C   s   t | jS )z9Return the first null terminated cstring from the buffer.)ctypescreate_string_buffervalue)rawr   r   r   r1      s   r1   c                 C   s>   | r| j ddddd}t|dkrtd|  dS |S dS )z=Return a tuple of (db, collection) from the 'db.coll' string.zutf-8ignore)errors.r   z$namespace doesn't contain period: %srJ   )decodesplitr)   r.   warning)rE   r]   r   r   r   r2      s   r2   r   )rU   structr5   bson.codec_optionsr   bson.sonr   ddtrace.extr   r9   ddtrace.internal.loggerr   r   r.   r,   r3   Structr*   objectr   rI   rL   r7   r1   r2   r   r   r   r   <module>   s:    
P
"