collapse from_owned_parts/from_hybrid_parts into from_parts

This commit is contained in:
Toby Lawrence 2020-11-15 15:49:05 -05:00
parent 2cd4e9e100
commit ee379362f5
6 changed files with 34 additions and 39 deletions

View File

@ -367,10 +367,10 @@ fn key_to_quoted(labels: Option<Labels>) -> proc_macro2::TokenStream {
.into_iter()
.map(|(key, val)| quote! { metrics::Label::new(#key, #val) });
quote! {
metrics::KeyData::from_hybrid_parts(&METRIC_NAME, vec![#(#labels),*])
metrics::KeyData::from_parts(&METRIC_NAME[..], vec![#(#labels),*])
}
}
Labels::Existing(e) => quote! { metrics::KeyData::from_hybrid_parts(&METRIC_NAME, #e) },
Labels::Existing(e) => quote! { metrics::KeyData::from_parts(&METRIC_NAME[..], #e) },
},
}
}

View File

@ -167,7 +167,7 @@ fn test_get_expanded_callsite_fast_path_dynamic_labels() {
"static METRIC_NAME : [metrics :: SharedString ; 1] = [metrics :: SharedString :: const_str (\"mykeyname\")] ; ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"recorder . myop_mytype (metrics :: Key :: Owned (",
"metrics :: KeyData :: from_hybrid_parts (& METRIC_NAME , vec ! [metrics :: Label :: new (\"key1\" , & value1)])",
"metrics :: KeyData :: from_parts (& METRIC_NAME [..] , vec ! [metrics :: Label :: new (\"key1\" , & value1)])",
") , 1) ; ",
"} ",
"}",
@ -192,7 +192,7 @@ fn test_get_expanded_callsite_regular_path() {
"static METRIC_NAME : [metrics :: SharedString ; 1] = [metrics :: SharedString :: const_str (\"mykeyname\")] ; ",
"if let Some (recorder) = metrics :: try_recorder () { ",
"recorder . myop_mytype (",
"metrics :: Key :: Owned (metrics :: KeyData :: from_hybrid_parts (& METRIC_NAME , mylabels)) , ",
"metrics :: Key :: Owned (metrics :: KeyData :: from_parts (& METRIC_NAME [..] , mylabels)) , ",
"1",
") ; ",
"} }",
@ -213,7 +213,7 @@ fn test_key_to_quoted_existing_labels() {
let stream = key_to_quoted(Some(Labels::Existing(Expr::Path(
parse_quote! { mylabels },
))));
let expected = "metrics :: KeyData :: from_hybrid_parts (& METRIC_NAME , mylabels)";
let expected = "metrics :: KeyData :: from_parts (& METRIC_NAME [..] , mylabels)";
assert_eq!(stream.to_string(), expected);
}
@ -226,7 +226,7 @@ fn test_key_to_quoted_inline_labels() {
(parse_quote! {"mylabel2"}, parse_quote! { "mylabel2" }),
])));
let expected = concat!(
"metrics :: KeyData :: from_hybrid_parts (& METRIC_NAME , vec ! [",
"metrics :: KeyData :: from_parts (& METRIC_NAME [..] , vec ! [",
"metrics :: Label :: new (\"mylabel1\" , mylabel1) , ",
"metrics :: Label :: new (\"mylabel2\" , \"mylabel2\")",
"])"
@ -237,6 +237,6 @@ fn test_key_to_quoted_inline_labels() {
#[test]
fn test_key_to_quoted_inline_labels_empty() {
let stream = key_to_quoted(Some(Labels::Inline(vec![])));
let expected = concat!("metrics :: KeyData :: from_hybrid_parts (& METRIC_NAME , vec ! [])");
let expected = concat!("metrics :: KeyData :: from_parts (& METRIC_NAME [..] , vec ! [])");
assert_eq!(stream.to_string(), expected);
}

View File

@ -126,7 +126,7 @@ where
fn enhance_key(&self, key: Key) -> Key {
let (name, mut labels) = key.into_owned().into_parts();
self.enhance_labels(&mut labels);
KeyData::from_owned_parts(name, labels).into()
KeyData::from_parts(name, labels).into()
}
}

View File

@ -52,7 +52,7 @@ fn test_basic_functionality() {
snapshot,
vec![(
MetricKind::Counter,
KeyData::from_owned_parts(
KeyData::from_parts(
"login_attempts",
vec![
Label::new("service", "login_service"),
@ -95,7 +95,7 @@ fn test_macro_forms() {
vec![
(
MetricKind::Counter,
KeyData::from_owned_parts(
KeyData::from_parts(
"login_attempts_no_labels",
vec![
Label::new("user", "ferris"),
@ -109,7 +109,7 @@ fn test_macro_forms() {
),
(
MetricKind::Counter,
KeyData::from_owned_parts(
KeyData::from_parts(
"login_attempts_static_labels",
vec![
Label::new("service", "login_service"),
@ -124,7 +124,7 @@ fn test_macro_forms() {
),
(
MetricKind::Counter,
KeyData::from_owned_parts(
KeyData::from_parts(
"login_attempts_dynamic_labels",
vec![
Label::new("node_name", "localhost"),
@ -139,7 +139,7 @@ fn test_macro_forms() {
),
(
MetricKind::Counter,
KeyData::from_owned_parts(
KeyData::from_parts(
"login_attempts_static_and_dynamic_labels",
vec![
Label::new("service", "login_service"),
@ -224,7 +224,7 @@ fn test_multiple_paths_to_the_same_callsite() {
vec![
(
MetricKind::Counter,
KeyData::from_owned_parts(
KeyData::from_parts(
"my_counter",
vec![
Label::new("shared_field", "path1"),
@ -239,7 +239,7 @@ fn test_multiple_paths_to_the_same_callsite() {
),
(
MetricKind::Counter,
KeyData::from_owned_parts(
KeyData::from_parts(
"my_counter",
vec![
Label::new("shared_field", "path2"),
@ -295,7 +295,7 @@ fn test_nested_spans() {
snapshot,
vec![(
MetricKind::Counter,
KeyData::from_owned_parts(
KeyData::from_parts(
"my_counter",
vec![
Label::new("shared_field", "inner"),
@ -340,7 +340,7 @@ fn test_label_filtering() {
snapshot,
vec![(
MetricKind::Counter,
KeyData::from_owned_parts(
KeyData::from_parts(
"login_attempts",
vec![
Label::new("service", "login_service"),

View File

@ -64,7 +64,7 @@ fn registry_benchmark(c: &mut Criterion) {
b.iter(|| {
let key = "simple_key";
let labels = vec![Label::new("type", "http")];
KeyData::from_owned_parts(key, labels)
KeyData::from_parts(key, labels)
})
})
.with_function("const key data overhead (basic)", |b| {
@ -90,7 +90,7 @@ fn registry_benchmark(c: &mut Criterion) {
b.iter(|| {
let key = "simple_key";
let labels = vec![Label::new("type", "http")];
Key::Owned(KeyData::from_owned_parts(key, labels))
Key::Owned(KeyData::from_parts(key, labels))
})
})
.with_function("cached key overhead (basic)", |b| {

View File

@ -74,6 +74,12 @@ impl From<&'static str> for NameParts {
}
}
impl From<&'static [SharedString]> for NameParts {
fn from(names: &'static [SharedString]) -> NameParts {
NameParts::from_static_names(names)
}
}
impl fmt::Display for NameParts {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = self.to_string();
@ -105,8 +111,8 @@ impl KeyData {
}
}
/// Creates a [`KeyData`] from a name.
pub fn from_owned_parts<N, L>(name: N, labels: L) -> Self
/// Creates a [`KeyData`] from a name and set of labels.
pub fn from_parts<N, L>(name: N, labels: L) -> Self
where
N: Into<NameParts>,
L: IntoLabels,
@ -117,17 +123,6 @@ impl KeyData {
}
}
/// Creates a [`KeyData`] from a name and vector of [`Label`]s.
pub fn from_hybrid_parts<L>(name_parts: &'static [SharedString], labels: L) -> Self
where
L: IntoLabels,
{
Self {
name_parts: NameParts::from_static_names(name_parts),
labels: Cow::owned(labels.into_labels()),
}
}
/// Creates a [`KeyData`] from a static name.
///
/// This function is `const`, so it can be used in a static context.
@ -363,7 +358,7 @@ mod tests {
assert_eq!(previous, Some(&42));
let labels = LABELS.to_vec();
let owned_labels = KeyData::from_hybrid_parts(&BORROWED_NAME, labels);
let owned_labels = KeyData::from_parts(&BORROWED_NAME[..], labels);
assert_eq!(&owned_labels, &BORROWED_LABELS);
let previous = keys.insert(owned_labels, 43);
@ -388,7 +383,7 @@ mod tests {
assert_eq!(previous, Some(&42));
let labels = LABELS.to_vec();
let owned_labels = Key::from(KeyData::from_hybrid_parts(&BORROWED_NAME, labels));
let owned_labels = Key::from(KeyData::from_parts(&BORROWED_NAME[..], labels));
let borrowed_labels = Key::from(&BORROWED_LABELS);
assert_eq!(owned_labels, borrowed_labels);
@ -405,19 +400,19 @@ mod tests {
let result1 = key1.to_string();
assert_eq!(result1, "KeyData(foobar)");
let key2 = KeyData::from_hybrid_parts(&FOOBAR_NAME, vec![Label::new("system", "http")]);
let key2 = KeyData::from_parts(&FOOBAR_NAME[..], vec![Label::new("system", "http")]);
let result2 = key2.to_string();
assert_eq!(result2, "KeyData(foobar, [system = http])");
let key3 = KeyData::from_hybrid_parts(
&FOOBAR_NAME,
let key3 = KeyData::from_parts(
&FOOBAR_NAME[..],
vec![Label::new("system", "http"), Label::new("user", "joe")],
);
let result3 = key3.to_string();
assert_eq!(result3, "KeyData(foobar, [system = http, user = joe])");
let key4 = KeyData::from_hybrid_parts(
&FOOBAR_NAME,
let key4 = KeyData::from_parts(
&FOOBAR_NAME[..],
vec![
Label::new("black", "black"),
Label::new("lives", "lives"),