|
|
|
@ -23,7 +23,7 @@ pub struct Config {
|
|
|
|
|
pub alerting: Option<AlertingConfig>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn resolve_datasource(config: &mut config::Config) -> anyhow::Result<DatasourceConfig> { |
|
|
|
|
fn resolve_datasource(config: &config::Config) -> anyhow::Result<DatasourceConfig> { |
|
|
|
|
if config.get::<String>("prometheus.url").is_ok() { |
|
|
|
|
return Ok(DatasourceConfig::Prometheus(PrometheusConfig { |
|
|
|
|
url: config.get("prometheus.url")?, |
|
|
|
@ -43,6 +43,36 @@ fn resolve_datasource(config: &mut config::Config) -> anyhow::Result<DatasourceC
|
|
|
|
|
return Err(anyhow::format_err!("no datasource found")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn resolve_alerting(config: &config::Config) -> anyhow::Result<Option<AlertingConfig>> { |
|
|
|
|
if config.get::<String>("alerting.type").is_err() { |
|
|
|
|
return Ok(None); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if config.get::<String>("alerting.endpoint").is_err() { |
|
|
|
|
return Err(anyhow::format_err!("missing endpoint param in alerting")); |
|
|
|
|
} |
|
|
|
|
if config.get::<String>("alerting.interval").is_err() { |
|
|
|
|
return Err(anyhow::format_err!("missing interval param in alerting")); |
|
|
|
|
} |
|
|
|
|
if config.get::<u64>("alerting.interval").is_err() { |
|
|
|
|
return Err(anyhow::format_err!("alerting interval should be a positive integer number")); |
|
|
|
|
} |
|
|
|
|
let analytic_type = config.get::<String>("alerting.type").unwrap(); |
|
|
|
|
if analytic_type != "webhook" { |
|
|
|
|
return Err(anyhow::format_err!("unknown alerting typy {}", analytic_type)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let endpoint = config.get::<String>("alerting.endpoint").unwrap(); |
|
|
|
|
let interval = config.get::<u64>("alerting.interval").unwrap(); |
|
|
|
|
return Ok(Some(AlertingConfig { |
|
|
|
|
alerting_type: AlertingType::Webhook(WebhookAlertingConfig{
|
|
|
|
|
endpoint |
|
|
|
|
}), |
|
|
|
|
interval |
|
|
|
|
})) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: use actual config and env variables
|
|
|
|
|
impl Config { |
|
|
|
|
pub fn new() -> anyhow::Result<Config> { |
|
|
|
@ -54,6 +84,7 @@ impl Config {
|
|
|
|
|
if std::path::Path::new("config.toml").exists() { |
|
|
|
|
config.merge(config::File::with_name("config")).unwrap(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
config |
|
|
|
|
.merge(config::Environment::with_prefix("HASTIC")) |
|
|
|
|
.unwrap(); |
|
|
|
@ -62,15 +93,11 @@ impl Config {
|
|
|
|
|
config.set("port", "8000").unwrap(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// let mut endpoint = None;
|
|
|
|
|
// if config.get::<String>("webhook.endpoint").is_ok() {
|
|
|
|
|
// endpoint = Some(config.get("webhook.endpoint").unwrap());
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
Ok(Config { |
|
|
|
|
port: config.get::<u16>("port").unwrap(), |
|
|
|
|
datasource_config: resolve_datasource(&mut config)?, |
|
|
|
|
alerting: None, |
|
|
|
|
datasource_config: resolve_datasource(&config)?, |
|
|
|
|
alerting: resolve_alerting(&config)? |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|