|
|
|
@ -27,6 +27,7 @@ pub struct Config {
|
|
|
|
|
|
|
|
|
|
fn resolve_datasource(config: &config::Config) -> anyhow::Result<DatasourceConfig> { |
|
|
|
|
if config.get::<String>("prometheus.url").is_ok() { |
|
|
|
|
println!("using Prometheus"); |
|
|
|
|
return Ok(DatasourceConfig::Prometheus(PrometheusConfig { |
|
|
|
|
url: config.get("prometheus.url")?, |
|
|
|
|
query: config.get("prometheus.query")?, |
|
|
|
@ -34,6 +35,7 @@ fn resolve_datasource(config: &config::Config) -> anyhow::Result<DatasourceConfi
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if config.get::<String>("influx.url").is_ok() { |
|
|
|
|
println!("using Influx"); |
|
|
|
|
return Ok(DatasourceConfig::Influx(InfluxConfig { |
|
|
|
|
url: config.get("influx.url")?, |
|
|
|
|
org_id: config.get("influx.org_id")?, |
|
|
|
@ -42,7 +44,7 @@ fn resolve_datasource(config: &config::Config) -> anyhow::Result<DatasourceConfi
|
|
|
|
|
})); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Err(anyhow::format_err!("no datasource found")); |
|
|
|
|
return Err(anyhow::format_err!("please configure a datasource")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn resolve_alerting(config: &config::Config) -> anyhow::Result<Option<AlertingConfig>> { |
|
|
|
@ -80,18 +82,32 @@ fn resolve_alerting(config: &config::Config) -> anyhow::Result<Option<AlertingCo
|
|
|
|
|
// config::Environment doesn't support nested configs, e.g. `alerting.type`,
|
|
|
|
|
// so I've copied this from:
|
|
|
|
|
// https://github.com/rust-lang/mdBook/blob/f3e5fce6bf5e290c713f4015947dc0f0ad172d20/src/config.rs#L132
|
|
|
|
|
// so that `__` can be used in env variables instead of `.`,
|
|
|
|
|
// so that `__` can be used in env variables instead of `.`,
|
|
|
|
|
// e.g. `HASTIC_ALERTING__TYPE` -> alerting.type
|
|
|
|
|
pub fn update_from_env(config: &mut config::Config) { |
|
|
|
|
let overrides = |
|
|
|
|
env::vars().filter_map(|(key, value)| parse_env(&key).map(|index| (index, value))); |
|
|
|
|
|
|
|
|
|
for (key, value) in overrides { |
|
|
|
|
println!("{} => {}", key, value); |
|
|
|
|
config.set(&key, value).unwrap(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn print_config(config: config::Config) { |
|
|
|
|
// TODO: support any nesting level
|
|
|
|
|
let sections = config.to_owned().cache.into_table().unwrap(); |
|
|
|
|
for (section_name, values) in sections { |
|
|
|
|
match values.clone().into_table() { |
|
|
|
|
Err(_) => println!("{} => {}", section_name, values), |
|
|
|
|
Ok(section) => { |
|
|
|
|
for (key, value) in section { |
|
|
|
|
println!("{}.{} => {}", section_name, key, value); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn parse_env(key: &str) -> Option<String> { |
|
|
|
|
const PREFIX: &str = "HASTIC_"; |
|
|
|
|
|
|
|
|
@ -117,7 +133,8 @@ impl Config {
|
|
|
|
|
config.set("port", 4347).unwrap(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: print resulted config (perfectly, it needs adding `derive(Debug)` in `subbeat`'s `DatasourceConfig`)
|
|
|
|
|
print_config(config.clone()); |
|
|
|
|
|
|
|
|
|
Ok(Config { |
|
|
|
|
port: config.get::<u16>("port").unwrap(), |
|
|
|
|
datasource_config: resolve_datasource(&config)?, |
|
|
|
|