hive-bash-mcp: test the command counter against an in-memory exporter
This commit is contained in:
parent
fc8ec13620
commit
0ae6b911ac
1 changed files with 74 additions and 11 deletions
|
|
@ -55,16 +55,20 @@ fn build() -> Option<Exporter> {
|
|||
.build();
|
||||
SdkMeterProvider::builder().with_reader(reader).build()
|
||||
})?;
|
||||
let commands = provider
|
||||
.meter("hyperhive.bash")
|
||||
.u64_counter("hyperhive.agent.bash.commands")
|
||||
.build();
|
||||
let commands = commands_counter(&provider);
|
||||
Some(Exporter {
|
||||
_provider: provider,
|
||||
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
|
||||
/// is in a later segment (e.g. the `cargo` in `cd repo && cargo build`),
|
||||
/// 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
|
||||
/// configured or the command has no identifiable head.
|
||||
pub fn record_command(cmd: &str) {
|
||||
let Some(head) = command_head(cmd) else {
|
||||
return;
|
||||
};
|
||||
let Some(exporter) = EXPORTER.get_or_init(build).as_ref() else {
|
||||
return;
|
||||
};
|
||||
exporter.commands.add(1, &[KeyValue::new("command", head)]);
|
||||
if let Some(exporter) = EXPORTER.get_or_init(build).as_ref() {
|
||||
count_command(&exporter.commands, cmd);
|
||||
}
|
||||
}
|
||||
|
||||
/// Add one to `counter`, keyed by `cmd`'s normalised head; nothing when it
|
||||
/// 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)]
|
||||
mod tests {
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use opentelemetry_sdk::metrics::InMemoryMetricExporter;
|
||||
use opentelemetry_sdk::metrics::data::{AggregatedMetrics, MetricData, ScopeMetrics};
|
||||
|
||||
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]
|
||||
fn skips_leading_cd_to_the_real_tool() {
|
||||
assert_eq!(
|
||||
|
|
|
|||
Loading…
Reference in a new issue