o
    <‘ðie5  ã                   @   sn  d Z ddlmZ ddlmZmZmZmZmZ ddl	Z	er4ddl
mZ ddlmZ ddlmZ ddlmZ d	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(d Z)d!Z*d"Z+d#Z,d$Z-d%Z.d&Z/d'Z0G d(d)„ d)eƒZ1G d*d+„ d+eƒZ2G d,d-„ d-eƒZ3G d.d/„ d/eƒZ4G d0d1„ d1eƒZ5G d2d3„ d3ƒZ6d4ed5 d6e7fd7d8„Z8d9ed: fd;d<„Z9dS )=a  
OpenTelemetry semantic convention attributes for Redis.

This module provides constants and helper functions for building OTel attributes
according to the semantic conventions for database clients.

Reference: https://opentelemetry.io/docs/specs/semconv/database/redis/
é    )ÚEnum)ÚTYPE_CHECKINGÚAnyÚDictÚOptionalÚUnionN)ÚConnectionPool)ÚAsyncDatabase)ÚConnectionPoolInterface)ÚSyncDatabasez	db.systemzdb.namespacezdb.operation.namezdb.response.status_codezdb.stored_procedure.namez
error.typeznetwork.peer.addressznetwork.peer.portzserver.addresszserver.portzdb.client.connection.pool.namezdb.client.connection.statezdb.client.connection.namezdb.client.geofailover.fail_fromzdb.client.geofailover.fail_tozdb.client.geofailover.reasonzredis.client.libraryzredis.client.connection.pubsubz$redis.client.connection.close.reasonz$redis.client.connection.notificationz%redis.client.operation.retry_attemptszredis.client.operation.blockingz%redis.client.pubsub.message.directionzredis.client.pubsub.channelzredis.client.pubsub.shardedzredis.client.errors.internalzredis.client.errors.categoryzredis.client.stream.namezredis.client.consumer_groupzredis.client.csc.resultzredis.client.csc.reasonc                   @   ó   e Zd ZdZdZdS )ÚConnectionStateÚidleÚusedN)Ú__name__Ú
__module__Ú__qualname__ÚIDLEÚUSED© r   r   ú]/root/parts/websockify/install/lib/python3.10/site-packages/redis/observability/attributes.pyr   C   ó    r   c                   @   r   )ÚPubSubDirectionÚpublishÚreceiveN)r   r   r   ÚPUBLISHÚRECEIVEr   r   r   r   r   H   r   r   c                   @   r   )Ú	CSCResultÚhitÚmissN)r   r   r   ÚHITÚMISSr   r   r   r   r   M   r   r   c                   @   r   )Ú	CSCReasonÚfullÚinvalidationN)r   r   r   ÚFULLÚINVALIDATIONr   r   r   r   r"   R   r   r"   c                   @   r   )ÚGeoFailoverReasonÚ	automaticÚmanualN)r   r   r   Ú	AUTOMATICÚMANUALr   r   r   r   r'   W   r   r'   c                   @   s   e Zd ZdZe			d2dee dee dee deee	f fdd„ƒZ
e							d3d	eeeef  d
ee dee dee dee dee dee deee	f fdd„ƒZe				d4dee dee dee dee deee	f f
dd„ƒZe		d5dee dee deee	f fdd„ƒZe		d5dedee dee deee	f fdd „ƒZe			d2d!ee d"ee d#ee deee	f fd$d%„ƒZe			d2dee d&ee d'ee deee	f fd(d)„ƒZed*ed+ d,ed+ d'edeee	f fd-d.„ƒZe	/d6dedededefd0d1„ƒZdS )7ÚAttributeBuilderzY
    Helper class to build OTel semantic convention attributes for Redis operations.
    NÚserver_addressÚserver_portÚdb_namespaceÚreturnc                 C   sL   t dtdtj› �i}| dur| |t< |dur||t< |dur$t|ƒ|t< |S )a*  
        Build base attributes common to all Redis operations.

        Args:
            server_address: Redis server address (FQDN or IP)
            server_port: Redis server port
            db_namespace: Redis database index

        Returns:
            Dictionary of base attributes
        Úredisz
