
    g                        d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZmZmZ ddlmZ ddlZdd	lmZ e
eef   Zd
ZddZedddd       Ze G d de             Z G d de      Ze G d de             Zy)a  This module provides a thin wrapper around the OpenTelemetry propagate API to allow
the OpenTelemetry contexts (and therefore Logfire contexts) to be transferred between
different code running in different threads, processes or even services.

In general, you should not need to use this module since Logfire will automatically
patch [`ThreadPoolExecutor`][concurrent.futures.ThreadPoolExecutor] and
[`ProcessPoolExecutor`][concurrent.futures.ProcessPoolExecutor] to carry over the context.
And existing plugins exist to propagate the context with
[requests](https://pypi.org/project/opentelemetry-instrumentation-requests/) and
[httpx](https://pypi.org/project/opentelemetry-instrumentation-httpx/).
    )annotations)contextmanager)	dataclass)AnyIteratorMapping)context	propagatetrace)TextMapPropagatorN)warn_at_user_stacklevel)get_contextattach_contextContextCarrierc                 4    i } t        j                  |        | S )aj  Create a new empty carrier dict and inject context into it.

    Returns:
        A new dict with the context injected into it.

    Usage:

    ```py
    from logfire.propagate import get_context, attach_context

    logfire_context = get_context()

    ...

    # later on in another thread, process or service
    with attach_context(logfire_context):
        ...
    ```

    You could also inject context into an existing mapping like headers with:

    ```py
    from logfire.propagate import get_context

    existing_headers = {'X-Foobar': 'baz'}
    existing_headers.update(get_context())
    ...
    ```
    )r
   injectcarriers    F/var/www/openai/venv/lib/python3.12/site-packages/logfire/propagate.pyr   r      s    < !GWN    F)third_partyc             #    K   t        j                         }t        j                         }|s9t	        |t
        t        f      r#|j                  }t	        |t
        t        f      r#|j                  |       }	 t        j                  |       d t        j                  |       y# t        j                  |       w xY ww)a  Attach a context as generated by [`get_context`][logfire.propagate.get_context] to the current execution context.

    Since `attach_context` is a context manager, it restores the previous context when exiting.

    Set `third_party` to `True` if using this inside a library intended to be used by others.
    This will respect the [`distributed_tracing` argument of `logfire.configure()`][logfire.configure(distributed_tracing)],
    so users will be warned about unintentional distributed tracing by default and they can suppress it.
    See [Unintentional Distributed Tracing](https://logfire.pydantic.dev/docs/how-to-guides/distributed-tracing/#unintentional-distributed-tracing) for more information.
    r   N)
otel_contextget_currentr
   get_global_textmap
isinstance#WarnOnExtractTraceContextPropagatorNoExtractTraceContextPropagatorwrappedextractattach)r   r   old_context
propagatornew_contexts        r   r   r   B   s      **,K--/J&IKj%kl#++J &IKj%kl$$W$5K)K(K(K(s$   A#C&C9B( C(B??Cc                  <    e Zd ZU dZded<   ddZd	dZed        Zy)
WrapperPropagatorz-Helper base class to wrap another propagator.r   r   c                :     | j                   j                  |i |S N)r   r    selfargskwargss      r   r    zWrapperPropagator.extracta   s    #t||##T4V44r   c                :     | j                   j                  |i |S r(   )r   r   r)   s      r   r   zWrapperPropagator.injectd   s    "t||""D3F33r   c                .    | j                   j                  S r(   )r   fields)r*   s    r   r/   zWrapperPropagator.fieldsg   s    ||"""r   N)r+   r   r,   r   returnotel_context.Context)r+   r   r,   r   )	__name__
__module____qualname____doc____annotations__r    r   propertyr/    r   r   r&   r&   [   s)    754 # #r   r&   c                  :     e Zd ZdZ	 d	 	 	 	 	 	 	 	 	 d fdZ xZS )r   zA propagator that ignores any trace context that was extracted by the wrapped propagator.

    Used when `logfire.configure(distributed_tracing=False)` is called.
    c                    t        |   ||g|i |}||k(  r|S t        j                  t        j                  |      |      S r(   )superr    r   set_span_in_contextget_current_span)r*   r   r	   r+   r,   result	__class__s         r   r    z'NoExtractTraceContextPropagator.extractr   sJ     'CDCFCWM(()?)?)H&QQr   r(   
r   r   r	   zotel_context.Context | Noner+   r   r,   r   r0   r1   )r2   r3   r4   r5   r    __classcell__r?   s   @r   r   r   l   sN     04RR -R 	R
 R 
R Rr   r   c                  J     e Zd ZU dZdZded<   	 d	 	 	 	 	 	 	 	 	 d fdZ xZS )r   zA propagator that warns the first time that trace context is extracted by the wrapped propagator.

    Used when `logfire.configure(distributed_tracing=None)` is called. This is the default behavior.
    Fboolwarnedc                   t        |   ||g|i |}| j                  s^||k7  rYt        j                  |      t        j                  |      k7  r.d| _        d}t        |t               t        j                  |       |S )NTzFound propagated trace context. See https://logfire.pydantic.dev/docs/how-to-guides/distributed-tracing/#unintentional-distributed-tracing.)	r;   r    rE   r   r=   r   RuntimeWarninglogfirewarn)r*   r   r	   r+   r,   r>   messager?   s          r   r    z+WarnOnExtractTraceContextPropagator.extract   s{     'CDCFC{{v0U5K5KG5TX]XnXnouXv5vDKz  $G^<LL!r   r(   r@   )r2   r3   r4   r5   rE   r6   r    rA   rB   s   @r   r   r      sS    
 FD
 04 - 	
  
 r   r   )r0   r   )r   r   r   rD   r0   zIterator[None])r5   
__future__r   
contextlibr   dataclassesr   typingr   r   r   opentelemetryr	   r   r
   r   !opentelemetry.propagators.textmapr   rH   logfire._internal.stack_infor   strr   __all__r   r   r&   r   r   r8   r   r   <module>rT      s   
 # % ! ) ) C C ?  @ c"
; F CH ) )0 #) # # R&7 R( *;  r   