Skip to content

Commit

Permalink
feat: Add results_kafka_cluster config
Browse files Browse the repository at this point in the history
  • Loading branch information
evanpurkhiser committed Jun 11, 2024
1 parent 34ed278 commit bdd5e3a
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,37 @@ pub struct Config {

/// The environment to report to sentry errors to.
pub sentry_env: Option<Cow<'static, str>>,

/// The kafka cluster to report results to. Expected to be a string of comma separated
/// addresses.
///
/// ```txt
/// 10.0.0.1:5000,10.0.0.2:6000
/// ```
#[serde(
deserialize_with = "list_deserialize",
serialize_with = "list_serializer"
)]
pub results_kafka_cluster: Vec<String>,
}

fn list_deserialize<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
where
D: serde::Deserializer<'de>,
{
let str_sequence = String::deserialize(deserializer)?;
let result = str_sequence
.split(',')
.map(|item| item.to_owned())
.collect();
Ok(result)
}

fn list_serializer<S>(list: &[String], serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
list.join(",").serialize(serializer)
}

impl Config {
Expand Down Expand Up @@ -48,6 +79,7 @@ mod tests {
r#"
sentry_dsn: my_dsn
sentry_env: my_env
results_kafka_cluster: '10.0.0.1,10.0.0.2:9000'
"#,
)?;

Expand All @@ -59,6 +91,7 @@ mod tests {
Config {
sentry_dsn: Some("my_dsn".to_owned()),
sentry_env: Some(Cow::from("my_env")),
results_kafka_cluster: vec!["10.0.0.1".to_owned(), "10.0.0.2:9000".to_owned()]
}
);
Ok(())
Expand All @@ -77,6 +110,10 @@ mod tests {
)?;

jail.set_env("UPTIME_CHECKER_SENTRY_ENV", "my_env_override");
jail.set_env(
"UPTIME_CHECKER_RESULTS_KAFKA_CLUSTER",
"10.0.0.1,10.0.0.2:7000",
);

let config = Config::extract(&Some(PathBuf::from("config.yaml")))
.expect("Invalid configuration");
Expand All @@ -86,6 +123,7 @@ mod tests {
Config {
sentry_dsn: Some("my_dsn".to_owned()),
sentry_env: Some(Cow::from("my_env_override")),
results_kafka_cluster: vec!["10.0.0.1".to_owned(), "10.0.0.2:7000".to_owned()]
}
);
Ok(())
Expand Down

0 comments on commit bdd5e3a

Please sign in to comment.