o
    پi                  
   @   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mZ d dlmZ d dl	m
Z
mZmZ d dlmZmZ d dlmZ eeZh dZG dd	 d	eZG d
d deZG dd dZ		ddedeee  deee  de
e fddZdS )    N)ABCabstractmethod)datetime)ListOptionalUnion)EmbeddingReqInputGenerateReqInput)
ServerArgs>   
audio_data
image_data
video_datainput_embedsc                   @   sr   e Zd ZdZdedeee  deee  fddZde	e
ef ded	efd
dZede	e
ef defddZdS )RequestMetricsExporterzZAbstract base class for exporting request-level performance metrics to a data destination.server_argsobj_skip_namesout_skip_namesc                 C   s"   || _ |pt | _|pt | _d S N)r   setr   r   selfr   r   r    r   `/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/managers/request_metrics_exporter.py__init__   s   zRequestMetricsExporter.__init__objout_dictreturnc           
         s~   i }t |D ]}|j}| jvr"|tvr"t||}|dur"|||< q|di } fdd| D }dt	|i|}	|	S )zFormat request-level output data containing performance metrics. This method
        should be called prior to writing the data record with `self.write_record()`.N	meta_infoc                    s    i | ]\}}| j vr||qS r   )r   ).0kvr   r   r   
<dictcomp>5   s    z>RequestMetricsExporter._format_output_data.<locals>.<dictcomp>request_parameters)
dataclassesfieldsnamer   ALWAYS_EXCLUDE_FIELDSgetattrgetitemsjsondumps)
r   r   r   request_paramsfield
field_namevaluer   filtered_out_meta_inforequest_output_datar   r!   r   _format_output_data!   s$   



z*RequestMetricsExporter._format_output_datac                    s   dS )zZWrite a data record corresponding to a single request, containing performance metric data.Nr   )r   r   r   r   r   r   write_record?   s   z#RequestMetricsExporter.write_recordN)__name__
__module____qualname____doc__r
   r   r   strr   r   r	   r   dictr3   r   r4   r   r   r   r   r      s,    






r   c                       sn   e Zd ZdZdedeee  deee  f fddZdefdd	Z	d
d Z
deeef defddZ  ZS )FileRequestMetricsExportera  Lightweight `RequestMetricsExporter` implementation that writes records to files on disk.

    Records are written to files in the directory specified by `--export-metrics-to-file-dir`
    server launch flag. File names are of the form `"sglang-request-metrics-{hour_suffix}.log"`.
    r   r   r   c                    s<   t  ||| t|d| _tj| jdd d | _d | _d S )Nexport_metrics_to_file_dirT)exist_ok)superr   r(   
export_dirosmakedirs_current_file_handler_current_hour_suffixr   	__class__r   r   r   N   s
   
z#FileRequestMetricsExporter.__init__hour_suffixc              
   C   s   | j |krg| jdur,z| j  W n ty+ } ztd|  W Y d}~nd}~ww d| d}tj| j	|}zt
|ddd| _|| _ W dS  tyf } ztd| d	|  d| _d| _  d}~ww dS )
z<Ensure the file handler is open for the current hour suffix.Nz'Failed to close previous file handler: zsglang-request-metrics-z.logazutf-8)encodingzFailed to open log file z: )rC   rB   close	Exceptionloggerwarningr@   pathjoinr?   openerror)r   rF   elog_filenamelog_filepathr   r   r   _ensure_file_handler\   s*   

z/FileRequestMetricsExporter._ensure_file_handlerc              
   C   s   | j durAz4z| j   W n ty' } ztd|  W Y d}~nd}~ww W d| _ d| _dS W d| _ d| _dS d| _ d| _w dS )zClose the current file handler.NzFailed to close file handler: )rB   rI   rJ   rK   rL   rC   )r   rQ   r   r   r   rI   s   s    


z FileRequestMetricsExporter.closer   r   c              
      s   t |jtrd|jv rd S z.t }|d}| jd u r%W d S ||  fdd}t	
|I d H  W d S  tyW } ztd|  W Y d }~d S d }~ww )NHEALTH_CHECKz	%Y%m%d_%Hc                      s(   t  j jd j  d S )N
)r+   dumprB   writeflushr   metrics_datar   r   r   
write_file   s   z;FileRequestMetricsExporter.write_record.<locals>.write_filez&Failed to write perf metrics to file: )
isinstanceridr9   r   nowstrftimerT   rB   r3   asyncio	to_threadrJ   rK   	exception)r   r   r   current_timerF   r\   rQ   r   rZ   r   r4   ~   s    


z'FileRequestMetricsExporter.write_record)r5   r6   r7   r8   r
   r   r   r9   r   rT   rI   r   r	   r   r:   r4   __classcell__r   r   rD   r   r;   G   s     


r;   c                	   @   sf   e Zd ZdZ		ddedeee  deee  fddZdd	d
Z	de
fddZdeddfddZdS )RequestMetricsExporterManagerzIManager class for creating and managing RequestMetricsExporter instances.Nr   r   r   c                 C   s0   || _ |pt | _|pt | _g | _|   d S r   )r   r   r   r   
_exporters_create_exportersr   r   r   r   r      s
   z&RequestMetricsExporterManager.__init__r   c                 C   s\   | j t| j| j| j zddlm} | j || j| j| j W dS  ty-   Y dS w )zKCreate and configure RequestMetricsExporter instances based on server args.r   )(create_private_request_metrics_exportersN)	rg   extend create_request_metrics_exportersr   r   r   8sglang.private.managers.request_metrics_exporter_factoryri   ImportError)r   ri   r   r   r   rh      s   
z/RequestMetricsExporterManager._create_exportersc                 C   s   t | jdkS )z>Return true if at least one RequestMetricsExporter is enabled.r   )lenrg   r!   r   r   r   exporter_enabled   s   z.RequestMetricsExporterManager.exporter_enabledr   c                    s$   | j D ]}|||I dH  qdS )z.Write a record using all configured exporters.N)rg   r4   )r   r   r   exporterr   r   r   r4      s   
z*RequestMetricsExporterManager.write_recordNN)r   N)r5   r6   r7   r8   r
   r   r   r9   r   rh   boolro   r:   r4   r   r   r   r   rf      s    



rf   r   r   r   r   c                 C   s    g }| j r|t| || |S )zDCreate and configure `RequestMetricsExporter`s based on server args.)export_metrics_to_fileappendr;   )r   r   r   metrics_exportersr   r   r   rk      s   
rk   rq   )ra   r$   r+   loggingr@   abcr   r   r   typingr   r   r   sglang.srt.managers.io_structr   r	   sglang.srt.server_argsr
   	getLoggerr5   rK   r'   r   r;   rf   r   r9   rk   r   r   r   r   <module>   s4    
3U2

