
    KCj5                     r    d 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
mZ  e
d      Z G d d	e      Zy)
a  
Request ID middleware.

Generates a unique ID per incoming request and:
1. Sets it in the request_id context var so every log line emitted
   while handling this request is automatically tagged with it.
2. Echoes it back in the X-Request-ID response header, so if the client
   reports "my request failed", they can give you the exact ID to grep
   in logs instead of "it failed around 3pm sometime."
    N)Request)BaseHTTPMiddleware)Response)
get_loggerrequest_id_ctxzapp.requestc                       e Zd ZdedefdZy)RequestContextMiddlewarerequestreturnc           
      N  K   |j                   j                  d      xs t        t        j                               }t        j                  |      }t        j                         }	  ||       d {   }t        j                  |       t        t        j                         |z
  dz        }||j                   d<   t        j                  |j                   d|j                  j                   d|j                    d| d       |S 7 # t        j                  |       w xY ww)NzX-Request-IDi   z -> z (zms))headersgetstruuiduuid4r   settimeperf_counterresetintloggerinfomethodurlpathstatus_code)selfr
   	call_next
request_idtokenstartresponse
elapsed_mss           KC:\Crop_Prediction\Backend\crop-ai-system\app\middleware\request_context.pydispatchz!RequestContextMiddleware.dispatch   s     __((8MC

<M
"":.!!#	(&w//H  '$++-5=>
+5(~~a 0 01h6J6J5K2j\Y\]	
  0  's1   A$D%'D 2D	3D 7BD%	D D""D%N)__name__
__module____qualname__r   r   r&        r%   r	   r	      s    g X r+   r	   )__doc__r   r   fastapir   starlette.middleware.baser   starlette.responsesr   app.core.logging_configr   r   r   r	   r*   r+   r%   <module>r1      s5   	    8 ( >	M	"1 r+   