o
    i                  
   @   s   d Z ddlZddlmZ ddlmZ ddlmZ zddlZW n  e	y> Z
 zede
  ed ede
 dZ
[
ww dd	lmZ G d
d deZdS )z/Sentry integration for frame processor metrics.    N)Optional)logger)BaseTaskManagerzException: zEIn order to use Sentry, you need to `pip install pipecat-ai[sentry]`.zMissing module: )FrameProcessorMetricsc                       s   e Zd ZdZ fddZdef fddZ fddZd	d
dee	 de
f fddZd	ddee	 f fddZd	d
dee	 f fddZd	ddee	 f fddZdd Z  ZS )SentryMetricszFrame processor metrics integration with Sentry monitoring.

    Extends FrameProcessorMetrics to send time-to-first-byte (TTFB) and
    processing metrics as Sentry transactions for performance monitoring
    and debugging.
    c                    s:   t    d| _d| _t | _| jstd d| _	dS )zInitialize the Sentry metrics collector.

        Sets up internal state for tracking transactions and verifies
        Sentry SDK initialization status.
        Nz=Sentry SDK not initialized. Sentry features will be disabled.)
super__init___ttfb_metrics_tx_processing_metrics_tx
sentry_sdkis_initialized_sentry_availabler   warning_sentry_taskself	__class__ U/home/ubuntu/.local/lib/python3.10/site-packages/pipecat/processors/metrics/sentry.pyr   "   s   



zSentryMetrics.__init__task_managerc                    sH   t  |I dH  | jr"t | _| jj|  |  dd| _	dS dS )zSetup the Sentry metrics system.

        Args:
            task_manager: The task manager to use for background operations.
        Nz::_sentry_task_handler)name)
r   setupr   asyncioQueue_sentry_queuer   create_task_sentry_task_handlerr   )r   r   r   r   r   r   0   s   
zSentryMetrics.setupc                    s`   t   I dH  | jr.| jdI dH  | jI dH  d| _t|  d tjdd dS dS )zClean up Sentry resources and flush pending transactions.

        Ensures all pending transactions are sent to Sentry before shutdown.
        Nz Flushing Sentry metricsg      @)timeout)	r   cleanupr   r   putr   tracer   flushr   r   r   r   r   =   s   zSentryMetrics.cleanupN
start_timer$   report_only_initial_ttfbc                   sn   t  j||dI dH  | jr3| jr5tjdd|   d| _t	|  d| jj
 d| jj d dS dS dS )	zStart tracking time-to-first-byte metrics.

        Args:
            start_time: Optional start timestamp override.
            report_only_initial_ttfb: Whether to report only the initial TTFB measurement.
        )r$   r%   Nttfbz	TTFB for opr   ! Sentry transaction started (ID:  Name: ))r   start_ttfb_metrics_should_report_ttfbr   r   start_transaction_processor_namer	   r   debugspan_idr   )r   r$   r%   r   r   r   r,   J   s   	z SentryMetrics.start_ttfb_metricsend_timer3   c                   H   t  j|dI dH  | jr | jr"| j| jI dH  d| _dS dS dS )zxStop tracking time-to-first-byte metrics.

        Args:
            end_time: Optional end timestamp override.
        r2   N)r   stop_ttfb_metricsr   r	   r   r    r   r3   r   r   r   r5   `      
zSentryMetrics.stop_ttfb_metricsc                   sb   t  j|dI dH  | jr/tjdd|   d| _t|  d| jj	 d| jj
 d dS dS )	z{Start tracking frame processing metrics.

        Args:
            start_time: Optional start timestamp override.
        r#   N
processingzProcessing for r'   r)   r*   r+   )r   start_processing_metricsr   r   r.   r/   r
   r   r0   r1   r   )r   r$   r   r   r   r9   l   s   z&SentryMetrics.start_processing_metricsc                   r4   )zvStop tracking frame processing metrics.

        Args:
            end_time: Optional end timestamp override.
        r2   N)r   stop_processing_metricsr   r
   r   r    r6   r   r   r   r:   }   r7   z%SentryMetrics.stop_processing_metricsc                    sV   d}|r)| j  I dH }|r| j d|jI dH  |du}| j   |sdS dS )z;Background task handler for completing Sentry transactions.TN)r   getr   get_event_looprun_in_executorfinish	task_done)r   runningtxr   r   r   r      s   
z"SentryMetrics._sentry_task_handler)__name__
__module____qualname____doc__r   r   r   r   r   floatboolr,   r5   r9   r:   r   __classcell__r   r   r   r   r      s    r   )rE   r   typingr   logurur   "pipecat.utils.asyncio.task_managerr   r   ModuleNotFoundErroreerror	Exception2pipecat.processors.metrics.frame_processor_metricsr   r   r   r   r   r   <module>   s   
