-
Notifications
You must be signed in to change notification settings - Fork 397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Sentry instrumentation for performance and error tracking #470
Merged
+169
−80
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d9d41d9
feat: Add Sentry support in FrameProcessor
cyrilS-dev 64605b3
feat: Enable metrics in DeepgramSTTService for Sentry
cyrilS-dev ad97e75
Update frame_processor.py
cyrilS-dev c442c22
Refactor to support flexible metrics implementation
cyrilS-dev 34014eb
Implement flexible metrics system with Sentry integration
cyrilS-dev bbbcf4f
Add missing newline at end of file
cyrilS-dev 3c5483c
Merge branch 'main' into sentry-impl
cyrilS-dev 3b3f1e5
Merge remote-tracking branch 'upstream/main' into sentry-impl
cyrilS-dev cd13aff
Refactor to Align with Merged PR #474
cyrilS-dev f7f4c95
Format with autopep8 & adjust logger level
cyrilS-dev a2adca1
Format with autopep8
cyrilS-dev 4ef47b2
Format with autopep8
cyrilS-dev 6d6d2cd
Format with autopep8
cyrilS-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,79 @@ | ||
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,52 @@ | ||
import time | ||
from loguru import logger | ||
|
||
try: | ||
import sentry_sdk | ||
sentry_available = sentry_sdk.is_initialized() | ||
if not sentry_available: | ||
logger.debug("Sentry SDK not initialized. Sentry features will be disabled.") | ||
except ImportError: | ||
sentry_available = False | ||
logger.debug("Sentry SDK not installed. Sentry features will be disabled.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done ✅ |
||
|
||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems this should be a warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done ✅