
    ,ha                    N   U d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZmZmZmZmZ erddlmZmZ dd	lmZ d
dlmZ ddlmZmZ ddlmZ ddlmZ 	 ddlm Z  ddl!m"Z" ddl#m$Z$ ddlm)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4  ejj                  d      Z6de7d<   d(dZ8d)dZ9d*dZ:	 d+	 	 	 	 	 	 	 d,dZ;d-dZ<d-dZ=d-dZ> G d d e      Z? G d! d"e?      Z@ G d# d$e?      ZA	 d+	 	 	 	 	 d.d%ZB G d& d'      ZCy# e%$ r!Z&ejN                  jQ                  d      e&dZ&[&ww xY w)/z1MySQL 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)SpanAttributeszGOpenTelemetry installation not found. You must install the API and SDK.   )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_removerc                    | r| j                         r|sy| j                  t        j                  t        j                  j
                               | j                  |       y)zRecords an exeception event.N)is_recording
set_statusr   Status
StatusCodeERRORrecord_exception)spanexcs     o/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/mysql/connector/opentelemetry/instrumentation.pyrecord_exception_eventr3   X   sD    t((*#OOELL!1!1!7!789#    c                J    | r| j                         sy| j                          y)z
Ends span.N)r*   end)r0   s    r2   end_spanr7   a   s    t((*HHJr4   c                v    | r7t        | t              r't        j                  d|       j	                         d   S y)z&Parse query to extract operation name. r   )
isinstancestrr(   subsplit)	operations    r2   get_operation_namer?   i   s2    Z	3/&**2y9??A!DDr4   c                   |r|j                         sy|i }| r| j                   nd|v}t        j                  t        t        j                  |rdndt
        |rdndi}|r| r| j                  n|j                  dt        d         |t        j                  <   | r| j                  n|j                  dt        d         |t        j                  <   t        | d	      r| j                  r	 | j                  j                  j                         \  |t         <   }| j                  j                  j#                         \  |t$        <   |t&        <   |t        j                     |k7  rt||t(        <   nj| r| j                  n|j                  d      |t         <   t        | d	      r8| j                  r,	 | j                  j                  j#                         |t$        <   |j1                  |       y# t*        $ r }t-        j.                  d
|       Y d}~6d}~ww xY w# t*        $ r }t-        j.                  d
|       Y d}~bd}~ww xY w)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
    Nunix_socketip_tcpinprocinetunixhostport_socketzConnection socket is down %s)r*   _unix_socketr   r   NET_TRANSPORTr!   _hostgetr   NET_PEER_NAME_portNET_PEER_PORThasattrrH   sockgetpeernamer$   getsocknamer"   r#   r%   	Exceptionr   warningset_attributes)cnxcnx_span
cnx_kwargsis_tcpattrssock_peer_portsock_errs          r2   set_connection_span_attrsr^   q   s   8 8002
%(!!!m:.MF 	  )$$&hh6vE CII*..9Nv9V"W 	n**+ CII*..9Nv9V"W 	n**+ 3	"s{{? KK$$002,-" KK$$002,-,-
 556.H 1?E,-
 !$C)F 	 ! 3	"s{{I,/KK,<,<,H,H,J() E")  I=xHHI"  I=xHHIs1   +A#G* -+H *	H3HH	H?H::H?c                     d fd}|S )zAAttach the connection span while executing the connection method.c                    | j                   r;| j                   j                         r!t        j                  | j                   d      n	t	               5   | g|i |cddd       S # 1 sw Y   yxY w)z#Connection span attacher decorator.Fend_on_exitN)_spanr*   r   use_spanr   )rW   argskwargsmethods      r2   wrapperz'with_cnx_span_attached.<locals>.wrapper   s`     YY399113 ^^II5
