o
    i'                  	   @   s  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 d dlmZ d dlmZ eee ee eeef eeef ee eeef eef Z e!dZ"ee#Z$G dd de%Z&dS )    N)Any)Optional)Text)SpanLink)_ORIGIN_KEY)_SAMPLING_PRIORITY_KEY)_USER_ID_KEY)NumericType)MAX_UINT_64BITS)W3C_TRACEPARENT_KEY)W3C_TRACESTATE_KEY)
get_logger)RLock)w3c_get_dd_list_memberz[^\x20-\x7E]+c                   @   sv  e Zd ZdZg dZ										dDdee dee dee dee d	ee	eef  d
ee	ee
f  dee deee  dee	eef  defddZdefddZdeddfddZdEddZdeddfddZedee
 fddZejdee
 ddfddZedefd d!Zedefd"d#Zedefd$d%Zedee fd&d'Zejdee ddfd(d'Zedee fd)d*Zejdee ddfd+d*Zedee fd,d-Zd.ededdfd/d0Z dededd fd1d2Z!d.ededd fd3d4Z"d.edee fd5d6Z#de	eef fd7d8Z$d.eddfd9d:Z%dFd;d<Z&d=edefd>d?Z'defd@dAZ(defdBdCZ)dS )GContextzXRepresents the state required to propagate a trace across execution
    boundaries.
    )
trace_idspan_id_lock_meta_metrics_span_links_baggage
_is_remote_reactivate__weakref__NTr   r   	dd_originsampling_prioritymetametricslock
span_linksbaggage	is_remotec                 C   s   |d ur|ni | _ |d ur|ni | _|	d ur|	ni | _|| _|| _|
| _d| _|d ur7t|d u r7|| j t	< |d ur@|| jt
< |d urH|| _ng | _|d urT|| _d S t | _d S NF)r   r   r   r   r   r   r   _DD_ORIGIN_INVALID_CHARS_REGEXsearchr   r   r   r   r   )selfr   r   r   r   r   r   r   r    r!   r"    r'   J/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/_trace/context.py__init__7   s"   


zContext.__init__returnc                 C   s$   | j | j| j| j| j| j| j| jfS N)r   r   r   r   r   r   r   r   r&   r'   r'   r(   __getstate__^   s   zContext.__getstate__statec              	   C   s0   |\| _ | _| _| _| _| _| _| _t | _	d S r+   )
r   r   r   r   r   r   r   r   r   r   )r&   r.   r'   r'   r(   __setstate__k   s   
zContext.__setstate__c                 C   s   | j   | S r+   )r   acquirer,   r'   r'   r(   	__enter__y   s   
zContext.__enter__argsc                 G   s   | j   d S r+   )r   release)r&   r2   r'   r'   r(   __exit__}   s   zContext.__exit__c                 C      | j tS )z3Return the context sampling priority for the trace.)r   getr   r,   r'   r'   r(   r         zContext.sampling_priorityvaluec                 C   sb   | j $ |d u rt| jv r| jt= 	 W d    d S || jt< W d    d S 1 s*w   Y  d S r+   )r   r   r   r&   r8   r'   r'   r(   r      s   
"c                 C   sb   | j t}| jd u s| jd u r|pdS |r|dd }n| jd}d| d| jdd| j S )N -   032xz00-016x)r   r6   r   r   r   split_traceflags)r&   tpr   r'   r'   r(   _traceparent   s   
zContext._traceparentc                 C   s   | j r
| j dkr
dS dS )Nr   0100)r   r,   r'   r'   r(   r@      s   zContext._traceflagsc                 C   sd   t | }| jtd}|r)|r)tdd|}|r"d| d| }|S d| }|S |r0d| }|S )Nr:   zdd=(.+?)(?:,|$)zdd=,)_w3c_get_dd_list_memberr   r6   r   resub)r&   dd_list_membertsts_w_out_ddr'   r'   r(   _tracestate   s   

