Skip to content
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

Supports same duration format in LogQL as Prometheus #1357

Merged
merged 1 commit into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/logql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ type logRange struct {
interval time.Duration
}

func mustNewRange(left LogSelectorExpr, interval time.Duration) *logRange {
func newLogRange(left LogSelectorExpr, interval time.Duration) *logRange {
return &logRange{
left: left,
interval: interval,
Expand Down
2 changes: 1 addition & 1 deletion pkg/logql/expr.y
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ logExpr:
;


logRangeExpr: logExpr DURATION { $$ = mustNewRange($1, $2) };
logRangeExpr: logExpr DURATION { $$ = newLogRange($1, $2) };


rangeAggregationExpr: rangeOp OPEN_PARENTHESIS logRangeExpr CLOSE_PARENTHESIS { $$ = newRangeAggregationExpr($3,$1) };
Expand Down
2 changes: 1 addition & 1 deletion pkg/logql/expr.y.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pkg/logql/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"strconv"
"text/scanner"
"time"

"github.com/prometheus/common/model"
)

var tokens = map[string]int{
Expand Down Expand Up @@ -64,12 +66,12 @@ func (l *lexer) Lex(lval *exprSymType) int {
d := ""
for r := l.Next(); r != scanner.EOF; r = l.Next() {
if string(r) == "]" {
i, err := time.ParseDuration(d)
i, err := model.ParseDuration(d)
if err != nil {
l.Error(err.Error())
return 0
}
lval.duration = i
lval.duration = time.Duration(i)
return DURATION
}
d += string(r)
Expand Down
32 changes: 31 additions & 1 deletion pkg/logql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ func TestParse(t *testing.T) {
operation: "rate",
},
},
{
in: `rate({ foo !~ "bar" }[5d])`,
exp: &rangeAggregationExpr{
left: &logRange{
left: &matchersExpr{matchers: []*labels.Matcher{mustNewMatcher(labels.MatchNotRegexp, "foo", "bar")}},
interval: 5 * 24 * time.Hour,
},
operation: "rate",
},
},
{
in: `count_over_time({ foo !~ "bar" }[1w])`,
exp: &rangeAggregationExpr{
left: &logRange{
left: &matchersExpr{matchers: []*labels.Matcher{mustNewMatcher(labels.MatchNotRegexp, "foo", "bar")}},
interval: 7 * 24 * time.Hour,
},
operation: "count_over_time",
},
},
{
in: `sum(rate({ foo !~ "bar" }[5h]))`,
exp: mustNewVectorAggregationExpr(&rangeAggregationExpr{
Expand All @@ -69,6 +89,16 @@ func TestParse(t *testing.T) {
operation: "rate",
}, "sum", nil, nil),
},
{
in: `sum(rate({ foo !~ "bar" }[1y]))`,
exp: mustNewVectorAggregationExpr(&rangeAggregationExpr{
left: &logRange{
left: &matchersExpr{matchers: []*labels.Matcher{mustNewMatcher(labels.MatchNotRegexp, "foo", "bar")}},
interval: 365 * 24 * time.Hour,
},
operation: "rate",
}, "sum", nil, nil),
},
{
in: `avg(count_over_time({ foo !~ "bar" }[5h])) by (bar,foo)`,
exp: mustNewVectorAggregationExpr(&rangeAggregationExpr{
Expand Down Expand Up @@ -149,7 +179,7 @@ func TestParse(t *testing.T) {
{
in: `rate({ foo !~ "bar" }[5minutes])`,
err: ParseError{
msg: "time: unknown unit minutes in duration 5minutes",
msg: `not a valid duration string: "5minutes"`,
line: 0,
col: 22,
},
Expand Down