o
    i                     @  s   d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
mZmZ ddlmZ d	Zdd
lmZ ejZ	 dZedddZG dd deZG dd deZdS )    )annotationsN)Iterator)contextmanager)baggagecontext)SpanSpanProcessor   )logfire_json_dumps)get_baggageset_baggage)truncate_stringi  valuesstrreturnIterator[None]c               	   k  s    t  }|  D ]?\}}t|ts'tjd| dt|j ddd t	|}t
|tkrAtjd| dt ddd t|td}t|||}q	t |}zd	V  W t | d	S t | w )
a  Context manager that attaches key/value pairs as OpenTelemetry baggage to the current context.

    See the [Baggage documentation](https://logfire.pydantic.dev/docs/reference/advanced/baggage/) for more details.

    Note: this function should always be used in a `with` statement; if you try to open and close it manually you may
    run into surprises because OpenTelemetry Baggage is stored in the same contextvar as the current span.

    Args:
        values: The key/value pairs to attach to baggage. These should not be large or sensitive.
            Strings longer than 1000 characters will be truncated with a warning.

    Example usage:

    ```python
    from logfire import set_baggage

    with set_baggage(my_id='123'):
        # All spans opened inside this block will have baggage '{"my_id": "123"}'
        with set_baggage(my_session='abc'):
            # All spans opened inside this block will have baggage '{"my_id": "123", "my_session": "abc"}'
            ...
    ```
    Baggage value for key "" is of type "z". Converting to string.   )
stacklevel" is too long. Truncating to z characters.
max_lengthN)r   get_currentitems
isinstancer   warningswarntype__name__r
   lenMAX_BAGGAGE_VALUE_LENGTHr   r   r   attachdetach)r   current_contextkeyvaluetoken r(   _/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/logfire/_internal/baggage.pyr   !   s(   

r   c                   @  s   e Zd Zd	d
ddZdS )NoForceFlushSpanProcessor0u  timeout_millisintr   boolc                 C  s   dS )NTr(   )selfr,   r(   r(   r)   force_flushS   s   z%NoForceFlushSpanProcessor.force_flushN)r+   )r,   r-   r   r.   )r   
__module____qualname__r0   r(   r(   r(   r)   r*   P   s    r*   c                   @  s   e Zd Zd
ddd	ZdS )$DirectBaggageAttributesSpanProcessorNspanr   parent_contextcontext.Context | Noner   Nonec                 C  s   |j pi }i }t| D ]D\}}t|ts'td| dt|j	 d qt
|tkr?td| dt d t|td}||v rN|| |krJqd| }|||< q|| d S )Nr   r   z!", skipping setting as attribute.r   z( characters before setting as attribute.r   zbaggage_conflict.)
attributesr   get_allr   r   r   r   r   r   r   r    r!   r   set_attributes)r/   r4   r5   existing_attrsattrskvr(   r(   r)   on_startX   s&   


z-DirectBaggageAttributesSpanProcessor.on_start)N)r4   r   r5   r6   r   r7   )r   r1   r2   r?   r(   r(   r(   r)   r3   W   s    r3   )r   r   r   r   )
__future__r   r   collections.abcr   
contextlibr   opentelemetryr   r   opentelemetry.sdk.tracer   r   json_encoderr
   __all__utilsr   r9   r   r!   r   r*   r3   r(   r(   r(   r)   <module>   s     
.