From ea6de10ed134c357f2e0cb8d6430cd8a83cd6b3f Mon Sep 17 00:00:00 2001 From: Maaz Ahmed <maaz.a@subcom.tech> Date: Tue, 9 Jan 2024 15:28:30 +0530 Subject: [PATCH] feat: MetricsQL alias fn --- src/query/fns/label.rs | 15 +++++++++++++++ tests/fns.rs | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/src/query/fns/label.rs b/src/query/fns/label.rs index 6caa4bc..9bd6ee8 100644 --- a/src/query/fns/label.rs +++ b/src/query/fns/label.rs @@ -7,6 +7,15 @@ use crate::query::{ ops::Operable, }; +/// MetricsQL's alias label manipulation query function +#[inline] +pub fn mql_alias<'a>( + qry_expr: impl Operable + 'a, + name: &'a str, +) -> QryFunc<impl Fn(&mut fmt::Formatter) -> fmt::Result + 'a> { + qry_fn!(alias, "{qry_expr}, \"{name}\"") +} + /// MetricsQL's label_set label manipulation query function #[inline] pub fn mql_label_set<'a>( @@ -126,4 +135,10 @@ mod tests { let qry = mql_label_set(Metric::new("metric"), &["label", "value"]).to_string(); assert_eq!(qry, r#"label_set(metric,"label","value")"#); } + + #[test] + fn label_alias() { + let qry = mql_alias(Metric::new("metric"), "alias").to_string(); + assert_eq!(qry, "alias(metric, \"alias\")"); + } } diff --git a/tests/fns.rs b/tests/fns.rs index c81ceb0..55e2a97 100644 --- a/tests/fns.rs +++ b/tests/fns.rs @@ -107,6 +107,13 @@ async fn mql_label_map() { utils::send_query(query).await.unwrap(); } +#[cfg(feature = "metricsql")] +#[tokio::test] +async fn mql_alias() { + let query = label::mql_alias(Metric::new("metric"), "alias"); + utils::send_query(query).await.unwrap(); +} + #[cfg(feature = "metricsql")] label_tests!( mql_label_set, -- GitLab