
    KCj                         d Z ddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
  eddg d	      Z ed
dddgd      Z eddddg      Z edd      Z edddg      Z G d de      Zy)uo  
Prometheus metrics.

Exposes /metrics on a separate port (settings.METRICS_PORT) rather than
mixing it into the main API port — this is standard practice so metrics
scraping traffic is isolated from client-facing traffic and can have
different access controls (typically metrics endpoints are firewalled
to internal monitoring infra only, never exposed publicly).
    N)Request)Counter	Histogram)BaseHTTPMiddleware)Responsehttp_requests_totalzTotal HTTP requestsmethodpathstatus_codehttp_request_duration_secondszHTTP request latency in secondsr
   r   )
g{Gz?g?g?g      ?g      ?   g      @   
      )bucketscrop_analysis_queued_totalz Total crop analysis tasks queuedcropdistrictcrop_analysis_cache_hits_totalz.Total crop analysis requests served from cachemodel_degraded_totalz:Count of times a specific model returned a degraded result
model_namec                       e Zd ZdedefdZy)PrometheusMiddlewarerequestreturnc                   K   t        j                         } ||       d {   }t        j                         |z
  }|j                  j                  d      }|r|j                  n|j
                  j                  }t        j                  |j                  ||j                        j                          t        j                  |j                  |      j                  |       |S 7 ƭw)Nrouter	   )r
   r   )timeperf_counterscopegetr   urlREQUEST_COUNTlabelsr
   r   incREQUEST_LATENCYobserve)selfr   	call_nextstartresponseelapsedr   
path_labels           CC:\Crop_Prediction\Backend\crop-ai-system\app\middleware\metrics.pydispatchzPrometheusMiddleware.dispatch2   s     !!#"7++##%-
 !!'*#(UZZgkk.>.>
>>
@T@T 	 	

#%gnn:FNNwW ,s    C+C)CC+N)__name__
__module____qualname__r   r   r0        r/   r   r   1   s    g X r5   r   )__doc__r   fastapir   prometheus_clientr   r   starlette.middleware.baser   starlette.responsesr   r$   r'   ANALYSIS_QUEUEDANALYSIS_CACHE_HITSMODEL_DEGRADEDr   r4   r5   r/   <module>r>      s      0 8 (% #%v;	  &Z $4 
 @N- r5   