@session.on("metrics_collected") def _on_metri...
# ask-ai
c
@session.on("metrics_collected") def _on_metrics_collected(ev: MetricsCollectedEvent): metrics.log_metrics(ev.metrics) I collect metrics but at the end how can i access them
t
Thanks for reaching out, Yusuf. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! To get the metrics you collect using
@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:
Copy code
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:
Copy code
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?
c
But i could not capture logs as in the metrics.log_metrics(ev.metrics)
t
Hey there! To help figure out why
metrics.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.dart
c
No there is no error. but i could not capture the call duration from user open phone to close the phone
t
Hey there! If you want to track how long a call lasts—from the moment the user picks up the phone to when they hang up—here are some handy ways to do that in LiveKit: 1. Use SIP Session and Call Duration Metrics: ◦ `durSession`: This one keeps an eye on the whole SIP session, starting from the INVITE request until the session ends. ◦ `durCall`: Tracks the actual call time, from when the call gets answered right up to when it finishes. 1. Set Up Call Duration Parameters: ◦ `max_call_duration`: You can set a cap on how long calls can last. ◦ `ringing_timeout`: This sets how long the callee has to pick up before the call times out. 1. Grab Start and End Times: ◦ Use the
started_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