File: //var/opt/nydus/ops/mysql/connector/opentelemetry/__pycache__/context_propagation.cpython-39.pyc
a
�,�h� � @ s� d Z ddlmZmZmZ ddlmZmZ ervddlm Z e rZddl
mZ ddlm
Z
mZ nddlmZ ddlm
Z
mZ er�dd lmZ eed
�dd�Zeed
�dd�ZdS )z$Trace context propagation utilities.� )�
TYPE_CHECKING�Any�Callable� )�OTEL_ENABLED�TRACEPARENT_HEADER_NAME)�OTEL_SYSTEM_AVAILABLE)�trace)�format_span_id�format_trace_id� )�MySQLConnectionAbstract)�span�returnc C s6 | � � }d}t|j�}t|j�}d}d�||||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
Z00�-)Zget_span_contextr �trace_idr
�span_id�join)r �ctx�versionr r Ztrace_flags� r �d/opt/nydus/tmp/pip-target-wkfpz8uv/lib64/python/mysql/connector/opentelemetry/context_propagation.py�build_traceparent_header7 s
r )�methodr c s dt t t d�� fdd�}|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
r
)�cnx�args�kwargsr c s� t r
| js � | g|�R i |��S t�� }d}|�� rLt|�}| jt|fd� z.� | g|�R i |��}W |dur�| jtd� n|dur�| jtd� 0 |S )zContext propagation decorator.N)�value)�name) r Zotel_context_propagationr Zget_current_spanZis_recordingr Zquery_attrs_appendr Zquery_attrs_remove)r r r Zcurrent_spanZ tp_header�result�r r r �wrapperc s
�z)with_context_propagation.<locals>.wrapper)r )r r! r r r �with_context_propagationT s r"