-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Logql comparison ops #2182
Logql comparison ops #2182
Conversation
b82336f
to
3efcfe3
Compare
@@ -1207,6 +1304,54 @@ func TestEngine_RangeQuery(t *testing.T) { | |||
}, | |||
}, | |||
}, | |||
{ | |||
`bytes_over_time({app="foo"}[30s]) > bool 1`, time.Unix(60, 0), time.Unix(120, 0), 15 * time.Second, 0, logproto.FORWARD, 10, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hahahaha this is so cool.
I can already see someone doing alert because he doesn't want to log too much.
/cc @slim-bean
// cannot compare metric & log queries | ||
in: `count_over_time({ foo != "bar" }[12m]) > { foo = "bar" }`, | ||
err: ParseError{ | ||
msg: "unexpected type for right leg of binary operation (>): *logql.matchersExpr", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice !
@@ -645,7 +645,7 @@ func TestMapping(t *testing.T) { | |||
in: `1 + sum by (cluster) (rate({foo="bar"}[5m]))`, | |||
expr: &binOpExpr{ | |||
op: OpTypeAdd, | |||
SampleExpr: &literalExpr{1}, | |||
SampleExpr: &literalExpr{value: 1}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YOU GOT LINTED
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:'(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
You nailed it !
Nice!!! This was quick @owen-d |
This PR introduces comparison operators:
Comparison operators
==
(equality)!=
(inequality)>
(greater than)>=
(greater than or equal to)<
(less than)<=
(less than or equal to)Comparison operators are defined between scalar/scalar, vector/scalar, and vector/vector value pairs. By default they filter. Their behavior can be modified by providing bool after the operator, which will return 0 or 1 for the value rather than filtering.
Between two scalars, the bool modifier must be provided and these operators result in another scalar that is either 0 (false) or 1 (true), depending on the comparison result.
1 >= 1
is equivalent to1
Between a vector and a scalar, these operators are applied to the value of every data sample in the vector, and vector elements between which the comparison result is false get dropped from the result vector. If the bool modifier is provided, vector elements that would be dropped instead have the value 0 and vector elements that would be kept have the value 1.
count_over_time({foo="bar"}[1m]) > 10
Filters the streams which log at elast 10 lines in the last minute.count_over_time({foo="bar"}[1m]) > bool 10
The same as above, but instead of filtering, attached the value 0 to streams that log less than 10 lines.Between two vectors, these operators behave as a filter by default, applied to matching entries. Vector elements for which the expression is not true or which do not find a match on the other side of the expression get dropped from the result, while the others are propagated into a result vector. If the bool modifier is provided, vector elements that would have been dropped instead have the value 0 and vector elements that would be kept have the value 1, with the grouping labels again becoming the output label set.
sum without(app) (count_over_time({app="foo"}[1m])) > sum without(app) (count_over_time({app="bar"}[1m]))
Returns the streams matchingapp=foo
without app labels that have higher counts within the last minute than their counterparts matchingapp=bar
without app labels.sum without(app) (count_over_time({app="foo"}[1m])) > bool sum without(app) (count_over_time({app="bar"}[1m]))
The same as above, but vectors have their values set to 1 if they pass the comparison or 0 if they fail/would otherwise have been filtered out./cc @cyriltovena
I'd appreciate if you can think of ways to simplify this.