
    qi                         d Z ddlZddlmZ ddlmZ ddlmZ 	 ddlZdd	lmZ  G d
 de      Zy# e	$ r7Z
 ej                  de
         ej                  d        ede
       dZ
[
ww xY w)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                        e Zd ZdZ fdZdef fdZ fdZdddee	   d	e
f fd
Zdddee	   f fdZdddee	   f fdZdddee	   f fdZd Z x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                     t         |           d| _        d| _        t	        j
                         | _        | j                  st        j                  d       d| _	        y)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__s    S/opt/pipecat/venv/lib/python3.12/site-packages/pipecat/processors/metrics/sentry.pyr   zSentryMetrics.__init__"   sO     	 $&*#!+!:!:!<%%NNZ[     task_managerc                    K   t         |   |       d{    | j                  rMt        j                         | _        | j                  j                  | j                         |  d      | _	        yy7 ^w)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   s     r   r   zSentryMetrics.setup0   sp      gmL)))!!!(D $ 1 1 = =))+TF:P2Q !> !D " 	*s   A7A5AA7c                 8  K   t         |           d{    | j                  rm| j                  j	                  d       d{    | j                   d{    d| _        t        j                  |  d       t        j                  d       yy7 ~7 Q7 ?w)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   s    r   r#   zSentryMetrics.cleanup=   s     
 go$$((...#### $DLLD6!9:;S)  	 .#s3   BB.BBBB<BBBN
start_timer(   report_only_initial_ttfbc                ^  K   t         |   ||       d{    | j                  r| j                  rtt	        j
                  dd| j                                | _        t        j                  |  d| j                  j                   d| j                  j                   d       yyy7 w)	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   s      r   r1   z SentryMetrics.start_ttfb_metricsJ   s      g(!<T ) 
 	
 	
 ##(>(>$.$@$@ !5!5!7 89%D! LL&9$:O:O:W:W9XX_`d`u`u`z`z_{{|} )?#		
s   B-B+BB-end_timer8   c                   K   t         |   |       d{    | j                  rB| j                  r5| j                  j                  | j                         d{    d| _        yyy7 S7 w)zxStop tracking time-to-first-byte metrics.

        Args:
            end_time: Optional end timestamp override.
        r7   N)r
   stop_ttfb_metricsr   r   r   r$   r   r8   r   s     r   r:   zSentryMetrics.stop_ttfb_metrics`   sl      g'':::!!d&;&;$$(()>)>???$(D! '<! 	; @"   A/A+AA/A-A/-A/c                B  K   t         |   |       d{    | j                  rtt        j                  dd| j                                | _        t        j                  |  d| j                  j                   d| j                  j                   d       yy7 w)	z{Start tracking frame processing metrics.

        Args:
            start_time: Optional start timestamp override.
        r'   N
processingzProcessing for r,   r.   r/   r0   )r
   start_processing_metricsr   r   r3   r4   r   r   r5   r6   r   )r   r(   r   s     r   r?   z&SentryMetrics.start_processing_metricsl   s      g.*.EEE!!*4*F*F&t';';'=&>?+D' LL&9$:U:U:]:]9^^efj  gB  gB  gG  gG  fH  HI  J " 	Fs   BBBBc                   K   t         |   |       d{    | j                  rB| j                  r5| j                  j                  | j                         d{    d| _        yyy7 S7 w)zvStop tracking frame processing metrics.

        Args:
            end_time: Optional end timestamp override.
        r7   N)r
   stop_processing_metricsr   r   r   r$   r;   s     r   rA   z%SentryMetrics.stop_processing_metrics}   sn      g-x-@@@!!d&A&A$$(()D)DEEE*.D' 'B! 	A Fr<   c                    K   d}|r| j                   j                          d{   }|r<| j                  j                         j	                  d|j
                         d{    |du}| j                   j                          |ryy7 e7 )w)z;Background task handler for completing Sentry transactions.TN)r   getr   get_event_looprun_in_executorfinish	task_done)r   runningtxs      r   r    z"SentryMetrics._sentry_task_handler   sx     ))--//B''668HHryyYYYnG((* /Ys'   "BB
=B"B#$BBB)__name__
__module____qualname____doc__r   r   r   r#   r   floatboolr1   r:   r?   rA   r    __classcell__)r   s   @r   r   r      s    ! * 04%e_OS, FJ 
)8E? 
) OS HUO " LP 
/% 
/+r   r   )rM   r   typingr   logurur   "pipecat.utils.asyncio.task_managerr   r   ModuleNotFoundErroreerror	Exception2pipecat.processors.metrics.frame_processor_metricsr   r    r   r   <module>rZ      s|    6    >, Uw+) w+  ,FLL;qc"#FLLXY
&qc*
++,s   0 A,2A''A,