Watch
0
0
Fork
You've already forked hyperhive
0

alert when claude usage runs ahead of the clock, per account

This commit is contained in:
atlas 2026-11-07 20:32:09 +01:00 • committed by mara
commit 319dd6a808

View file

@ -224,14 +224,30 @@ pub fn record_open_todos(count: u64) {
inst.open_todos.record(count, &[]);
}
/// Attributes of a usage series: `window`, plus `account` when the login is
/// known. An unknown account omits the label rather than sending an empty one.
fn claude_usage_attrs(account: Option<&str>, window: &str) -> Vec<KeyValue> {
let mut attrs = vec![KeyValue::new("window", window.to_owned())];
if let Some(account) = account.filter(|a| !a.is_empty()) {
attrs.push(KeyValue::new("account", account.to_owned()));
}
attrs
}
/// Record one subscription usage window (`five_hour`, `seven_day`, …):
/// `percent` is 0-100, `resets_at` unix seconds. No-op when OTEL isn't
/// configured, same as [`record`].
pub fn record_claude_usage(window: &str, percent: f64, resets_at: Option<u64>) {
/// `percent` is 0-100, `resets_at` unix seconds. `account` is the opaque
/// id of the claude login, shared by every agent on that login. No-op when
/// OTEL isn't configured, same as [`record`].
pub fn record_claude_usage(
account: Option<&str>,
window: &str,
percent: f64,
resets_at: Option<u64>,
) {
let Some(inst) = INSTRUMENTS.get_or_init(build).as_ref() else {
return;
};
let attrs = [KeyValue::new("window", window.to_owned())];
let attrs = claude_usage_attrs(account, window);
inst.claude_usage_percent.record(percent, &attrs);
if let Some(resets_at) = resets_at {
inst.claude_usage_resets_at.record(resets_at, &attrs);
@ -382,7 +398,9 @@ pub fn enabled() -> bool {
#[cfg(test)]
mod tests {
use super::{TURN_DURATION_BOUNDS_MS, tool_call_counts};
use opentelemetry::KeyValue;
use super::{TURN_DURATION_BOUNDS_MS, claude_usage_attrs, tool_call_counts};
#[test]
fn a_turns_stored_tool_breakdown_becomes_per_tool_counts() {
@ -411,4 +429,18 @@ mod tests {
.is_some_and(|&b| b > 10_000.0)
);
}
#[test]
fn usage_attrs_carry_account_only_when_known() {
let has = |attrs: &[KeyValue], k: &str| attrs.iter().any(|kv| kv.key.as_str() == k);
let known = claude_usage_attrs(Some("acct-1"), "seven_day");
assert!(has(&known, "window"));
assert!(
known
.iter()
.any(|kv| kv.key.as_str() == "account" && kv.value.as_str() == "acct-1")
);
assert!(!has(&claude_usage_attrs(None, "seven_day"), "account"));
assert!(!has(&claude_usage_attrs(Some(""), "seven_day"), "account"));
}
}