Skip to content

Commit 3d5178d

Browse files
fixup! feat: add option to output structured json
1 parent 8118a42 commit 3d5178d

2 files changed

Lines changed: 19 additions & 18 deletions

File tree

src/run/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ mod tests {
139139
mode: RunnerMode::Instrumentation,
140140
instruments: vec!["mongodb".into()],
141141
mongo_uri_env_name: Some("MONGODB_URI".into()),
142-
message_format: Some("json".into()),
142+
message_format: Some(crate::run::MessageFormat::Json),
143143
skip_upload: true,
144144
skip_setup: true,
145145
command: vec!["cargo".into(), "codspeed".into(), "bench".into()],

src/run/mod.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ fn show_banner() {
3737
debug!("codspeed v{}", VERSION);
3838
}
3939

40-
#[derive(ValueEnum, Clone, Default, Debug, Serialize)]
41-
#[serde(rename_all = "lowercase")]
42-
pub enum RunnerMode {
43-
#[default]
44-
Instrumentation,
45-
Walltime,
46-
}
47-
4840
#[derive(Args, Debug)]
4941
pub struct RunArgs {
5042
/// The upload URL to use for uploading the results, useful for on-premises installations
@@ -87,8 +79,8 @@ pub struct RunArgs {
8779
#[arg(long)]
8880
pub mongo_uri_env_name: Option<String>,
8981

90-
#[arg(long)]
91-
pub message_format: Option<String>,
82+
#[arg(long, hide = true)]
83+
pub message_format: Option<MessageFormat>,
9284

9385
/// Only for debugging purposes, skips the upload of the results
9486
#[arg(
@@ -107,6 +99,19 @@ pub struct RunArgs {
10799
pub command: Vec<String>,
108100
}
109101

102+
#[derive(ValueEnum, Clone, Default, Debug, Serialize)]
103+
#[serde(rename_all = "lowercase")]
104+
pub enum RunnerMode {
105+
#[default]
106+
Instrumentation,
107+
Walltime,
108+
}
109+
110+
#[derive(ValueEnum, Clone, Debug, PartialEq)]
111+
pub enum MessageFormat {
112+
Json,
113+
}
114+
110115
#[cfg(test)]
111116
impl RunArgs {
112117
/// Constructs a new `RunArgs` with default values for testing purposes
@@ -129,12 +134,7 @@ impl RunArgs {
129134
}
130135

131136
pub async fn run(args: RunArgs, api_client: &CodSpeedAPIClient) -> Result<()> {
132-
let output_json = args
133-
.message_format
134-
.as_ref()
135-
.map(|format| format == "json")
136-
.unwrap_or(false);
137-
137+
let output_json = args.message_format == Some(MessageFormat::Json);
138138
let mut config = Config::try_from(args)?;
139139
let provider = run_environment::get_provider(&config)?;
140140
let codspeed_config = CodSpeedConfig::load()?;
@@ -207,7 +207,8 @@ pub async fn run(args: RunArgs, api_client: &CodSpeedAPIClient) -> Result<()> {
207207
let run_id = upload_result.run_id.clone();
208208
poll_results::poll_results(api_client, &provider, upload_result.run_id).await?;
209209
if output_json {
210-
// TODO: Refactor `log_json` to accept a serde_json::Value arguemnt
210+
// TODO: Refactor `log_json` to avoid having to format the json manually
211+
// We could make use of structured logging for this https://docs.rs/log/latest/log/#structured-logging
211212
log_json!(format!(
212213
"{{\"event\": \"run_finished\", \"run_id\": \"{}\"}}",
213214
run_id

0 commit comments

Comments
 (0)