Attribute Macro metered::error_count

source · []
#[error_count]
Expand description

A procedural macro that generates a new metric that measures the amount of times each variant of an error has been thrown, to be used as crate-specific replacement for metered::ErrorCount.

#[error_count(name = LibErrorCount, visibility = pub)]
#[derive(Debug, Error)]
pub enum LibError {
    ReadError,
    InitError,
}

#[error_count(name = ErrorCount, visibility = pub)]
#[derive(Debug, Error)]
pub enum Error {
    MyLibrary(#[from] #[nested] LibError),
}

#[derive(Default, Debug)]
pub struct Baz {
    metrics: BazMetrics,
}

#[metered(registry = BazMetrics)]
impl Baz {
    #[measure(ErrorCount)]
    pub fn biz(&self) -> Result<(), Error> {        
        Err(LibError::InitError.into())
    }   
}

let baz = Baz::default();
baz.biz();
assert_eq!(baz.metrics.biz.error_count.my_library.read_error.get(), 0);
assert_eq!(baz.metrics.biz.error_count.my_library.init_error.get(), 1);
  • name is required and must be a valid Rust ident, this is the name of the generated struct containing a counter for each enum variant.
  • visibility specifies to visibility of the generated struct, it defaults to pub(crate).
  • skip_cleared allows to make the serializer skip “cleared” entries, that is entries for which the Clearable::is_cleared function returns true (for counters, by default, whether they are 0). It defaults to whether the feature error-count-skip-cleared-by-default is enabled. By default, this feature is disabled, and no entry will be skipped.

The error_count macro may only be applied to any enums that have a std::error::Error impl. The generated struct may then be included in measure attributes to measure the amount of errors returned of each variant defined in your error enum.