
    ng\j:G                     F   % S r SSKJr  SSKJrJrJr  SSK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  \(       a  SS	KJr  SS
KJr  SSKJr  Sq\\   \S'   SrS\\   4S jrS\S   4S jr      SHS\S\ S\\   S\\!   S\\   S\\"   S\\#   S\\!   SS4S jjr$SSS\ SS4S jr% SIS\S \S!\!SS4S" jjr&\" S#S$S%9SJS& 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*  SKS+\\   S,\\"   SS4S- jjr+S.\S/\S0\#SS4S1 jr,S\SS4S2 jr- SLS\S\!S3\S4\!S,\"S\!S5\#SS4S6 jjr.  SKS7\	S8\\   S9\\#   SS4S: jjr/  SKS;\ S<\\   S=\\   SS4S> jjr0 SMS=\\   SS4S? jjr1S\S\!S3\S4\!S/\SS4S@ jr2SASBSCSBSD\SS4SE jr3SJSF jr4S\#4SG jr5g)Na  
Async-compatible API for recording observability metrics.

This module provides an async-safe interface for Redis async client code to record
metrics without needing to know about OpenTelemetry internals. It reuses the same
RedisMetricsCollector and configuration as the sync recorder.

Usage in Redis async client code:
    from redis.asyncio.observability.recorder import record_operation_duration

    start_time = time.monotonic()
    # ... execute Redis command ...
    await 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ListOptional)ConnectionStateGeoFailoverReasonPubSubDirection)CloseReasonRedisMetricsCollector)get_observability_instance)!get_observables_registry_instance)deprecated_functionstr_if_bytes)ConnectionPool)AsyncDatabase)
OTelConfigN_async_metrics_collectorconnection_countreturnc                  p   [         b  [         $  [        5       R                  5       n U b  U R                  R                  (       d  gU R                  5       R                  [        R                  [        R                  5      n[        XR                  5      q [         $ ! [         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   r   get_provider_managerconfigenabled_telemetryget_meter_provider	get_meterr   
METER_NAMEMETER_VERSIONImportError	Exception)managermeters     _/home/edenadmin/noVNC/venv/lib/python3.13/site-packages/redis/asyncio/observability/recorder.py_get_or_create_collectorr#   /   s      +'',.CCE?'.."B"B **,66!,,.C.Q.Q
 $9#O ''  s   6B AB 
B5)	B54B5r   c                  |   #     [        5       R                  5       n U c  gU R                  $ ! [         a     gf = f7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"   _get_configr%   P   s>     ,.CCE?~~ s$   <, <, <
9<9<command_nameduration_secondsserver_addressserver_portdb_namespaceerroris_blockingretry_attemptsc                 z   #    [        5       nUc  g UR                  U UUUUUUUUUS9
  g! [         a     gf = f7f)a	  
Record a Redis command execution duration.

This is an async-safe API that Redis async client 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
    retry_attempts: Number of retry attempts made

Example:
    >>> start = time.monotonic()
    >>> # ... execute command ...
    >>> await 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#   record_operation_durationr   )	r&   r'   r(   r)   r*   r+   r,   r-   	collectors	            r"   r2   r2   `   sa     > )*I++%-)#%!/)#) 	, 	
  s   ;+ ;
8;8;connection_poolr   c                 j   #    [        5       nUc  g UR                  U US9  g! [         a     gf = f7f)z
Record connection creation time.

Args:
    connection_pool: Connection pool implementation
    duration_seconds: Time taken to create connection in seconds
N)r4   r'   )r#   record_connection_create_timer   )r4   r'   r3   s      r"   r6   r6      sI      )*I//+- 	0 	
     3# 3
0303	pool_nameconnection_statecounterc                 l   #    [        5       nUc  g UR                  U UUS9  g! [         a     gf = f7f)z
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)
N)r8   r9   r:   )r#   record_connection_countr   )r8   r9   r:   r3   s       r"   r<   r<      sL      )*I))- 	* 	

     4$ 4
1414z{Connection count is now tracked via record_connection_count(). This functionality will be removed in the next major versionz7.4.0)reasonversionc                  n   #    [        5       n U c  gS n U R                  US9  g! [         a     gf = f7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
+ "     )rJ   )r#   init_connection_countr   )r3   rK   s     r"   rN   rN      sK      )*I''( 	( 	
  s   5% 5
2525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7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)rI   r4   countrS   rQ   rO   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  rM   )r#   opentelemetry.metricsrQ   r   registerrD   r   )rO   r3   rW   rG   rQ   s   `   @r"   register_pools_connection_countr[      sY      )*I5	   AB%%)+D	
  s(   A-A A
AAAAc                 h   #    [        5       nUc  g UR                  U S9  g! [         a     gf = f7f)zU
Record a connection timeout event.

Args:
    pool_name: Connection pool identifier
Nr8   )r#   record_connection_timeoutr   r8   r3   s     r"   r^   r^     F      )*I++ 	, 	
     2" 2
/2/2c                 j   #    [        5       nUc  g UR                  U US9  g! [         a     gf = f7f)z
Record time taken to obtain a connection from the pool.

Args:
    pool_name: Connection pool identifier
    duration_seconds: Wait time in seconds
N)r8   r'   )r#   record_connection_wait_timer   )r8   r'   r3   s      r"   rc   rc     sI      )*I--- 	. 	
  r7   close_reasonr/   c                 j   #    [        5       nUc  g UR                  U US9  g! [         a     gf = f7f)z
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
N)rd   r/   )r#   record_connection_closedr   )rd   r/   r3   s      r"   rf   rf   5  sI      )*I**%! 	+ 	
  r7   connection_namemaint_notificationrelaxedc                 l   #    [        5       nUc  g UR                  U UUS9  g! [         a     gf = f7f)z
Record a connection timeout relaxation event.

Args:
    connection_name: Connection identifier (pool name)
    maint_notification: Maintenance notification type
    relaxed: True to count up (relaxed), False to count down (unrelaxed)
N)rg   rh   ri   )r#   !record_connection_relaxed_timeoutr   )rg   rh   ri   r3   s       r"   rk   rk   M  sL      )*I33+1 	4 	

  r=   c                 h   #    [        5       nUc  g UR                  U S9  g! [         a     gf = f7f)zw
Record a connection handoff event (e.g., after MOVING notification).

Args:
    pool_name: Connection pool identifier
Nr]   )r#   record_connection_handoffr   r_   s     r"   rm   rm   h  r`   ra   r0   r1   is_internalc           
      t   #    [        5       nUc  g UR                  U UUUUUUS9  g! [         a     gf = f7f)aL  
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)
N)r(   r)   r0   r1   r/   r-   rn   )r#   record_error_countr   )r(   r)   r0   r1   r/   r-   rn   r3   s           r"   rp   rp   }  sX     * )*I$$)#!5/!)# 	% 	
  s   8( 8
5858	directionchannelshardedc                    #    [        5       nUc  gUnUb4  [        5       I Sh  vN nUb  UR                  (       a  SnO[        U5      n UR	                  U UUS9  g N9! [
         a     gf = f7f)z
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
N)rq   rr   rs   )r#   r%   hide_pubsub_channel_namesr   record_pubsub_messager   )rq   rr   rs   r3   effective_channelr   s         r"   rv   rv     s      )*I  "}$&"B"B $ !-W 5''% 	( 	
 %  s3   !A/A&A/A A/
A,)A/+A,,A/lag_secondsstream_nameconsumer_groupc                    #    [        5       nUc  gUnUb(  [        5       I Sh  vN nUb  UR                  (       a  Sn UR                  U UUS9  g N-! [         a     gf = f7f)z
Record the lag of a streaming message.

Args:
    lag_seconds: Lag in seconds
    stream_name: Stream name
    consumer_group: Consumer group name
Nrx   ry   rz   )r#   r%   hide_stream_namesrecord_streaming_lagr   )rx   ry   rz   r3   effective_stream_namer   s         r"   r~   r~     s}      )*I ("}$&":":$(!&&#-) 	' 	
 %  s2   !A#AA#A A#
A A#A  A#c           
      6  #    [        5       nUc  gU (       d  g [        R                  " 5       R                  5       n[	        5       I Sh  vN nUSL=(       a    UR
                  n[        U [        5      (       a  U R                  5        Hy  u  pgU(       a  SO
[        U5      nU HZ  n	U	 HQ  n
U
u  p[        U5      nUR                  S5      u  p[        SU[        U5      S-  -
  5      nUR                  UUUS9  MS     M\     M{     gU  Hv  n[        US   5      nU(       a  SOUnUS    HQ  n
U
u  p[        U5      nUR                  S5      u  p[        SU[        U5      S-  -
  5      nUR                  UUUS9  MS     Mx     g GN<! [         a     gf = f7f)a  
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)
N-g        i  r|   r      )r#   r   now	timestampr%   r}   
isinstancedictitemsr   splitmaxintr~   r   )responserz   r3   r   r   r}   ry   stream_messagesr   messagesmessage
message_id_r   rx   stream_entrys                   r"   "record_streaming_lag_from_responser     s     )*I-lln&&( #}$"$.K63K3K h%%080@,-D<3L & !0H#+(/
%1*%=
'1'7'7'<	&)#sS^d5J/J&K!66(3(=+9 7  $, !0	 1A& !)*<?;0A{%+AG$+MJ!-j!9J#-#3#3C#8LI"%c3Y$1F+F"GK22$/$9'5 3   /	 !)1 %P  sH   F0F	 
FB=F	 F	A<F	 FF	 	
FFFFc                 p   #    [        5       nUc  g UR                  U UUUUS9  g! [         a     gf = f7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')
N)r(   r)   r0   r1   rh   )r#   record_maint_notification_countr   )r(   r)   r0   r1   rh   r3   s         r"   r   r   /  sR     " )*I	11)#!5/1 	2 	
  s   6& 6
3636	fail_fromr   fail_tor>   c                 l   #    [        5       nUc  g UR                  U UUS9  g! [         a     gf = f7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#   record_geo_failoverr   )r   r   r>   r3   s       r"   r   r   P  sL      )*I%% 	& 	

  r=   c                      Sq g)zK
Reset the global async collector (used for testing or re-initialization).
N)r    rM   r"   reset_collectorr   k  s
    
  $rM   c                  &   #    [        5       n U SL$ 7f)zg
Check if observability is enabled.

Returns:
    True if metrics are being collected, False otherwise
N)r#   )r3   s    r"   
is_enabledr   s  s      )*ID  s   )NNNNNN)r   )r   N)NN)TrB   )6__doc__r   typingr   r   r   redis.observability.attributesr   r   r	   redis.observability.metricsr
   r   redis.observability.providersr   redis.observability.registryr   redis.utilsr   r   redis.asyncio.connectionr   redis.asyncio.multidb.databaser   redis.observability.configr   r   __annotations__rD   r#   r%   strfloatr   r   boolr2   r6   r<   rN   r[   r^   rc   rf   rk   rm   rp   rv   r~   r   r   r   r   r   r   rM   r"   <module>r      s  ,  0 0 
 K D J 97<5 =A (#89 @ 2 (+@"A B8L1 & %)!%"&!%"&$(111 SM1 #	1
 3-1 I1 $1 SM1 
1h% 
6 %  
	6 C

4 C
+,	
:	* 
2 +/&*;'# 
0  
	6	8 $$$ $ 	$
 $ $ $ 
$R """"c]" d^" 
	"N "&$(# SM 
	H %)ASMA 
AH  	
  
B  
	6$!$ !rM   