Skip to content

Commit d760643

Browse files
feat: add MetricsQL range functions (#6)
1 parent 7c5f707 commit d760643

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎src/parser/function.rs‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,23 @@ lazy_static! {
247247
ValueType::Vector,
248248
false
249249
),
250+
// MetricsQL extension.
251+
function!(
252+
"distinct_over_time",
253+
vec![ValueType::Matrix],
254+
0,
255+
ValueType::Vector,
256+
false
257+
),
250258
function!("end", vec![], 0, ValueType::Scalar, true),
259+
// MetricsQL extension.
260+
function!(
261+
"entropy_over_time",
262+
vec![ValueType::Matrix],
263+
0,
264+
ValueType::Vector,
265+
false
266+
),
251267
function!("exp", vec![ValueType::Vector], 0, ValueType::Vector, false),
252268
function!(
253269
"first_over_time",
@@ -668,13 +684,20 @@ mod tests {
668684
let rate = get_function("rate").unwrap();
669685
assert_eq!(rate.variadic, 0);
670686
assert!(!rate.experimental);
671-
672687
for func_name in ["max_of", "min_of"] {
673688
let func = get_function(func_name).unwrap();
674689
assert_eq!(func.arg_types, vec![ValueType::Scalar, ValueType::Scalar]);
675690
assert_eq!(func.variadic, 0);
676691
assert_eq!(func.return_type, ValueType::Scalar);
677692
assert!(func.experimental);
678693
}
694+
695+
for func_name in ["distinct_over_time", "entropy_over_time"] {
696+
let func = get_function(func_name).unwrap();
697+
assert_eq!(func.arg_types, vec![ValueType::Matrix]);
698+
assert_eq!(func.variadic, 0);
699+
assert_eq!(func.return_type, ValueType::Vector);
700+
assert!(!func.experimental);
701+
}
679702
}
680703
}

‎src/parser/parse.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,30 @@ mod tests {
13451345
Expr::new_call(get_function("rate").unwrap(), FunctionArgs::new_args(ex))
13461346
})
13471347
}),
1348+
("distinct_over_time(some_metric[5m])", {
1349+
Expr::new_matrix_selector(
1350+
Expr::from(VectorSelector::from("some_metric")),
1351+
duration::MINUTE_DURATION * 5,
1352+
)
1353+
.and_then(|ex| {
1354+
Expr::new_call(
1355+
get_function("distinct_over_time").unwrap(),
1356+
FunctionArgs::new_args(ex),
1357+
)
1358+
})
1359+
}),
1360+
("entropy_over_time(some_metric[5m])", {
1361+
Expr::new_matrix_selector(
1362+
Expr::from(VectorSelector::from("some_metric")),
1363+
duration::MINUTE_DURATION * 5,
1364+
)
1365+
.and_then(|ex| {
1366+
Expr::new_call(
1367+
get_function("entropy_over_time").unwrap(),
1368+
FunctionArgs::new_args(ex),
1369+
)
1370+
})
1371+
}),
13481372
("round(some_metric)", {
13491373
let ex = Expr::from(VectorSelector::from("some_metric"));
13501374
Expr::new_call(get_function("round").unwrap(), FunctionArgs::new_args(ex))

0 commit comments

Comments
 (0)