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
47 changes: 47 additions & 0 deletions src/run_environment/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use git2::Repository;
use serde_json::Value;
use simplelog::SharedLogger;
use std::collections::BTreeMap;
use std::path::Path;

use crate::api_client::CodSpeedAPIClient;
use crate::executor::{ExecutorConfig, ExecutorName};
Expand Down Expand Up @@ -145,6 +146,7 @@ pub trait RunEnvironmentProvider {
instruments: config.instruments.get_active_instrument_names(),
executor: executor_name,
exclude_allocations: config.exclude_allocations,
macro_runner_config: read_macro_runner_config(Path::new(MACRO_RUNNER_CONFIG_PATH)),
system_info: system_info.clone(),
},
run_environment: self.get_run_environment(),
Expand All @@ -158,6 +160,29 @@ pub trait RunEnvironmentProvider {
}
}

/// Where a CodSpeed Macro Runner writes the JSON object describing its configuration.
const MACRO_RUNNER_CONFIG_PATH: &str = "/etc/codspeed/macro-runner-config.json";

/// The JSON object at `path`, or `None` outside a macro runner. An unreadable or malformed file is
/// ignored so it never fails the upload.
fn read_macro_runner_config(path: &Path) -> Option<serde_json::Map<String, Value>> {
let raw = match std::fs::read_to_string(path) {
Ok(raw) => raw,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return None,
Err(error) => {
warn!("Ignoring {}, could not read it: {error}", path.display());
return None;
}
};
match serde_json::from_str(&raw) {
Ok(config) => Some(config),
Err(error) => {
warn!("Ignoring {}, not a JSON object: {error}", path.display());
None
}
}
}

fn get_commit_hash_default_impl(repository_root_path: &str) -> Result<String> {
let repo = Repository::open(repository_root_path).context(format!(
"Failed to open repository at path: {repository_root_path}"
Expand All @@ -182,4 +207,26 @@ mod tests {
// ensure that the commit hash is correct, thus it has 40 characters
assert_eq!(commit_hash.len(), 40);
}

#[test]
fn test_read_macro_runner_config() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("macro-runner-config.json");
assert_eq!(read_macro_runner_config(&path), None);
assert_eq!(read_macro_runner_config(dir.path()), None);

std::fs::write(
&path,
r#"{"macroAgentVersion":"0.4.0","runtimeConfig":{"isolation":"strict"}}"#,
)
.unwrap();
let config = read_macro_runner_config(&path).unwrap();
assert_eq!(config["macroAgentVersion"], "0.4.0");
assert_eq!(config["runtimeConfig"]["isolation"], "strict");

for malformed in ["", "not json", r#"["0.4.0"]"#, r#""0.4.0""#] {
std::fs::write(&path, malformed).unwrap();
assert_eq!(read_macro_runner_config(&path), None);
}
}
}
4 changes: 4 additions & 0 deletions src/upload/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ pub struct Runner {
/// byte-identical to legacy runs that predate this field.
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub exclude_allocations: bool,
/// Configuration of the CodSpeed Macro Runner executing the job, as the JSON object the
/// machine writes to `/etc/codspeed/macro-runner-config.json`. Forwarded as is.
#[serde(skip_serializing_if = "Option::is_none")]
pub macro_runner_config: Option<serde_json::Map<String, serde_json::Value>>,
#[serde(flatten)]
pub system_info: SystemInfo,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ expression: upload_metadata
"MongoDB"
],
"executor": "valgrind",
"macroRunnerConfig": {
"macroAgentVersion": "0.4.0",
"hostImageName": "packer-ubuntu24-x86-post-20261120093416",
"guestImageName": "packer-ubuntu24-x86-post-20261120093416",
"runtimeConfig": {
"isolation": "strict"
}
},
"os": "ubuntu",
"osVersion": "20.04",
"arch": "x86_64",
Expand Down
12 changes: 11 additions & 1 deletion src/upload/upload_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ mod tests {
instruments: vec![InstrumentName::MongoDB],
executor: ExecutorName::Valgrind,
exclude_allocations: false,
macro_runner_config: json!({
"macroAgentVersion": "0.4.0",
"hostImageName": "packer-ubuntu24-x86-post-20261120093416",
"guestImageName": "packer-ubuntu24-x86-post-20261120093416",
"runtimeConfig": { "isolation": "strict" },
})
.as_object()
.cloned(),
system_info: SystemInfo::test(),
},
run_environment: RunEnvironment::GithubActions,
Expand Down Expand Up @@ -87,7 +95,7 @@ mod tests {
hash,
// Caution: when changing this value, we need to ensure that
// the related backend snapshot remains the same
@"75a9b81695bbea8934d88e221dd3dd98392170904d4775a84145767117555a60"
@"cd04217a89681d057c73e78a651ac8e7dba5f01133679df0637f4f68022d0223"
);
assert_json_snapshot!(upload_metadata);
}
Expand All @@ -104,6 +112,7 @@ mod tests {
instruments: instruments.get_active_instrument_names(),
executor: get_executor_from_mode(&RunnerMode::Telemetry, None).name(),
exclude_allocations: false,
macro_runner_config: None,
system_info: SystemInfo::test(),
};

Expand Down Expand Up @@ -131,6 +140,7 @@ mod tests {
instruments: vec![],
executor: ExecutorName::Valgrind,
exclude_allocations: false,
macro_runner_config: None,
system_info: SystemInfo {
os: crate::system::SupportedOs::Linux(
crate::system::LinuxDistribution::Other {
Expand Down
Loading