9D	0 #///	0 	0 	0s   A))A2)rW   'MySQLConnectionAbstract're   r   rf   r   returnr    rg   rh   s   ` r2   with_cnx_span_attachedrm      s    0 Nr4   c                     d fd}|S )z:Create a query span while executing the connection method.c                   t        j                  dj                         t        j                  t        t        j
                  | j                  t        j                  t        t        j                  t        d| j                         i}| j                  r| j                  j                         r{| j                  j                  j                  j!                         t"        j$                  j&                  t#        j(                  | j                  j+                               g|      n	t-               5   | g|i |cddd       S # 1 sw Y   yxY w)Query span creator decorator.z%Creating query span for connection.%sconnection_typenamekindlinks
attributesN)r   info__name__r   r   DB_USER_user	THREAD_IDr   THREAD_NAMEr   get_wrapped_classrc   r*   _tracerstart_as_current_spanupperr   SpanKindCLIENTLinkget_span_contextr   )rW   re   rf   query_span_attributesrg   s       r2   rh   z$with_cnx_query_span.<locals>.wrapper   s    ;V__M $$i""CII$$&7&&(;s446'
 YY399113 [[..&&(&&::cii88:;<,	 / 

 :E	0 #///	0 	0 	0s   5EE)rW   TracedMySQLConnectionre   r   rf   r   rj   r   rk   rl   s   ` r2   with_cnx_query_spanr      s    0( Nr4   c                     d fd}|S )z6Create a query span while executing the cursor method.c                   t        j                  dj                         t        | j                  d      rt        | j                  d      nt        | j                  d      }t        j                  t        t        j                  |j                  t        j                  t        t        j                  t        d| j                         i}| j                  j!                  t#        |d         xs dt$        j&                  j(                  | j*                  g|      5   | g|i |cddd       S # 1 sw Y   yxY w)	rp   z!Creating query span for cursor.%s_connection_cnxcursor_typer   zSQL statementrr   N)r   rw   rx   rP   _wrappedgetattrr   r   ry   rz   r{   r   r|   r   r}   r~   r   r?   r   r   r   _connection_span_link)curre   rf   
connectionr   rg   s        r2   rh   z'with_cursor_query_span.<locals>.wrapper   s   7I s||]3 CLL-0v. 	 $$i""J$4$4$$&7&&(;3002'
 [[..#DG,?&&,,-,	 / 
 	0 #///	0 	0 	0s   D44D=)r   TracedMySQLCursorre   r   rf   r   rj   r   rk   rl   s   ` r2   with_cursor_query_spanr      s    04 Nr4   c                  V     e Zd ZdZedd       Zd	dZd
 fdZddZddZ	ddZ
 xZS )BaseMySQLTracerz<Base class that provides basic object wrapper functionality.c                     y)z"Must be implemented by subclasses.Nrk   selfs    r2   __init__zBaseMySQLTracer.__init__  s    r4   c                b    || j                   v rt        | |      S t        | j                  |      S )zGets 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__r   r   )r   attrs     r2   __getattr__zBaseMySQLTracer.__getattr__  s.     4== 4&&t}}d++r4   c                    d| j                   vr|| j                   d<   y || j                   v s|dk(  rt        | 	  ||       y | j                  j                  ||       y )Nr   
autocommit)r   super__setattr__r   )r   rs   value	__class__s      r2   r   zBaseMySQLTracer.__setattr__!  sY    T]]*(-DMM*%4== DL$8Ge,!!$.r4   c                :    | j                   j                          | S )Magic method.)r   	__enter__r   s    r2   r   zBaseMySQLTracer.__enter__-  s    !r4   c                <     | j                   j                  |i | y)r   N)r   __exit__r   re   rf   s      r2   r   zBaseMySQLTracer.__exit__2  s    //r4   c                B    | j                   j                  j                  S )zGets the wrapped class name.)r   r   rx   r   s    r2   r}   z!BaseMySQLTracer.get_wrapped_class6  s    }}&&///r4   )rj   None)r   r;   rj   r   )rs   r;   r   r   rj   r   )rj   r   )re   r   rf   r   rj   r   rj   r;   )rx   
__module____qualname____doc__r   r   r   r   r   r   r}   __classcell__)r   s   @r2   r   r     s1    F1 1,
/
00r4   r   c                  Z    e Zd ZdZ	 	 	 	 	 	 ddZedd       Zedd       Zedd       Zy)	r   z;Wrapper class for a `MySQLCursor` or `CMySQLCursor` object.c                p    || _         || _        t        j                  |j	                               | _        yzConstructor.N)r   r~   r   r   r   r   )r   wrappedtracerconnection_spans       r2   r   zTracedMySQLCursor.__init__>  s/     07%+16,,.2
"r4   c                :     | j                   j                  |i |S Instrument method.)r   executer   s      r2   r   zTracedMySQLCursor.executeK  s      %t}}$$d5f55r4   c                :     | j                   j                  |i |S r   )r   executemanyr   s      r2   r   zTracedMySQLCursor.executemanyP        )t}}(($9&99r4   c                :     | j                   j                  |i |S r   )r   callprocr   s      r2   r   zTracedMySQLCursor.callprocU        &t}}%%t6v66r4   N)r   z'MySQLCursorAbstract'r   ztrace.Tracerr   
trace.Spanre   r   rf   r   rj   r   )	rx   r   r   r   r   r   r   r   r   rk   r4   r2   r   r   ;  sb    E
&
 
 $	
 6 6 : : 7 7r4   r   c                  p   e Zd ZdZd"dZd#dZed$d       Zed$d       Zed$d       Z	ed$d       Z
