Skip to content

Commit

Permalink
Start serializing log range
Browse files Browse the repository at this point in the history
  • Loading branch information
jeschkies committed Nov 10, 2023
1 parent d3142cc commit 95d4706
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 25 deletions.
51 changes: 36 additions & 15 deletions pkg/logql/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"regexp"
"time"

jsoniter "github.com/json-iterator/go"

Expand Down Expand Up @@ -128,26 +129,31 @@ func (v *JSONSerializer) VisitRangeAggregation(e *syntax.RangeAggregationExpr) {

v.WriteMore()
v.WriteObjectField("range")
// TODO: decide whether LogRange should have a visitor method
syntax.Dispatch(e.Left, v) //nolint:errcheck
v.WriteObjectEnd()
v.Flush()
}

func (v *JSONSerializer) VisitLogRange(e *syntax.LogRange) {
v.WriteObjectStart()
v.WriteObjectField("interval_nanos")
v.WriteInt64(int64(e.Left.Interval))
v.WriteInt64(int64(e.Interval))
v.WriteMore()
v.WriteObjectField("offset_nanos")
v.WriteInt64(int64(e.Left.Offset))
v.WriteInt64(int64(e.Offset))

// Serialize log selector pipeline as string.
v.WriteMore()
encodeLogSelector(v.Stream, e.Left.Left)
v.WriteObjectField("log_selector")
encodeLogSelector(v.Stream, e.Left)

if e.Left.Unwrap != nil {
if e.Unwrap != nil {
v.WriteMore()
v.WriteObjectField("unwrap")
v.WriteString(e.Left.Unwrap.String())
v.WriteString(e.Unwrap.String())
v.WriteObjectEnd()
}

v.WriteObjectEnd()
v.WriteObjectEnd()
v.Flush()
}
Expand Down Expand Up @@ -271,7 +277,7 @@ func decodeGrouping(iter *jsoniter.Iterator) (*syntax.Grouping, error) {

func encodeLogSelector(s *jsoniter.Stream, e syntax.LogSelectorExpr) {
s.WriteObjectStart()
s.WriteObjectField("log_selector")
s.WriteObjectField("raw")

s.WriteString(e.String())

Expand All @@ -281,6 +287,8 @@ func encodeLogSelector(s *jsoniter.Stream, e syntax.LogSelectorExpr) {

func decodeLogSelector(iter *jsoniter.Iterator) (syntax.LogSelectorExpr, error) {

iter.ReadObject()

raw := iter.ReadString()
expr, err := syntax.ParseExpr(raw)
if err != nil {
Expand Down Expand Up @@ -351,9 +359,7 @@ func decodeVectorAgg(iter *jsoniter.Iterator) (*syntax.VectorAggregationExpr, er
}

func decodeRangeAgg(iter *jsoniter.Iterator) (*syntax.RangeAggregationExpr, error) {
expr := &syntax.RangeAggregationExpr{
Left: &syntax.LogRange{},
}
expr := &syntax.RangeAggregationExpr{}
var err error

for f := iter.ReadObject(); f != ""; f = iter.ReadObject() {
Expand All @@ -364,14 +370,29 @@ func decodeRangeAgg(iter *jsoniter.Iterator) (*syntax.RangeAggregationExpr, erro
tmp := iter.ReadFloat64()
expr.Params = &tmp
case "range":
// TODO
iter.Skip()
expr.Left, err = decodeLogRange(iter)
case "grouping":
expr.Grouping, err = decodeGrouping(iter)
}
}

return expr, err
}

func decodeLogRange(iter *jsoniter.Iterator) (*syntax.LogRange, error) {
expr := &syntax.LogRange{}
var err error

for f := iter.ReadObject(); f != ""; f = iter.ReadObject() {
switch f {
case "log_selector":
expr.Left.Left, err = decodeLogSelector(iter)
expr.Left, err = decodeLogSelector(iter)
case "interval_nanos":
expr.Interval = time.Duration(iter.ReadInt64())
case "offset_nanos":
expr.Offset = time.Duration(iter.ReadInt64())
case "unwrap":
// TODO:
expr.Unwrap =
}
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/logql/syntax/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type RootVisitor interface {
SampleExprVisitor
LogSelectorExprVisitor
StageExprVisitor

VisitLogRange(*LogRange)
}

type SampleExprVisitor interface {
Expand Down Expand Up @@ -58,6 +60,8 @@ func Dispatch(root Expr, v RootVisitor) error {
DispatchLogSelectorExpr(e, v)
case StageExpr:
DispatchStageExpr(e, v)
case *LogRange:
v.VisitLogRange(e)
default:
return fmt.Errorf("unpexpected root expression type: got (%T)", e)
}
Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/json-iterator/go/iter_float.go

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

3 changes: 2 additions & 1 deletion vendor/github.com/json-iterator/go/iter_skip_strict.go

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

3 changes: 2 additions & 1 deletion vendor/github.com/json-iterator/go/reflect_dynamic.go

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

3 changes: 2 additions & 1 deletion vendor/github.com/json-iterator/go/reflect_extension.go

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

3 changes: 2 additions & 1 deletion vendor/github.com/json-iterator/go/reflect_json_number.go

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

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

3 changes: 2 additions & 1 deletion vendor/github.com/json-iterator/go/reflect_map.go

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

3 changes: 2 additions & 1 deletion vendor/github.com/json-iterator/go/reflect_optional.go

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

3 changes: 2 additions & 1 deletion vendor/github.com/json-iterator/go/reflect_slice.go

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

3 changes: 2 additions & 1 deletion vendor/github.com/json-iterator/go/reflect_struct_encoder.go

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

0 comments on commit 95d4706

Please sign in to comment.