redis-py:vN)Ú	DB_SYSTEMÚREDIS_CLIENT_LIBRARYr1   Ú__version__ÚSERVER_ADDRESSÚSERVER_PORTÚstrÚDB_NAMESPACE)r-   r.   r/   Úattrsr   r   r   Úbuild_base_attributesa   s   þz&AttributeBuilder.build_base_attributesÚcommand_nameÚ
batch_sizeÚnetwork_peer_addressÚnetwork_peer_portÚstored_procedure_nameÚretry_attemptsÚis_blockingc                 C   sŒ   i }| durt | tƒr| jddd�} |  ¡ |t< |dur ||t< |dur(||t< |dur0||t< |dur<|dkr<||t< |durD||t	< |S )aw  
        Build attributes for a Redis operation (command execution).

        Args:
            command_name: Redis command name (e.g., 'GET', 'SET', 'MULTI'), can be str or bytes
            batch_size: Number of commands in batch (for pipelines/transactions)
            network_peer_address: Resolved peer address
            network_peer_port: Peer port number
            stored_procedure_name: Lua script name or SHA1 digest
            retry_attempts: Number of retry attempts made
            is_blocking: Whether the operation is a blocking command

        Returns:
            Dictionary of operation attributes
        Nzutf-8Úreplace)Úerrorsr   )
Ú
isinstanceÚbytesÚdecodeÚupperÚDB_OPERATION_NAMEÚNETWORK_PEER_ADDRESSÚNETWORK_PEER_PORTÚDB_STORED_PROCEDURE_NAMEÚ%REDIS_CLIENT_OPERATION_RETRY_ATTEMPTSÚREDIS_CLIENT_OPERATION_BLOCKING)r;   r<   r=   r>   r?   r@   rA   r9   r   r   r   Úbuild_operation_attributes‚   s    
z+AttributeBuilder.build_operation_attributesÚ	pool_nameÚconnection_stateÚconnection_nameÚ	is_pubsubc                 C   sN   t  ¡ }| dur| |t< |dur|j|t< |dur||t< |dur%||t< |S )ay  
        Build attributes for connection pool metrics.

        Args:
            pool_name: Unique connection pool name
            connection_state: Connection state ('idle' or 'used')
            is_pubsub: Whether this is a PubSub connection
            connection_name: Unique connection name

        Returns:
            Dictionary of connection pool attributes
        N)r,   r:   ÚDB_CLIENT_CONNECTION_POOL_NAMEÚvalueÚDB_CLIENT_CONNECTION_STATEÚREDIS_CLIENT_CONNECTION_PUBSUBÚDB_CLIENT_CONNECTION_NAME)rO   rP   rQ   rR   r9   r   r   r   Úbuild_connection_attributes´   s   
z,AttributeBuilder.build_connection_attributesÚ
error_typeÚis_internalc                 C   s~   i }| dur5| j j|t< t| dƒr| jdur| j|t< nd|t< t| dƒr1| jdur1| jj|t< nd|t< |dur=||t	< |S )zÿ
        Build error attributes.

        Args:
            is_internal: Whether the error is internal (e.g., timeout, network error)
            error_type: The exception that occurred

        Returns:
            Dictionary of error attributes
        NÚstatus_codeÚerrorrY   Úother)
Ú	__class__r   Ú
ERROR_TYPEÚhasattrr[   ÚDB_RESPONSE_STATUS_CODErY   rT   ÚREDIS_CLIENT_ERROR_CATEGORYÚREDIS_CLIENT_ERROR_INTERNAL)rY   rZ   r9   r   r   r   Úbuild_error_attributes×   s   ÿ
z'AttributeBuilder.build_error_attributesÚ	directionÚchannelÚshardedc                 C   s6   t  ¡ }| j|t< |dur||t< |dur||t< |S )a.  
        Build attributes for a PubSub message.

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

        Returns:
            Dictionary of PubSub message attributes
        N)r,   r:   rT   Ú%REDIS_CLIENT_PUBSUB_MESSAGE_DIRECTIONÚREDIS_CLIENT_PUBSUB_CHANNELÚREDIS_CLIENT_PUBSUB_SHARDED)re   rf   rg   r9   r   r   r   Úbuild_pubsub_message_attributesý   s   
