Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
allow empty string for SOLANA_METRICS_CONFIG sanity checking
Browse files Browse the repository at this point in the history
  • Loading branch information
jbiseda committed Oct 4, 2023
1 parent 9761e6f commit 16f2a3c
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions metrics/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub enum MetricsError {
ConfigIncomplete,
#[error("SOLANA_METRICS_CONFIG database mismatch: {0}")]
DbMismatch(String),
#[error("SOLANA_METRICS_CONFIG is empty")]
ConfigEmptyString,
}

impl From<MetricsError> for String {
Expand Down Expand Up @@ -80,10 +82,12 @@ impl InfluxDbMetricsWriter {
}

fn build_write_url() -> Result<String, MetricsError> {
let config = get_metrics_config().map_err(|err| {
info!("metrics disabled: {}", err);
err
})?;
let config = get_metrics_config()
.map_err(|err| {
info!("metrics disabled: {}", err);
err
})?
.ok_or(MetricsError::ConfigEmptyString)?;

info!(
"metrics configuration: host={} db={} username={}",
Expand Down Expand Up @@ -402,10 +406,14 @@ impl MetricsConfig {
}
}

fn get_metrics_config() -> Result<MetricsConfig, MetricsError> {
fn get_metrics_config() -> Result<Option<MetricsConfig>, MetricsError> {
let mut config = MetricsConfig::default();
let config_var = env::var("SOLANA_METRICS_CONFIG")?;

if config_var.is_empty() {
return Ok(None);
}

for pair in config_var.split(',') {
let nv: Vec<_> = pair.split('=').collect();
if nv.len() != 2 {
Expand All @@ -425,13 +433,13 @@ fn get_metrics_config() -> Result<MetricsConfig, MetricsError> {
return Err(MetricsError::ConfigIncomplete);
}

Ok(config)
Ok(Some(config))
}

pub fn metrics_config_sanity_check(cluster_type: ClusterType) -> Result<(), MetricsError> {
let config = match get_metrics_config() {
Ok(config) => config,
Err(MetricsError::VarError(std::env::VarError::NotPresent)) => return Ok(()),
Ok(Some(config)) => config,
Ok(None) | Err(MetricsError::VarError(std::env::VarError::NotPresent)) => return Ok(()),
Err(e) => return Err(e),
};
match &config.db[..] {
Expand All @@ -446,7 +454,7 @@ pub fn metrics_config_sanity_check(cluster_type: ClusterType) -> Result<(), Metr
}

pub fn query(q: &str) -> Result<String, MetricsError> {
let config = get_metrics_config()?;
let config = get_metrics_config()?.ok_or(MetricsError::ConfigEmptyString)?;
let query_url = format!(
"{}/query?u={}&p={}&q={}",
&config.host, &config.username, &config.password, &q
Expand Down

0 comments on commit 16f2a3c

Please sign in to comment.