creamy-byte-34126
08/09/2025, 1:19 PMtall-airline-16756
08/09/2025, 1:19 PMtall-airline-16756
08/09/2025, 1:19 PM@session.on("metrics_collected")
, you can tap into the MetricsCollectedEvent
and the UsageCollector
class that LiveKit provides. Here’s a simple way to do it:
1. First, use the metrics.log_metrics()
function to log the metrics data whenever a MetricsCollectedEvent
pops up.
2. If you want to gather metrics across the whole session, just create a UsageCollector
and collect each metrics event like this:
usage_collector = metrics.UsageCollector()
@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent):
metrics.log_metrics(ev.metrics)
usage_collector.collect(ev.metrics)
1. Then, set up a shutdown callback to log a neat summary of all the metrics once the session wraps up:
async def log_usage():
summary = usage_collector.get_summary()
<http://logger.info|logger.info>(f"Usage: {summary}")
ctx.add_shutdown_callback(log_usage)
This way, you’ll be able to log metrics as they come in and also have a nice summary ready at the end of the session. Pretty handy, right?creamy-byte-34126
08/09/2025, 1:23 PMtall-airline-16756
08/09/2025, 1:23 PMmetrics.log_metrics(ev.metrics)
isn’t capturing logs, could you share a bit more about your setup?
• Is the metrics
module properly imported and ready to use in your code?
• Are you seeing any error messages or logs that might hint at why log_metrics
isn’t working?
• Would you mind sharing how you’ve set up the event subscription and metrics collection in your code? Sources: Logs, metrics, and telemetry | LiveKit Docs | Capturing metrics | LiveKit Docs | Capturing metrics | LiveKit Docs | Capturing metrics | LiveKit Docs | lib/src/proto/livekit_metrics.pbjson.dartcreamy-byte-34126
08/09/2025, 1:29 PMtall-airline-16756
08/09/2025, 1:30 PMstarted_at_ns
and ended_at_ns
timestamps from the SIP call details to get the exact call length.
1. Extra Metrics to Consider:
◦ The audio_duration
metric is great for tracking how long audio processing takes during the call.
These tips should help you measure call durations accurately and tweak the settings just the way you need. If you have any questions or want to chat more about it, I’m here to help!
Sources: SIP APIs | LiveKit Docs | livekit/livekit_sip.pb.go | livekit-agents/livekit/agents/metrics/utils.py | pkg/stats/monitor.go