z0AttributeBuilder.build_pubsub_message_attributesÚstream_nameÚconsumer_groupÚconsumer_namec                 C   s,   t  ¡ }| dur| |t< |dur||t< |S )a   
        Build attributes for a streaming operation.

        Args:
            stream_name: Name of the stream
            consumer_group: Name of the consumer group
            consumer_name: Name of the consumer

        Returns:
            Dictionary of streaming attributes
        N)r,   r:   ÚREDIS_CLIENT_STREAM_NAMEÚREDIS_CLIENT_CONSUMER_GROUP)rl   rm   rn   r9   r   r   r   Úbuild_streaming_attributes  s   z+AttributeBuilder.build_streaming_attributesÚresultÚreasonc                 C   s@   t  ¡ }| dur| |t< |dur|j|t< |dur|j|t< |S )a\  
        Build attributes for a Client Side Caching (CSC) operation.

        Args:
            pool_name: Connection pool name (used only for csc_items metric)
            result: CSC result ('hit' or 'miss')
            reason: Reason for CSC eviction ('full' or 'invalidation')

        Returns:
            Dictionary of CSC attributes
        N)r,   r:   rS   rT   ÚREDIS_CLIENT_CSC_RESULTÚREDIS_CLIENT_CSC_REASON)rO   rr   rs   r9   r   r   r   Úbuild_csc_attributes4  s   

z%AttributeBuilder.build_csc_attributesÚ	fail_from©r   r	   Úfail_toc                 C   s.   t  ¡ }t| ƒ|t< t|ƒ|t< |j|t< |S )a	  
        Build attributes for a geo failover.

        Args:
            fail_from: Database failed from
            fail_to: Database failed to
            reason: Reason for the failover

        Returns:
            Dictionary of geo failover attributes
        )r,   r:   Úget_db_nameÚDB_CLIENT_GEOFAILOVER_FAIL_FROMÚDB_CLIENT_GEOFAILOVER_FAIL_TOrT   ÚDB_CLIENT_GEOFAILOVER_REASON)rw   ry   rs   r9   r   r   r   Úbuild_geo_failover_attributesR  s
   
z.AttributeBuilder.build_geo_failover_attributesr   c                 C   s   | › d|› d|› �S )a  
        Build a unique connection pool name.

        Args:
            server_address: Redis server address
            server_port: Redis server port
            db_namespace: Redis database index

        Returns:
            Unique pool name in format "address:port/db"
        ú:ú/r   )r-   r.   r/   r   r   r   Úbuild_pool_namek  s   z AttributeBuilder.build_pool_name)NNN)NNNNNNN)NNNN)NN)r   )r   r   r   Ú__doc__Ústaticmethodr   r7   Úintr   r   r:   r   rE   ÚboolrN   r   rX   Ú	Exceptionrd   r   rk   rq   r   r"   rv   r'   r~   r�   r   r   r   r   r,   \   sú    ýÿþý
ü ùÿþýüûúù
ø1üÿþýü
û"þÿþ
ý%ýÿþý
üýÿþý
üýÿþý
üÿþý
üýÿþýür,   Úpool)r
   r   r0   c                 C   sN   | j  dd¡}| j  dd¡}t| ddƒ}|r |› d|› d|› �S |› d|› �S )	aõ  
    Get a short string representation of a connection pool for observability.

    This provides a concise pool identifier suitable for use as a metric attribute,
    in the format: host:port_uniqueID (matching go-redis format)

    Args:
        pool: Connection pool instance

    Returns:
        Short pool name in format "host:port_uniqueID"

    Example:
        >>> pool = ConnectionPool(host='localhost', port=6379, db=0)
        >>> get_pool_name(pool)
        'localhost:6379_a1b2c3d4'
    ÚhostÚunknownÚportië  Ú_pool_idÚ r   Ú_)Úconnection_kwargsÚgetÚgetattr)r‡   rˆ   rŠ   Úpool_idr   r   r   Úget_pool_name  s   r’   Údatabaserx   c                 C   s6   | j  ¡ d }| j  ¡ d }| j}|› d|› d|› �S )zÈ
    Get a short string representation of a database for observability.

    Args:
        database: Database instance

    Returns:
        Short database name in format "{host}:{port}/{weight}"
    rˆ   rŠ   r   r€   )ÚclientÚget_connection_kwargsÚweight)r“   rˆ   rŠ   r–   r   r   r   rz   �  s   rz   ):r‚   Úenumr   Útypingr   r   r   r   r   r1   Úredis.asyncio.connectionr   Úredis.asyncio.multidb.databaser	   Úredis.connectionr
   Úredis.multidb.databaser   r2   r8   rH   ra   rK   r_   rI   rJ   r5   r6   rS   rU   rW   r{   r|   r}   r3   rV   Ú$REDIS_CLIENT_CONNECTION_CLOSE_REASONÚ$REDIS_CLIENT_CONNECTION_NOTIFICATIONrL   rM   rh   ri   rj   rc   rb   ro   rp   rt   ru   r   r   r   r"   r'   r,   r7   r’   rz   r   r   r   r   Ú<module>   sd    	  %