From 95d4706a5a13c2bbf119ffb89075255d32526145 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Fri, 10 Nov 2023 17:39:32 +0100 Subject: [PATCH] Start serializing log range --- pkg/logql/serialize.go | 51 +++++++++++++------ pkg/logql/syntax/walk.go | 4 ++ .../github.com/json-iterator/go/iter_float.go | 2 +- .../json-iterator/go/iter_skip_strict.go | 3 +- .../json-iterator/go/reflect_dynamic.go | 3 +- .../json-iterator/go/reflect_extension.go | 3 +- .../json-iterator/go/reflect_json_number.go | 3 +- .../go/reflect_json_raw_message.go | 3 +- .../json-iterator/go/reflect_map.go | 3 +- .../json-iterator/go/reflect_optional.go | 3 +- .../json-iterator/go/reflect_slice.go | 3 +- .../go/reflect_struct_encoder.go | 3 +- 12 files changed, 59 insertions(+), 25 deletions(-) diff --git a/pkg/logql/serialize.go b/pkg/logql/serialize.go index 9672e82b82081..689daa2b3bdd5 100644 --- a/pkg/logql/serialize.go +++ b/pkg/logql/serialize.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "regexp" + "time" jsoniter "github.com/json-iterator/go" @@ -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() } @@ -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()) @@ -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 { @@ -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() { @@ -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 = } } diff --git a/pkg/logql/syntax/walk.go b/pkg/logql/syntax/walk.go index c242aa3a2970d..64e45ce5a380a 100644 --- a/pkg/logql/syntax/walk.go +++ b/pkg/logql/syntax/walk.go @@ -18,6 +18,8 @@ type RootVisitor interface { SampleExprVisitor LogSelectorExprVisitor StageExprVisitor + + VisitLogRange(*LogRange) } type SampleExprVisitor interface { @@ -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) } diff --git a/vendor/github.com/json-iterator/go/iter_float.go b/vendor/github.com/json-iterator/go/iter_float.go index 8a3d8b6fb43c2..caf16feec89d9 100644 --- a/vendor/github.com/json-iterator/go/iter_float.go +++ b/vendor/github.com/json-iterator/go/iter_float.go @@ -66,7 +66,7 @@ func (iter *Iterator) ReadBigInt() (ret *big.Int) { return ret } -//ReadFloat32 read float32 +// ReadFloat32 read float32 func (iter *Iterator) ReadFloat32() (ret float32) { c := iter.nextToken() if c == '-' { diff --git a/vendor/github.com/json-iterator/go/iter_skip_strict.go b/vendor/github.com/json-iterator/go/iter_skip_strict.go index 6cf66d0438dbe..f1ad6591bb0c4 100644 --- a/vendor/github.com/json-iterator/go/iter_skip_strict.go +++ b/vendor/github.com/json-iterator/go/iter_skip_strict.go @@ -1,4 +1,5 @@ -//+build !jsoniter_sloppy +//go:build !jsoniter_sloppy +// +build !jsoniter_sloppy package jsoniter diff --git a/vendor/github.com/json-iterator/go/reflect_dynamic.go b/vendor/github.com/json-iterator/go/reflect_dynamic.go index 8b6bc8b433286..71a0fe273011f 100644 --- a/vendor/github.com/json-iterator/go/reflect_dynamic.go +++ b/vendor/github.com/json-iterator/go/reflect_dynamic.go @@ -1,9 +1,10 @@ package jsoniter import ( - "github.com/modern-go/reflect2" "reflect" "unsafe" + + "github.com/modern-go/reflect2" ) type dynamicEncoder struct { diff --git a/vendor/github.com/json-iterator/go/reflect_extension.go b/vendor/github.com/json-iterator/go/reflect_extension.go index 74a97bfe5abfb..a820f10cab787 100644 --- a/vendor/github.com/json-iterator/go/reflect_extension.go +++ b/vendor/github.com/json-iterator/go/reflect_extension.go @@ -2,12 +2,13 @@ package jsoniter import ( "fmt" - "github.com/modern-go/reflect2" "reflect" "sort" "strings" "unicode" "unsafe" + + "github.com/modern-go/reflect2" ) var typeDecoders = map[string]ValDecoder{} diff --git a/vendor/github.com/json-iterator/go/reflect_json_number.go b/vendor/github.com/json-iterator/go/reflect_json_number.go index 98d45c1ec2550..52e11bf3fb11c 100644 --- a/vendor/github.com/json-iterator/go/reflect_json_number.go +++ b/vendor/github.com/json-iterator/go/reflect_json_number.go @@ -2,9 +2,10 @@ package jsoniter import ( "encoding/json" - "github.com/modern-go/reflect2" "strconv" "unsafe" + + "github.com/modern-go/reflect2" ) type Number string diff --git a/vendor/github.com/json-iterator/go/reflect_json_raw_message.go b/vendor/github.com/json-iterator/go/reflect_json_raw_message.go index eba434f2f16a3..521e38a89f302 100644 --- a/vendor/github.com/json-iterator/go/reflect_json_raw_message.go +++ b/vendor/github.com/json-iterator/go/reflect_json_raw_message.go @@ -2,8 +2,9 @@ package jsoniter import ( "encoding/json" - "github.com/modern-go/reflect2" "unsafe" + + "github.com/modern-go/reflect2" ) var jsonRawMessageType = reflect2.TypeOfPtr((*json.RawMessage)(nil)).Elem() diff --git a/vendor/github.com/json-iterator/go/reflect_map.go b/vendor/github.com/json-iterator/go/reflect_map.go index 5829671301353..696194bde7ef2 100644 --- a/vendor/github.com/json-iterator/go/reflect_map.go +++ b/vendor/github.com/json-iterator/go/reflect_map.go @@ -2,11 +2,12 @@ package jsoniter import ( "fmt" - "github.com/modern-go/reflect2" "io" "reflect" "sort" "unsafe" + + "github.com/modern-go/reflect2" ) func decoderOfMap(ctx *ctx, typ reflect2.Type) ValDecoder { diff --git a/vendor/github.com/json-iterator/go/reflect_optional.go b/vendor/github.com/json-iterator/go/reflect_optional.go index fa71f47489121..112c110a2932d 100644 --- a/vendor/github.com/json-iterator/go/reflect_optional.go +++ b/vendor/github.com/json-iterator/go/reflect_optional.go @@ -1,8 +1,9 @@ package jsoniter import ( - "github.com/modern-go/reflect2" "unsafe" + + "github.com/modern-go/reflect2" ) func decoderOfOptional(ctx *ctx, typ reflect2.Type) ValDecoder { diff --git a/vendor/github.com/json-iterator/go/reflect_slice.go b/vendor/github.com/json-iterator/go/reflect_slice.go index 9441d79df33b4..f363a7169b8c0 100644 --- a/vendor/github.com/json-iterator/go/reflect_slice.go +++ b/vendor/github.com/json-iterator/go/reflect_slice.go @@ -2,9 +2,10 @@ package jsoniter import ( "fmt" - "github.com/modern-go/reflect2" "io" "unsafe" + + "github.com/modern-go/reflect2" ) func decoderOfSlice(ctx *ctx, typ reflect2.Type) ValDecoder { diff --git a/vendor/github.com/json-iterator/go/reflect_struct_encoder.go b/vendor/github.com/json-iterator/go/reflect_struct_encoder.go index 152e3ef5a93c6..edf77bf597f2e 100644 --- a/vendor/github.com/json-iterator/go/reflect_struct_encoder.go +++ b/vendor/github.com/json-iterator/go/reflect_struct_encoder.go @@ -2,10 +2,11 @@ package jsoniter import ( "fmt" - "github.com/modern-go/reflect2" "io" "reflect" "unsafe" + + "github.com/modern-go/reflect2" ) func encoderOfStruct(ctx *ctx, typ reflect2.Type) ValEncoder {