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

Decouple logql engine/AST from execution context #1605

Merged
merged 4 commits into from
Jan 29, 2020
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
50 changes: 28 additions & 22 deletions pkg/logql/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logql
import (
"bytes"
"context"
"errors"
"fmt"
"regexp"
"strconv"
Expand Down Expand Up @@ -212,30 +213,46 @@ const (
type SampleExpr interface {
// Selector is the LogQL selector to apply when retrieving logs.
Selector() LogSelectorExpr
// Evaluator returns a `StepEvaluator` that can evaluate the expression step by step
Evaluator() StepEvaluator
// Close all resources used.
Close() error
}

// StepEvaluator evaluate a single step of a query.
type StepEvaluator interface {
Next() (bool, int64, promql.Vector)
// Close all resources used.
Close() error
}

type stepEvaluator struct {
fn func() (bool, int64, promql.Vector)
close func() error
}

// StepEvaluatorFn is a function to chain multiple `StepEvaluator`.
type StepEvaluatorFn func() (bool, int64, promql.Vector)
func newStepEvaluator(fn func() (bool, int64, promql.Vector), close func() error) (StepEvaluator, error) {
if fn == nil {
return nil, errors.New("nil step evaluator fn")
}

// Next implements `StepEvaluator`
func (s StepEvaluatorFn) Next() (bool, int64, promql.Vector) {
return s()
if close == nil {
close = func() error { return nil }
}

return &stepEvaluator{
fn: fn,
close: close,
}, nil
}

func (e *stepEvaluator) Next() (bool, int64, promql.Vector) {
return e.fn()
}

func (e *stepEvaluator) Close() error {
return e.close()
}

type rangeAggregationExpr struct {
left *logRange
operation string

iterator RangeVectorIterator
}

func newRangeAggregationExpr(left *logRange, operation string) SampleExpr {
Expand All @@ -245,13 +262,6 @@ func newRangeAggregationExpr(left *logRange, operation string) SampleExpr {
}
}

func (e *rangeAggregationExpr) Close() error {
if e.iterator == nil {
return nil
}
return e.iterator.Close()
}

func (e *rangeAggregationExpr) Selector() LogSelectorExpr {
return e.left.left
}
Expand Down Expand Up @@ -296,10 +306,6 @@ func mustNewVectorAggregationExpr(left SampleExpr, operation string, gr *groupin
}
}

func (v *vectorAggregationExpr) Close() error {
return v.left.Close()
}

func (v *vectorAggregationExpr) Selector() LogSelectorExpr {
return v.left.Selector()
}
Loading