U
    4JeN                     @   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e
jeje e
je
j edddZde
jfe
jeje e
je
j dd	d
dZeeje d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krt  }||| j}|s$|S t| j|d }|s>|S |d}|d}|d}|d}	|dksv|dkrz|S |d	kr|d
r|S |dkr|S ||| j}
|
dkrd}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&   V/tmp/pip-unpacked-wheel-7_167w8m/mysql/opentelemetry/trace/propagation/tracecontext.pyextract$   s>    






z%TraceContextTextMapPropagator.extract)r
   r   setterr   c                 C   sz   t |}| }|t jkr dS dt|j dt|j d|jd}|	|| j
| |jrv|j }|	|| j| dS )z~Injects SpanContext into the carrier.

        See `mysql.opentelemetry.propagators.textmap.TextMapPropagator.inject`
        Nz00--Z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.inject)r   c                 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      s0   

6
r   )r   r4   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   