Skip to content

Commit

Permalink
feat: introduce explicit error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Dec 13, 2024
1 parent 06fee28 commit c7563ff
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 136 deletions.
102 changes: 47 additions & 55 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ unexpected_cfgs = { level = "warn", check-cfg = ["cfg(iroh_docsrs)"] }
unused-async = "warn"

[dependencies]
anyhow = { version = "1" }
erased_set = "0.8"
http-body-util = "0.1.0"
hyper = { version = "1", features = ["server", "http1"] }
Expand All @@ -37,6 +36,7 @@ prometheus-client = { version = "0.22", optional = true }
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
serde = { version = "1", features = ["derive"] }
struct_iterable = "0.1"
thiserror = "2.0.6"
time = { version = "0.3.21", features = ["serde-well-known"] }
tokio = { version = "1", features = ["rt", "net", "fs"]}
tracing = "0.1"
Expand Down
6 changes: 3 additions & 3 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ impl Core {
/// Encodes the current metrics registry to a string in
/// the prometheus text exposition format.
#[cfg(feature = "metrics")]
pub fn encode(&self) -> Result<String, std::fmt::Error> {
pub fn encode(&self) -> String {
let mut buf = String::new();
encode(&mut buf, &self.registry)?;
Ok(buf)
encode(&mut buf, &self.registry).expect("writing to string always works");
buf
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ use std::collections::HashMap;
/// Reexport to make matching versions easier.
pub use struct_iterable;

/// Potential errors from this library.
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Indicates that the metrics have not been enabled.
#[error("Metrics not enabled")]
NoMetrics,
/// Any IO related error.
#[error("IO: {0}")]
Io(#[from] std::io::Error),
}

/// Increment the given counter by 1.
#[macro_export]
macro_rules! inc {
Expand All @@ -40,7 +51,7 @@ macro_rules! set {
}

/// Parse Prometheus metrics from a string.
pub fn parse_prometheus_metrics(data: &str) -> anyhow::Result<HashMap<String, f64>> {
pub fn parse_prometheus_metrics(data: &str) -> HashMap<String, f64> {
let mut metrics = HashMap::new();
for line in data.lines() {
if line.starts_with('#') {
Expand All @@ -57,7 +68,7 @@ pub fn parse_prometheus_metrics(data: &str) -> anyhow::Result<HashMap<String, f6
}
metrics.insert(metric.to_string(), value.unwrap());
}
Ok(metrics)
metrics
}

/// Configuration for pushing metrics to a remote endpoint.
Expand Down
4 changes: 2 additions & 2 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use std::net::SocketAddr;

/// Start a server to serve the OpenMetrics endpoint.
#[cfg(feature = "metrics")]
pub async fn start_metrics_server(addr: SocketAddr) -> anyhow::Result<()> {
pub async fn start_metrics_server(addr: SocketAddr) -> std::io::Result<()> {
crate::service::run(addr).await
}

Expand All @@ -63,7 +63,7 @@ pub async fn start_metrics_server(addr: SocketAddr) -> anyhow::Result<()> {
pub async fn start_metrics_dumper(
path: std::path::PathBuf,
interval: std::time::Duration,
) -> anyhow::Result<()> {
) -> Result<(), crate::Error> {
crate::service::dumper(&path, interval).await
}

Expand Down
Loading

0 comments on commit c7563ff

Please sign in to comment.