mirror of https://github.com/grafana/loki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
4.6 KiB
200 lines
4.6 KiB
|
4 years ago
|
package syntax
|
||
|
7 years ago
|
|
||
|
|
import (
|
||
|
7 years ago
|
"errors"
|
||
|
5 years ago
|
"fmt"
|
||
|
5 years ago
|
"sort"
|
||
|
7 years ago
|
"strings"
|
||
|
5 years ago
|
"sync"
|
||
|
7 years ago
|
"text/scanner"
|
||
|
7 years ago
|
|
||
|
4 years ago
|
"github.com/prometheus/prometheus/model/labels"
|
||
|
5 years ago
|
promql_parser "github.com/prometheus/prometheus/promql/parser"
|
||
|
5 years ago
|
|
||
|
|
"github.com/grafana/loki/pkg/logqlmodel"
|
||
|
4 years ago
|
"github.com/grafana/loki/pkg/util"
|
||
|
7 years ago
|
)
|
||
|
|
|
||
|
5 years ago
|
const errAtleastOneEqualityMatcherRequired = "queries require at least one regexp or equality matcher that does not have an empty-compatible value. For instance, app=~\".*\" does not meet this requirement, but app=~\".+\" will"
|
||
|
|
|
||
|
5 years ago
|
var parserPool = sync.Pool{
|
||
|
|
New: func() interface{} {
|
||
|
|
p := &parser{
|
||
|
|
p: &exprParserImpl{},
|
||
|
|
Reader: strings.NewReader(""),
|
||
|
|
lexer: &lexer{},
|
||
|
|
}
|
||
|
|
return p
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
const maxInputSize = 5120
|
||
|
|
|
||
|
7 years ago
|
func init() {
|
||
|
|
// Improve the error messages coming out of yacc.
|
||
|
|
exprErrorVerbose = true
|
||
|
5 years ago
|
// uncomment when you need to understand yacc rule tree.
|
||
|
|
// exprDebug = 3
|
||
|
7 years ago
|
for str, tok := range tokens {
|
||
|
|
exprToknames[tok-exprPrivate+1] = str
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
type parser struct {
|
||
|
|
p *exprParserImpl
|
||
|
|
*lexer
|
||
|
|
expr Expr
|
||
|
|
*strings.Reader
|
||
|
|
}
|
||
|
|
|
||
|
|
func (p *parser) Parse() (Expr, error) {
|
||
|
|
p.lexer.errs = p.lexer.errs[:0]
|
||
|
|
p.lexer.Scanner.Error = func(_ *scanner.Scanner, msg string) {
|
||
|
|
p.lexer.Error(msg)
|
||
|
|
}
|
||
|
|
e := p.p.Parse(p)
|
||
|
|
if e != 0 || len(p.lexer.errs) > 0 {
|
||
|
|
return nil, p.lexer.errs[0]
|
||
|
|
}
|
||
|
|
return p.expr, nil
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
// ParseExpr parses a string and returns an Expr.
|
||
|
5 years ago
|
func ParseExpr(input string) (Expr, error) {
|
||
|
|
expr, err := parseExprWithoutValidation(input)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
if err := validateExpr(expr); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return expr, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func parseExprWithoutValidation(input string) (expr Expr, err error) {
|
||
|
5 years ago
|
if len(input) >= maxInputSize {
|
||
|
5 years ago
|
return nil, logqlmodel.NewParseError(fmt.Sprintf("input size too long (%d > %d)", len(input), maxInputSize), 0, 0)
|
||
|
5 years ago
|
}
|
||
|
|
|
||
|
7 years ago
|
defer func() {
|
||
|
5 years ago
|
if r := recover(); r != nil {
|
||
|
7 years ago
|
var ok bool
|
||
|
|
if err, ok = r.(error); ok {
|
||
|
5 years ago
|
if errors.Is(err, logqlmodel.ErrParse) {
|
||
|
6 years ago
|
return
|
||
|
|
}
|
||
|
5 years ago
|
err = logqlmodel.NewParseError(err.Error(), 0, 0)
|
||
|
7 years ago
|
}
|
||
|
|
}
|
||
|
|
}()
|
||
|
5 years ago
|
|
||
|
5 years ago
|
p := parserPool.Get().(*parser)
|
||
|
|
defer parserPool.Put(p)
|
||
|
|
|
||
|
|
p.Reader.Reset(input)
|
||
|
|
p.lexer.Init(p.Reader)
|
||
|
|
return p.Parse()
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
func validateExpr(expr Expr) error {
|
||
|
|
switch e := expr.(type) {
|
||
|
|
case SampleExpr:
|
||
|
|
err := validateSampleExpr(e)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
case LogSelectorExpr:
|
||
|
|
err := validateMatchers(e.Matchers())
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
default:
|
||
|
5 years ago
|
return logqlmodel.NewParseError(fmt.Sprintf("unexpected expression type: %v", e), 0, 0)
|
||
|
5 years ago
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// validateMatchers checks whether a query would touch all the streams in the query range or uses at least one matcher to select specific streams.
|
||
|
|
func validateMatchers(matchers []*labels.Matcher) error {
|
||
|
5 years ago
|
if len(matchers) == 0 {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
_, matchers = util.SplitFiltersAndMatchers(matchers)
|
||
|
|
if len(matchers) == 0 {
|
||
|
5 years ago
|
return logqlmodel.NewParseError(errAtleastOneEqualityMatcherRequired, 0, 0)
|
||
|
5 years ago
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
7 years ago
|
// ParseMatchers parses a string and returns labels matchers, if the expression contains
|
||
|
|
// anything else it will return an error.
|
||
|
|
func ParseMatchers(input string) ([]*labels.Matcher, error) {
|
||
|
|
expr, err := ParseExpr(input)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
4 years ago
|
matcherExpr, ok := expr.(*MatchersExpr)
|
||
|
7 years ago
|
if !ok {
|
||
|
|
return nil, errors.New("only label matchers is supported")
|
||
|
|
}
|
||
|
4 years ago
|
return matcherExpr.Mts, nil
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
6 years ago
|
// ParseSampleExpr parses a string and returns the sampleExpr
|
||
|
5 years ago
|
func ParseSampleExpr(input string) (SampleExpr, error) {
|
||
|
6 years ago
|
expr, err := ParseExpr(input)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
sampleExpr, ok := expr.(SampleExpr)
|
||
|
|
if !ok {
|
||
|
|
return nil, errors.New("only sample expression supported")
|
||
|
|
}
|
||
|
5 years ago
|
|
||
|
6 years ago
|
return sampleExpr, nil
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
func validateSampleExpr(expr SampleExpr) error {
|
||
|
5 years ago
|
switch e := expr.(type) {
|
||
|
4 years ago
|
case *BinOpExpr:
|
||
|
5 years ago
|
if err := validateSampleExpr(e.SampleExpr); err != nil {
|
||
|
5 years ago
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
return validateSampleExpr(e.RHS)
|
||
|
4 years ago
|
case *LiteralExpr:
|
||
|
5 years ago
|
return nil
|
||
|
|
default:
|
||
|
5 years ago
|
return validateMatchers(expr.Selector().Matchers())
|
||
|
5 years ago
|
}
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
// ParseLogSelector parses a log selector expression `{app="foo"} |= "filter"`
|
||
|
5 years ago
|
func ParseLogSelector(input string, validate bool) (LogSelectorExpr, error) {
|
||
|
|
expr, err := parseExprWithoutValidation(input)
|
||
|
7 years ago
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
logSelector, ok := expr.(LogSelectorExpr)
|
||
|
|
if !ok {
|
||
|
|
return nil, errors.New("only log selector is supported")
|
||
|
|
}
|
||
|
5 years ago
|
if validate {
|
||
|
|
if err := validateExpr(expr); err != nil {
|
||
|
5 years ago
|
return nil, err
|
||
|
|
}
|
||
|
|
}
|
||
|
7 years ago
|
return logSelector, nil
|
||
|
|
}
|
||
|
5 years ago
|
|
||
|
|
// ParseLabels parses labels from a string using logql parser.
|
||
|
|
func ParseLabels(lbs string) (labels.Labels, error) {
|
||
|
|
ls, err := promql_parser.ParseMetric(lbs)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
sort.Sort(ls)
|
||
|
|
return ls, nil
|
||
|
|
}
|