o
    3Ih*                     @   s6  d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZ d dlmZ eeeeefZedeeeeee	efZe eZdedejdee d	eeeje
eeeef d
f f  fddZdejdee d	ejfddZdedejdee d	ejfddZdejdee d	eej fddZG dd deZdS )    N)OrderedDict)MutableMapping)MappingOptionalSequenceTupleUnion)typeskeyvaluemax_lenreturn.c                 C   s  | rt | tstd|  dS t |trt||S t |trrd}g }|D ]I}t||}|du r5|| q$t|}|tvrNtd|j	| dd tD   dS |du rU|}n||krhtd| |j	t|j	  dS || q$t
|S tdt|j	| dd tD  dS )	ak  Checks if attribute value is valid and cleans it if required.

    The function returns the cleaned value or None if the value is not valid.

    An attribute value is valid if it is either:
        - A primitive type: string, boolean, double precision floating
            point (IEEE 754-1985) or integer.
        - An array of primitive type values. The array MUST be homogeneous,
            i.e. it MUST NOT contain values of different types.

    An attribute needs cleansing if:
        - Its length is greater than the maximum allowed length.
        - It needs to be encoded/decoded e.g, bytes to strings.
    +invalid key `%s`. must be non-empty string.NzLInvalid type %s in attribute '%s' value sequence. Expected one of %s or Nonec                 S      g | ]}|j qS  __name__.0
valid_typer   r   a/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/opentelemetry/attributes/__init__.py
<listcomp>U   s    z$_clean_attribute.<locals>.<listcomp>z>Attribute %r mixes types %s and %s in attribute value sequencezYInvalid type %s for attribute '%s' value. Expected one of %s or a sequence of those typesc                 S   r   r   r   r   r   r   r   r   t       )
isinstancestr_loggerwarning_VALID_ATTR_VALUE_TYPES_clean_attribute_valuer   appendtyper   tuple)r
   r   r   sequence_first_valid_typecleaned_seqelementelement_typer   r   r   _clean_attribute*   sV   





r&   c                 C   sb  | d u s	t | tr|d urt | tr| d | } | S t | trCi }|  D ]\}}|r0t |ts7td| q%t|||d||< q%|S t | trd }g }| D ]K}|d u rZ|	| qN|d urit |tri|d | }t
|}|tvr{t||d}t
|}|d u r|}n||krtd|jt
|j  d S |	| qNt|S tdt
| j ddd tD  d	)
Nr   )r
   r   r   r   z1Mixed types %s and %s in attribute value sequencezInvalid type z& for attribute value. Expected one of c                 S   r   r   r   r   r   r   r   r      r   z3_clean_extended_attribute_value.<locals>.<listcomp>z or a sequence of those types)r   r   r   r   itemsr   r   _clean_extended_attributer   r   r    _clean_extended_attribute_valuer   r!   	TypeError_VALID_ANY_VALUE_TYPES)r   r   cleaned_dictr
   r$   r"   r#   r%   r   r   r   r*   y   s^   


r*   c              
   C   sb   | rt | tstd|  dS zt||dW S  ty0 } ztd| | W Y d}~dS d}~ww )a.  Checks if attribute value is valid and cleans it if required.

    The function returns the cleaned value or None if the value is not valid.

    An attribute value is valid if it is an AnyValue.
    An attribute needs cleansing if:
        - Its length is greater than the maximum allowed length.
    r   Nr'   zAttribute %s: %s)r   r   r   r   r*   r+   )r
   r   r   	exceptionr   r   r   r)      s   r)   limitc                 C   sd   | d u rd S t | tr!z|  } W n ty    td Y d S w |d ur0t | tr0| d | } | S )Nz$Byte attribute could not be decoded.)r   bytesdecodeUnicodeDecodeErrorr   r   r   )r   r/   r   r   r   r      s   

r   c                   @   s   e Zd ZdZ					ddee deej dedee d	ef
d
dZ	de
fddZde
dejfddZde
dejddfddZde
ddfddZdd ZdefddZdd ZdS )BoundedAttributeszAn ordered dict with a fixed max capacity.

    Oldest elements are dropped when the dict is full and a new element is
    added.
    NTFmaxlen
attributes	immutablemax_value_lenextended_attributesc                 C   sr   |d urt |tr|dk rtd|| _d| _|| _|| _i | _t	 | _
|r4| D ]\}}|| |< q+|| _d S )Nr   z.maxlen must be valid int greater or equal to 0)r   int
ValueErrorr4   droppedr7   _extended_attributes_dict	threadingRLock_lockr(   
_immutable)selfr4   r5   r6   r7   r8   r
   r   r   r   r   __init__   s    


zBoundedAttributes.__init__r   c                 C   s   t | j S N)dictr=   rB   r   r   r   __repr__  s   zBoundedAttributes.__repr__r
   c                 C   s
   | j | S rD   )r=   rB   r
   r   r   r   __getitem__     
zBoundedAttributes.__getitem__r   c                 C   s  t | ddrt| jx | jd ur&| jdkr&|  jd7  _	 W d    d S | jr1t||| j}nt||| j}|d u rE	 W d    d S || j	v rO| j	|= n'| jd urvt
| j	| jkrvt| j	tsht| j	| _	| j	jdd |  jd7  _|| j	|< W d    d S 1 sw   Y  d S )NrA   Fr      )last)getattrr+   r@   r4   r;   r<   r)   r7   r&   r=   lenr   r   popitem)rB   r
   r   r   r   r   __setitem__  s0   

"zBoundedAttributes.__setitem__c                 C   sD   t | ddrt| j | j|= W d    d S 1 sw   Y  d S )NrA   F)rM   r+   r@   r=   rH   r   r   r   __delitem__,  s
   
"zBoundedAttributes.__delitem__c                 C   s8   | j  t| j W  d    S 1 sw   Y  d S rD   )r@   iterr=   copyrF   r   r   r   __iter__2  s   $zBoundedAttributes.__iter__c                 C   s
   t | jS rD   )rN   r=   rF   r   r   r   __len__6  rJ   zBoundedAttributes.__len__c                 C   s
   | j  S rD   )r=   rS   rF   r   r   r   rS   9  rJ   zBoundedAttributes.copy)NNTNF)r   
__module____qualname____doc__r   r9   r	   _ExtendedAttributesboolrC   r   rG   AnyValuerI   rP   rQ   rT   rU   rS   r   r   r   r   r3      s2    
r3   ) loggingr>   collectionsr   collections.abcr   typingr   r   r   r   r   opentelemetry.utilr	   rZ   r   r0   r9   floatr   r    r,   	getLoggerr   r   AttributeValuer&   r[   r*   r)   r   r3   r   r   r   r   <module>   sd   
"
O
E

