Skip to content

Commit

Permalink
Use global paths for macros (metrics-rs#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
CinchBlue authored Apr 13, 2023
1 parent 6d65723 commit aee9a2a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
29 changes: 16 additions & 13 deletions metrics-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fn get_describe_code(
quote! {
{
// Only do this work if there's a recorder installed.
if let Some(recorder) = metrics::try_recorder() {
if let Some(recorder) = ::metrics::try_recorder() {
recorder.#describe_ident(#name.into(), #unit, #description.into());
}
}
Expand All @@ -237,7 +237,7 @@ where
Some((op_type, op_value)) => {
let op_ident = format_ident!("{}", op_type);
let op_value = if metric_type == "histogram" {
quote! { metrics::__into_f64(#op_value) }
quote! { ::metrics::__into_f64(#op_value) }
} else {
quote! { #op_value }
};
Expand All @@ -248,7 +248,7 @@ where
{
#statics
// Only do this work if there's a recorder installed.
if let Some(recorder) = metrics::try_recorder() {
if let Some(recorder) = ::metrics::try_recorder() {
#locals
let handle = recorder.#register_ident(#metric_key);
handle.#op_ident(#op_value);
Expand All @@ -262,7 +262,7 @@ where
{
#statics
#locals
metrics::recorder().#register_ident(#metric_key)
::metrics::recorder().#register_ident(#metric_key)
}
}
}
Expand Down Expand Up @@ -310,13 +310,15 @@ fn generate_statics(name: &Expr, labels: &Option<Labels>) -> TokenStream2 {
if let Labels::Inline(pairs) = labels {
let labels = pairs
.iter()
.map(|(key, val)| quote! { metrics::Label::from_static_parts(#key, #val) })
.map(
|(key, val)| quote! { ::metrics::Label::from_static_parts(#key, #val) },
)
.collect::<Vec<_>>();
let labels_len = labels.len();
let labels_len = quote! { #labels_len };

quote! {
static METRIC_LABELS: [metrics::Label; #labels_len] = [#(#labels),*];
static METRIC_LABELS: [::metrics::Label; #labels_len] = [#(#labels),*];
}
} else {
quote! {}
Expand All @@ -331,11 +333,11 @@ fn generate_statics(name: &Expr, labels: &Option<Labels>) -> TokenStream2 {
let key_static = if use_name_static && use_labels_static {
if has_labels {
quote! {
static METRIC_KEY: metrics::Key = metrics::Key::from_static_parts(METRIC_NAME, &METRIC_LABELS);
static METRIC_KEY: ::metrics::Key = ::metrics::Key::from_static_parts(METRIC_NAME, &METRIC_LABELS);
}
} else {
quote! {
static METRIC_KEY: metrics::Key = metrics::Key::from_static_name(METRIC_NAME);
static METRIC_KEY: ::metrics::Key = ::metrics::Key::from_static_name(METRIC_NAME);
}
}
} else {
Expand Down Expand Up @@ -370,27 +372,27 @@ fn generate_metric_key(name: &Expr, labels: &Option<Labels>) -> (TokenStream2, T
let labels = labels.as_ref().unwrap();
let quoted_labels = labels_to_quoted(labels);
quote! {
let key = metrics::Key::from_parts(METRIC_NAME, #quoted_labels);
let key = ::metrics::Key::from_parts(METRIC_NAME, #quoted_labels);
}
} else if !use_name_static && !use_labels_static {
// The name is not static, and neither are the labels. Since `use_labels_static`
// cannot be false unless labels _are_ specified, we know this unwrap is safe.
let labels = labels.as_ref().unwrap();
let quoted_labels = labels_to_quoted(labels);
quote! {
let key = metrics::Key::from_parts(#name, #quoted_labels);
let key = ::metrics::Key::from_parts(#name, #quoted_labels);
}
} else {
// The name is not static, but the labels are. This could technically mean that there
// simply are no labels, so we have to discriminate in a slightly different way
// to figure out the correct key.
if has_labels {
quote! {
let key = metrics::Key::from_static_labels(#name, &METRIC_LABELS);
let key = ::metrics::Key::from_static_labels(#name, &METRIC_LABELS);
}
} else {
quote! {
let key = metrics::Key::from_name(#name);
let key = ::metrics::Key::from_name(#name);
}
}
};
Expand All @@ -401,7 +403,8 @@ fn generate_metric_key(name: &Expr, labels: &Option<Labels>) -> (TokenStream2, T
fn labels_to_quoted(labels: &Labels) -> proc_macro2::TokenStream {
match labels {
Labels::Inline(pairs) => {
let labels = pairs.iter().map(|(key, val)| quote! { metrics::Label::new(#key, #val) });
let labels =
pairs.iter().map(|(key, val)| quote! { ::metrics::Label::new(#key, #val) });
quote! { vec![#(#labels),*] }
}
Labels::Existing(e) => quote! { #e },
Expand Down
1 change: 1 addition & 0 deletions metrics/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ pub fn macros() {
let t = trybuild::TestCases::new();
t.pass("tests/macros/01_basic.rs");
t.pass("tests/macros/02_trailing_comma.rs");
t.pass("tests/macros/03_mod_aliasing.rs");
}
29 changes: 29 additions & 0 deletions metrics/tests/macros/03_mod_aliasing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! This test is to show that we can still use `::metrics::*` macros even though we have imported
//! the `framework::metrics` mod (otherwise, there would be a compilation error).

pub mod framework {
pub mod metrics {
pub struct Key;
pub struct Label;

macro_rules! register_counter {
($x:expr, $($y:expr),+) => {};
}
}
}

use framework::*; // This exposes mod `framework::metrics`.

const UPLOAD_METRIC_NAME: &'static str = "some_metric";
const UPLOAD_METRIC_LABEL_SUCCESS: &'static str = "success";
const UPLOAD_METRIC_LABEL_PROCESS_TYPE: &'static str = "process_type";

#[inline]
pub fn register_metrics() {
::metrics::register_counter!(
UPLOAD_METRIC_NAME,
&[(UPLOAD_METRIC_LABEL_PROCESS_TYPE, ""), (UPLOAD_METRIC_LABEL_SUCCESS, ""),]
);
}

fn main() {}

0 comments on commit aee9a2a

Please sign in to comment.