U
    4Je                  	   @   sZ  d Z ddlmZ ddlm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 eeZd	ejfejee ejej ed
ddZd	ejfejee ejej d	dddZg Zee
dZedD ]VZe Zz$ee e!eded"   W q e#k
r&   e$de  Y qX qe%ea&ej'dddZ(ej'd	dddZ)d	S )ax  
API for propagation of context.

The propagators for the
``mysql.opentelemetry.propagators.composite.CompositePropagator`` can be defined
via configuration in the ``OTEL_PROPAGATORS`` environment variable. This
variable should be set to a comma-separated string of names of values for the
``opentelemetry_propagator`` entry point. For example, setting
``OTEL_PROPAGATORS`` to ``tracecontext,baggage`` (which is the default value)
would instantiate
``mysql.opentelemetry.propagators.composite.CompositePropagator`` with 2
propagators, one of type
``mysql.opentelemetry.trace.propagation.tracecontext.TraceContextTextMapPropagator``
and other of type ``mysql.opentelemetry.baggage.propagation.W3CBaggagePropagator``.
Notice that these propagator classes are defined as
``opentelemetry_propagator`` entry points in the ``pyproject.toml`` file of
``opentelemetry``.

Example::

    import flask
    import requests
    from opentelemetry import propagate


    PROPAGATOR = propagate.get_global_textmap()


    def get_header_from_flask_request(request, key):
        return request.headers.get_all(key)

    def set_header_into_requests_request(request: requests.Request,
                                            key: str, value: str):
        request.headers[key] = value

    def example_route():
        context = PROPAGATOR.extract(
            get_header_from_flask_request,
            flask.request
        )
        request_to_downstream = requests.Request(
            "GET", "http://httpbin.org/get"
        )
        PROPAGATOR.inject(
            set_header_into_requests_request,
            request_to_downstream,
            context=context
        )
        session = requests.Session()
        session.send(request_to_downstream.prepare())


.. _Propagation API Specification:
    https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/api-propagators.md
    )	getLogger)environ)Optional)Context)OTEL_PROPAGATORS)	compositetextmap)entry_pointsN)carriercontextgetterreturnc                 C   s   t  j| ||dS )aZ  Uses the configured propagator to extract a Context from the carrier.

    Args:
        getter: an object which contains a get function that can retrieve zero
            or more values from the carrier and a keys function that can get all the keys
            from carrier.
        carrier: and object which contains values that are
            used to construct a Context. This object
            must be paired with an appropriate getter
            which understands how to extract a value from it.
        context: an optional Context to use. Defaults to root
            context if not set.
    )r   )get_global_textmapextract)r
   r   r    r   J/tmp/pip-unpacked-wheel-7_167w8m/mysql/opentelemetry/propagate/__init__.pyr   S   s    r   )r
   r   setterr   c                 C   s   t  j| ||d dS )a  Uses the configured propagator to inject a Context into the carrier.

    Args:
        carrier: An object that contains a representation of HTTP
            headers. Should be paired with setter, which
            should know how to set header values on the carrier.
        context: An optional Context to use. Defaults to current
            context if not set.
        setter: An optional `Setter` object that can set values
            on the carrier.
    )r   r   N)r   inject)r
   r   r   r   r   r   r   h   s    r   ztracecontext,baggage,Zopentelemetry_propagator)groupnamez(Failed to load configured propagator: %s)r   c                   C   s   t S N_HTTP_TEXT_FORMATr   r   r   r   r      s    r   )http_text_formatr   c                 C   s   | a d S r   r   )r   r   r   r   set_global_textmap   s    r   )*__doc__loggingr   osr   typingr   Z#mysql.opentelemetry.context.contextr   Z)mysql.opentelemetry.environment_variablesr   Zmysql.opentelemetry.propagatorsr   r   Z,mysql.opentelemetry.util._importlib_metadatar	   __name__loggerZdefault_getterZCarrierTZGetterr   Zdefault_setterZSetterr   ZpropagatorsgetZenviron_propagatorssplitZ
propagatorstripappendnextiterload	Exception	exceptionZCompositePropagatorr   ZTextMapPropagatorr   r   r   r   r   r   <module>   sd   8



