o
    pfK                     @   s   d dl Z d dlZd dlmZ d dlmZmZ d dlmZ e	ej
 e	ej e	ej e  G dd deZeddZe  d	efd
dZd	efddZdS )    N)	Collector)GaugeMetricFamilyREGISTRY)Storagec                       s2   e Zd Zdef fddZdd Zdd Z  ZS )TilekilnCollectorstoragec                    s   || _ t   d| _d S )Nr   )_TilekilnCollector__storagesuper__init___TilekilnCollector__i)selfr   	__class__ L/home/ertert/spirit/venv/lib/python3.10/site-packages/tilekiln/prometheus.pyr
      s   

zTilekilnCollector.__init__c              	   c   s    t ddddgd}t ddg dd}t d	d
ddgd}| j D ]B}||jt|jg|j ||jt|jg|j t	dt
|jd D ]}||jt|jt|jd | g|jd |  qGq!|V  |V  |V  d S )Ntilekiln_stored_bytes_sumzTotal size of tilestilesetzoom)labelstilekiln_stored_byteszTile percentiles)r   r   quantiletilekiln_stored_countzTiles in tilekiln storager      )r   r   metrics
add_metricidstrr   size	num_tilesrangelenpercentiles)r   r   	quantilestotalmetricir   r   r   collect   s*   "
zTilekilnCollector.collectc                 C   s   | j d | _ d S )Nr   )r   )r   r   r   r   update/   s   zTilekilnCollector.update)__name__
__module____qualname__r   r
   r&   r'   __classcell__r   r   r   r   r      s    r    tilekiln_metrics_storage_secondszTime spent updating metricsr   c                 C   s   |    dS )zUpdate storage metrics while tracking call time

    The easiest way to monitor a function is to annotate it. Rather than require
    prometheus in storage.py, we wrap it and annotate the wrapper to track call time.
    N)update_metrics)r   r   r   r   monitored_update_metrics8   s   r.   c                 C   s6   t | }t| tj||d 	 t|  t| q)z+Start a prometheus server for storage info.)portaddr)r   r   registerprometheus_clientstart_http_serverr.   timesleep)r   r0   r/   r5   	collectorr   r   r   serve_prometheusB   s   

r7   )r4   r2   prometheus_client.registryr   prometheus_client.corer   r   tilekiln.storager   
unregisterGC_COLLECTORPLATFORM_COLLECTORPROCESS_COLLECTORdisable_created_metricsr   SummaryMETRIC_UPDATE_TIMEr.   r7   r   r   r   r   <module>   s     !	