o
    i                     @   s  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
 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 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 G dd dejZG dd dejZdddZdd ZdS )    )config)Pin)	SPAN_KIND)dbapi)Psycopg2FetchTracedCursor)Psycopg2TracedCursor)Psycopg3FetchTracedCursor)Psycopg3TracedCursor)_patch_extensions)ext_service)SpanKind)	SpanTypes)db)net)sql)core)	COMPONENTc                       s&   e Zd Zd fdd	Zdd Z  ZS )Psycopg3TracedConnectionNc                    2   |s
t jjrtnt}tt| j||t j|d d S N)
cursor_cls)r   psycopgtrace_fetch_methodsr   r	   superr   __init__selfconnpinr   	__class__ _/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/contrib/internal/psycopg/connection.pyr         z!Psycopg3TracedConnection.__init__c                    s    fdd}||i |S )z8Execute a query and return a cursor to read its results.c               
      sP   z   }|dd rd|_|j| i |W S  ty' } z|d d }~ww )Nbinary   )cursorgetformatexecute	Exceptionwith_traceback)argskwargscurexr   r!   r"   patched_execute   s   
z9Psycopg3TracedConnection.execute.<locals>.patched_executer!   )r   r,   r-   r1   r!   r0   r"   r)      s   	z Psycopg3TracedConnection.executeNN)__name__
__module____qualname__r   r)   __classcell__r!   r!   r   r"   r      s    r   c                       s"   e Zd ZdZd fdd	Z  ZS )Psycopg2TracedConnectionz6TracedConnection wraps a Connection with tracing code.Nc                    r   r   )r   r   r   r   r   r   r7   r   r   r   r!   r"   r   .   r#   z!Psycopg2TracedConnection.__init__r2   )r3   r4   r5   __doc__r   r6   r!   r!   r   r"   r7   +   s    r7   Nc                 C   s   t | dr
| jr
| S d}|r|jdd}|j}|rt| || }tjdi}zt | dr4t| j	}nt| j
j	}W n tyG   i }Y nw |rs|tj|dtj|ddtj|dtj|d	tj|d
d|di t||d| |S )z<Wrap will patch the instance so that its queries are traced.closedN_extensions_to_patch
postgresqldsnhostporti8  dbnameuserzdb.applicationapplication_name)tags_config)hasattrr9   rC   r'   r
   r   SYSTEMr   parse_pg_dsnr<   infor*   updater   TARGET_HOSTTARGET_PORTSERVER_ADDRESSNAMEUSERr   onto)r   traced_conn_clsr   rC   extensions_to_patchcrB   r<   r!   r!   r"   
patch_conn6   s>   

rR   c                    s    fdd}|S )Nc                    s    j dkrtnt}t }|r| r|jjs | |i |}n:tj	dd
| j| j t||jtj|ttjt|jjtj|jjidd | |i |}W d    n1 sUw   Y  t|||dS )Nr   zpsycopg.patched_connectz{}.{}T)	span_nameservice	span_typer   rB   measured)r   rO   )r3   r   r7   r   get_fromenabledrC   trace_connectr   context_with_datar(   r4   r   r   SQLr   r   CLIENTr   integration_namer   rE   	dbms_namerR   )connect_func_r,   r-   rO   r   r   psycopg_moduler!   r"   patched_connecth   s&   


z0patched_connect_factory.<locals>.patched_connectr!   )rb   rc   r!   ra   r"   patched_connect_factoryg   s   rd   )N) ddtracer   ddtrace._trace.pinr   ddtrace.constantsr   ddtrace.contribr   'ddtrace.contrib.internal.psycopg.cursorr   r   r   r	   +ddtrace.contrib.internal.psycopg.extensionsr
   $ddtrace.contrib.internal.trace_utilsr   ddtrace.extr   r   r   r   r   ddtrace.internalr   ddtrace.internal.constantsr   TracedConnectionr   r7   rR   rd   r!   r!   r!   r"   <module>   s*    
1