o
    iV!                     @   s&  d dl Z d dlZd dlZd dlZd dl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 eeZzLd 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 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* dZ+dZ,W n e-y   dZ+e. Z,dZe
Ze
Z'dZdZ e
Z&Y nw de/fddZ0	d2de1de1de2e1e1f dB de'fddZ3dd Z4de1de1de1de'fdd Z5d!ee1e1f dB dedB fd"d#Z6d$d% Z7				d3d&e1d'e8d(e8dB d)e2e1e
f dB d*edB d+e
fd,d-Z9dedB fd.d/Z:ed0d1 Z;dS )4    N)Mapping)contextmanager)Any)init_logger)TRACE_HEADERSLoadingSpanAttributes)trace)Context)OTLPSpanExporter)inject)"OTEL_EXPORTER_OTLP_TRACES_PROTOCOL)Resource)TracerProvider)BatchSpanProcessor)SpanKindTracerset_tracer_provider)TraceContextTextMapPropagatorTFreturnc                   C   s   t S N)_IS_OTEL_AVAILABLE r   r   G/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm/tracing/otel.pyis_otel_available8   s   r   instrumenting_module_nameotlp_traces_endpointextra_attributesc                 C   s   t s	tdt |tjd< i }| |d< tt |d< |r#|| t	|}t
|d}t|}|t| t| t|j || }|S )z.Initializes the OpenTelemetry tracer provider.z|OpenTelemetry is not available. Unable to initialize a tracer. Ensure OpenTelemetry packages are installed. Original error:
"OTEL_EXPORTER_OTLP_TRACES_ENDPOINTzvllm.instrumenting_module_namezvllm.process_id)resource)r   
ValueErrorotel_import_error_tracebackosenvironstrgetpidupdater   creater   get_span_exporteradd_span_processorr   r   atexitregistershutdown
get_tracer)r   r   r   resource_attrsr   trace_providerspan_exportertracerr   r   r   init_otel_tracer<   s(   




r1   c                 C   sL   t jtd}|dkrt| dd}|S |dkrt| d}|S td| d)NgrpcT)endpointinsecurezhttp/protobuf)r3   zUnsupported OTLP protocol 'z' is configured)r!   r"   getr   OTLPGrpcExporterOTLPHttpExporterr   )r3   protocolexporterr   r   r   r'   ^   s   
r'   process_kindprocess_namec                 C   s*   t jd}|s
dS ||d}t| ||S )zP
    Backend-specific initialization for OpenTelemetry in a worker process.
    r   N)zvllm.process_kindzvllm.process_name)r!   r"   r5   r1   )r   r:   r;   otlp_endpointextra_attrsr   r   r   init_otel_worker_traceri   s   
r>   headersc                 C   s   t r
| r
t | S dS )z#Extracts context from HTTP headers.N)r   r   extract)r?   r   r   r   extract_trace_context   s   rA   c              	      s   t jjt jjt jjjt jt	jj
i |r | |p!jjt fdd}t fdd}trJ|S |S )z4Internal wrapper logic for sync and async functions.c               	      s   t }t }|j| d- t  | i |I d H W  d    W  d    S 1 s2w   Y  W d    d S 1 sBw   Y  d S N)context
attributesrecord_exceptionr   r,   _get_smart_contextstart_as_current_spanpropagate_trace_to_envargskwargsr0   ctx
code_attrsfinal_span_namefuncmodule_namerE   r   r   async_wrapper   s   
	Rz&instrument_otel.<locals>.async_wrapperc               	      s   t }t }|j| d* t  | i |W  d    W  d    S 1 s.w   Y  W d    d S 1 s>w   Y  d S rB   rF   rJ   rN   r   r   sync_wrapper   s   
	Rz%instrument_otel.<locals>.sync_wrapper)r   CODE_FUNCTION__qualname__CODE_NAMESPACE
__module__CODE_FILEPATH__code__co_filenameCODE_LINENOr#   co_firstlinenor%   	functoolswrapsinspectiscoroutinefunction)rQ   	span_namerD   rE   rS   rT   r   rN   r   instrument_otel   s   


rc   rb   
start_timeend_timerD   rC   kindc           
      C   s   t sdS tt}|dur|nt }| ||d}|dur ||d< |jdi |}	|r/|	| |dur;|	j|d dS |	  dS )z8Manually create and end a span with explicit timestamps.N)namerC   rd   rf   )re   r   )r   r   r,   __name__rG   
start_spanset_attributesend)
rb   rd   re   rD   rC   rf   r0   rM   span_kwargsspanr   r   r   manual_instrument_otel   s    	

rn   c                  C   sx   t  } |  jrdS i }tjdtjd }r||d< tjdtjd }r/||d< |s6ttj}t 	|S )z
    Determines the parent context.
    1. If a Span is already active in this process, use it.
    2. If not, extract from os.environ, handling the case-sensitivity mismatch.
    NtraceparentTRACEPARENT
tracestate
TRACESTATE)
r   get_current_spanget_span_contextis_validr!   r"   r5   dictr   r@   )current_spancarriertptsr   r   r   rG      s   

rG   c                  c   s    t sdV  dS dd tD } z%ttj dV  W |  D ]\}}|du r-tj|d q|tj|< qdS |  D ]\}}|du rItj|d q9|tj|< q9w )z
    Temporarily injects the current OTel context into os.environ.
    This ensures that any subprocesses (like vLLM workers) spawned
    within this context inherit the correct traceparent.
    Nc                 S   s   i | ]	}|t j|qS r   )r!   r"   r5   ).0kr   r   r   
<dictcomp>   s    z*propagate_trace_to_env.<locals>.<dictcomp>)r   r   r   r!   r"   itemspop)original_statekeyoriginal_valuer   r   r   rI      s"   
rI   r   )NNNN)<r)   r^   r`   r!   	tracebackcollections.abcr   
contextlibr   typingr   vllm.loggerr   vllm.tracing.utilsr   r   rh   loggeropentelemetryr   opentelemetry.context.contextr	   5opentelemetry.exporter.otlp.proto.grpc.trace_exporterr
   r6   5opentelemetry.exporter.otlp.proto.http.trace_exporterr7   opentelemetry.propagater   'opentelemetry.sdk.environment_variablesr   opentelemetry.sdk.resourcesr   opentelemetry.sdk.tracer   opentelemetry.sdk.trace.exportr   opentelemetry.tracer   r   r   ,opentelemetry.trace.propagation.tracecontextr   r   r    ImportError
format_excboolr   r#   rv   r1   r'   r>   rA   rc   intrn   rG   rI   r   r   r   r   <module>   s   
"
"4
!