o
    :Fd                     @   sT   d dl Z d dlZd dlZedeZee  e	ej
 G dd dZdS )    Nz{}c                   @   sL   e Zd ZdZdZdZdddddZed	d
 Zedd Z	edddZ
dS )MetricsZOCI_CLI_METRICS_INV_DELAY   ZOCI_CLI_METRICS_PATHZnum_invocationsZnum_auth_failuresZnum_slow_invocationsZnum_conn_failures)ZNUM_INVOCATIONSZNUM_AUTH_FAILURESZNUM_SLOW_INVOCATIONSZNUM_CONN_FAILURESc                 C   sF   t j}t jtjv r!ztttt j}W |S  ty    Y |S w |S N)	r   !CLI_STANDARD_PRE_INVOCATION_DELAY%CLI_STANDARD_PRE_INVOCATION_DELAY_VARosenvironabsintgetenv	Exception)clsdelay r   7usr/lib/python3.10/site-packages/oci_cli/cli_metrics.pyget_pre_invocation_delay   s   z Metrics.get_pre_invocation_delayc                 C   s   t jtjv S r   )r   CLOUDSHELL_ENV_VARr   r   )r   r   r   r   is_metrics_enabled'   s   zMetrics.is_metrics_enabledFc              	   C   s  t  s|rttj d  d S dd l}t	t j
}tj|t j| }|rCttj d |  ttj d t j|   d}zt|}t|  }W d    n1 s]w   Y  W n tyl   Y n	 tyt   Y nw |d7 }|rttj d t|  t|d?}z|||j|jB  W n ty   |rttj d  Y W d    d S w |d	|  |||j W d    n1 sw   Y  |rttj d
  d S d S )NzMetrics is not enabledr   zMetrics is enabled with value: zMetric being logged:    zNew value of metric: wz*Unable to write metric to file due to lockz%d
z Metric file updated successfully)r   r   loggerdebugociZbase_clientZutc_nowfcntlr   r   r   pathjoinMETRICSopenr
   readstripr   IOErrorstrflockLOCK_EXLOCK_NBwriteLOCK_UN)r   Zmetric_namer   r   Z
metric_dirZmetric_fileZmetric_valuemr   r   r   update_metric+   sN   

zMetrics.update_metricN)F)__name__
__module____qualname__r   r   r   r   classmethodr   r   r(   r   r   r   r   r      s    


r   )r   r   logging	getLoggerformatr)   r   
addHandlerNullHandlersetLevelDEBUGr   r   r   r   r   <module>   s   