Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions rust/check/purity/clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,22 @@ disallowed-methods = [
{ path = "std::sync::mpsc::channel", reason = "Decision cores consume supplied data and time; move execution and live resources to runtime." },
{ path = "crossbeam::channel::unbounded", reason = "Decision cores consume supplied data and time; move execution and live resources to runtime." },
{ path = "crossbeam_channel::unbounded", reason = "Decision cores consume supplied data and time; move execution and live resources to runtime." },
{ path = "lk_agents_telemetry::scoped::start", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::scoped::start_at", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::scoped::end", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::scoped::event", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::scoped::event_at", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::scoped::measure", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::scoped::reserve", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::scoped::random_seed", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::observability::start", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::observability::start_at", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::observability::end", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::observability::event", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::observability::event_at", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::observability::measure", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::productstats::measure", reason = "Product statistics record through a process-global sink: async crates only." },
{ path = "lk_agents_telemetry::observability::reserve", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::observability::random_seed", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::process::Instruments::set", reason = "The process's own instruments are recorded by the worker, not by a sans-I/O machine." },
{ path = "lk_agents_telemetry::process::Instruments::observe", reason = "The process's own instruments are recorded by the worker, not by a sans-I/O machine." },
]

disallowed-types = [
{ path = "lk_agents_telemetry::scoped::ScopedSpan", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::scoped::Tracer", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::observability::ScopedSpan", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "lk_agents_telemetry::observability::Tracer", reason = "Sans-I/O machines record through their Trace; scoped spans belong to async work." },
{ path = "parking_lot::ReentrantMutex", reason = "Execute tasks and retain synchronization only in runtime." },
{ path = "lock_api::Mutex", reason = "Execute tasks and retain synchronization only in runtime." },
{ path = "lock_api::RwLock", reason = "Execute tasks and retain synchronization only in runtime." },
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/lk-agents-inference/src/fallback/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ impl<B: Backend, T: Send + 'static, S: Clone + Send + 'static> ChainRun<'_, B, T
&& let Some(provider) = chain.entries[previous].stat_labels()
{
let fallback = lk_agents_abi::productstats::ProviderFallbacks { provider };
lk_agents_telemetry::productstats::count(&fallback);
lk_agents_telemetry::productstats::measure(&fallback, 1u64);
}
let attempt_id = chain.attempts.fetch_add(1, Ordering::Relaxed) + 1;
chain.select(index, attempt_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ impl Pump {
}

async fn run_loop(&self, mut commands: media_queue::AudioReceiver<Cmd>) {
productstats::count(&ProviderRequests { provider: self.stats.clone() });
productstats::measure(&ProviderRequests { provider: self.stats.clone() }, 1u64);
let mut connection = RealtimeConnection::new(&self.connect.conn, self.clock.now());
let mut effects = connection.start(self.clock.now());
let mut reconnect = false;
Expand All @@ -518,12 +518,18 @@ impl Pump {
}
RealtimeConnectionEffect::Retry { error, backoff } => {
let _ = backoff.announce(&self.label, &error);
productstats::count(&ProviderRetries { provider: self.stats.clone() });
productstats::measure(
&ProviderRetries { provider: self.stats.clone() },
1u64,
);
self.emit_error(error, true);
ControlFlow::Continue(())
}
RealtimeConnectionEffect::Reconnect { error } => {
productstats::count(&ProviderReconnects { provider: self.stats.clone() });
productstats::measure(
&ProviderReconnects { provider: self.stats.clone() },
1u64,
);
let subject = &self.label;
lk_agents_core::request::retrying(
log::Level::Warn,
Expand All @@ -538,7 +544,7 @@ impl Pump {
RealtimeConnectionEffect::Failed { error } => {
let error_type = FailureType::of_api(&error);
let provider = self.stats.clone();
productstats::count(&ProviderFailures { provider, error_type });
productstats::measure(&ProviderFailures { provider, error_type }, 1u64);
self.emit_error(error, false);
self.close().await;
ControlFlow::Break(())
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/lk-agents-inference/src/stt/retrying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Report<'_> {
) {
if !self.stt.retries_itself() {
let provider = telemetry::stat_labels(Operation::Stt, &self.stt.id());
lk_agents_telemetry::productstats::count(&labels(provider));
lk_agents_telemetry::productstats::measure(&labels(provider), 1u64);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions rust/crates/lk-agents-inference/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ pub(crate) struct RequestStats<'a> {

impl RequestStats<'_> {
pub fn record(self) {
use lk_agents_telemetry::productstats::{count, record};
use lk_agents_telemetry::productstats::measure;
let provider = self.labels;
count(&ProviderRequests { provider: provider.clone() });
measure(&ProviderRequests { provider: provider.clone() }, 1u64);
if self.retries > 0 {
record(&ProviderRetries { provider: provider.clone() }, u64::from(self.retries));
measure(&ProviderRetries { provider: provider.clone() }, u64::from(self.retries));
}
if let Some(error) = self.failure {
let error_type = FailureType::of(error);
count(&ProviderFailures { provider: provider.clone(), error_type });
measure(&ProviderFailures { provider: provider.clone(), error_type }, 1u64);
}
if let Some(seconds) = self.duration {
record(&ProviderDuration { provider }, seconds);
measure(&ProviderDuration { provider }, seconds);
}
}
}
Expand Down Expand Up @@ -288,7 +288,7 @@ impl LlmTrace {
let labels = stat_labels(Operation::Llm, &self.id);
if let Some(at) = self.first_chunk {
let ttft = ProviderTtft { provider: labels.clone() };
lk_agents_telemetry::productstats::record(&ttft, seconds(self.started, at));
lk_agents_telemetry::productstats::measure(&ttft, seconds(self.started, at));
}
RequestStats {
labels,
Expand Down Expand Up @@ -515,7 +515,7 @@ impl TtsTrace {
let labels = stat_labels(Operation::Tts, &self.id);
if let Some(ttfb) = metrics.and_then(|m| m.ttfb).filter(|t| *t >= 0.0) {
let ttfb_labels = ProviderTtfb { provider: labels.clone() };
lk_agents_telemetry::productstats::record(&ttfb_labels, ttfb);
lk_agents_telemetry::productstats::measure(&ttfb_labels, ttfb);
}
RequestStats {
labels,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn llm() -> ProviderLabels {
lk_agents_telemetry::productstats::provider(Operation::Llm, "openai", "gpt-4o")
}

fn record<M: MetricDecl<Audience = decl::ProductStats>>(
fn measure<M: MetricDecl<Audience = decl::ProductStats>>(
stats: &ProductStats,
labels: &M,
n: impl Into<lk_agents_abi::telemetry::record::Number>,
Expand Down Expand Up @@ -120,20 +120,20 @@ async fn an_export_carries_only_declared_product_statistics_keys() {
plugin("livekit.plugins.openai", "1.2.3");
plugin("my_company.secret_plugin", "0.1");
stats.configure(Some(destination(&url)));
record(&stats, &ProviderRequests { provider: llm() }, 2u64);
record(
measure(&stats, &ProviderRequests { provider: llm() }, 2u64);
measure(
&stats,
&ProviderFailures { provider: llm(), error_type: FailureType::ApiStatusError },
1u64,
);
record(&stats, &ProviderTtft { provider: llm() }, 0.3);
measure(&stats, &ProviderTtft { provider: llm() }, 0.3);
let stack = [lk_agents_abi::telemetry::record::StackFrame {
function: "requests_get".into(),
file: "/home/someone/app.py".into(),
line: 9,
}];
let signature = lk_agents_telemetry::productstats::stall_signature(Some(&stack));
record(&stats, &EventLoopStalls { signature }, 1u64);
measure(&stats, &EventLoopStalls { signature }, 1u64);
assert!(stats.flush(LIMIT).await);
let requests = received(&mut rx);
assert_eq!(requests.len(), 1);
Expand Down Expand Up @@ -223,10 +223,10 @@ async fn only_livekit_cloud_receives_product_statistics() {
assert_eq!(Destination::cloud("wss://p.livekit.cloud", "", ""), None);
let (url, mut rx) = endpoint(ok).await;
let stats = stats();
record(&stats, &ProviderRequests { provider: llm() }, 1u64);
measure(&stats, &ProviderRequests { provider: llm() }, 1u64);
assert!(stats.flush(LIMIT).await);
stats.configure(None);
record(&stats, &ProviderRequests { provider: llm() }, 1u64);
measure(&stats, &ProviderRequests { provider: llm() }, 1u64);
assert!(stats.flush(LIMIT).await);
stats.configure(Some(destination(&url)));
assert!(stats.flush(LIMIT).await);
Expand All @@ -242,7 +242,7 @@ async fn drops_are_counted_and_exported_next() {
let (url, mut rx) = endpoint(ok).await;
let stats = stats();
for _ in 0..2 {
record(&stats, &ProviderRequests { provider: llm() }, 1u64);
measure(&stats, &ProviderRequests { provider: llm() }, 1u64);
assert!(stats.flush(LIMIT).await, "a window waits for a credential");
}
stats.configure(Some(destination(&refusing)));
Expand Down Expand Up @@ -277,7 +277,7 @@ async fn a_customer_observability_refusal_leaves_product_statistics_on() {
stats.configure(Some(destination(&url)));
for _ in 0..2 {
customer.record(counter(Some("AJ_1"), 1));
record(&stats, &ProviderRequests { provider: llm() }, 1u64);
measure(&stats, &ProviderRequests { provider: llm() }, 1u64);
customer.flush(None, LIMIT).await;
assert!(stats.flush(LIMIT).await);
}
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/lk-agents-net/src/export/stall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn event_loop_blocked(
};
telemetry::measure(cx, &EventLoopBlockedDuration { severity }, duration.as_secs_f64());
let signature = telemetry::productstats::stall_signature(stack.as_deref());
telemetry::productstats::count(&EventLoopStalls { signature });
telemetry::productstats::measure(&EventLoopStalls { signature }, 1u64);
let now = cx.now_nanos();
let suppressed = {
let RateLimit { sent, suppressed } = &mut *blocked.0.lock();
Expand Down
14 changes: 7 additions & 7 deletions rust/crates/lk-agents-runtime/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ fn record_product_stats(metrics: &AgentMetrics) {
use lk_agents_abi::productstats::{
Operation, ProviderDuration, ProviderRequests, ProviderTtft,
};
use lk_agents_telemetry::productstats::record;
use lk_agents_telemetry::productstats::measure;
let labels = |operation| {
let md = metrics.metadata();
let id = lk_agents_abi::ProviderId {
Expand All @@ -480,24 +480,24 @@ fn record_product_stats(metrics: &AgentMetrics) {
match metrics {
AgentMetrics::Stt(m) => {
if let Some(duration) = m.duration.filter(|d| *d > 0.0) {
record(&ProviderDuration { provider: labels(Operation::Stt) }, duration);
measure(&ProviderDuration { provider: labels(Operation::Stt) }, duration);
}
}
AgentMetrics::RealtimeModel(m) if !m.cancelled => {
let provider = labels(Operation::Realtime);
if let Some(ttft) = m.ttft.filter(|t| *t >= 0.0) {
record(&ProviderTtft { provider: provider.clone() }, ttft);
measure(&ProviderTtft { provider: provider.clone() }, ttft);
}
record(&ProviderDuration { provider }, m.duration);
measure(&ProviderDuration { provider }, m.duration);
}
AgentMetrics::EotInference(m) => {
let provider = labels(Operation::Eot);
record(&ProviderRequests { provider: provider.clone() }, u64::from(m.num_requests));
record(&ProviderDuration { provider }, m.prediction_duration);
measure(&ProviderRequests { provider: provider.clone() }, u64::from(m.num_requests));
measure(&ProviderDuration { provider }, m.prediction_duration);
}
AgentMetrics::Vad(m) if m.inference_count > 0 => {
let provider = labels(Operation::Vad);
record(&ProviderRequests { provider }, u64::from(m.inference_count));
measure(&ProviderRequests { provider }, u64::from(m.inference_count));
}
_ => {}
}
Expand Down
4 changes: 2 additions & 2 deletions rust/crates/lk-agents-telemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
// `#[span]` functions name the span API by its crate path, here as in every caller.
extern crate self as lk_agents_telemetry;

mod observability;
pub mod process;
pub mod productstats;
mod scoped;
pub mod span;

pub use scoped::*;
pub use observability::*;
Original file line number Diff line number Diff line change
Expand Up @@ -363,5 +363,5 @@ impl<D: SpanDecl> Drop for ScopedSpan<D> {
}

#[cfg(test)]
#[path = "scoped_tests.rs"]
#[path = "observability_tests.rs"]
mod tests;
11 changes: 3 additions & 8 deletions rust/crates/lk-agents-telemetry/src/productstats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ pub fn enabled() -> bool {
///
/// ```
/// use lk_agents_abi::productstats::JobStarts;
/// lk_agents_telemetry::productstats::record(&JobStarts, 1u64);
/// lk_agents_telemetry::productstats::measure(&JobStarts, 1u64);
/// ```
///
/// A customer instrument does not compile:
///
/// ```compile_fail,E0271
/// use lk_agents_abi::telemetry::instruments::WorkerProcessReadyDuration;
/// lk_agents_telemetry::productstats::record(&WorkerProcessReadyDuration, 1.0);
/// lk_agents_telemetry::productstats::measure(&WorkerProcessReadyDuration, 1.0);
/// ```
pub fn record<M: MetricDecl<Audience = ProductStats>>(labels: &M, value: impl Into<Number>) {
pub fn measure<M: MetricDecl<Audience = ProductStats>>(labels: &M, value: impl Into<Number>) {
let Some(sink) = SINK.read().clone() else { return };
let mut out = KeyValues::new();
labels.write(&mut out);
Expand All @@ -70,11 +70,6 @@ pub fn record<M: MetricDecl<Audience = ProductStats>>(labels: &M, value: impl In
});
}

/// Adds one to counter `M`.
pub fn count<M: MetricDecl<Audience = ProductStats>>(labels: &M) {
record(labels, 1u64);
}

/// The labels of a provider statistic; an empty provider or model is `unknown`.
pub fn provider(
operation: lk_agents_abi::productstats::Operation,
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/lk-agents-worker/src/ipc/job_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl Inner {
(None, true) => JobOutcome::Cancelled,
(None, false) => JobOutcome::Ok,
};
lk_agents_telemetry::productstats::count(&JobEnds { outcome });
lk_agents_telemetry::productstats::measure(&JobEnds { outcome }, 1u64);
if let Some(job) = self.job.get() {
lk_agents_net::job_identity::close(&job.key);
}
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/lk-agents-worker/src/ipc/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ impl Driver {
Exit { code, .. } if code < 0 => CrashSignature::Signaled,
Exit { .. } => CrashSignature::Exited,
};
lk_agents_telemetry::productstats::count(&ProcessCrashes { signature });
lk_agents_telemetry::productstats::measure(&ProcessCrashes { signature }, 1u64);
runner_log!(self.host.runner(), log::Level::Error; "{} exited with non-zero exit code {}", runner_name(&self.opts), exit.code);
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/lk-agents-worker/src/ipc/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub(crate) fn start_job(
let job = &start.info.job;
let room = job.room.clone().unwrap_or_default();
let key = JobKey(job.id.clone());
lk_agents_telemetry::productstats::count(&lk_agents_abi::productstats::JobStarts);
lk_agents_telemetry::productstats::measure(&lk_agents_abi::productstats::JobStarts, 1u64);
if let Some(exporter) = exporter() {
if let Some(cloud) = cloud {
exporter.configure_cloud(cloud.clone());
Expand Down
Loading