Watch
0
0
Fork
You've already forked hyperhive
0

swarm-stt: metrics and a Grafana board for the speech-to-text service

This commit is contained in:
damocles 2026-11-12 09:46:37 +01:00 • committed by mara
commit b180680427

View file

@ -10,10 +10,11 @@ use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use std::time::{Duration, Instant};
use anyhow::{Context as _, Result, bail};
use futures_util::StreamExt as _;
use metrics::{Metrics, Outcome};
use swarm_queue_client::{ClientSecret, QueueConfig};
use swarm_secret_client::stored_secret::StoredSecret;
use swarm_stt::engine::Whisper;
@ -21,6 +22,8 @@ use swarm_stt::{Config, Error, Service, cache::Cache};
use swarm_wire::stt::{Response, subject_wildcard};
use tokio::sync::Semaphore;
mod metrics;
/// The cert-auth role this service logs in to the store as. `swarm-bao.nix`
/// writes it, matching the leaf's subject.
const CERT_ROLE: &str = "swarm-stt";
@ -133,6 +136,7 @@ async fn main() -> Result<()> {
} = settings(&var, &number)?;
let per_agent: Arc<Mutex<HashSet<String>>> = Arc::default();
let queue = connect_queue(&var).await?;
let metrics = Arc::new(Metrics::from_env());
tokio::spawn(async move {
loop {
@ -154,6 +158,7 @@ async fn main() -> Result<()> {
);
while let Some(request) = requests.next().await {
let (queue, service) = (queue.clone(), Arc::clone(&service));
let metrics = Arc::clone(&metrics);
// Held at once overall, and one per asking agent, so one agent
// cannot fill every slot. The agent is the subject's last token.
let agent = request
@ -178,13 +183,19 @@ async fn main() -> Result<()> {
};
let response = match turned_away {
Ok(held) => {
let response = answer(&service, &request.payload).await;
drop(held);
let count = metrics.hold();
let started = Instant::now();
let (response, outcome) = answer(&service, &request.payload).await;
metrics.request(outcome, Some(started.elapsed()));
drop((held, count));
response
}
Err(message) => Response::Failed {
message: message.to_owned(),
},
Err(message) => {
metrics.request(Outcome::Busy, None);
Response::Failed {
message: message.to_owned(),
}
}
};
match serde_json::to_vec(&response) {
Ok(body) => {
@ -226,20 +237,33 @@ fn take_slot(agents: &Arc<Mutex<HashSet<String>>>, agent: &str) -> Option<Slot>
})
}
/// The wire answer to one request's audio.
async fn answer(service: &Service<Whisper>, audio: &[u8]) -> Response {
/// The wire answer to one request's audio, and what came of it.
async fn answer(service: &Service<Whisper>, audio: &[u8]) -> (Response, Outcome) {
match service.transcribe(audio).await {
Ok(reply) => Response::Ok {
text: reply.text,
cached: reply.cached,
},
Ok(reply) => {
let outcome = if reply.cached {
Outcome::Cached
} else {
Outcome::Transcribed
};
(
Response::Ok {
text: reply.text,
cached: reply.cached,
},
outcome,
)
}
Err(Error::Engine(message)) => {
tracing::warn!(%message, "a transcription failed");
Response::Failed { message }
(Response::Failed { message }, Outcome::Failed)
}
Err(e) => Response::Failed {
message: e.to_string(),
},
Err(e) => (
Response::Failed {
message: e.to_string(),
},
Outcome::Refused,
),
}
}