Skip to content

Commit

Permalink
fix doc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hlbarber committed Sep 2, 2023
1 parent 8ad6a70 commit 0a32d83
Showing 1 changed file with 57 additions and 36 deletions.
93 changes: 57 additions & 36 deletions metrics/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,6 @@ macro_rules! metadata_var {
}};
}

#[doc(hidden)]
#[macro_export]
macro_rules! default_target {
() => {
module_path!()
};
($target:expr) => {
$target
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! default_level {
() => {
$crate::Level::INFO
};
($level:expr) => {
$level
};
}

#[doc(hidden)]
#[macro_export]
macro_rules! count {
Expand Down Expand Up @@ -120,17 +98,23 @@ macro_rules! key_var {
/// ```
#[macro_export]
macro_rules! register_counter {
($(target: $target:expr,)? $(level: $level:expr,)? $name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
(target: $target:expr, level: $level:expr, $name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
{
let metric_key = $crate::key_var!($name, $($label_key $(=> $label_value)?),*);
let metadata = $crate::metadata_var!(
$crate::default_target!($($target)?),
$crate::default_level!($($level)?)
);
let metadata = $crate::metadata_var!($target, $level);

$crate::recorder().register_counter(&metric_key, metadata)
}
};
(target: $target:expr, $name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
$crate::register_counter!(target: $target, level: $crate::Level::INFO, $name $(, $label_key $(=> $label_value)?)*)
};
(level: $level:expr, $name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
$crate::register_counter!(target: module_path!(), level: $level, $name $(, $label_key$(=> $label_value)?)*)
};
($name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
$crate::register_counter!(target: module_path!(), level: $crate::Level::INFO, $name $(, $label_key$(=> $label_value)?)*)
};
}

/// Describes a counter.
Expand Down Expand Up @@ -228,10 +212,22 @@ macro_rules! describe_counter {
/// ```
#[macro_export]
macro_rules! counter {
($(target: $target:expr,)? $(level: $level:expr,)? $name:expr, $op_val:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {{
let handle = $crate::register_counter!($(target: $target,)? $(level: $level,)? $name $(, $label_key $(=> $label_value)?)*);
(target: $target:expr, level: $level:expr, $name:expr, $op_val:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
let handle = $crate::register_counter!(target: $target, level: $level, $name $(, $label_key $(=> $label_value)?)*);
handle.increment($op_val);
}};
};
(target: $target:expr, $name:expr $(, $label_key:expr, $op_val:expr $(=> $label_value:expr)?)* $(,)?) => {
let handle = $crate::register_counter!(target: $target, $name $(, $label_key $(=> $label_value)?)*);
handle.increment($op_val);
};
(level: $level:expr, $name:expr $(, $label_key:expr, $op_val:expr $(=> $label_value:expr)?)* $(,)?) => {
let handle = $crate::register_counter!(level: $level, $name $(, $label_key $(=> $label_value)?)*);
handle.increment($op_val);
};
($name:expr, $op_val:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
let handle = $crate::register_counter!($name $(, $label_key $(=> $label_value)?)*);
handle.increment($op_val);
};
}

/// Sets a counter to an absolute value.
Expand Down Expand Up @@ -282,10 +278,22 @@ macro_rules! counter {
/// ```
#[macro_export]
macro_rules! absolute_counter {
($(target: $target:expr,)? $(level: $level:expr,)? $name:expr, $op_val:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {{
let handle = $crate::register_counter!($(target: $target,)? $(level: $level,)? $name $(, $label_key $(=> $label_value)?)*);
(target: $target:expr, level: $level:expr, $name:expr, $op_val:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
let handle = $crate::register_counter!(target: $target, level: $level, $name $(, $label_key $(=> $label_value)?)*);
handle.absolute($op_val);
}};
};
(target: $target:expr, $name:expr $(, $label_key:expr, $op_val:expr $(=> $label_value:expr)?)* $(,)?) => {
let handle = $crate::register_counter!(target: $target, $name $(, $label_key $(=> $label_value)?)*);
handle.absolute($op_val);
};
(level: $level:expr, $name:expr, $op_val:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
let handle = $crate::register_counter!(level: $level, $name $(, $label_key $(=> $label_value)?)*);
handle.absolute($op_val);
};
($name:expr, $op_val:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
let handle = $crate::register_counter!($name $(, $label_key $(=> $label_value)?)*);
handle.absolute($op_val);
};
}

/// Increments a counter.
Expand Down Expand Up @@ -329,7 +337,20 @@ macro_rules! absolute_counter {
/// ```
#[macro_export]
macro_rules! increment_counter {
($(target: $target:expr,)? $(level: $level:expr,)? $name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {{
$crate::counter!($(target: $target,)? $(level: $level,)? $name, 1 $(, $label_key $(=> $label_value)?)*);
}};
(target: $target:expr, level: $level:expr, $name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
let handle = $crate::register_counter!(target: $target, level: $level, $name $(, $label_key $(=> $label_value)?)*);
handle.increment(1);
};
(target: $target:expr, $name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
let handle = $crate::register_counter!(target: $target, $name $(, $label_key $(=> $label_value)?)*);
handle.absolute(1);
};
(level: $level:expr, $name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
let handle = $crate::register_counter!(level: $level, $name $(, $label_key $(=> $label_value)?)*);
handle.absolute(1);
};
($name:expr $(, $label_key:expr $(=> $label_value:expr)?)* $(,)?) => {
let handle = $crate::register_counter!($name $(, $label_key $(=> $label_value)?)*);
handle.absolute(1);
};
}

0 comments on commit 0a32d83

Please sign in to comment.