Skip to content

Commit

Permalink
fix changes to fully qualified metrics crate ref change in macros
Browse files Browse the repository at this point in the history
  • Loading branch information
tobz committed Apr 14, 2023
1 parent 179a083 commit 3dd299a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 49 deletions.
136 changes: 90 additions & 46 deletions metrics-macros/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn test_get_describe_code() {

let expected = concat!(
"{ ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"recorder . describe_mytype (\"mykeyname\" . into () , None , \"a counter\" . into ()) ; ",
"} ",
"}",
Expand All @@ -24,6 +24,28 @@ fn test_get_describe_code() {
assert_eq!(stream.to_string(), expected);
}

#[test]
fn test_get_describe_code_with_qualified_unit_rooted() {
// Now with unit.
let units: ExprPath = parse_quote! { ::metrics::Unit::Nanoseconds };
let stream = get_describe_code(
"mytype",
parse_quote! { "mykeyname" },
Some(Expr::Path(units)),
parse_quote! { "a counter" },
);

let expected = concat!(
"{ ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"recorder . describe_mytype (\"mykeyname\" . into () , Some (:: metrics :: Unit :: Nanoseconds) , \"a counter\" . into ()) ; ",
"} ",
"}",
);

assert_eq!(stream.to_string(), expected);
}

#[test]
fn test_get_describe_code_with_qualified_unit() {
// Now with unit.
Expand All @@ -37,7 +59,7 @@ fn test_get_describe_code_with_qualified_unit() {

let expected = concat!(
"{ ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"recorder . describe_mytype (\"mykeyname\" . into () , Some (metrics :: Unit :: Nanoseconds) , \"a counter\" . into ()) ; ",
"} ",
"}",
Expand All @@ -59,7 +81,7 @@ fn test_get_describe_code_with_relative_unit() {

let expected = concat!(
"{ ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"recorder . describe_mytype (\"mykeyname\" . into () , Some (Unit :: Nanoseconds) , \"a counter\" . into ()) ; ",
"} ",
"}",
Expand All @@ -76,7 +98,7 @@ fn test_get_describe_code_with_constants() {

let expected = concat!(
"{ ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"recorder . describe_mytype (KEY_NAME . into () , None , COUNTER_DESC . into ()) ; ",
"} ",
"}",
Expand All @@ -98,7 +120,7 @@ fn test_get_describe_code_with_constants_and_with_qualified_unit() {

let expected = concat!(
"{ ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"recorder . describe_mytype (KEY_NAME . into () , Some (metrics :: Unit :: Nanoseconds) , COUNTER_DESC . into ()) ; ",
"} ",
"}",
Expand All @@ -107,6 +129,28 @@ fn test_get_describe_code_with_constants_and_with_qualified_unit() {
assert_eq!(stream.to_string(), expected);
}

#[test]
fn test_get_describe_code_with_constants_and_with_qualified_unit_rooted() {
// Now with unit.
let units: ExprPath = parse_quote! { ::metrics::Unit::Nanoseconds };
let stream = get_describe_code(
"mytype",
parse_quote! { KEY_NAME },
Some(Expr::Path(units)),
parse_quote! { COUNTER_DESC },
);

let expected = concat!(
"{ ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"recorder . describe_mytype (KEY_NAME . into () , Some (:: metrics :: Unit :: Nanoseconds) , COUNTER_DESC . into ()) ; ",
"} ",
"}",
);

assert_eq!(stream.to_string(), expected);
}

#[test]
fn test_get_describe_code_with_constants_and_with_relative_unit() {
// Now with unit.
Expand All @@ -120,7 +164,7 @@ fn test_get_describe_code_with_constants_and_with_relative_unit() {

let expected = concat!(
"{ ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"recorder . describe_mytype (KEY_NAME . into () , Some (Unit :: Nanoseconds) , COUNTER_DESC . into ()) ; ",
"} ",
"}",
Expand All @@ -136,8 +180,8 @@ fn test_get_register_and_op_code_register_static_name_no_labels() {
let expected = concat!(
"{ ",
"static METRIC_NAME : & 'static str = \"mykeyname\" ; ",
"static METRIC_KEY : metrics :: Key = metrics :: Key :: from_static_name (METRIC_NAME) ; ",
"metrics :: recorder () . register_mytype (& METRIC_KEY) ",
"static METRIC_KEY : :: metrics :: Key = :: metrics :: Key :: from_static_name (METRIC_NAME) ; ",
":: metrics :: recorder () . register_mytype (& METRIC_KEY) ",
"}",
);

Expand All @@ -153,9 +197,9 @@ fn test_get_register_and_op_code_register_static_name_static_inline_labels() {
let expected = concat!(
"{ ",
"static METRIC_NAME : & 'static str = \"mykeyname\" ; ",
"static METRIC_LABELS : [metrics :: Label ; 1usize] = [metrics :: Label :: from_static_parts (\"key1\" , \"value1\")] ; ",
"static METRIC_KEY : metrics :: Key = metrics :: Key :: from_static_parts (METRIC_NAME , & METRIC_LABELS) ; ",
"metrics :: recorder () . register_mytype (& METRIC_KEY) ",
"static METRIC_LABELS : [:: metrics :: Label ; 1usize] = [:: metrics :: Label :: from_static_parts (\"key1\" , \"value1\")] ; ",
"static METRIC_KEY : :: metrics :: Key = :: metrics :: Key :: from_static_parts (METRIC_NAME , & METRIC_LABELS) ; ",
":: metrics :: recorder () . register_mytype (& METRIC_KEY) ",
"}",
);

Expand All @@ -171,8 +215,8 @@ fn test_get_register_and_op_code_register_static_name_dynamic_inline_labels() {
let expected = concat!(
"{ ",
"static METRIC_NAME : & 'static str = \"mykeyname\" ; ",
"let key = metrics :: Key :: from_parts (METRIC_NAME , vec ! [metrics :: Label :: new (\"key1\" , & value1)]) ; ",
"metrics :: recorder () . register_mytype (& key) ",
"let key = :: metrics :: Key :: from_parts (METRIC_NAME , vec ! [:: metrics :: Label :: new (\"key1\" , & value1)]) ; ",
":: metrics :: recorder () . register_mytype (& key) ",
"}",
);

Expand All @@ -192,8 +236,8 @@ fn test_get_register_and_op_code_register_static_name_existing_labels() {
let expected = concat!(
"{ ",
"static METRIC_NAME : & 'static str = \"mykeyname\" ; ",
"let key = metrics :: Key :: from_parts (METRIC_NAME , mylabels) ; ",
"metrics :: recorder () . register_mytype (& key) ",
"let key = :: metrics :: Key :: from_parts (METRIC_NAME , mylabels) ; ",
":: metrics :: recorder () . register_mytype (& key) ",
"}",
);

Expand All @@ -211,8 +255,8 @@ fn test_get_register_and_op_code_register_owned_name_no_labels() {

let expected = concat!(
"{ ",
"let key = metrics :: Key :: from_name (String :: from (\"owned\")) ; ",
"metrics :: recorder () . register_mytype (& key) ",
"let key = :: metrics :: Key :: from_name (String :: from (\"owned\")) ; ",
":: metrics :: recorder () . register_mytype (& key) ",
"}",
);

Expand All @@ -231,9 +275,9 @@ fn test_get_register_and_op_code_register_owned_name_static_inline_labels() {

let expected = concat!(
"{ ",
"static METRIC_LABELS : [metrics :: Label ; 1usize] = [metrics :: Label :: from_static_parts (\"key1\" , \"value1\")] ; ",
"let key = metrics :: Key :: from_static_labels (String :: from (\"owned\") , & METRIC_LABELS) ; ",
"metrics :: recorder () . register_mytype (& key) ",
"static METRIC_LABELS : [:: metrics :: Label ; 1usize] = [:: metrics :: Label :: from_static_parts (\"key1\" , \"value1\")] ; ",
"let key = :: metrics :: Key :: from_static_labels (String :: from (\"owned\") , & METRIC_LABELS) ; ",
":: metrics :: recorder () . register_mytype (& key) ",
"}",
);

Expand All @@ -252,8 +296,8 @@ fn test_get_register_and_op_code_register_owned_name_dynamic_inline_labels() {

let expected = concat!(
"{ ",
"let key = metrics :: Key :: from_parts (String :: from (\"owned\") , vec ! [metrics :: Label :: new (\"key1\" , & value1)]) ; ",
"metrics :: recorder () . register_mytype (& key) ",
"let key = :: metrics :: Key :: from_parts (String :: from (\"owned\") , vec ! [:: metrics :: Label :: new (\"key1\" , & value1)]) ; ",
":: metrics :: recorder () . register_mytype (& key) ",
"}",
);

Expand All @@ -272,8 +316,8 @@ fn test_get_register_and_op_code_register_owned_name_existing_labels() {

let expected = concat!(
"{ ",
"let key = metrics :: Key :: from_parts (String :: from (\"owned\") , mylabels) ; ",
"metrics :: recorder () . register_mytype (& key) ",
"let key = :: metrics :: Key :: from_parts (String :: from (\"owned\") , mylabels) ; ",
":: metrics :: recorder () . register_mytype (& key) ",
"}",
);

Expand All @@ -292,8 +336,8 @@ fn test_get_register_and_op_code_op_static_name_no_labels() {
let expected = concat!(
"{ ",
"static METRIC_NAME : & 'static str = \"mykeyname\" ; ",
"static METRIC_KEY : metrics :: Key = metrics :: Key :: from_static_name (METRIC_NAME) ; ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"static METRIC_KEY : :: metrics :: Key = :: metrics :: Key :: from_static_name (METRIC_NAME) ; ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"let handle = recorder . register_mytype (& METRIC_KEY) ; ",
"handle . myop (1) ; ",
"} ",
Expand All @@ -316,9 +360,9 @@ fn test_get_register_and_op_code_op_static_name_static_inline_labels() {
let expected = concat!(
"{ ",
"static METRIC_NAME : & 'static str = \"mykeyname\" ; ",
"static METRIC_LABELS : [metrics :: Label ; 1usize] = [metrics :: Label :: from_static_parts (\"key1\" , \"value1\")] ; ",
"static METRIC_KEY : metrics :: Key = metrics :: Key :: from_static_parts (METRIC_NAME , & METRIC_LABELS) ; ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"static METRIC_LABELS : [:: metrics :: Label ; 1usize] = [:: metrics :: Label :: from_static_parts (\"key1\" , \"value1\")] ; ",
"static METRIC_KEY : :: metrics :: Key = :: metrics :: Key :: from_static_parts (METRIC_NAME , & METRIC_LABELS) ; ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"let handle = recorder . register_mytype (& METRIC_KEY) ; ",
"handle . myop (1) ; ",
"} ",
Expand All @@ -341,8 +385,8 @@ fn test_get_register_and_op_code_op_static_name_dynamic_inline_labels() {
let expected = concat!(
"{ ",
"static METRIC_NAME : & 'static str = \"mykeyname\" ; ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"let key = metrics :: Key :: from_parts (METRIC_NAME , vec ! [metrics :: Label :: new (\"key1\" , & value1)]) ; ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"let key = :: metrics :: Key :: from_parts (METRIC_NAME , vec ! [:: metrics :: Label :: new (\"key1\" , & value1)]) ; ",
"let handle = recorder . register_mytype (& key) ; ",
"handle . myop (1) ; ",
"} ",
Expand All @@ -365,8 +409,8 @@ fn test_get_register_and_op_code_op_static_name_existing_labels() {
let expected = concat!(
"{ ",
"static METRIC_NAME : & 'static str = \"mykeyname\" ; ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"let key = metrics :: Key :: from_parts (METRIC_NAME , mylabels) ; ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"let key = :: metrics :: Key :: from_parts (METRIC_NAME , mylabels) ; ",
"let handle = recorder . register_mytype (& key) ; ",
"handle . myop (1) ; ",
"} ",
Expand All @@ -387,8 +431,8 @@ fn test_get_register_and_op_code_op_owned_name_no_labels() {

let expected = concat!(
"{ ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"let key = metrics :: Key :: from_name (String :: from (\"owned\")) ; ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"let key = :: metrics :: Key :: from_name (String :: from (\"owned\")) ; ",
"let handle = recorder . register_mytype (& key) ; ",
"handle . myop (1) ; ",
"} ",
Expand All @@ -410,9 +454,9 @@ fn test_get_register_and_op_code_op_owned_name_static_inline_labels() {

let expected = concat!(
"{ ",
"static METRIC_LABELS : [metrics :: Label ; 1usize] = [metrics :: Label :: from_static_parts (\"key1\" , \"value1\")] ; ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"let key = metrics :: Key :: from_static_labels (String :: from (\"owned\") , & METRIC_LABELS) ; ",
"static METRIC_LABELS : [:: metrics :: Label ; 1usize] = [:: metrics :: Label :: from_static_parts (\"key1\" , \"value1\")] ; ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"let key = :: metrics :: Key :: from_static_labels (String :: from (\"owned\") , & METRIC_LABELS) ; ",
"let handle = recorder . register_mytype (& key) ; ",
"handle . myop (1) ; ",
"} ",
Expand All @@ -434,8 +478,8 @@ fn test_get_register_and_op_code_op_owned_name_dynamic_inline_labels() {

let expected = concat!(
"{ ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"let key = metrics :: Key :: from_parts (String :: from (\"owned\") , vec ! [metrics :: Label :: new (\"key1\" , & value1)]) ; ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"let key = :: metrics :: Key :: from_parts (String :: from (\"owned\") , vec ! [:: metrics :: Label :: new (\"key1\" , & value1)]) ; ",
"let handle = recorder . register_mytype (& key) ; ",
"handle . myop (1) ; ",
"} ",
Expand All @@ -457,8 +501,8 @@ fn test_get_register_and_op_code_op_owned_name_existing_labels() {

let expected = concat!(
"{ ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"let key = metrics :: Key :: from_parts (String :: from (\"owned\") , mylabels) ; ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"let key = :: metrics :: Key :: from_parts (String :: from (\"owned\") , mylabels) ; ",
"let handle = recorder . register_mytype (& key) ; ",
"handle . myop (1) ; ",
"} ",
Expand All @@ -479,8 +523,8 @@ fn test_get_register_and_op_code_op_owned_name_constant_key_labels() {

let expected = concat!(
"{ ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"let key = metrics :: Key :: from_parts (String :: from (\"owned\") , vec ! [metrics :: Label :: new (LABEL_KEY , \"some_val\")]) ; ",
"if let Some (recorder) = :: metrics :: try_recorder () { ",
"let key = :: metrics :: Key :: from_parts (String :: from (\"owned\") , vec ! [:: metrics :: Label :: new (LABEL_KEY , \"some_val\")]) ; ",
"let handle = recorder . register_mytype (& key) ; ",
"handle . myop (1) ; ",
"} ",
Expand All @@ -507,8 +551,8 @@ fn test_labels_to_quoted_inline_labels() {
let stream = labels_to_quoted(&labels);
let expected = concat!(
"vec ! [",
"metrics :: Label :: new (\"mylabel1\" , mylabel1) , ",
"metrics :: Label :: new (\"mylabel2\" , \"mylabel2\")",
":: metrics :: Label :: new (\"mylabel1\" , mylabel1) , ",
":: metrics :: Label :: new (\"mylabel2\" , \"mylabel2\")",
"]"
);
assert_eq!(stream.to_string(), expected);
Expand Down
5 changes: 2 additions & 3 deletions metrics-observer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{error::Error, io};

use chrono::Local;
use metrics::Unit;
use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::IntoAlternateScreen};
use tui::{
backend::TermionBackend,
layout::{Constraint, Direction, Layout},
Expand All @@ -28,8 +28,7 @@ use self::selector::Selector;

fn main() -> Result<(), Box<dyn Error>> {
let stdout = io::stdout().into_raw_mode()?;
let stdout = MouseTerminal::from(stdout);
let stdout = AlternateScreen::from(stdout);
let stdout = MouseTerminal::from(stdout).into_alternate_screen()?;
let backend = TermionBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;

Expand Down

0 comments on commit 3dd299a

Please sign in to comment.