Skip to content

Commit 7ceae59

Browse files
committed
Collect metrics
1 parent 73a8dd7 commit 7ceae59

2 files changed

Lines changed: 33 additions & 5 deletions

File tree

livekit-ffi/src/server/audio_stream.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
// limitations under the License.
1414

1515
use std::borrow::Cow;
16-
use std::time::Duration;
16+
use std::time::{Duration, Instant};
1717

1818
use futures_util::StreamExt;
1919
use livekit::track::Track;
2020
use livekit::webrtc::{audio_stream::native::NativeAudioStream, prelude::*};
2121
use livekit::{registered_audio_filter_plugin, AudioFilterAudioStream, AudioFilterStreamInfo};
22+
use metrics_logger::metrics::histogram;
2223
use tokio::sync::{broadcast, mpsc, oneshot};
2324

2425
use super::audio_plugin::AudioStreamKind;
@@ -373,6 +374,9 @@ impl FfiAudioStream {
373374
break;
374375
}
375376
frame = native_stream.next() => {
377+
378+
let t0 = Instant::now();
379+
376380
let Some(frame) = frame else {
377381
break;
378382
};
@@ -447,6 +451,8 @@ impl FfiAudioStream {
447451
}
448452
}
449453

454+
let delta = t0.elapsed();
455+
histogram!("forward_audio_frame").record(delta.as_millis() as f64);
450456
}
451457
}
452458
}

livekit-ffi/src/server/mod.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@
1515
use std::{
1616
error::Error,
1717
sync::{
18-
atomic::{AtomicU64, Ordering},
19-
Arc,
18+
Arc, atomic::{AtomicU64, Ordering}
2019
},
2120
thread,
22-
time::Duration,
21+
time::{Duration, Instant},
2322
};
2423

2524
use dashmap::{mapref::one::MappedRef, DashMap};
2625
use downcast_rs::{impl_downcast, Downcast};
2726
use livekit::webrtc::{
2827
native::apm::AudioProcessingModule, native::audio_resampler::AudioResampler, prelude::*,
2928
};
30-
use metrics_logger::{LogMode, MetricsLogger, metrics};
29+
use metrics_logger::{LogMode, MetricsLogger, metrics::{self, histogram}};
3130
use parking_lot::{deadlock, Mutex};
3231
use tokio::{sync::oneshot, task::JoinHandle};
3332

@@ -173,13 +172,19 @@ impl FfiServer {
173172
}
174173

175174
pub fn send_event(&self, message: proto::ffi_event::Message) -> FfiResult<()> {
175+
176+
let t0 = Instant::now();
177+
176178
let cb = self
177179
.config
178180
.lock()
179181
.as_ref()
180182
.map_or_else(|| Err(FfiError::NotConfigured), |c| Ok(c.callback_fn.clone()))?;
181183

182184
cb(proto::FfiEvent { message: Some(message) });
185+
186+
let delta = t0.elapsed();
187+
histogram!("send_event").record(delta.as_millis() as f64);
183188
Ok(())
184189
}
185190

@@ -197,7 +202,12 @@ impl FfiServer {
197202
where
198203
T: FfiHandle,
199204
{
205+
let t0 = Instant::now();
206+
200207
self.ffi_handles.insert(id, Box::new(handle));
208+
209+
let delta = t0.elapsed();
210+
histogram!("store_handle").record(delta.as_millis() as f64);
201211
}
202212

203213
pub fn retrieve_handle<T>(
@@ -211,6 +221,8 @@ impl FfiServer {
211221
return Err(FfiError::InvalidRequest("handle is invalid".into()));
212222
}
213223

224+
let t0 = Instant::now();
225+
214226
let handle =
215227
self.ffi_handles.get(&id).ok_or(FfiError::InvalidRequest("handle not found".into()))?;
216228

@@ -220,6 +232,9 @@ impl FfiServer {
220232
return Err(FfiError::InvalidRequest(msg.into()));
221233
}
222234

235+
let delta = t0.elapsed();
236+
histogram!("retrieve_handle").record(delta.as_millis() as f64);
237+
223238
let handle = handle.map(|v| v.downcast_ref::<T>().unwrap());
224239
Ok(handle)
225240
}
@@ -246,8 +261,15 @@ impl FfiServer {
246261
}
247262

248263
pub fn drop_handle(&self, id: FfiHandleId) -> bool {
264+
265+
let t0 = Instant::now();
266+
249267
let existed = self.ffi_handles.remove(&id).is_some();
250268
self.handle_dropped_txs.remove(&id);
269+
270+
let delta = t0.elapsed();
271+
histogram!("drop_handle").record(delta.as_millis() as f64);
272+
251273
return existed;
252274
}
253275

0 commit comments

Comments
 (0)