Skip to content

Commit

Permalink
feat(runner): allow mongo destination to be dynamically set
Browse files Browse the repository at this point in the history
  • Loading branch information
adriencaccia committed Jan 12, 2024
1 parent 14241e2 commit 3605fc3
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 87 deletions.
8 changes: 7 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ pub struct AppArgs {
#[arg(long)]
pub working_directory: Option<String>,

/// Whether to activate the MongoDB instrument or not
/// if not provided, the instrument will not be activated
pub mongo_db: Option<bool>,

/// The name of the environment variable that contains the MongoDB URI to patch,
/// if not provided it will be read from the CODSPEED_MONGO_INSTR_URI_ENV_NAME environment variable
/// if not provided it will be read from the CODSPEED_MONGO_INSTR_URI_ENV_NAME environment variable.
///
/// It requires the `mongo_db` flag to be set to `true`.
#[arg(long)]
pub mongo_uri_env_name: Option<String>,

Expand Down
11 changes: 11 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub struct Config {
pub token: Option<String>,
pub working_directory: Option<String>,
pub command: String,

pub mongodb: bool,
pub mongo_uri_env_name: Option<String>,

pub skip_upload: bool,
Expand All @@ -26,6 +28,7 @@ impl Config {
token: None,
working_directory: None,
command: "".into(),
mongodb: false,
mongo_uri_env_name: None,
skip_upload: false,
skip_setup: false,
Expand All @@ -43,13 +46,15 @@ impl TryFrom<AppArgs> for Config {
.map_err(|e| anyhow!("Invalid upload URL: {}, {}", raw_upload_url, e))?;
let skip_upload = args.skip_upload || env::var("CODSPEED_SKIP_UPLOAD") == Ok("true".into());
let token = args.token.or_else(|| env::var("CODSPEED_TOKEN").ok());
let mongodb = args.mongo_db.is_some_and(|v| v);
let mongo_uri_env_name = args
.mongo_uri_env_name
.or_else(|| env::var("CODSPEED_MONGO_INSTR_URI_ENV_NAME").ok());
Ok(Self {
upload_url,
token,
working_directory: args.working_directory,
mongodb,
mongo_uri_env_name,
command: args.command.join(" "),
skip_upload,
Expand All @@ -72,6 +77,7 @@ mod tests {
upload_url: None,
token: None,
working_directory: None,
mongo_db: None,
mongo_uri_env_name: None,
skip_upload: false,
skip_setup: false,
Expand All @@ -81,6 +87,7 @@ mod tests {
assert_eq!(config.upload_url, Url::parse(DEFAULT_UPLOAD_URL).unwrap());
assert_eq!(config.token, None);
assert_eq!(config.working_directory, None);
assert!(!config.mongodb);
assert_eq!(config.mongo_uri_env_name, None);
assert!(!config.skip_upload);
assert!(!config.skip_setup);
Expand All @@ -94,6 +101,7 @@ mod tests {
upload_url: Some("https://example.com/upload".into()),
token: Some("token".into()),
working_directory: Some("/tmp".into()),
mongo_db: Some(true),
mongo_uri_env_name: Some("MONGODB_URI".into()),
skip_upload: true,
skip_setup: true,
Expand All @@ -107,6 +115,7 @@ mod tests {
);
assert_eq!(config.token, Some("token".into()));
assert_eq!(config.working_directory, Some("/tmp".into()));
assert!(config.mongodb);
assert_eq!(config.mongo_uri_env_name, Some("MONGODB_URI".into()));
assert!(config.skip_upload);
assert!(config.skip_setup);
Expand All @@ -129,6 +138,7 @@ mod tests {
upload_url: None,
token: None,
working_directory: None,
mongo_db: Some(true),
mongo_uri_env_name: None,
skip_upload: false,
skip_setup: false,
Expand All @@ -139,6 +149,7 @@ mod tests {
assert_eq!(config.upload_url, Url::parse(DEFAULT_UPLOAD_URL).unwrap());
assert_eq!(config.token, Some("token_from_env".into()));
assert_eq!(config.working_directory, None);
assert!(config.mongodb);
assert_eq!(
config.mongo_uri_env_name,
Some("MONGODB_URI_FROM_ENV".into())
Expand Down
65 changes: 37 additions & 28 deletions src/instruments/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use log::warn;
use serde::{Deserialize, Serialize};

use crate::config::Config;
Expand All @@ -6,7 +7,7 @@ pub mod mongo_tracer;

#[derive(Debug, PartialEq, Eq)]
pub struct MongoDBConfig {
pub uri_env_name: String,
pub uri_env_name: Option<String>,
}

#[derive(Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -37,12 +38,16 @@ impl Instruments {

impl From<&Config> for Instruments {
fn from(config: &Config) -> Self {
let mongodb = config
.mongo_uri_env_name
.as_ref()
.map(|uri_env_name| MongoDBConfig {
let mongodb = match (config.mongodb, &config.mongo_uri_env_name) {
(true, uri_env_name) => Some(MongoDBConfig {
uri_env_name: uri_env_name.clone(),
});
}),
(_, Some(_)) => {
warn!("The MongoDB instrument is disabled but a MongoDB URI environment variable name was provided, ignoring it");
None
}
_ => None,
};

Self { mongodb }
}
Expand All @@ -54,16 +59,14 @@ impl Instruments {
pub fn test() -> Self {
Self {
mongodb: Some(MongoDBConfig {
uri_env_name: "MONGODB_URI".into(),
uri_env_name: Some("MONGODB_URI".into()),
}),
}
}
}

#[cfg(test)]
mod tests {
use temp_env::with_var;

use super::*;

#[test]
Expand All @@ -73,25 +76,31 @@ mod tests {
}

#[test]
fn test_from_env_mongodb() {
with_var(
"CODSPEED_MONGO_INSTR_URI_ENV_NAME",
Some("MONGODB_URI"),
|| {
let config = Config {
mongo_uri_env_name: Some("MONGODB_URI".into()),
..Config::test()
};
let instruments = Instruments::from(&config);
assert_eq!(
instruments.mongodb,
Some(MongoDBConfig {
uri_env_name: "MONGODB_URI".into()
})
);

assert!(instruments.is_mongodb_enabled());
},
fn test_from_config() {
let config = Config {
mongodb: true,
mongo_uri_env_name: Some("MONGODB_URI".into()),
..Config::test()
};
let instruments = Instruments::from(&config);
assert_eq!(
instruments.mongodb,
Some(MongoDBConfig {
uri_env_name: Some("MONGODB_URI".into())
})
);
assert!(instruments.is_mongodb_enabled());
}

#[test]
fn test_from_config_mongodb_disabled() {
let config = Config {
mongodb: false,
mongo_uri_env_name: Some("MONGODB_URI".into()),
..Config::test()
};
let instruments = Instruments::from(&config);
assert_eq!(instruments.mongodb, None);
assert!(!instruments.is_mongodb_enabled());
}
}
Loading

0 comments on commit 3605fc3

Please sign in to comment.