
    ng\jd                     t   % S r SSKJr  SSKJrJrJrJr  SSKJrJ	r	J
r
JrJrJr  SSKJrJr  SSKJr  SSKJr  SSKJrJrJr  \(       a  SS	KJr  SS
KJr  SSKJr  Sq\\   \ S'   Sr!Sr"\" S/SSS9       SXS\#S\$S\\#   S\\%   S\\#   S\\&   S\\'   S\\%   S\\%   SS4S jj5       r(SSS\$SS4S  jr) SYS!\#S"\	S#\%SS4S$ jjr*\" S%S&S'9SZS( j5       r+\" S%S&S'9S)\S   SS4S* j5       r,S!\#SS4S+ jr-S!\#S\$SS4S, jr.  S[S-\\   S.\\&   SS4S/ jjr/S0\#S1\#S2\'SS4S3 jr0S!\#SS4S4 jr1       S\S\\#   S\\%   S5\\#   S6\\%   S.\\&   S\\%   S7\'SS4S8 jjr2  S[S9\S:\\#   S;\\'   SS4S< jjr3\" S=/S>SS9   S]S?\$S@\\#   SA\\#   S=\\#   SS4
SB jj5       r4\" S=/S>SS9  S[SA\\#   S=\\#   SS4SC jj5       r5S\#S\%S5\#S6\%S1\#SS4SD jr6 S^SE\\   4SF jjr7SZSG jr8 S^SH\S!\\#   SS4SI jjr9 S^SJ\%SK\\
   SS4SL jjr:SM\%SS4SN jr;SOSPSQSPSK\SS4SR jr<S\\   4SS jr=S\ST   4SU jr>SZSV jr?S\'4SW jr@g)_aT  
Simple, clean API for recording observability metrics.

This module provides a straightforward interface for Redis core code to record
metrics without needing to know about OpenTelemetry internals.

Usage in Redis core code:
    from redis.observability.recorder import record_operation_duration

    start_time = time.monotonic()
    # ... execute Redis command ...
    record_operation_duration(
        command_name='SET',
        duration_seconds=time.monotonic() - start_time,
        server_address='localhost',
        server_port=6379,
        db_namespace='0',
        error=None
    )
    )datetime)TYPE_CHECKINGCallableListOptional)AttributeBuilderConnectionState	CSCReason	CSCResultGeoFailoverReasonPubSubDirection)CloseReasonRedisMetricsCollector)get_observability_instance)!get_observables_registry_instance)deprecated_argsdeprecated_functionstr_if_bytes)ConnectionPoolInterface)SyncDatabase)
