-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get config from files, environment vars, & command line flags (#…
…112) Add `make_config` util function that is used to load config files `make_config` asks for a: 1) default config 2) a list of possible file paths where a config can be loaded 3) a prefix that this config's env vars will be labeled (eg, `IROH_GATEWAY_PORT=4000`, the prefix is `IROH_GATEWAY` & the field that you are trying to set is `port` 4) command line flag overrides The method layers these options, starting with the default config and ending with the command line flags. Alos, allows using `IROH_METRICS_*` env vars to set fields in the metrics config. Also allows for `IROH_INSTANCE_ID` to set `metrics::Config.instance_id` & `IROH_ENV` to set `metrics::Config.service_env` fields.
- Loading branch information
Showing
38 changed files
with
1,270 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,9 @@ | ||
use git_version::git_version; | ||
use iroh_metrics::config::Config as MetricsConfig; | ||
|
||
pub fn metrics_config(logger_only: bool) -> iroh_metrics::config::Config { | ||
pub fn metrics_config_with_compile_time_info(cfg: MetricsConfig) -> MetricsConfig { | ||
// compile time configuration | ||
let service_name = env!("CARGO_PKG_NAME").to_string(); | ||
let build = git_version!().to_string(); | ||
let version = env!("CARGO_PKG_VERSION").to_string(); | ||
|
||
// runtime configuration | ||
let instance_id = std::env::var("IROH_INSTANCE_ID") | ||
.unwrap_or_else(|_| names::Generator::default().next().unwrap()); | ||
let service_env = std::env::var("IROH_ENV").unwrap_or_else(|_| "dev".to_string()); | ||
iroh_metrics::config::Config::new( | ||
service_name, | ||
instance_id, | ||
build, | ||
version, | ||
service_env, | ||
logger_only, | ||
) | ||
cfg.with_service_name(env!("CARGO_PKG_NAME").to_string()) | ||
.with_build(git_version!().to_string()) | ||
.with_version(env!("CARGO_PKG_VERSION").to_string()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use config::{ConfigError, Map, Source, Value}; | ||
use iroh_metrics::config::Config as MetricsConfig; | ||
use iroh_rpc_client::Config as RpcClientConfig; | ||
use iroh_util::insert_into_config_map; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// CONFIG_FILE_NAME is the name of the optional config file located in the iroh home directory | ||
pub const CONFIG_FILE_NAME: &str = "ctl.config.toml"; | ||
/// ENV_PREFIX should be used along side the config field name to set a config field using | ||
/// environment variables | ||
pub const ENV_PREFIX: &str = "IROH_CTL"; | ||
|
||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] | ||
pub struct Config { | ||
pub rpc_client: RpcClientConfig, | ||
pub metrics: MetricsConfig, | ||
} | ||
|
||
impl Source for Config { | ||
fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> { | ||
Box::new(self.clone()) | ||
} | ||
fn collect(&self) -> Result<Map<String, Value>, ConfigError> { | ||
let mut map: Map<String, Value> = Map::new(); | ||
insert_into_config_map(&mut map, "rpc_client", self.rpc_client.collect()?); | ||
insert_into_config_map(&mut map, "metrics", self.metrics.collect()?); | ||
Ok(map) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use config::Config as ConfigBuilder; | ||
|
||
#[test] | ||
fn test_collect() { | ||
let default = Config::default(); | ||
let mut expect: Map<String, Value> = Map::new(); | ||
expect.insert( | ||
"rpc_client".to_string(), | ||
Value::new(None, default.rpc_client.collect().unwrap()), | ||
); | ||
expect.insert( | ||
"metrics".to_string(), | ||
Value::new(None, default.metrics.collect().unwrap()), | ||
); | ||
let got = default.collect().unwrap(); | ||
|
||
for key in got.keys() { | ||
let left = expect.get(key).unwrap(); | ||
let right = got.get(key).unwrap(); | ||
assert_eq!(left, right); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_build_config_from_struct() { | ||
let expect = Config::default(); | ||
let got: Config = ConfigBuilder::builder() | ||
.add_source(expect.clone()) | ||
.build() | ||
.unwrap() | ||
.try_deserialize() | ||
.unwrap(); | ||
|
||
assert_eq!(expect, got); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pub mod config; | ||
pub mod metrics; | ||
pub mod status; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use git_version::git_version; | ||
use iroh_metrics::config::Config as MetricsConfig; | ||
|
||
pub fn metrics_config_with_compile_time_info(cfg: MetricsConfig) -> MetricsConfig { | ||
// compile time configuration | ||
cfg.with_service_name(env!("CARGO_PKG_NAME").to_string()) | ||
.with_build(git_version!().to_string()) | ||
.with_version(env!("CARGO_PKG_VERSION").to_string()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.