o
    QDi
                     @   sH   d dl mZmZmZmZmZmZ G dd dZG dd dZdd Z	dS )	    )connection_errors_totalconnections_totalerrors_totalexecute_many_totalexecute_totalquery_duration_secondsc                   @   s*   e Zd ZdZd
ddZdd Zdd	 ZdS )ExceptionCounterByTypea  A context manager that counts exceptions by type.

    Exceptions increment the provided counter, whose last label's name
    must match the `type_label` argument.

    In other words:

    c = Counter('http_request_exceptions_total', 'Counter of exceptions',
                ['method', 'type'])
    with ExceptionCounterByType(c, extra_labels={'method': 'GET'}):
        handle_get_request()
    typeNc                 C   s   || _ || _t|| _d S N)_counter_type_labeldict_labels)selfcounter
type_labelextra_labels r   T/var/www/Datamplify/venv/lib/python3.10/site-packages/django_prometheus/db/common.py__init__   s   zExceptionCounterByType.__init__c                 C   s   d S r
   r   )r   r   r   r   	__enter__   s   z ExceptionCounterByType.__enter__c                 C   s<   |d ur| j | j|ji | jjdi | j   d S d S )Nr   )r   updater   __name__r   labelsinc)r   typvalue	tracebackr   r   r   __exit__!   s   zExceptionCounterByType.__exit__)r	   N)r   
__module____qualname____doc__r   r   r   r   r   r   r   r      s
    
r   c                       s*   e Zd ZdZ fddZdddZ  ZS )DatabaseWrapperMixinz=Extends the DatabaseWrapper to count connections and cursors.c                    sN   t | j| j  z
t j|i |W S  ty&   t| j| j   w r
   )	r   r   aliasvendorr   superget_new_connection	Exceptionr   r   argskwargs	__class__r   r   r&   *   s   z'DatabaseWrapperMixin.get_new_connectionNc                 C   s   | j jt| j| j| jdS )N)factory)
connectioncursorExportingCursorWrapperCURSOR_CLASSr#   r$   )r   namer   r   r   create_cursor2   s   z"DatabaseWrapperMixin.create_cursorr
   )r   r   r    r!   r&   r3   __classcell__r   r   r+   r   r"   '   s    r"   c                    s&    dG  fddd| }|S )zWReturns a CursorWrapper class that knows its database's alias and
    vendor name.
    )r#   r$   c                       s8   e Zd ZdZ fddZ fddZ  ZS )z-ExportingCursorWrapper.<locals>.CursorWrapperz/Extends the base CursorWrapper to count events.c              	      s   t   tjdi  / ttd t j|i |W  d    W  d    S 1 s4w   Y  W d    d S 1 sDw   Y  d S N)r   r   )	r   r   r   r   timer   r   r%   executer(   r,   r#   r   r$   r   r   r7   ?   s   
Rz5ExportingCursorWrapper.<locals>.CursorWrapper.executec              	      s   t t| tt| tjdi  4 ttd t	 j
||g|R i |W  d    W  d    S 1 sGw   Y  W d    d S 1 sWw   Y  d S r5   )r   r   r   lenr   r   r6   r   r   r%   executemany)r   query
param_listr)   r*   r8   r   r   r:   G   s   
Rz9ExportingCursorWrapper.<locals>.CursorWrapper.executemany)r   r   r    r!   r7   r:   r4   r   r#   r   r$   r+   r   CursorWrapper<   s    r>   r   )cursor_classr#   r$   r>   r   r=   r   r0   6   s   
r0   N)
django_prometheus.dbr   r   r   r   r   r   r   r"   r0   r   r   r   r   <module>   s     