OTelConfigN_metrics_collector	csc_itemsconnection_count
batch_sizezXThe batch_size argument is no longer used and will be removed in the next major version.z7.2.1)args_to_warnreasonversioncommand_nameduration_secondsserver_addressserver_portdb_namespaceerroris_blockingretry_attemptsreturnc	                     [         c  [        5       q [         c  g [         R                  U UUUUUUUUUS9
  g! [         a     gf = f)aF  
Record a Redis command execution duration.

This is a simple, clean API that Redis core code can call directly.
If observability is not enabled, this returns immediately with zero overhead.

Args:
    command_name: Redis command name (e.g., 'GET', 'SET')
    duration_seconds: Command execution time in seconds
    server_address: Redis server address
    server_port: Redis server port
    db_namespace: Redis database index
    error: Exception if command failed, None if successful
    is_blocking: Whether the operation is a blocking command
    batch_size: Number of commands in batch (for pipelines/transactions)
    retry_attempts: Number of retry attempts made

Example:
    >>> start = time.monotonic()
    >>> # ... execute command ...
    >>> record_operation_duration('SET', time.monotonic() - start, 'localhost', 6379, '0')
N)
r   r    r!   r"   r#   
error_typenetwork_peer_addressnetwork_peer_portr%   r&   )r   _get_or_create_collectorrecord_operation_duration	Exception)	r   r    r!   r"   r#   r$   r%   r   r&   s	            W/home/edenadmin/noVNC/venv/lib/python3.13/site-packages/redis/observability/recorder.pyr-   r-   2   sg    R !57%44%-)#%!/)#) 	5 	
  s   8 
AAconnection_poolr   c                     [         c  [        5       q [         c  g [         R                  U US9  g! [         a     gf = f)aO  
Record connection creation time.

Args:
    connection_pool: Connection pool implementation
    duration_seconds: Time taken to create connection in seconds

Example:
    >>> start = time.monotonic()
    >>> # ... create connection ...
    >>> record_connection_create_time('ConnectionPool<localhost:6379>', time.monotonic() - start)
Nr0   r    )r   r,   record_connection_create_timer.   r2   s     r/   r3   r3   t   sN    & !57%88+- 	9 	
     0 
==	pool_nameconnection_statecounterc                     [         c  [        5       q [         c  g [         R                  U UUS9  g! [         a     gf = f)a<  
Record a connection count change for a single state.

Args:
    pool_name: Connection pool identifier
    connection_state: State to update (IDLE or USED)
    counter: Number to add (positive) or subtract (negative)

Example:
    # New connection created (goes to IDLE first)
    >>> record_connection_count('pool_abc123', ConnectionState.IDLE, 1)

    # Acquire from pool (transition)
    >>> record_connection_count('pool_abc123', ConnectionState.IDLE, -1)
    >>> record_connection_count('pool_abc123', ConnectionState.USED, 1)

    # Release to pool (transition)
    >>> record_connection_count('pool_abc123', ConnectionState.USED, -1)
    >>> record_connection_count('pool_abc123', ConnectionState.IDLE, 1)

    # Pool disconnect 5 idle connections
    >>> record_connection_count('pool_abc123', ConnectionState.IDLE, -5)
Nr5   r6   r7   )r   r,   record_connection_countr.   r9   s      r/   r:   r:      sQ    < !57%22- 	3 	

     1 
>>z{Connection count is now tracked via record_connection_count(). This functionality will be removed in the next major versionz7.4.0)r   r   c                  f    [        5       n U c  gS n U R                  US9  g! [         a     gf = f)z:
Initialize observable gauge for connection count metric.
Nc                     [        5       nUR                  [        5      n/ nU H  nUR                  U" 5       5        M     U$ N)r   getCONNECTION_COUNT_REGISTRY_KEYextend__observables_registry	callbacksobservationscallbacks        r/   observable_callback2init_connection_count.<locals>.observable_callback   sC    @B(,,-JK	!H
+ "     rG   )r,   init_connection_countr.   )	collectorrH   s     r/   rL   rL      sI     )*I''( 	( 	
  s   # 
00connection_poolsc                    ^ ^ [        5       nUc  g SSKJm  UU 4S jn[        5       nUR	                  [
        U5        g! [         a     gf = f)z?
Add connection pools to connection count observable registry.
Nr   Observationc            	      z   > / n T H1  nUR                  5        H  u  p#U R                  T" X#S95        M     M3     U $ )N
attributes)get_connection_countappend)rF   r0   countrT   rQ   rN   s       r/   connection_count_callbackBregister_pools_connection_count.<locals>.connection_count_callback   sD    L#3)8)M)M)O%E ''E(QR *P $4  rJ   )r,   opentelemetry.metricsrQ   r   registerr@   r.   )rN   rM   rX   rD   rQ   s   `   @r/   register_pools_connection_countr\      sW     )*I5	   AB%%)+D	
  s   -A   
AAc                 ~    [         c  [        5       q [         c  g [         R                  U S9  g! [         a     gf = f)z
Record a connection timeout event.

Args:
    pool_name: Connection pool identifier

Example:
    >>> record_connection_timeout('ConnectionPool<localhost:6379>')
Nr5   )r   r,   record_connection_timeoutr.   r^   s    r/   r_   r_     K     !57%44 	5 	
     / 
<<c                     [         c  [        5       q [         c  g [         R                  U US9  g! [         a     gf = f)aP  
Record time taken to obtain a connection from the pool.

Args:
    pool_name: Connection pool identifier
    duration_seconds: Wait time in seconds

Example:
    >>> start = time.monotonic()
    >>> # ... wait for connection from pool ...
    >>> record_connection_wait_time('ConnectionPool<localhost:6379>', time.monotonic() - start)
Nr5   r    )r   r,   record_connection_wait_timer.   rc   s     r/   rd   rd     sN    $ !57%66- 	7 	
  r4   close_reasonr)   c                     [         c  [        5       q [         c  g [         R                  U US9  g! [         a     gf = f)a  
Record a connection closed event.

Args:
    close_reason: Reason for closing (e.g. 'error', 'application_close')
    error_type: Error type if closed due to error

Example:
    >>> record_connection_closed('ConnectionPool<localhost:6379>', 'idle_timeout')
Nre   r)   )r   r,   record_connection_closedr.   rg   s     r/   rh   rh   >  sN      !57%33%! 	4 	
  r4   connection_namemaint_notificationrelaxedc                     [         c  [        5       q [         c  g [         R                  U UUS9  g! [         a     gf = f)a?  
Record a connection timeout relaxation event.

Args:
    connection_name: Connection identifier
    maint_notification: Maintenance notification type
    relaxed: True to count up (relaxed), False to count down (unrelaxed)

Example:
    >>> record_connection_relaxed_timeout('localhost:6379_a1b2c3d4', 'MOVING', True)
Nri   rj   rk   )r   r,   !record_connection_relaxed_timeoutr.   rm   s      r/   rn   rn   \  sQ    $ !57%<<+1 	= 	

  r;   c                 ~    [         c  [        5       q [         c  g [         R                  U S9  g! [         a     gf = f)z
Record a connection handoff event (e.g., after MOVING notification).

Args:
    pool_name: Connection pool identifier

Example:
    >>> record_connection_handoff('ConnectionPool<localhost:6379>')
Nr^   )r   r,   record_connection_handoffr.   r^   s    r/   rp   rp   }  r`   ra   r*   r+   is_internalc           
          [         c  [        5       q [         c  g [         R                  U UUUUUUS9  g! [         a     gf = f)a  
Record error count.

Args:
    server_address: Server address
    server_port: Server port
    network_peer_address: Network peer address
    network_peer_port: Network peer port
    error_type: Error type (Exception)
    retry_attempts: Retry attempts
    is_internal: Whether the error is internal (e.g., timeout, network error)

Example:
    >>> record_error_count('localhost', 6379, 'localhost', 6379, ConnectionError(), 3)
Nr!   r"   r*   r+   r)   r&   rq   )r   r,   record_error_countr.   rs   s          r/   rt   rt     s]    4 !57%--)#!5/!)# 	. 	
  s   5 
AA	directionchannelshardedc                     [         c  [        5       q [         c  gUnUb   [        5       nUb  UR                  (       a  Sn [         R	                  U UUS9  g! [
         a     gf = f)a  
Record a PubSub message (published or received).

Args:
    direction: Message direction ('publish' or 'receive')
    channel: Pub/Sub channel name
    sharded: True if sharded Pub/Sub channel

Example:
    >>> record_pubsub_message(PubSubDirection.PUBLISH, 'channel', False)
N)ru   rv   rw   )r   r,   _get_confighide_pubsub_channel_namesrecord_pubsub_messager.   )ru   rv   rw   effective_channelconfigs        r/   r{   r{     sz    $ !57%  &"B"B $00% 	1 	

      A 
A#"A#consumer_namez[The consumer_name argument is no longer used and will be removed in the next major version.lag_secondsstream_nameconsumer_groupc                     [         c  [        5       q [         c  gUnUb   [        5       nUb  UR                  (       a  Sn [         R	                  U UUS9  g! [
         a     gf = f)z
Record the lag of a streaming message.

Args:
    lag_seconds: Lag in seconds
    stream_name: Stream name
    consumer_group: Consumer group name
    consumer_name: Consumer name
Nr   r   r   )r   r,   ry   hide_stream_namesrecord_streaming_lagr.   )r   r   r   r   effective_stream_namer}   s         r/   r   r     sz    , !57% (&":":$(!//#-) 	0 	

  r~   c           
      >   [         c  [        5       q [         c  gU (       d  g [        R                  " 5       R	                  5       n[        5       nUSL=(       a    UR                  n[        U [        5      (       a  U R                  5        H}  u  pgU(       a  SO
[        U5      nU H^  n	U	 HU  n
U
u  p[        U5      nUR                  S5      u  p[        SU[        U5      S-  -
  5      n[         R                  UUUS9  MW     M`     M     gU  Hz  n[        US   5      nU(       a  SOUnUS    HU  n
U
u  p[        U5      nUR                  S5      u  p[        SU[        U5      S-  -
  5      n[         R                  UUUS9  MW     M|     g! [         a     gf = f)a5  
Record streaming lag from XREAD/XREADGROUP response.

Parses the response and calculates lag for each message based on message ID timestamp.

Args:
    response: Response from XREAD/XREADGROUP command
    consumer_group: Consumer group name (for XREADGROUP)
    consumer_name: Consumer name (for XREADGROUP)
N-g        i  r   r      )r   r,   r   now	timestampry   r   
isinstancedictitemsr   splitmaxintr   r.   )responser   r   r   r}   r   r   stream_messagesr   messagesmessage
message_id_r   r   stream_entrys                   r/   "record_streaming_lag_from_responser     s   . !57%-lln&&( "$.K63K3K h%%080@,-D<3L & !0H#+(/
%1*%=
'1'7'7'<	&)#sS^d5J/J&K*??(3(=+9 @  $, !0	 1A& !)*<?;0A{%+AG$+MJ!-j!9J#-#3#3C#8LI"%c3Y$1F+F"GK&;;$/$9'5 <   /	 !)   s   C*F B F 
FFc                     [         c  [        5       q [         c  g [         R                  U UUUUS9  g! [         a     gf = f)a  
Record a maintenance notification count.

Args:
    server_address: Server address
    server_port: Server port
    network_peer_address: Network peer address
    network_peer_port: Network peer port
    maint_notification: Maintenance notification type (e.g., 'MOVING', 'MIGRATING')

Example:
    >>> record_maint_notification_count('localhost', 6379, 'localhost', 6379, 'MOVING')
Nr!   r"   r*   r+   rj   )r   r,   record_maint_notification_countr.   r   s        r/   r   r   h  sW    , !57%	::)#!5/1 	; 	
  s   3 
A A resultc                 ~    [         c  [        5       q [         c  g [         R                  U S9  g! [         a     gf = f)z]
Record a Client Side Caching (CSC) request.

Args:
    result: CSC result ('hit' or 'miss')
Nr   )r   r,   record_csc_requestr.   r   s    r/   r   r     sK     !57%-- 	. 	
  ra   c                      [         c  [        5       q [         c  gS n  [         R                  U S9  g! [         a     gf = f)z3
Initialize observable gauge for CSC items metric.
Nc                     [        5       nUR                  [        5      n/ nU H  nUR                  U" 5       5        M     U$ r>   )r   r?   CSC_ITEMS_REGISTRY_KEYrA   rB   s        r/   rH   +init_csc_items.<locals>.observable_callback  sC    @B(,,-CD	!H
+ " rJ   rK   )r   r,   init_csc_itemsr.   )rH   s    r/   r   r     sP     !57%))( 	* 	
  s   2 
??rG   c                    ^ ^^ [         c  [        5       q [         c  gSSKJm  UU U4S jn [	        5       nUR                  [        U5        g! [         a     gf = f)z
Adds given callback to CSC items observable registry.

Args:
    callback: Callback function that returns the cache size
    pool_name: Connection pool name for observability
Nr   rP   c                  B   > T " T" 5       [         R                  " TS9S9/$ )Nr^   rS   )r   build_csc_attributes)rQ   rG   r5   s   r/   csc_items_callback7register_csc_items_callback.<locals>.csc_items_callback  s(    
+@@9U
 	
rJ   )r   r,   rZ   rQ   r   r[   r   r.   )rG   r5   r   rD   rQ   s   ``  @r/   register_csc_items_callbackr     sY     !57% 2
@B%%&<>PQ s    A 
AArW   r   c                     [         c  [        5       q [         c  g [         R                  U US9  g! [         a     gf = f)zt
Record a Client Side Caching (CSC) eviction.

Args:
    count: Number of evictions
    reason: Reason for eviction
NrW   r   )r   r,   record_csc_evictionr.   r   s     r/   r   r     sN     !57%.. 	/ 	
  r4   bytes_savedc                 ~    [         c  [        5       q [         c  g [         R                  U S9  g! [         a     gf = f)zt
Record the number of bytes saved by using Client Side Caching (CSC).

Args:
    bytes_saved: Number of bytes saved
Nr   )r   r,   record_csc_network_savedr.   r   s    r/   r   r     sK     !57%33# 	4 	
  ra   	fail_fromr   fail_toc                     [         c  [        5       q [         c  g [         R                  U UUS9  g! [         a     gf = f)z
Record a geo failover.

Args:
    fail_from: Database failed from
    fail_to: Database failed to
    reason: Reason for the failover
Nr   r   r   )r   r,   record_geo_failoverr.   r   s      r/   r   r     sQ     !57%.. 	/ 	

  r;   c                  J    [        5       R                  5       n U b  U R                  R                  (       d  gU R	                  5       R                  [        R                  [        R                  5      n[        XR                  5      $ ! [         a     g[         a     gf = f)z
Get or create the global metrics collector.

Returns:
    RedisMetricsCollector instance if observability is enabled, None otherwise
N)r   get_provider_managerr}   enabled_telemetryget_meter_provider	get_meterr   
METER_NAMEMETER_VERSIONImportErrorr.   )managermeters     r/   r,   r,   9  s    ,.CCE?'.."B"B **,66!,,.C.Q.Q
 %UNN;;  s   6B
 AB
 

B"	B"!B"r   c                  t     [        5       R                  5       n U c  gU R                  $ ! [         a     gf = f)z
Get the OTel configuration from the observability manager.

Returns:
    OTelConfig instance if observability is enabled, None otherwise
N)r   r   r}   r.   )r   s    r/   ry   ry   T  s<    ,.CCE?~~ s   * * 
77c                      Sq g)zE
Reset the global collector (used for testing or re-initialization).
N)r    rJ   r/   reset_collectorr   d  s
    
 rJ   c                  4    [         c
  [        5       q [         SL$ )zg
Check if observability is enabled.

Returns:
    True if metrics are being collected, False otherwise
N)r   r,   r   rJ   r/   
is_enabledr   l  s     !57T))rJ   )NNNNNNN)r   )r'   N)NN)NNNNNNT)NNNr>   )A__doc__r   typingr   r   r   r   redis.observability.attributesr   r	   r
   r   r   r   redis.observability.metricsr   r   redis.observability.providersr   redis.observability.registryr   redis.utilsr   r   r   redis.connectionr   redis.multidb.databaser   redis.observability.configr   r   __annotations__r   r@   strfloatr   r.   boolr-   r3   r:   rL   r\   r_   rd   rh   rn   rp   rt   r{   r   r   r   r   r   r   r   r   r   r,   ry   r   r   r   rJ   r/   <module>r      s  *  : :  K D J J J835 7; H23 :$  2  e %)!%"&!%"& $$(::: SM: #	:
 3-: I: $: : SM: 
:
:z. 
H **%* * 
	*Z C

4 C
45	
:	6 
B +/&*;'# 
<  
	B	8 %)!%*.'+&*$(*SM*#* #3-*  }	*
 #* SM* * 
*^ ""%%c]% d^% 
	%P !"h "&$(#'	$$#$ SM$ C=	$
 
$
$N !"h %)#'GSMG C=G 
	G
GT$$$ $ 	$
 $ 
$P #'Y0>  $!!}! 
!L #'Y 
6	0  
	<(+@"A 6Xl+  *D *rJ   