ed$d       Zed$d	       Zed$d
       Zed$d       Zed$d       Zed$d       Zed$d       Zed$d       Zeed%d              Zed$d       Zed$d       Zed$d       Zed$d       Zed$d       Zed$d       Zed$d       Zed$d       Zed$d       Zed$d       Zeed%d              Zeed%d              Z eed&d              Z!e!jD                  ed'd              Z!ed$d       Z#ed$d        Z$y!)(r   zCWrapper class for a `MySQLConnection` or `CMySQLConnection` object.c                >    || _         | j                   j                  }yr   r   sql_mode)r   r   _s      r2   r   zTracedMySQLConnection.__init__^  s    3: MM""r4   c                z    t         | j                  j                  |i || j                  | j                        S )zWraps the object method.)r   r   r   )r   r   cursorr~   rc   r   s      r2   r   zTracedMySQLConnection.cursorf  s6     (DMM(($9&9<< JJ
 	
r4   c                :     | j                   j                  |i |S r   )r   cmd_change_userr   s      r2   r   z%TracedMySQLConnection.cmd_change_usern  s      -t}},,d=f==r4   c                :     | j                   j                  |i |S r   )r   commitr   s      r2   r   zTracedMySQLConnection.commits  s      $t}}##T4V44r4   c                :     | j                   j                  |i |S r   )r   rollbackr   s      r2   r   zTracedMySQLConnection.rollbackx  r   r4   c                :     | j                   j                  |i |S r   )r   	cmd_queryr   s      r2   r   zTracedMySQLConnection.cmd_query}        't}}&&777r4   c                :     | j                   j                  |i |S r   )r   cmd_init_dbr   s      r2   r   z!TracedMySQLConnection.cmd_init_db  r   r4   c                :     | j                   j                  |i |S r   )r   cmd_refreshr   s      r2   r   z!TracedMySQLConnection.cmd_refresh  r   r4   c                :     | j                   j                  |i |S r   )r   cmd_quitr   s      r2   r   zTracedMySQLConnection.cmd_quit  r   r4   c                :     | j                   j                  |i |S r   )r   cmd_shutdownr   s      r2   r   z"TracedMySQLConnection.cmd_shutdown        *t}}))4:6::r4   c                :     | j                   j                  |i |S r   )r   cmd_statisticsr   s      r2   r   z$TracedMySQLConnection.cmd_statistics        ,t}}++T<V<<r4   c                :     | j                   j                  |i |S r   )r   cmd_process_killr   s      r2   r   z&TracedMySQLConnection.cmd_process_kill        .t}}--t>v>>r4   c                :     | j                   j                  |i |S r   )r   	cmd_debugr   s      r2   r   zTracedMySQLConnection.cmd_debug  r   r4   c                :     | j                   j                  |i |S r   )r   cmd_pingr   s      r2   r   zTracedMySQLConnection.cmd_ping  r   r4   c                .    | j                   j                  S r   )r   databaser   s    r2   r   zTracedMySQLConnection.database       }}%%%r4   c                :     | j                   j                  |i |S r   )r   is_connectedr   s      r2   r   z"TracedMySQLConnection.is_connected  r   r4   c                :     | j                   j                  |i |S r   )r   reset_sessionr   s      r2   r   z#TracedMySQLConnection.reset_session  s      +t}}**D;F;;r4   c                :     | j                   j                  |i |S r   )r   pingr   s      r2   r   zTracedMySQLConnection.ping  s      "t}}!!42622r4   c                :     | j                   j                  |i |S r   )r   
info_queryr   s      r2   r   z TracedMySQLConnection.info_query  s      (t}}''888r4   c                :     | j                   j                  |i |S r   )r   cmd_stmt_preparer   s      r2   r   z&TracedMySQLConnection.cmd_stmt_prepare  r   r4   c                :     | j                   j                  |i |S r   )r   cmd_stmt_executer   s      r2   r   z&TracedMySQLConnection.cmd_stmt_execute  r   r4   c                :     | j                   j                  |i |S r   )r   cmd_stmt_closer   s      r2   r   z$TracedMySQLConnection.cmd_stmt_close  r   r4   c                :     | j                   j                  |i |S r   )r   cmd_stmt_send_long_datar   s      r2   r   z-TracedMySQLConnection.cmd_stmt_send_long_data  s      5t}}44dEfEEr4   c                :     | j                   j                  |i |S r   )r   cmd_stmt_resetr   s      r2   r   z$TracedMySQLConnection.cmd_stmt_reset  r   r4   c                :     | j                   j                  |i |S r   )r   cmd_reset_connectionr   s      r2   r   z*TracedMySQLConnection.cmd_reset_connection  s      2t}}114B6BBr4   c                .    | j                   j                  S r   )r   	time_zoner   s    r2   r   zTracedMySQLConnection.time_zone  s     }}&&&r4   c                .    | j                   j                  S r   r   r   s    r2   r   zTracedMySQLConnection.sql_mode  r   r4   c                .    | j                   j                  S r   r   r   r   s    r2   r   z TracedMySQLConnection.autocommit  s     }}'''r4   c                &    || j                   _        y)r   Nr  )r   r   s     r2   r   z TracedMySQLConnection.autocommit  s     $) r4   c                :     | j                   j                  |i |S r   )r   set_charset_collationr   s      r2   r  z+TracedMySQLConnection.set_charset_collation  s      3t}}22DCFCCr4   c                :     | j                   j                  |i |S r   )r   start_transactionr   s      r2   r  z'TracedMySQLConnection.start_transaction  s      /t}}..???r4   N)r   ri   rj   r   )re   r   rf   r   rj   r   r   r   )rj   bool)r   r  rj   r   )%rx   r   r   r   r   r   r   r   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  rk   r4   r2   r   r   [  s   M#
 > > 5 5 7 7 8 8 : : : : 7 7 ; ; = = ? ? 8 8 7 7 &  & ; ; < < 3 3 9 9 ? ? ? ? = = F F = = C C '  ' &  & (  ( )  ) D D @ @r4   r   c                T     t        j                         	 	 	 	 	 	 d fd       }|S )z.Retrurn the instrumented version of `connect`.c                    t        fdt        D              rt        j                  d        | i S t	        j
                  dt              }|j                  t        t        j                  j                        t        <   |t        <   t	        j                  t           d      5 }t        d|        | i }t        ||       t        |	      cddd       S # 1 sw Y   yxY w)
zWraps the connection object returned by the method `connect`.

        Instrumentation for PooledConnections is not supported.
        c              3  &   K   | ]  }|v  
 y wNrk   ).0keyrf   s     r2   	<genexpr>z7_instrument_connect.<locals>.wrapper.<locals>.<genexpr>  s     6sf}6s   z4Instrumentation for pooled connections not supportedMySQL Connector/Pythoninstrumenting_module_nameinstrumenting_library_versiontracer_providerrs   rt   Fra   Nr   )anyr   r   rU   r   
get_tracerr   
start_spanr   r   r   r&   r'   rd   r^   r   )re   rf   r   rX   rW   connectr  s    `   r2   rh   z$_instrument_connect.<locals>.wrapper  s     666NNQRD+F++!!&>*6+
 #)"3"3%ENN,A,A #4 #
 %+ ! ^^F?3G 	8%dHf= 4*6*C &c8V<(	 	 	s   1.C))C2)re   r   rf   r   rj   z9Union['MySQLConnectionAbstract', 'PooledMySQLConnection'])	functoolswraps)r  r  rh   s   `` r2   _instrument_connectr    s@     __W&&!&	B& &P Nr4   c                  f    e Zd ZU dZdZded<   ddZddZddZ	 d	 	 	 	 	 ddZ	dd	Z
	 	 	 	 dd
Zy)MySQLInstrumentorz8MySQL instrumentation supporting mysql-connector-python.NzOptional[MySQLInstrumentor]	_instancec                    | j                   Ct        j                  | g|i || _         t        | j                   dt        j
                         | j                   S )zQSinglenton.

        Restricts the instantiation to a singular instance.
        _original_connect)r!  object__new__setattrr   r  )clsre   rf   s      r2   r%  zMySQLInstrumentor.__new__=  sI    
 == "NN3@@@CMCMM#6	8I8IJ}}r4   c                    dt          gS )zoReturn a list of python packages with versions
        that the will be instrumented (e.g., versions >= 8.1.0).zmysql-connector-python >= )r    r   s    r2   instrumentation_dependenciesz.MySQLInstrumentor.instrumentation_dependenciesI  s     --D,EFGGr4   c                    t         j                  t        | d      k7  rt        j                  d       yt        t        | d      |j                  d            t         _        y)a  Instrument 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  r  )r   r  r   r   rU   r  rL   r   rf   s     r2   
instrumentzMySQLInstrumentor.instrumentN  sO     .A BBNNPQ/D"56"JJ'89
	r4   c                   t        |t              rt        j                  d       |S t	        |d      rt	        |d      s,t        j                  d|j
                  j                         |S t        j                  dt        |      }|j                  t        t        j                  j                        |_        ||_        t!        ||j                         t        |      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.rc   r~   z+Instrumentation for class %s not supported.r  r  r  r  )r:   r   r   rU   rP   r   rx   r   r  r   r  r   r   r   rc   r~   r^   )r   r   r  r   s       r2   instrument_connectionz'MySQLInstrumentor.instrument_connection_  s    " j"78NN=>z7+7:y3QNN=$$-- !!&>*6+

 ",,%ENN,A,A - 

 $
!*j.>.>?$Z88r4   c                    t         j                  t        | d      k(  rt        j                  d       yt        | d      t         _        y)zUninstrument the library.r#  z5MySQL Connector/Python module already uninstrumented.N)r   r  r   r   rU   r+  s     r2   uninstrumentzMySQLInstrumentor.uninstrument  s:     .A BBNNRS#D*=>	r4   c                f   t        |d      s,t        j                  d|j                  j                         |S t        |t              st        j                  d       |S |j                  r;|j                  j                         r!|j                  j                          d|_        |j                  S )a  Disable instrumentation in a MySQL connection.

        Args:
            connection: instrumented connection instance.

        Returns:
            connection: uninstrumented connection instace.

        NOTE: Instrumentation for pooled connections not supported.
        rc   z-Uninstrumentation for class %s not supported.z"Connection already uninstrumented.N)rP   r   rU   r   rx   r:   r   rc   r*   r6   r   )r   r   s     r2   uninstrument_connectionz)MySQLInstrumentor.uninstrument_connection  s     z7+NN?$$-- *&;<NN?@ 
 0 0 = = ?  "#J"""r4   )re   r   rf   r   rj   r   )rj   zCollection[str])rf   r   rj   r   r  )r   ri   r  Optional[trace.TracerProvider]rj   ri   )r   ri   rj   ri   )rx   r   r   r   r!  __annotations__r%  r)  r,  r.  r0  r2  rk   r4   r2   r   r   8  sa    B-1I*1
H

( ;?(9-(9 8(9 
#	(9T?#3#	"#r4   r   )r0   r   r1   zOptional[Exception]rj   r   )r0   r   rj   r   )r>   r;   rj   r;   r  )rW   z#Optional['MySQLConnectionAbstract']rX   r   rY   zOptional[Dict[str, Any]]rj   r   )rg   r	   rj   r	   )r  HCallable[..., Union['MySQLConnectionAbstract', 'PooledMySQLConnection']]r  r3  rj   r5  )Dr   
__future__r   r  reabcr   r   
contextlibr   typingr   r   r	   r
   r   r   r   	abstractsr   r   poolingr   r9   r   	constantsr   r   r   versionr   opentelemetryr   opentelemetry.sdk.tracer   opentelemetry.semconv.tracer   ImportErrormissing_dependencies_errerrorsProgrammingErrorr   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   compiler(   r4  r3   r7   r?   r^   rm   r   r   r   r   r   r  r   rk   r4   r2   <module>rG     s\  : 8 #  	 # " R R R  I/  <  "	$ $6:    '1bjj&?  ? ,0R#	,R#R# )R# 
	R#j
4@+0c +0\7 7@g@O g@X 7;0U030 N0fv# v#q  $



+
+Q#$$s   C> >D$DD$