
    g                    V   U d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZmZmZ d dlmZ d dlmZ e	r6d 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  edee
   ee
   e      Zeez  ZdZ de!d<   	 	 	 	 	 	 	 	 ddZ"	 d	 	 	 	 	 	 	 	 	 ddZ#ddZ$edd       Z%edd       Z%ddZ%y)    )annotationsN)	find_spec)
ModuleType)TYPE_CHECKINGAnyLiteralcastoverload)Requirement)Logfire)PsycopgInstrumentor)Psycopg2Instrumentor)AsyncConnection
Connection)
connection)TypeVarPsycopgConnectionpsycopgpsycopg2z.tuple[Literal['psycopg'], Literal['psycopg2']]PACKAGE_NAMESc                   |%t         D ]  }t        |      st        | |fi |  y|t         v rt        | fd|i| yt	        |t
              r+t        t        d   |j                        }t        | |fi | y|j                  j                  D ]I  }|j                  j                  d      d   }|t         v s*|rt        d| d      t        | ||fi |  y t        d|      )	zInstrument a `psycopg` connection or module so that spans are automatically created for each query.

    See the `Logfire.instrument_psycopg` method for details.
    Nnamer   .r   zBExtra keyword arguments are only supported when instrumenting the z module, not a connection.zDon't know how to instrument )r   r   instrument_psycopg_instrument_psycopg
isinstancer   r	   r   __name__	__class____mro__
__module__split	TypeError
ValueError)logfire_instanceconn_or_modulekwargspackagemodule_nameclss         [/var/www/openai/venv/lib/python3.12/site-packages/logfire/_internal/integrations/psycopg.pyr   r      s    $G!"#3WGG % 		=	(,L>LVL	NJ	/7#89>;R;RS+[CFC "++33Cnn**3/2G-'#\]d\ee  A  $$4g~XQWX 4 4^4FG
HH    c                   t        |      }|t        j                  |      }t        ||j                  |      }|j                  d      r(|dk(  r#dd l}|j                  j                         |_	         |j                  dd|i| j                  j                         | j                  j                         d| y |j                  || j                  j                                y )Nenable_commenterr   r   skip_dep_check)tracer_providermeter_provider)r0    )_get_instrumentor	importlibimport_modulecheck_version__version__get
psycopg.pqpqversion__libpq_version__
instrumentconfigget_tracer_providerget_meter_providerinstrument_connection)r%   r   connr'   instrumentormodr/   r   s           r+   r   r   B   s     %T*L| %%d+&tS__lK::()di.? %,JJ$6$6$8C! 	
)	
 $4#:#:#N#N#P"2"9"9"L"L"N 	
 	**4AQAXAXAlAlAn*or,   c                .   t        j                  t              5  |j                         D ]S  }t	        |      }|j
                  | k(  s|j                  j                  |j                         d         sK d d d        y 	 d d d        y# 1 sw Y   yxY w)Nr   TF)	
contextlibsuppress	Exceptioninstrumentation_dependenciesr   r   	specifiercontainsr"   )r   r;   rC   depreqs        r+   r6   r6   e   ss    			Y	'<<>Cc"Cxx4CMM$:$:7==?1;M$N 
(	'> 
(  
( s   .B	,B6B BBc                     y Nr2   r   s    r+   r3   r3   o   s    HKr,   c                     y rO   r2   rP   s    r+   r3   r3   s   s    JMr,   c                    	 t        j                  d|        } t        || j	                          d             S # t        $ r t        d|  d|  d      w xY w)Nzopentelemetry.instrumentation.zRun `pip install 'logfire[z]'` to install z instrumentation.Instrumentor)r4   r5   ImportErrorgetattr
capitalize)r   instrumentor_modules     r+   r3   r3   w   sw    e'558VW[V\6]^ L7&4??+<*=\(JKMM  e6tfOD6Qbcddes	   < A)r%   r   r&   z[ModuleType | Literal['psycopg', 'psycopg2'] | None | PsycopgConnection | Psycopg2Connectionr'   r   returnNonerO   )
r%   r   r   zLiteral['psycopg', 'psycopg2']rB   r   r'   r   rX   rY   )r   strr;   rZ   rC   rS   rX   bool)r   zLiteral['psycopg']rX   r   )r   zLiteral['psycopg2']rX   r   )r   rZ   rX   rS   )&
__future__r   rF   r4   importlib.utilr   typesr   typingr   r   r   r	   r
   packaging.requirementsr   logfirer   %opentelemetry.instrumentation.psycopgr   &opentelemetry.instrumentation.psycopg2r   r   r   r   psycopg2._psycopgr   Psycopg2Connectiontyping_extensionsr   r   rS   r   __annotations__r   r   r6   r3   r2   r,   r+   <module>rh      s   "   $  > > . IK3B) 3Z_oVYFZ\no&)==L AX= W#I#Io#I #I 
	#IR  p p
( p  p 	 p
 
 pF 
 K 
 K 
 M 
 MNr,   