-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Sentry instrumentation for performance and error tracking (#…
…470) * feat: Add Sentry support in FrameProcessor This update add optional Sentry integration for performance tracking and error monitoring. Key changes include: - Add conditional Sentry import and initialization check - Implement Sentry spans in FrameProcessorMetrics to measure TTFB (Time To First Byte) and processing time when Sentry is available - Maintain existing metrics functionality with MetricsFrame regardless of Sentry availability * feat: Enable metrics in DeepgramSTTService for Sentry This commit enhances the DeepgramSTTService class to enable metrics generation for use with Sentry. Key changes include: 1. Enable general metrics generation: - Implement `can_generate_metrics` method, returning True when VAD is enabled - This allows metrics to be collected and used by both Sentry and the metrics system in frame_processor.py 2. Integrate Sentry-compatible performance tracking: - Add start_ttfb_metrics and start_processing_metrics calls in the VAD speech detection handler - Implement stop_ttfb_metrics call when receiving transcripts - Add stop_processing_metrics for final transcripts 3. Enhance VAD support for metrics: - Add `vad_enabled` property to check VAD event availability - Implement VAD-based speech detection handler for precise metric timing These changes enable detailed performance tracking via both Sentry and the general metrics system when VAD is active. This allows for better monitoring and analysis of the speech-to-text process, providing valuable insights through Sentry and any other metrics consumers in the pipeline. * Update frame_processor.py * Refactor to support flexible metrics implementation - Modified the __init__ method to accept a metrics parameter that is either FrameProcessorMetrics or one of its subclasses - Updated the metrics initialization to create an instance with the processor's name - Moved all FrameProcessorMetrics-related logic to a new processors\metrics\base.py file * Implement flexible metrics system with Sentry integration 1. Created a new metrics module in processors/metrics/ 2. Implemented FrameProcessorMetrics base class in base.py: 3. Implemented SentryMetrics class in sentry.py: - Inherits from FrameProcessorMetrics - Integrates with Sentry SDK for advanced metrics tracking - Implements Sentry-specific span creation and management for TTFB and processing metrics - Handles cases where Sentry is not available or initialized
- Loading branch information
1 parent
e4f5e56
commit 33cf91b
Showing
4 changed files
with
169 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import time | ||
|
||
from pipecat.frames.frames import MetricsFrame | ||
from pipecat.metrics.metrics import ( | ||
LLMTokenUsage, | ||
LLMUsageMetricsData, | ||
MetricsData, | ||
ProcessingMetricsData, | ||
TTFBMetricsData, | ||
TTSUsageMetricsData) | ||
|
||
from loguru import logger | ||
|
||
|
||
class FrameProcessorMetrics: | ||
def __init__(self): | ||
self._start_ttfb_time = 0 | ||
self._start_processing_time = 0 | ||
self._should_report_ttfb = True | ||
|
||
def _processor_name(self): | ||
return self._core_metrics_data.processor | ||
|
||
def _model_name(self): | ||
return self._core_metrics_data.model | ||
|
||
def set_core_metrics_data(self, data: MetricsData): | ||
self._core_metrics_data = data | ||
|
||
def set_processor_name(self, name: str): | ||
self._core_metrics_data = MetricsData(processor=name) | ||
|
||
async def start_ttfb_metrics(self, report_only_initial_ttfb): | ||
if self._should_report_ttfb: | ||
self._start_ttfb_time = time.time() | ||
self._should_report_ttfb = not report_only_initial_ttfb | ||
|
||
async def stop_ttfb_metrics(self): | ||
if self._start_ttfb_time == 0: | ||
return None | ||
|
||
value = time.time() - self._start_ttfb_time | ||
logger.debug(f"{self._processor_name()} TTFB: {value}") | ||
ttfb = TTFBMetricsData( | ||
processor=self._processor_name(), | ||
value=value, | ||
model=self._model_name()) | ||
self._start_ttfb_time = 0 | ||
return MetricsFrame(data=[ttfb]) | ||
|
||
async def start_processing_metrics(self): | ||
self._start_processing_time = time.time() | ||
|
||
async def stop_processing_metrics(self): | ||
if self._start_processing_time == 0: | ||
return None | ||
|
||
value = time.time() - self._start_processing_time | ||
logger.debug(f"{self._processor_name()} processing time: {value}") | ||
processing = ProcessingMetricsData( | ||
processor=self._processor_name(), value=value, model=self._model_name()) | ||
self._start_processing_time = 0 | ||
return MetricsFrame(data=[processing]) | ||
|
||
async def start_llm_usage_metrics(self, tokens: LLMTokenUsage): | ||
logger.debug( | ||
f"{self._processor_name()} prompt tokens: {tokens.prompt_tokens}, completion tokens: {tokens.completion_tokens}") | ||
value = LLMUsageMetricsData( | ||
processor=self._processor_name(), | ||
model=self._model_name(), | ||
value=tokens) | ||
return MetricsFrame(data=[value]) | ||
|
||
async def start_tts_usage_metrics(self, text: str): | ||
characters = TTSUsageMetricsData( | ||
processor=self._processor_name(), | ||
model=self._model_name(), | ||
value=len(text)) | ||
logger.debug(f"{self._processor_name()} usage characters: { | ||
characters.value}") | ||
return MetricsFrame(data=[characters]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import time | ||
from loguru import logger | ||
|
||
try: | ||
import sentry_sdk | ||
sentry_available = sentry_sdk.is_initialized() | ||
if not sentry_available: | ||
logger.warning( | ||
"Sentry SDK not initialized. Sentry features will be disabled.") | ||
except ImportError: | ||
sentry_available = False | ||
logger.warning( | ||
"Sentry SDK not installed. Sentry features will be disabled.") | ||
|
||
from pipecat.processors.metrics.frame_processor_metrics import FrameProcessorMetrics | ||
|
||
|
||
class SentryMetrics(FrameProcessorMetrics): | ||
def __init__(self): | ||
super().__init__() | ||
self._ttfb_metrics_span = None | ||
self._processing_metrics_span = None | ||
|
||
async def start_ttfb_metrics(self, report_only_initial_ttfb): | ||
if self._should_report_ttfb: | ||
self._start_ttfb_time = time.time() | ||
if sentry_available: | ||
self._ttfb_metrics_span = sentry_sdk.start_span( | ||
op="ttfb", | ||
description=f"TTFB for {self._processor_name()}", | ||
start_timestamp=self._start_ttfb_time | ||
) | ||
logger.debug(f"Sentry Span ID: {self._ttfb_metrics_span.span_id} Description: { | ||
self._ttfb_metrics_span.description} started.") | ||
self._should_report_ttfb = not report_only_initial_ttfb | ||
|
||
async def stop_ttfb_metrics(self): | ||
stop_time = time.time() | ||
if sentry_available: | ||
self._ttfb_metrics_span.finish(end_timestamp=stop_time) | ||
|
||
async def start_processing_metrics(self): | ||
self._start_processing_time = time.time() | ||
if sentry_available: | ||
self._processing_metrics_span = sentry_sdk.start_span( | ||
op="processing", | ||
description=f"Processing for {self._processor_name()}", | ||
start_timestamp=self._start_processing_time | ||
) | ||
logger.debug(f"Sentry Span ID: {self._processing_metrics_span.span_id} Description: { | ||
self._processing_metrics_span.description} started.") | ||
|
||
async def stop_processing_metrics(self): | ||
stop_time = time.time() | ||
if sentry_available: | ||
self._processing_metrics_span.finish(end_timestamp=stop_time) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters