o
    9&iN                     @   sf   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	m
Z
 d dlmZ G dd dejZdS )	    N)trace)Context)textmap)format_span_idformat_trace_id)
TraceStatec                	   @   s   e Zd ZdZdZdZdZeeZ	de
jfde
jdeje de
je
j d	efd
dZde
jfde
jdeje de
je
j d	dfddZed	eje fddZdS )TraceContextTextMapPropagatorz6Extracts and injects using w3c TraceContext's headers.Ztraceparent
tracestatezK^[ 	]*([0-9a-f]{2})-([0-9a-f]{32})-([0-9a-f]{16})-([0-9a-f]{2})(-.*)?[ 	]*$Ncarriercontextgetterreturnc              	   C   s   |du rt  }||| j}|s|S t| j|d }|s|S |d}|d}|d}|d}	|dks;|dkr=|S |d	krH|d
rH|S |dkrN|S ||| j}
|
du r\d}nt	|
}t
jt|dt|ddt
t|	d|d}t
t
||S )zExtracts SpanContext from the carrier.

        See `mysql.opentelemetry.propagators.textmap.TextMapPropagator.extract`
        Nr               Z 00000000000000000000000000000000Z0000000000000000Z00   ff   T)trace_idspan_idZ	is_remotetrace_flagstrace_state)r   get_TRACEPARENT_HEADER_NAMEresearch_TRACEPARENT_HEADER_FORMAT_REgroup_TRACESTATE_HEADER_NAMEr   Zfrom_headerr   ZSpanContextintZ
TraceFlagsZset_span_in_contextZNonRecordingSpan)selfr
   r   r   headermatchversionr   r   r   Ztracestate_headersr	   span_context r&   ]C:\wamp64\www\opt\env\Lib\site-packages\mysql/opentelemetry/trace/propagation/tracecontext.pyextract$   s>   






z%TraceContextTextMapPropagator.extractsetterc                 C   s~   t |}| }|t jkrdS dt|j dt|j d|jd}|	|| j
| |jr=|j }|	|| j| dS dS )z~Injects SpanContext into the carrier.

        See `mysql.opentelemetry.propagators.textmap.TextMapPropagator.inject`
        Nz00--02x)r   Zget_current_spanZget_span_contextZINVALID_SPAN_CONTEXTr   r   r   r   r   setr   r   Z	to_headerr   )r!   r
   r   r)   spanr%   Ztraceparent_stringZtracestate_stringr&   r&   r'   injectW   s   


&
z$TraceContextTextMapPropagator.injectc                 C   s   | j | jhS )zReturns a set with the fields set in `inject`.

        See
        `mysql.opentelemetry.propagators.textmap.TextMapPropagator.fields`
        )r   r   )r!   r&   r&   r'   fieldsk   s   z$TraceContextTextMapPropagator.fields)__name__
__module____qualname____doc__r   r   Z_TRACEPARENT_HEADER_FORMATr   compiler   r   Zdefault_getterZCarrierTtypingOptionalr   ZGetterr(   Zdefault_setterZSetterr.   propertySetstrr/   r&   r&   r&   r'   r      s>    


6

r   )r   r5   Zmysql.opentelemetryr   Z#mysql.opentelemetry.context.contextr   Zmysql.opentelemetry.propagatorsr   Zmysql.opentelemetry.tracer   r   Zmysql.opentelemetry.trace.spanr   ZTextMapPropagatorr   r&   r&   r&   r'   <module>   s   