HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.60.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 5 05:00:59 EST 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //var/opt/nydus/ops/mysql/connector/opentelemetry/__pycache__/instrumentation.cpython-39.pyc
a

�,�hd�
@sfUdZddlmZddlZddlZddlmZmZddlm	Z	ddl
mZmZm
Z
mZmZmZmZer�ddlmZmZdd	lmZd
dlmZddlmZmZdd
lmZddlmZz,ddlm Z ddl!m"Z"ddl#m$Z$dZ%Wnje&�yLz ddl'm Z ddl(m$Z$dZ%Wn4e&�yFZ)zej*�+d�e)�WYdZ)[)n
dZ)[)00Yn0ddlm,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7e�8d�Z9de:d<dddd�dd�Z;ddd �d!d"�Z<d#d#d$�d%d&�Z=dAd'dd(dd)�d*d+�Z>d,d,d-�d.d/�Z?d,d,d-�d0d1�Z@d,d,d-�d2d3�ZAGd4d5�d5e�ZBGd6d7�d7eB�ZCGd8d9�d9eB�ZDdBd:d;d:d<�d=d>�ZEGd?d@�d@�ZFdS)Cz1MySQL instrumentation supporting mysql-connector.�)�annotationsN)�ABC�abstractmethod)�nullcontext)�
TYPE_CHECKING�Any�Callable�
Collection�Dict�Optional�Union�)�MySQLConnectionAbstract�MySQLCursorAbstract)�PooledMySQLConnection�)�	connector)�
CNX_POOL_ARGS�DEFAULT_CONFIGURATION)�logger)�VERSION_TEXT)�trace)�TracerProvider)�SpanAttributesTFz�Bundled installation has missing dependencies. Please use `pip install mysql-connector-python[opentelemetry]`, or for an editable install use `pip install -e '.[opentelemetry]'`, to install the dependencies required by the bundled opentelemetry package.�)�CONNECTION_SPAN_NAME�	DB_SYSTEM�DEFAULT_THREAD_ID�DEFAULT_THREAD_NAME�FIRST_SUPPORTED_VERSION�NET_SOCK_FAMILY�NET_SOCK_HOST_ADDR�NET_SOCK_HOST_PORT�NET_SOCK_PEER_ADDR�NET_SOCK_PEER_PORT�OPTION_CNX_SPAN�OPTION_CNX_TRACERz
^/\*.*?\*/z
re.Pattern�leading_comment_remover�
trace.SpanzOptional[Exception]�None)�span�exc�returncCs6|r|��r|sdS|�t�tjj��|�|�dS)zRecords an exeception event.N)�is_recordingZ
set_statusrZStatusZ
StatusCode�ERRORZrecord_exception)r*r+�r/�`/opt/nydus/tmp/pip-target-wkfpz8uv/lib64/python/mysql/connector/opentelemetry/instrumentation.py�record_exception_eventdsr1)r*r,cCs|r|��sdS|��dS)z
Ends span.N)r-�end)r*r/r/r0�end_spanmsr3�str)�	operationr,cCs&|r"t|t�r"t�d|���dSdS)z&Parse query to extract operation name.�r)�
isinstancer4r'�sub�split)r5r/r/r0�get_operation_nameusr:z#Optional['MySQLConnectionAbstract']zOptional[Dict[str, Any]])�cnx�cnx_span�
cnx_kwargsr,c
Cs�|r|��sdS|duri}|r(|jnd|v}tjttj|rBdndt|rNdndi}|�r,|rd|jn|�dtd�|tj	<|r�|j
n|�dtd�|tj<t|d	��r�|j
�r�z0|j
j��\|t<}|j
j��\|t<|t<Wn2t�y}zt�d
|�WYd}~n"d}~00|tj|k�r�||t<nv|�r8|jn|�d�|t<t|d	��r�|j
�r�z|j
j��|t<Wn2t�y�}zt�d
|�WYd}~n
d}~00|�|�dS)a�Defines connection span attributes. If `cnx` is None then we use `cnx_kwargs`
    to get basic net information. Basic net attributes are defined such as:

    * DB_SYSTEM
    * NET_TRANSPORT
    * NET_SOCK_FAMILY

    Socket-level attributes [*] are also defined [**].

    [*]: Socket-level attributes identify peer and host that are directly connected to
    each other. Since instrumentations may have limited knowledge on network
    information, instrumentations SHOULD populate such attributes to the best of
    their knowledge when populate them at all.

    [**]: `CMySQLConnection` connections have no access to socket-level
    details so socket-level attributes aren't included. `MySQLConnection`
    connections, on the other hand, do include socket-level attributes.

    References:
        [1]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/            specification/trace/semantic_conventions/span-general.md
    NZunix_socketZip_tcpZinprocZinet�unix�host�port�_socketzConnection socket is down %s)r-Z_unix_socketrrZ
NET_TRANSPORTr Z_host�getrZ
NET_PEER_NAMEZ_portZ
NET_PEER_PORT�hasattrrA�sock�getpeernamer#�getsocknamer!r"�	Exceptionr�warningr$Zset_attributes)r;r<r=Zis_tcp�attrsZsock_peer_portZsock_errr/r/r0�set_connection_span_attrs}sJ���
�
��"
�"rJr)�methodr,csddddd��fdd�}|S)zAAttach the connection span while executing the connection method.�'MySQLConnectionAbstract'r�r;�args�kwargsr,cs`|jr |j��r tj|jdd�nt��&�|g|�Ri|��Wd�S1sR0YdS)z#Connection span attacher decorator.F�Zend_on_exitN)�_spanr-r�use_spanr)r;rNrO�rKr/r0�wrapper�s����z'with_cnx_span_attached.<locals>.wrapperr/�rKrTr/rSr0�with_cnx_span_attached�srVcsddddd��fdd�}|S)z:Create a query span while executing the connection method.�TracedMySQLConnectionrrMcs�t�d�j�tjttj|jtjttj	t
d|��i}|jrp|j�
�rp|jj�j��tjjt�|j���g|d�nt��&�|g|�Ri|��Wd�S1s�0YdS)�Query span creator decorator.z%Creating query span for connection.%sZconnection_type��name�kind�links�
attributesN)r�info�__name__rr�DB_USER�_user�	THREAD_IDr�THREAD_NAMEr�get_wrapped_classrQr-�_tracer�start_as_current_span�upperr�SpanKind�CLIENT�Link�get_span_contextr)r;rNrO�query_span_attributesrSr/r0rT�s&�
����z$with_cnx_query_span.<locals>.wrapperr/rUr/rSr0�with_cnx_query_span�srmcsddddd��fdd�}|S)z6Create a query span while executing the cursor method.�TracedMySQLCursorr)�currNrOr,cs�t�d�j�t|jd�r&t|jd�n
t|jd�}tjttj|j	tj
ttjt
d|��i}|jjt|d�pldtjj|jg|d��&�|g|�Ri|��Wd�S1s�0YdS)	rXz!Creating query span for cursor.%s�_connectionZ_cnxZcursor_typerz
SQL statementrYN)rr^r_rC�_wrapped�getattrrrr`rarbrrcrrdrerfr:rrhri�_connection_span_link)rorNrO�
connectionrlrSr/r0rT�s$
�
���z'with_cursor_query_span.<locals>.wrapperr/rUr/rSr0�with_cursor_query_span�srucszeZdZdZedd�dd��Zddd�d	d
�Zdddd��fdd
�Zdd�dd�Zdddd�dd�Z	dd�dd�Z
�ZS)�BaseMySQLTracerz<Base class that provides basic object wrapper functionality.r)�r,cCsdS)z"Must be implemented by subclasses.Nr/��selfr/r/r0�__init__szBaseMySQLTracer.__init__r4r)�attrr,cCs ||jvrt||�St|j|�S)z�Gets an attribute.

        Attributes defined in the wrapper object have higher precedence
        than those wrapped object equivalent. Attributes not found in
        the wrapper are then searched in the wrapped object.
        )�__dict__rrrq)ryr{r/r/r0�__getattr__ s

zBaseMySQLTracer.__getattr__)rZ�valuer,csNd|jvr||jd<dS||jvs*|dkr<t��||�dS|j�||�dS)Nrq�
autocommit)r|�super�__setattr__rq)ryrZr~��	__class__r/r0r�-s

zBaseMySQLTracer.__setattr__cCs|j��|S)�
Magic method.)rq�	__enter__rxr/r/r0r�9s
zBaseMySQLTracer.__enter__�rNrOr,cOs|jj|i|��dS)r�N)rq�__exit__�ryrNrOr/r/r0r�>szBaseMySQLTracer.__exit__cCs
|jjjS)zGets the wrapped class name.)rqr�r_rxr/r/r0rdBsz!BaseMySQLTracer.get_wrapped_class)r_�
__module__�__qualname__�__doc__rrzr}r�r�r�rd�
__classcell__r/r/r�r0rvs
rvc@sdeZdZdZdddd�dd�Zedddd	�d
d��Zedddd	�dd
��Zedddd	�dd��ZdS)rnz;Wrapper class for a `MySQLCursor` or `CMySQLCursor` object.z'MySQLCursorAbstract'ztrace.Tracerr(��wrapped�tracer�connection_spancCs ||_||_t�|���|_dS�zConstructor.N)rqrerrjrkrs)ryr�r�r�r/r/r0rzJs
�zTracedMySQLCursor.__init__rr�cOs|jj|i|��S��Instrument method.)rq�executer�r/r/r0r�WszTracedMySQLCursor.executecOs|jj|i|��Sr�)rq�executemanyr�r/r/r0r�\szTracedMySQLCursor.executemanycOs|jj|i|��Sr�)rq�callprocr�r/r/r0r�aszTracedMySQLCursor.callprocN)	r_r�r�r�rzrur�r�r�r/r/r/r0rnGs
rnc@s�eZdZdZddd�dd�Zdddd	�d
d�Zedddd	�dd
��Zedddd	�dd��Zedddd	�dd��Z	edddd	�dd��Z
edddd	�dd��Zedddd	�dd��Zedddd	�dd��Z
edddd	�dd��Zedddd	�dd��Zedddd	�dd��Zedddd	�d d!��Zedddd	�d"d#��Zeeddd$d	�d%d&���Zedddd	�d'd(��Zedddd	�d)d*��Zedddd	�d+d,��Zedddd	�d-d.��Zedddd	�d/d0��Zedddd	�d1d2��Zedddd	�d3d4��Zedddd	�d5d6��Zedddd	�d7d8��Zedddd	�d9d:��Zeeddd$d	�d;d<���Zeeddd$d	�d=d>���Z eeddd?d	�d@dA���Z!e!j"ed?ddB�dCdA���Z!edddd	�dDdE��Z#edddd	�dFdG��Z$dHS)IrWzCWrapper class for a `MySQLConnection` or `CMySQLConnection` object.rLr))r�r,cCs||_|jj}dSr��rq�sql_mode)ryr��_r/r/r0rzjszTracedMySQLConnection.__init__rrnr�cOs t|jj|i|��|j|jd�S)zWraps the object method.r�)rnrq�cursorrerQr�r/r/r0r�rs
�zTracedMySQLConnection.cursorcOs|jj|i|��Sr�)rq�cmd_change_userr�r/r/r0r�zsz%TracedMySQLConnection.cmd_change_usercOs|jj|i|��Sr�)rq�commitr�r/r/r0r�szTracedMySQLConnection.commitcOs|jj|i|��Sr�)rq�rollbackr�r/r/r0r��szTracedMySQLConnection.rollbackcOs|jj|i|��Sr�)rq�	cmd_queryr�r/r/r0r��szTracedMySQLConnection.cmd_querycOs|jj|i|��Sr�)rq�cmd_init_dbr�r/r/r0r��sz!TracedMySQLConnection.cmd_init_dbcOs|jj|i|��Sr�)rq�cmd_refreshr�r/r/r0r��sz!TracedMySQLConnection.cmd_refreshcOs|jj|i|��Sr�)rq�cmd_quitr�r/r/r0r��szTracedMySQLConnection.cmd_quitcOs|jj|i|��Sr�)rq�cmd_shutdownr�r/r/r0r��sz"TracedMySQLConnection.cmd_shutdowncOs|jj|i|��Sr�)rq�cmd_statisticsr�r/r/r0r��sz$TracedMySQLConnection.cmd_statisticscOs|jj|i|��Sr�)rq�cmd_process_killr�r/r/r0r��sz&TracedMySQLConnection.cmd_process_killcOs|jj|i|��Sr�)rq�	cmd_debugr�r/r/r0r��szTracedMySQLConnection.cmd_debugcOs|jj|i|��Sr�)rq�cmd_pingr�r/r/r0r��szTracedMySQLConnection.cmd_pingr4cOs|jjSr�)rq�databaser�r/r/r0r��szTracedMySQLConnection.databasecOs|jj|i|��Sr�)rq�is_connectedr�r/r/r0r��sz"TracedMySQLConnection.is_connectedcOs|jj|i|��Sr�)rq�
reset_sessionr�r/r/r0r��sz#TracedMySQLConnection.reset_sessioncOs|jj|i|��Sr�)rq�pingr�r/r/r0r��szTracedMySQLConnection.pingcOs|jj|i|��Sr�)rq�
info_queryr�r/r/r0r��sz TracedMySQLConnection.info_querycOs|jj|i|��Sr�)rq�cmd_stmt_preparer�r/r/r0r��sz&TracedMySQLConnection.cmd_stmt_preparecOs|jj|i|��Sr�)rq�cmd_stmt_executer�r/r/r0r��sz&TracedMySQLConnection.cmd_stmt_executecOs|jj|i|��Sr�)rq�cmd_stmt_closer�r/r/r0r��sz$TracedMySQLConnection.cmd_stmt_closecOs|jj|i|��Sr�)rq�cmd_stmt_send_long_datar�r/r/r0r��sz-TracedMySQLConnection.cmd_stmt_send_long_datacOs|jj|i|��Sr�)rq�cmd_stmt_resetr�r/r/r0r��sz$TracedMySQLConnection.cmd_stmt_resetcOs|jj|i|��Sr�)rq�cmd_reset_connectionr�r/r/r0r��sz*TracedMySQLConnection.cmd_reset_connectioncOs|jjSr�)rq�	time_zoner�r/r/r0r��szTracedMySQLConnection.time_zonecOs|jjSr�r�r�r/r/r0r��szTracedMySQLConnection.sql_mode�boolcOs|jjSr��rqrr�r/r/r0r�sz TracedMySQLConnection.autocommit)r~r,cCs||j_dS)r�Nr�)ryr~r/r/r0rscOs|jj|i|��Sr�)rq�set_charset_collationr�r/r/r0r�sz+TracedMySQLConnection.set_charset_collationcOs|jj|i|��Sr�)rq�start_transactionr�r/r/r0r�sz'TracedMySQLConnection.start_transactionN)%r_r�r�r�rzr�rmr�r�r�r�r�r�r�r�r�r�r�r��propertyr�r�r�r�r�r�r�r�r�r�r�r�r�r�setterr�r�r/r/r/r0rWgs�rWzHCallable[..., Union['MySQLConnectionAbstract', 'PooledMySQLConnection']]�Optional[trace.TracerProvider])�connect�tracer_providerr,cs&t���dddd���fdd��}|S)z.Retrurn the instrumented version of `connect`.rz9Union['MySQLConnectionAbstract', 'PooledMySQLConnection']r�cs�t�fdd�tD��r.t�d��|i���Stjdt�d�}|jttj	j
d��t<|�t<tj
�tdd��@}td	|���|i���}t||��t|d
�Wd	�S1s�0Yd	S)z�Wraps the connection object returned by the method `connect`.

        Instrumentation for PooledConnections is not supported.
        c3s|]}|�vVqdS)Nr/)�.0�key�rOr/r0�	<genexpr> �z7_instrument_connect.<locals>.wrapper.<locals>.<genexpr>z4Instrumentation for pooled connections not supported�MySQL Connector/Python�Zinstrumenting_module_nameZinstrumenting_library_versionr��rZr[FrPN�r�)�anyrrrHr�
get_tracerr�
start_spanrrhrir%r&rRrJrW)rNrOr�r<r;�r�r�r�r0rTs&
��
�z$_instrument_connect.<locals>.wrapper)�	functools�wraps)r�r�rTr/r�r0�_instrument_connects(r�c@s�eZdZUdZdZded<dddd�dd�Zd	d
�dd�Zdd
d�dd�Zddddd�dd�Z	dd
d�dd�Z
ddd�dd�ZdS)�MySQLInstrumentorz8MySQL instrumentation supporting mysql-connector-python.NzOptional[MySQLInstrumentor]�	_instancerr�cOs:|jdur4tj|g|�Ri|��|_t|jdtj�|jS)zQSinglenton.

        Restricts the instantiation to a singular instance.
        N�_original_connect)r��object�__new__�setattrrr�)�clsrNrOr/r/r0r�Is
zMySQLInstrumentor.__new__zCollection[str]rwcCsdt��gS)zoReturn a list of python packages with versions
        that the will be instrumented (e.g., versions >= 8.1.0).zmysql-connector-python >= )rrxr/r/r0�instrumentation_dependenciesUsz.MySQLInstrumentor.instrumentation_dependenciesr))rOr,cKs<tjt|d�krt�d�dStt|d�|�d�d�t_dS)aInstrument the library.

        Args:
            trace_module: reference to the 'trace' module from opentelemetry.
            tracer_provider (optional): TracerProvider instance.

        NOTE: Instrumentation for pooled connections not supported.
        r�z3MySQL Connector/Python module already instrumented.Nr�r�)rr�rrrrHr�rB�ryrOr/r/r0�
instrumentZs	
�zMySQLInstrumentor.instrumentrLr�)rtr�r,cCs�t|t�rt�d�|St|d�r,t|d�s@t�d|jj�|Stjdt	|d�}|j
ttjj
d�|_||_t||j�t|d�S)	a�Enable instrumentation in a MySQL connection.

        Args:
            connection: uninstrumented connection instance.
            trace_module: reference to the 'trace' module from opentelemetry.
            tracer_provider (optional): TracerProvider instance.

        Returns:
            connection: instrumented connection instace.

        NOTE: Instrumentation for pooled connections not supported.
        z Connection already instrumented.rQrez+Instrumentation for class %s not supported.r�r�r�r�)r7rWrrHrCr�r_rr�rr�rrhrirQrerJ)ryrtr�r�r/r/r0�instrument_connectionks(

���z'MySQLInstrumentor.instrument_connectioncKs.tjt|d�krt�d�dSt|d�t_dS)zUninstrument the library.r�z5MySQL Connector/Python module already uninstrumented.N)rr�rrrrHr�r/r/r0�uninstrument�s
zMySQLInstrumentor.uninstrument)rtr,cCs\t|d�st�d|jj�|St|t�s6t�d�|S|jrV|j��rV|j�	�d|_|j
S)aDisable instrumentation in a MySQL connection.

        Args:
            connection: instrumented connection instance.

        Returns:
            connection: uninstrumented connection instace.

        NOTE: Instrumentation for pooled connections not supported.
        rQz-Uninstrumentation for class %s not supported.z"Connection already uninstrumented.N)rCrrHr�r_r7rWrQr-r2rq)ryrtr/r/r0�uninstrument_connection�s
�


z)MySQLInstrumentor.uninstrument_connection)N)r_r�r�r�r��__annotations__r�r�r�r�r�r�r/r/r/r0r�Ds
�*r�)N)N)Gr��
__future__rr��re�abcrr�
contextlibr�typingrrrr	r
rrZ	abstractsrrZpoolingrr6r�	constantsrrr�versionrZ
opentelemetryrZopentelemetry.sdk.tracerZopentelemetry.semconv.tracerZOTEL_SYSTEM_AVAILABLE�ImportErrorZmysql.opentelemetryZ!mysql.opentelemetry.semconv.traceZmissing_dependencies_err�errorsZProgrammingErrorrrrrrr r!r"r#r$r%r&�compiler'r�r1r3r:rJrVrmrurvrnrWr�r�r/r/r/r0�<module>s\$��8	�U
 . -�3