Skip to content

Commit

Permalink
improve prometheus error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
koushiro committed Sep 3, 2024
1 parent a2b458c commit 9b5c096
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
47 changes: 30 additions & 17 deletions core/src/layers/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ use crate::*;
/// let registry = prometheus::default_registry();
///
/// let op = Operator::new(builder)?
/// .layer(PrometheusLayer::builder().register(registry)?)
/// .layer(PrometheusLayer::builder().register(registry).expect("register metrics successfully"))
/// .finish();
/// debug!("operator: {op:?}");
///
Expand Down Expand Up @@ -114,7 +114,7 @@ impl PrometheusLayer {
/// let builder = services::Memory::default();
///
/// let op = Operator::new(builder)?
/// .layer(PrometheusLayer::register_default()?)
/// .layer(PrometheusLayer::register_default().expect("register metrics successfully"))
/// .finish();
/// debug!("operator: {op:?}");
///
Expand Down Expand Up @@ -143,15 +143,16 @@ impl PrometheusLayer {
/// let builder = services::Memory::default();
/// let registry = prometheus::default_registry();
///
/// let duration_seconds_buckets = prometheus::exponential_buckets(0.01, 2.0, 16)?;
/// let bytes_buckets = prometheus::exponential_buckets(1.0, 2.0, 16)?;
/// let duration_seconds_buckets = prometheus::exponential_buckets(0.01, 2.0, 16).unwrap();
/// let bytes_buckets = prometheus::exponential_buckets(1.0, 2.0, 16).unwrap();
/// let op = Operator::new(builder)?
/// .layer(
/// PrometheusLayer::builder()
/// .operation_duration_seconds_buckets(duration_seconds_buckets)
/// .operation_bytes_buckets(bytes_buckets)
/// .enable_path_label(1)
/// .register(registry)?
/// .register(registry)
/// .expect("register metrics successfully")
/// )
/// .finish();
/// debug!("operator: {op:?}");
Expand Down Expand Up @@ -216,12 +217,13 @@ impl PrometheusLayerBuilder {
/// let builder = services::Memory::default();
/// let registry = prometheus::default_registry();
///
/// let buckets = prometheus::exponential_buckets(0.01, 2.0, 16)?;
/// let buckets = prometheus::exponential_buckets(0.01, 2.0, 16).unwrap();
/// let op = Operator::new(builder)?
/// .layer(
/// PrometheusLayer::builder()
/// .operation_duration_seconds_buckets(buckets)
/// .register(registry)?
/// .register(registry)
/// .expect("register metrics successfully")
/// )
/// .finish();
/// debug!("operator: {op:?}");
Expand Down Expand Up @@ -253,12 +255,13 @@ impl PrometheusLayerBuilder {
/// let builder = services::Memory::default();
/// let registry = prometheus::default_registry();
///
/// let buckets = prometheus::exponential_buckets(1.0, 2.0, 16)?;
/// let buckets = prometheus::exponential_buckets(1.0, 2.0, 16).unwrap();
/// let op = Operator::new(builder)?
/// .layer(
/// PrometheusLayer::builder()
/// .operation_bytes_buckets(buckets)
/// .register(registry)?
/// .register(registry)
/// .expect("register metrics successfully")
/// )
/// .finish();
/// debug!("operator: {op:?}");
Expand Down Expand Up @@ -298,7 +301,8 @@ impl PrometheusLayerBuilder {
/// .layer(
/// PrometheusLayer::builder()
/// .enable_path_label(1)
/// .register(registry)?
/// .register(registry)
/// .expect("register metrics successfully")
/// )
/// .finish();
/// debug!("operator: {op:?}");
Expand All @@ -321,15 +325,17 @@ impl PrometheusLayerBuilder {
self.operation_duration_seconds_buckets
),
&labels,
)?;
)
.map_err(Error::parse_prometheus_error)?;
let operation_bytes = HistogramVec::new(
histogram_opts!(
observe::METRIC_OPERATION_BYTES.name(),
observe::METRIC_OPERATION_BYTES.help(),
self.operation_bytes_buckets
),
&labels,
)?;
)
.map_err(Error::parse_prometheus_error)?;

let labels = OperationLabels::names(true, self.path_label_level);
let operation_errors_total = GenericCounterVec::new(
Expand All @@ -338,11 +344,18 @@ impl PrometheusLayerBuilder {
observe::METRIC_OPERATION_ERRORS_TOTAL.help(),
),
&labels,
)?;

registry.register(Box::new(operation_duration_seconds.clone()))?;
registry.register(Box::new(operation_bytes.clone()))?;
registry.register(Box::new(operation_errors_total.clone()))?;
)
.map_err(Error::parse_prometheus_error)?;

registry
.register(Box::new(operation_duration_seconds.clone()))
.map_err(Error::parse_prometheus_error)?;
registry
.register(Box::new(operation_bytes.clone()))
.map_err(Error::parse_prometheus_error)?;
registry
.register(Box::new(operation_errors_total.clone()))
.map_err(Error::parse_prometheus_error)?;

Ok(PrometheusLayer {
interceptor: PrometheusInterceptor {
Expand Down
13 changes: 6 additions & 7 deletions core/src/types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ impl Error {
pub fn is_temporary(&self) -> bool {
self.status == ErrorStatus::Temporary
}

/// Convert the [`prometheus::Error`] to [`Self`].
#[cfg(feature = "layers-prometheus")]
pub fn parse_prometheus_error(err: prometheus::Error) -> Self {
Self::new(ErrorKind::Unexpected, err.to_string()).set_source(err)
}
}

impl From<Error> for io::Error {
Expand All @@ -414,13 +420,6 @@ impl From<Error> for io::Error {
}
}

#[cfg(feature = "layers-prometheus")]
impl From<prometheus::Error> for Error {
fn from(err: prometheus::Error) -> Self {
Self::new(ErrorKind::Unexpected, err.to_string())
}
}

#[cfg(test)]
mod tests {
use anyhow::anyhow;
Expand Down

0 comments on commit 9b5c096

Please sign in to comment.