o
    g                     @   s  U 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mZmZ d dlmZ d dlmZ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 G dd deZG dd deZdae
ej e d< eda!ee d< de
e" de
e" de
e	e"e"f  deddf
ddZ#ededZ$	d#de"dede
e	e"ee"e%e&e'ee" ee% ee& ee' f f  dee$ge$f fdd Z(de	e"ee"e%e&e'ee" ee% ee& ee' df	 f ddfd!d"Z)dS )$    N)wraps)Enum)AnyCallableDictOptionalSequenceUnionTypeVar)trace)SERVICE_NAMEResource)TracerProvider)BatchSpanProcessor)OTLPSpanExporter)	Component)Systemc                   @   s:   e Zd ZdZdZ	 dZ	 dZ	 dZ	 dede	fdd	Z
d
S )OpenTelemetryGranularityz+The granularity of the OpenTelemetry spans.none	operationoperation_and_segmentallotherreturnc                 C   s(   t jt jt jt jg}|| ||k S )zCompare two granularities.)r   ALLOPERATION_AND_SEGMENT	OPERATIONNONEindex)selfr   order r!   `/var/www/visachat/venv/lib/python3.10/site-packages/chromadb/telemetry/opentelemetry/__init__.py__lt__$   s   zOpenTelemetryGranularity.__lt__N)__name__
__module____qualname____doc__r   r   r   r   r   boolr#   r!   r!   r!   r"   r      s    r   c                       s"   e Zd Zdef fddZ  ZS )OpenTelemetryClientsystemc                    s>   t  | t|jj|jj|jjt|jjr|jjnd d S )Nr   )	super__init__	otel_initsettingschroma_otel_service_namechroma_otel_collection_endpointchroma_otel_collection_headersr   chroma_otel_granularity)r   r*   	__class__r!   r"   r,   0   s   
zOpenTelemetryClient.__init__)r$   r%   r&   r   r,   __classcell__r!   r!   r3   r"   r)   /   s    r)   tracerr   granularityotel_service_nameotel_collection_endpointotel_collection_headersotel_granularityr   c                 C   s`   |t jkrdS ttt| id}t|d}|ttt||d t	
| t	ta|adS )a4  Initializes module-level state for OpenTelemetry.

    Parameters match the environment variables which configure OTel as documented
    at https://docs.trychroma.com/deployment/observability.
    - otel_service_name: The name of the service for OTel tagging and aggregation.
    - otel_collection_endpoint: The endpoint to which OTel spans are sent
        (e.g. api.honeycomb.com).
    - otel_collection_headers: The headers to send with OTel spans
        (e.g. {"x-honeycomb-team": "abc123"}).
    - otel_granularity: The granularity of the spans to emit.
    N
attributes)resource)endpointheaders)r   r   r   r   strr   add_span_processorr   r   r   set_tracer_provider
get_tracerr$   r6   r7   )r8   r9   r:   r;   r>   providerr!   r!   r"   r-   B   s   


	
r-   T)bound
trace_nametrace_granularityr=   c                    s   dt dt f fdd}|S )z!A decorator that traces a method.fr   c                    sF   t  rt  fdd}|S t  fdd}|S )Nc                     s   t k r| i |I d H S ts| i |I d H S tj d tdtjdi | i |I d H W  d    S 1 sAw   Y  d S Nr<   pod_nameHOSTNAMEr7   r6   start_as_current_spanadd_attributes_to_current_spanosenvirongetargskwargsr=   rJ   rI   rH   r!   r"   async_wrapper   s   $z6trace_method.<locals>.decorator.<locals>.async_wrapperc                     s|   t k r| i |S ts| i |S tj d tdtjdi | i |W  d    S 1 s7w   Y  d S rK   rN   rT   rW   r!   r"   wrapper   s   $z0trace_method.<locals>.decorator.<locals>.wrapper)asyncioiscoroutinefunctionr   )rJ   rX   rY   r=   rI   rH   )rJ   r"   	decorator   s   
ztrace_method.<locals>.decorator)rF   )rH   rI   r=   r]   r!   r\   r"   trace_methodj   s   "r^   c                 C   s:   t tjkrdS tsdS t }|dd |  D  dS )z#Add attributes to the current span.Nc                 S   s   i | ]\}}|d ur||qS Nr!   ).0kvr!   r!   r"   
<dictcomp>   s    z2add_attributes_to_current_span.<locals>.<dictcomp>)r7   r   r   r6   r   get_current_spanset_attributesitems)r=   spanr!   r!   r"   rP      s   
rP   r_   )*rZ   rQ   	functoolsr   enumr   typingr   r   r   r   r   r	   r
   opentelemetryr   opentelemetry.sdk.resourcesr   r   opentelemetry.sdk.tracer   opentelemetry.sdk.trace.exportr   5opentelemetry.exporter.otlp.proto.grpc.trace_exporterr   chromadb.configr   r   r   r)   r6   Tracer__annotations__r7   rA   r-   rF   r(   floatintr^   rP   r!   r!   r!   r"   <module>   s   
 $
%
: