Skip to content

Commit

Permalink
Avoiding the build to fail if prometheus fail
Browse files Browse the repository at this point in the history
  • Loading branch information
sagojez committed Sep 17, 2024
1 parent 96dd10a commit 28f7c43
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 30 deletions.
61 changes: 37 additions & 24 deletions src/algebra/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,55 @@
use anyhow::anyhow;
use metrics_exporter_prometheus::PrometheusBuilder;

pub const SUCCESSFULLY_REFRESHED_GAUGE: &str = "successfully_refreshed";
pub const FAILED_TO_REFRESH_GAUGE: &str = "failed_to_refresh";
pub const REFRESH_TOTAL: &str = "refresh_total";

#[derive(Clone, Debug)]
pub struct Metrics;
pub struct Metrics {
is_installed: bool,
}

impl Metrics {
pub fn new() -> anyhow::Result<Self> {
PrometheusBuilder::new().install().map_err(|e| {
tracing::error!("Failed to install prometheus exporter: {}", e);
anyhow!(e)
})?;

metrics::describe_gauge!(
SUCCESSFULLY_REFRESHED_GAUGE,
"The number of successfully refreshed connections"
);

metrics::describe_gauge!(
FAILED_TO_REFRESH_GAUGE,
"The number of failed to refresh connections"
);

metrics::describe_gauge!(REFRESH_TOTAL, "The total number of refreshes");

Ok(Self)
let metric = PrometheusBuilder::new()
.install()
.map_err(|e| {
tracing::error!("Failed to install prometheus exporter: {}", e);
})
.ok();

if metric.is_some() {
metrics::describe_gauge!(
SUCCESSFULLY_REFRESHED_GAUGE,
"The number of successfully refreshed connections"
);

metrics::describe_gauge!(
FAILED_TO_REFRESH_GAUGE,
"The number of failed to refresh connections"
);

metrics::describe_gauge!(REFRESH_TOTAL, "The total number of refreshes");

Ok(Self { is_installed: true })
} else {
Ok(Self {
is_installed: false,
})
}
}

pub fn add_refreshed(&self, value: u64) {
metrics::increment_gauge!(SUCCESSFULLY_REFRESHED_GAUGE, value as f64);
metrics::increment_gauge!(REFRESH_TOTAL, value as f64);
if self.is_installed {
metrics::increment_gauge!(SUCCESSFULLY_REFRESHED_GAUGE, value as f64);
metrics::increment_gauge!(REFRESH_TOTAL, value as f64);
}
}

pub fn add_failed_to_refresh(&self, value: u64) {
metrics::increment_gauge!(FAILED_TO_REFRESH_GAUGE, value as f64);
metrics::increment_gauge!(REFRESH_TOTAL, value as f64);
if self.is_installed {
metrics::increment_gauge!(FAILED_TO_REFRESH_GAUGE, value as f64);
metrics::increment_gauge!(REFRESH_TOTAL, value as f64);
}
}
}
17 changes: 11 additions & 6 deletions src/algebra/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,17 @@ pub async fn refresh(
let (successes, failures): (Vec<_>, Vec<_>) =
results.into_iter().partition(|result| result.is_ok());

tracing::info!("Refreshed {} connections: {:?}", successes.len(), successes);
tracing::info!(
"Failed to refresh {} connections: {:?}",
failures.len(),
failures
);
if !successes.is_empty() {
tracing::info!("Refreshed {} connections: {:?}", successes.len(), successes);
}

if !failures.is_empty() {
tracing::info!(
"Failed to refresh {} connections: {:?}",
failures.len(),
failures
);
}

metrics.add_refreshed(successes.len() as u64);
metrics.add_failed_to_refresh(failures.len() as u64);
Expand Down

0 comments on commit 28f7c43

Please sign in to comment.