From aee9a2a3d8151c414fc57db440f0e5300b517022 Mon Sep 17 00:00:00 2001 From: CinchBlue Date: Thu, 13 Apr 2023 16:52:06 -0700 Subject: [PATCH] Use global paths for macros (#358) --- metrics-macros/src/lib.rs | 29 ++++++++++++++----------- metrics/tests/macros.rs | 1 + metrics/tests/macros/03_mod_aliasing.rs | 29 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 metrics/tests/macros/03_mod_aliasing.rs diff --git a/metrics-macros/src/lib.rs b/metrics-macros/src/lib.rs index 39d90391..5ebbb4e8 100644 --- a/metrics-macros/src/lib.rs +++ b/metrics-macros/src/lib.rs @@ -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()); } } @@ -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 } }; @@ -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); @@ -262,7 +262,7 @@ where { #statics #locals - metrics::recorder().#register_ident(#metric_key) + ::metrics::recorder().#register_ident(#metric_key) } } } @@ -310,13 +310,15 @@ fn generate_statics(name: &Expr, labels: &Option) -> 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::>(); 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! {} @@ -331,11 +333,11 @@ fn generate_statics(name: &Expr, labels: &Option) -> 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 { @@ -370,7 +372,7 @@ fn generate_metric_key(name: &Expr, labels: &Option) -> (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` @@ -378,7 +380,7 @@ fn generate_metric_key(name: &Expr, labels: &Option) -> (TokenStream2, T 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 @@ -386,11 +388,11 @@ fn generate_metric_key(name: &Expr, labels: &Option) -> (TokenStream2, T // 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); } } }; @@ -401,7 +403,8 @@ fn generate_metric_key(name: &Expr, labels: &Option) -> (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 }, diff --git a/metrics/tests/macros.rs b/metrics/tests/macros.rs index b835b216..eb9bfc10 100644 --- a/metrics/tests/macros.rs +++ b/metrics/tests/macros.rs @@ -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"); } diff --git a/metrics/tests/macros/03_mod_aliasing.rs b/metrics/tests/macros/03_mod_aliasing.rs new file mode 100644 index 00000000..c2655e5c --- /dev/null +++ b/metrics/tests/macros/03_mod_aliasing.rs @@ -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() {}