
    ,h                     |    d Z ddlmZmZmZ ddlmZmZ erddlm	Z	 ddl
mZmZ erddlmZ d	ed
efdZded
efdZy)z$Trace context propagation utilities.    )TYPE_CHECKINGAnyCallable   )OTEL_ENABLEDTRACEPARENT_HEADER_NAME)trace)format_span_idformat_trace_id   )MySQLConnectionAbstractspanreturnc                     | j                         }d}t        |j                        }t        |j                        }d}dj                  ||||g      S )ah  Build a traceparent header according to the provided span.

    The context information from the provided span is used to build the traceparent
    header that will be propagated to the MySQL server. For particulars regarding
    the header creation, refer to [1].

    This method assumes version 0 of the W3C specification.

    Args:
        span (opentelemetry.trace.span.Span): current span in trace.

    Returns:
        traceparent_header (str): HTTP header field that identifies requests in a
        tracing system.

    References:
        [1]: https://www.w3.org/TR/trace-context/#traceparent-header
    00-)get_span_contextr   trace_idr
   span_idjoin)r   ctxversionr   r   trace_flagss         s/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/mysql/connector/opentelemetry/context_propagation.pybuild_traceparent_headerr   0   sP    ( 


!CGs||,HS[[)GK88Wh=>>    methodc                 <     dddt         dt         dt         f fd}|S )a  Perform trace context propagation.

    The trace context is propagated via query attributes. The `traceparent` header
    from W3C specification [1] is used, in this sense, the attribute name is
    `traceparent` (is RESERVED, avoid using it), and its value is built as per
    instructed in [1].

    If opentelemetry API/SDK is unavailable or there is no recording span,
    trace context propagation is skipped.

    References:
        [1]: https://www.w3.org/TR/trace-context/#traceparent-header
    cnxr   argskwargsr   c                 \   t         r| j                  s | g|i |S t        j                         }d}|j	                         r#t        |      }| j                  t        |f       	  | g|i |}|| j                  t               |S # || j                  t               w w xY w)zContext propagation decorator.N)value)name)	r   otel_context_propagationr	   get_current_spanis_recordingr   query_attrs_appendr   query_attrs_remove)r   r    r!   current_span	tp_headerresultr   s         r   wrapperz)with_context_propagation.<locals>.wrapper]   s     3#?#?#////--/	$$&0>I""*A9)M"N	EC1$1&1F$&&,C&D $&&,C&D %s   *B B+)r   )r   r-   s   ` r   with_context_propagationr.   N   s,    . s c c & Nr   N)__doc__typingr   r   r   	constantsr   r   opentelemetryr	   opentelemetry.trace.spanr
   r   	abstractsr   strr   r.    r   r   <module>r7      sN   : + 0 / < $H 3?3 ?3 ?<"X "( "r   