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

Expose QueryType function. #1719

Merged
merged 2 commits into from
Feb 19, 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
24 changes: 13 additions & 11 deletions pkg/logql/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

const (
typeMetric = "metric"
typeFilter = "filter"
typeLimited = "limited"
QueryTypeMetric = "metric"
QueryTypeFilter = "filter"
QueryTypeLimited = "limited"
)

var (
Expand Down Expand Up @@ -55,7 +55,10 @@ var (
)

func RecordMetrics(status, query string, rangeType QueryRangeType, stats stats.Result) {
queryType := queryType(query)
queryType, err := QueryType(query)
if err != nil {
level.Warn(util.Logger).Log("msg", "error parsing query type", "err", err)
}
rt := string(rangeType)
bytesPerSeconds.WithLabelValues(status, queryType, rt).
Observe(float64(stats.Summary.BytesProcessedPerSeconds))
Expand All @@ -69,20 +72,19 @@ func RecordMetrics(status, query string, rangeType QueryRangeType, stats stats.R
ingesterLineTotal.Add(float64(stats.Ingester.TotalLinesSent))
}

func queryType(query string) string {
func QueryType(query string) (string, error) {
expr, err := ParseExpr(query)
if err != nil {
level.Warn(util.Logger).Log("msg", "error parsing query type", "err", err)
return ""
return "", err
}
switch expr.(type) {
case SampleExpr:
return typeMetric
return QueryTypeMetric, nil
case *matchersExpr:
return typeLimited
return QueryTypeLimited, nil
case *filterExpr:
return typeFilter
return QueryTypeFilter, nil
default:
return ""
return "", nil
}
}
31 changes: 19 additions & 12 deletions pkg/logql/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@ import (
"testing"
)

func Test_queryType(t *testing.T) {
func TestQueryType(t *testing.T) {
tests := []struct {
name string
query string
want string
name string
query string
want string
wantErr bool
}{
{"bad", "ddd", ""},
{"limited", `{app="foo"}`, typeLimited},
{"limited multi label", `{app="foo" ,fuzz=~"foo"}`, typeLimited},
{"filter", `{app="foo"} |= "foo"`, typeFilter},
{"metrics", `rate({app="foo"} |= "foo"[5m])`, typeMetric},
{"filters", `{app="foo"} |= "foo" |= "f" != "b"`, typeFilter},
{"bad", "ddd", "", true},
{"limited", `{app="foo"}`, QueryTypeLimited, false},
{"limited multi label", `{app="foo" ,fuzz=~"foo"}`, QueryTypeLimited, false},
{"filter", `{app="foo"} |= "foo"`, QueryTypeFilter, false},
{"metrics", `rate({app="foo"} |= "foo"[5m])`, QueryTypeMetric, false},
{"metrics binary", `rate({app="foo"} |= "foo"[5m]) + count_over_time({app="foo"} |= "foo"[5m]) / rate({app="foo"} |= "foo"[5m]) `, QueryTypeMetric, false},
{"filters", `{app="foo"} |= "foo" |= "f" != "b"`, QueryTypeFilter, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := queryType(tt.query); got != tt.want {
t.Errorf("queryType() = %v, want %v", got, tt.want)
got, err := QueryType(tt.query)
if (err != nil) != tt.wantErr {
t.Errorf("QueryType() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("QueryType() = %v, want %v", got, tt.want)
}
})
}
Expand Down