hive-bash-mcp: test the command counter against an in-memory exporter
This commit is contained in:
parent
fc8ec13620
commit
0ae6b911ac
2 changed files with 74 additions and 11 deletions
|
|
@ -35,6 +35,7 @@ tracing.workspace = true
|
||||||
tracing-subscriber.workspace = true
|
tracing-subscriber.workspace = true
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
opentelemetry_sdk = { workspace = true, features = ["testing"] }
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
|
|
||||||
# `hive-bash-daemon` — long-running per-agent bash task runner. Spawns
|
# `hive-bash-daemon` — long-running per-agent bash task runner. Spawns
|
||||||
|
|
|
||||||
|
|
@ -55,16 +55,20 @@ fn build() -> Option<Exporter> {
|
||||||
.build();
|
.build();
|
||||||
SdkMeterProvider::builder().with_reader(reader).build()
|
SdkMeterProvider::builder().with_reader(reader).build()
|
||||||
})?;
|
})?;
|
||||||
let commands = provider
|
let commands = commands_counter(&provider);
|
||||||
.meter("hyperhive.bash")
|
|
||||||
.u64_counter("hyperhive.agent.bash.commands")
|
|
||||||
.build();
|
|
||||||
Some(Exporter {
|
Some(Exporter {
|
||||||
_provider: provider,
|
_provider: provider,
|
||||||
commands,
|
commands,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn commands_counter(provider: &SdkMeterProvider) -> Counter<u64> {
|
||||||
|
provider
|
||||||
|
.meter("hyperhive.bash")
|
||||||
|
.u64_counter("hyperhive.agent.bash.commands")
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
/// Builtins whose presence as a segment head means the *real* command
|
/// Builtins whose presence as a segment head means the *real* command
|
||||||
/// is in a later segment (e.g. the `cargo` in `cd repo && cargo build`),
|
/// is in a later segment (e.g. the `cargo` in `cd repo && cargo build`),
|
||||||
/// so we skip the whole segment.
|
/// so we skip the whole segment.
|
||||||
|
|
@ -152,19 +156,77 @@ pub fn command_head(cmd: &str) -> Option<String> {
|
||||||
/// Count one bash task's normalised command head. No-op when OTEL isn't
|
/// Count one bash task's normalised command head. No-op when OTEL isn't
|
||||||
/// configured or the command has no identifiable head.
|
/// configured or the command has no identifiable head.
|
||||||
pub fn record_command(cmd: &str) {
|
pub fn record_command(cmd: &str) {
|
||||||
let Some(head) = command_head(cmd) else {
|
if let Some(exporter) = EXPORTER.get_or_init(build).as_ref() {
|
||||||
return;
|
count_command(&exporter.commands, cmd);
|
||||||
};
|
}
|
||||||
let Some(exporter) = EXPORTER.get_or_init(build).as_ref() else {
|
}
|
||||||
return;
|
|
||||||
};
|
/// Add one to `counter`, keyed by `cmd`'s normalised head; nothing when it
|
||||||
exporter.commands.add(1, &[KeyValue::new("command", head)]);
|
/// has none.
|
||||||
|
fn count_command(counter: &Counter<u64>, cmd: &str) {
|
||||||
|
if let Some(head) = command_head(cmd) {
|
||||||
|
counter.add(1, &[KeyValue::new("command", head)]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
use opentelemetry_sdk::metrics::InMemoryMetricExporter;
|
||||||
|
use opentelemetry_sdk::metrics::data::{AggregatedMetrics, MetricData, ScopeMetrics};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
/// What the counter exported for each `command`, after a flush.
|
||||||
|
fn counted(commands: &[&str]) -> BTreeMap<String, u64> {
|
||||||
|
let exporter = InMemoryMetricExporter::default();
|
||||||
|
let provider = SdkMeterProvider::builder()
|
||||||
|
.with_reader(PeriodicReader::builder(exporter.clone()).build())
|
||||||
|
.build();
|
||||||
|
let counter = commands_counter(&provider);
|
||||||
|
for cmd in commands {
|
||||||
|
count_command(&counter, cmd);
|
||||||
|
}
|
||||||
|
provider.force_flush().expect("flush");
|
||||||
|
let mut counts = BTreeMap::new();
|
||||||
|
for resource in exporter.get_finished_metrics().expect("metrics") {
|
||||||
|
for metric in resource.scope_metrics().flat_map(ScopeMetrics::metrics) {
|
||||||
|
assert_eq!(metric.name(), "hyperhive.agent.bash.commands");
|
||||||
|
let AggregatedMetrics::U64(MetricData::Sum(sum)) = metric.data() else {
|
||||||
|
panic!("a counter exports a u64 sum");
|
||||||
|
};
|
||||||
|
for point in sum.data_points() {
|
||||||
|
let command = point
|
||||||
|
.attributes()
|
||||||
|
.find(|kv| kv.key.as_str() == "command")
|
||||||
|
.map(|kv| kv.value.to_string())
|
||||||
|
.expect("every count carries its command");
|
||||||
|
counts.insert(command, point.value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
counts
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn each_task_counts_once_under_its_command_head() {
|
||||||
|
let counts = counted(&[
|
||||||
|
"cd /repo && cargo build",
|
||||||
|
"cargo test -p x",
|
||||||
|
"sudo env A=1 hive-forge view 1",
|
||||||
|
]);
|
||||||
|
assert_eq!(
|
||||||
|
counts,
|
||||||
|
BTreeMap::from([("cargo".to_owned(), 2), ("hive-forge".to_owned(), 1)])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn a_command_without_a_head_counts_nothing() {
|
||||||
|
assert!(counted(&["cd /repo", " "]).is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn skips_leading_cd_to_the_real_tool() {
|
fn skips_leading_cd_to_the_real_tool() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue