o
    #f                  	   @   sL  d dl Z 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 d dlmZ e eZdae Zejdejd	ejf d
ZdedefddZdedefddZd#dedeje
 ddfddZ	d#dedddeje
 de
fddZede
fddZ ede
de!fddZ"ede!ddfdd Z#ed!Z$ed"Z%dS )$    N)wraps)environ)uuid4)Context_RuntimeContext)OTEL_PYTHON_CONTEXT)entry_points_F.)boundfuncreturnc                    sR   t  dtjtjtjf dtjtjtjf dtjtj f fdd}tt|S )zvA decorator used to initialize the global RuntimeContext

    Returns:
        A wrapper of the decorated method.
    argskwargsr   c               	      s   t 3 td u r.d}tt|}ztttd|d  aW n t	y-   t
d| Y nw W d    n1 s8w   Y   | i |S )Ncontextvars_contextopentelemetry_context)groupnamezFailed to load context: %s)_RUNTIME_CONTEXT_LOCK_RUNTIME_CONTEXTr   getr   nextiterr   load	Exceptionlogger	exception)r   r   default_contextconfigured_contextr    Z/home/ubuntu/dev/venv/lib/python3.10/site-packages/mysql/opentelemetry/context/__init__.pywrapper*   s.   	z&_load_runtime_context.<locals>.wrapper)r   typingTupleAnyDictOptionalcastr	   )r   r!   r   r   r    _load_runtime_context#   s   
r(   keynamec                 C   s   | d t t  S )ae  To allow cross-cutting concern to control access to their local state,
    the RuntimeContext API provides a function which takes a keyname as input,
    and returns a unique key.
    Args:
        keyname: The key name is for debugging purposes and is not required to be unique.
    Returns:
        A unique string representing the newly created key.
    -)strr   )r)   r   r   r    
create_keyL   s   	r,   keycontextobjectc                 C   s   |dur	| | S t  | S )av  To access the local state of a concern, the RuntimeContext API
    provides a function which takes a context and a key as input,
    and returns a value.

    Args:
        key: The key of the value to retrieve.
        context: The context from which to retrieve the value, if None, the current context is used.

    Returns:
        The value associated with the key.
    N)r   get_current)r-   r.   r   r   r    	get_valueX   s   r1   valuec                 C   s&   |du rt  }| }||| < t|S )a  To record the local state of a cross-cutting concern, the
    RuntimeContext API provides a function which takes a context, a
    key, and a value as input, and returns an updated context
    which contains the new value.

    Args:
        key: The key of the entry to set.
        value: The value of the entry to set.
        context: The context to copy, if None, the current context is used.

    Returns:
        A new `Context` containing the value set.
    N)r0   copyr   )r-   r2   r.   
new_valuesr   r   r    	set_valueg   s
   r5   c                   C   s   t  S )zTo access the context associated with program execution,
    the Context API provides a function which takes no arguments
    and returns a Context.

    Returns:
        The current `Context` object.
    )r   r0   r   r   r   r    r0   ~   s   	r0   c                 C   s
   t | S )a  Associates a Context with the caller's current execution unit. Returns
    a token that can be used to restore the previous Context.

    Args:
        context: The Context to set as current.

    Returns:
        A token that can be used with `detach` to reset the context.
    )r   attach)r.   r   r   r    r6      s   
r6   tokenc                 C   s0   zt |  W dS  ty   td Y dS w )zResets the Context associated with the caller's current execution unit
    to the value it had before attaching a specified Context.

    Args:
        token: The Token that was returned by a previous call to attach a Context.
    zFailed to detach contextN)r   detachr   r   r   )r7   r   r   r    r8      s
   r8   suppress_instrumentationsuppress_http_instrumentation)N)&logging	threadingr"   	functoolsr   osr   uuidr   #mysql.opentelemetry.context.contextr   r   )mysql.opentelemetry.environment_variablesr   ,mysql.opentelemetry.util._importlib_metadatar   	getLogger__name__r   r   Lockr   TypeVarCallabler$   r	   r(   r+   r,   r&   r1   r5   r0   r/   r6   r8   _SUPPRESS_INSTRUMENTATION_KEY"_SUPPRESS_HTTP_INSTRUMENTATION_KEYr   r   r   r    <module>   sD   
)