zContext._tracestatec                 C   r5   )zGet the origin of the trace.)r   r6   r   r,   r'   r'   r(   r      r7   zContext.dd_originc                 C   sb   | j $ |du rt| jv r| jt= 	 W d   dS || jt< W d   dS 1 s*w   Y  dS )zSet the origin of the trace.N)r   r   r   r9   r'   r'   r(   r      s   
"c                 C   s&   | j t}|rtt|ddS dS )zGet the user ID of the trace.utf-8encodingN)r   r6   r   strbase64	b64decode)r&   user_idr'   r'   r(   
dd_user_id   s   zContext.dd_user_idc                 C   sx   | j / |du rt| jv r| jt= 	 W d   dS ttt|dddd| jt< W d   dS 1 s5w   Y  dS )zSet the user ID of the trace.NrM   rN   )r   r   r   rP   rQ   	b64encodebytesr9   r'   r'   r(   rT      s   
""c                 C   s   | j du rdS t| j @ S )z&Return the trace ID as a 64-bit value.N)r   _MAX_UINT_64BITSr,   r'   r'   r(   _trace_id_64bits   s   

zContext._trace_id_64bitskeyc                 C   s   || j |< dS )z|Sets a baggage item in this span context.
        Note that this operation mutates the baggage of this span context
        Nr   )r&   rY   r8   r'   r'   r(   set_baggage_item   s   zContext.set_baggage_itemc              	   C   s    | j ||| j| j| j| jddS )zDReturn a shallow copy of the context with the given correlation IDs.F)r   r   r   r   r   r!   r"   )	__class__r   r   r   r   )r&   r   r   r'   r'   r(   copy   s   zContext.copyc                 C   s>   t | j}|||< | j| j| jd}| j|_| j|_||_|S )zwReturns a copy of this span with a new baggage item.
        Useful for instantiating new child span contexts.
        )r   r   )dictr   r\   r   r   r   r   )r&   rY   r8   new_baggagectxr'   r'   r(   _with_baggage_item   s   
zContext._with_baggage_itemc                 C   s   | j |dS )z)Gets a baggage item in this span context.N)r   r6   r&   rY   r'   r'   r(   get_baggage_item      zContext.get_baggage_itemc                 C   s   | j S )z/Returns all baggage items in this span context.rZ   r,   r'   r'   r(   get_all_baggage_items  s   zContext.get_all_baggage_itemsc                 C   s   || j v r| j |= dS dS )z-Remove a baggage item from this span context.NrZ   rb   r'   r'   r(   remove_baggage_item  s   
zContext.remove_baggage_itemc                 C   s   | j   dS )z1Removes all baggage items from this span context.N)r   clearr,   r'   r'   r(   remove_all_baggage_items
  rd   z Context.remove_all_baggage_itemsotherc                 C   s~   t |tr=| j- | j|jko,| j|jko,| j|jko,| j|jko,| j|jko,| j|jk	 W  d    S 1 s8w   Y  dS r#   )	
isinstancer   r   r   r   r   r   r   r   )r&   ri   r'   r'   r(   __eq__  s   





 	zContext.__eq__c                 C   s>   d| j  d| j d| j d| j d| j d| j d| j dS )	NzContext(trace_id=z
, span_id=z, _meta=z, _metrics=z, _span_links=z, _baggage=z, _is_remote=))r   r   r   r   r   r   r   r,   r'   r'   r(   __repr__  s   zContext.__repr__c                 C   s
   t | jS r+   )hashr   r,   r'   r'   r(   __hash__"  s   
zContext.__hash__)
NNNNNNNNNT)r*   r   )r*   N)*__name__
__module____qualname____doc__	__slots__r   intrP   floatr^   r	   r   listr   r   boolr)   _ContextStater-   r/   r1   r4   propertyr   setterrB   r@   rL   r   r   rT   rX   r[   r]   ra   rc   re   rf   rh   rk   rm   ro   r'   r'   r'   r(   r   %   s    
	

'
		
r   )'rQ   rG   typingr   r   r   ddtrace._trace._span_linkr   ddtrace.constantsr   r   r   ddtrace.internal.compatr	   ddtrace.internal.constantsr
   rW   r   r   ddtrace.internal.loggerr   ddtrace.internal.threadsr   ddtrace.internal.utils.httpr   rF   tupleru   r^   rP   rw   rx   ry   compiler$   rp   logobjectr   r'   r'   r'   r(   <module>   s<    



