From 916a1e7c1c09c62e52fe3b5d192e854dc5a0ccc3 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Mon, 12 Jul 2021 15:11:12 +0200 Subject: [PATCH 01/27] Filter instan queries and shard them. --- pkg/querier/queryrange/roundtrip.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pkg/querier/queryrange/roundtrip.go b/pkg/querier/queryrange/roundtrip.go index 400e156726250..16536f949d5c1 100644 --- a/pkg/querier/queryrange/roundtrip.go +++ b/pkg/querier/queryrange/roundtrip.go @@ -150,6 +150,8 @@ func (r roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) } return r.labels.RoundTrip(req) + case InstanteQueryOp: + return r.log.RoundTrip(req) default: return r.next.RoundTrip(req) } @@ -190,9 +192,10 @@ func validateLimits(req *http.Request, reqLimit uint32, limits Limits) error { } const ( - QueryRangeOp = "query_range" - SeriesOp = "series" - LabelNamesOp = "labels" + InstanteQueryOp = "instant_query" + QueryRangeOp = "query_range" + SeriesOp = "series" + LabelNamesOp = "labels" ) func getOperation(path string) string { @@ -203,6 +206,8 @@ func getOperation(path string) string { return SeriesOp case strings.HasSuffix(path, "/labels") || strings.HasSuffix(path, "/label"): return LabelNamesOp + case strings.HasSuffix(path, "/v1/query"): + return InstanteQueryOp default: return "" } From 63f7aec67dd4bf066b8eda99d6fc8c566329ad79 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Mon, 12 Jul 2021 17:53:41 +0200 Subject: [PATCH 02/27] [WIP] Test instant query sharding --- pkg/loghttp/query.go | 2 + pkg/querier/queryrange/codec.go | 55 ++ pkg/querier/queryrange/downstreamer.go | 12 + pkg/querier/queryrange/queryrange.pb.go | 633 ++++++++++++++++--- pkg/querier/queryrange/queryrange.proto | 9 + pkg/querier/queryrange/querysharding.go | 1 - pkg/querier/queryrange/querysharding_test.go | 49 ++ pkg/querier/queryrange/roundtrip.go | 99 ++- pkg/querier/queryrange/roundtrip_test.go | 4 + 9 files changed, 766 insertions(+), 98 deletions(-) diff --git a/pkg/loghttp/query.go b/pkg/loghttp/query.go index 90b7d40352adb..4d73b9d80e177 100644 --- a/pkg/loghttp/query.go +++ b/pkg/loghttp/query.go @@ -173,6 +173,7 @@ type InstantQuery struct { Ts time.Time Limit uint32 Direction logproto.Direction + Shards []string } // ParseInstantQuery parses an InstantQuery request from an http request. @@ -190,6 +191,7 @@ func ParseInstantQuery(r *http.Request) (*InstantQuery, error) { if err != nil { return nil, err } + request.Shards = shards(r) request.Direction, err = direction(r) if err != nil { diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index ecee3b2b0812f..d8274d5655ae0 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -75,6 +75,48 @@ func (r *LokiRequest) LogToSpan(sp opentracing.Span) { func (*LokiRequest) GetCachingOptions() (res queryrange.CachingOptions) { return } +func (r *LokiInstantRequest) GetStep() int64 { + return 0 +} + +func (r *LokiInstantRequest) GetEnd() int64 { + return r.TimeTs.UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)) +} + +func (r *LokiInstantRequest) GetStart() int64 { + return r.TimeTs.UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)) +} + +func (r *LokiInstantRequest) WithStartEnd(s int64, e int64) queryrange.Request { + new := *r + new.TimeTs = time.Unix(0, s*int64(time.Millisecond)) + return &new +} + +func (r *LokiInstantRequest) WithQuery(query string) queryrange.Request { + new := *r + new.Query = query + return &new +} + +func (r *LokiInstantRequest) WithShards(shards logql.Shards) *LokiInstantRequest { + new := *r + new.Shards = shards.Encode() + return &new +} + +func (r *LokiInstantRequest) LogToSpan(sp opentracing.Span) { + sp.LogFields( + otlog.String("query", r.GetQuery()), + otlog.String("ts", timestamp.Time(r.GetStart()).String()), + otlog.Int64("limit", int64(r.GetLimit())), + otlog.String("direction", r.GetDirection().String()), + otlog.String("shards", strings.Join(r.GetShards(), ",")), + ) +} + +func (*LokiInstantRequest) GetCachingOptions() (res queryrange.CachingOptions) { return } + func (r *LokiSeriesRequest) GetEnd() int64 { return r.EndTs.UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)) } @@ -173,6 +215,19 @@ func (Codec) DecodeRequest(_ context.Context, r *http.Request) (queryrange.Reque Path: r.URL.Path, Shards: req.Shards, }, nil + case InstantQueryOp: + req, err := loghttp.ParseInstantQuery(r) + if err != nil { + return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + } + return &LokiInstantRequest{ + Query: req.Query, + Limit: req.Limit, + Direction: req.Direction, + TimeTs: req.Ts.UTC(), + Path: r.URL.Path, + Shards: req.Shards, + }, nil case SeriesOp: req, err := logql.ParseAndValidateSeriesQuery(r) if err != nil { diff --git a/pkg/querier/queryrange/downstreamer.go b/pkg/querier/queryrange/downstreamer.go index 0b70a97c9ae13..7ee4e9b9e5b7a 100644 --- a/pkg/querier/queryrange/downstreamer.go +++ b/pkg/querier/queryrange/downstreamer.go @@ -25,6 +25,17 @@ type DownstreamHandler struct { } func ParamsToLokiRequest(params logql.Params) *LokiRequest { + if params.Start() == params.End() { + return &LokiRequest{ + Query: params.Query(), + Limit: params.Limit(), + Step: int64(params.Step() / time.Millisecond), + StartTs: params.Start(), + EndTs: params.End(), + Direction: params.Direction(), + Path: "/loki/api/v1/query", // TODO(owen-d): make this derivable + } + } return &LokiRequest{ Query: params.Query(), Limit: params.Limit(), @@ -58,6 +69,7 @@ type instance struct { func (in instance) Downstream(ctx context.Context, queries []logql.DownstreamQuery) ([]logqlmodel.Result, error) { return in.For(ctx, queries, func(qry logql.DownstreamQuery) (logqlmodel.Result, error) { + // todo handle instant queries req := ParamsToLokiRequest(qry.Params).WithShards(qry.Shards).WithQuery(qry.Expr.String()).(*LokiRequest) logger, ctx := spanlogger.New(ctx, "DownstreamHandler.instance") defer logger.Finish() diff --git a/pkg/querier/queryrange/queryrange.pb.go b/pkg/querier/queryrange/queryrange.pb.go index 893f3e51c7168..85ddd5ec4cf4f 100644 --- a/pkg/querier/queryrange/queryrange.pb.go +++ b/pkg/querier/queryrange/queryrange.pb.go @@ -132,6 +132,89 @@ func (m *LokiRequest) GetShards() []string { return nil } +type LokiInstantRequest struct { + Query string `protobuf:"bytes,1,opt,name=query,proto3" json:"query,omitempty"` + Limit uint32 `protobuf:"varint,2,opt,name=limit,proto3" json:"limit,omitempty"` + TimeTs time.Time `protobuf:"bytes,3,opt,name=timeTs,proto3,stdtime" json:"timeTs"` + Direction logproto.Direction `protobuf:"varint,4,opt,name=direction,proto3,enum=logproto.Direction" json:"direction,omitempty"` + Path string `protobuf:"bytes,5,opt,name=path,proto3" json:"path,omitempty"` + Shards []string `protobuf:"bytes,6,rep,name=shards,proto3" json:"shards"` +} + +func (m *LokiInstantRequest) Reset() { *m = LokiInstantRequest{} } +func (*LokiInstantRequest) ProtoMessage() {} +func (*LokiInstantRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_51b9d53b40d11902, []int{1} +} +func (m *LokiInstantRequest) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *LokiInstantRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_LokiInstantRequest.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalTo(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *LokiInstantRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_LokiInstantRequest.Merge(m, src) +} +func (m *LokiInstantRequest) XXX_Size() int { + return m.Size() +} +func (m *LokiInstantRequest) XXX_DiscardUnknown() { + xxx_messageInfo_LokiInstantRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_LokiInstantRequest proto.InternalMessageInfo + +func (m *LokiInstantRequest) GetQuery() string { + if m != nil { + return m.Query + } + return "" +} + +func (m *LokiInstantRequest) GetLimit() uint32 { + if m != nil { + return m.Limit + } + return 0 +} + +func (m *LokiInstantRequest) GetTimeTs() time.Time { + if m != nil { + return m.TimeTs + } + return time.Time{} +} + +func (m *LokiInstantRequest) GetDirection() logproto.Direction { + if m != nil { + return m.Direction + } + return logproto.FORWARD +} + +func (m *LokiInstantRequest) GetPath() string { + if m != nil { + return m.Path + } + return "" +} + +func (m *LokiInstantRequest) GetShards() []string { + if m != nil { + return m.Shards + } + return nil +} + type LokiResponse struct { Status string `protobuf:"bytes,1,opt,name=Status,proto3" json:"status"` Data LokiData `protobuf:"bytes,2,opt,name=Data,proto3" json:"data,omitempty"` @@ -147,7 +230,7 @@ type LokiResponse struct { func (m *LokiResponse) Reset() { *m = LokiResponse{} } func (*LokiResponse) ProtoMessage() {} func (*LokiResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_51b9d53b40d11902, []int{1} + return fileDescriptor_51b9d53b40d11902, []int{2} } func (m *LokiResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -243,7 +326,7 @@ type LokiSeriesRequest struct { func (m *LokiSeriesRequest) Reset() { *m = LokiSeriesRequest{} } func (*LokiSeriesRequest) ProtoMessage() {} func (*LokiSeriesRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_51b9d53b40d11902, []int{2} + return fileDescriptor_51b9d53b40d11902, []int{3} } func (m *LokiSeriesRequest) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -317,7 +400,7 @@ type LokiSeriesResponse struct { func (m *LokiSeriesResponse) Reset() { *m = LokiSeriesResponse{} } func (*LokiSeriesResponse) ProtoMessage() {} func (*LokiSeriesResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_51b9d53b40d11902, []int{3} + return fileDescriptor_51b9d53b40d11902, []int{4} } func (m *LokiSeriesResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -376,7 +459,7 @@ type LokiLabelNamesRequest struct { func (m *LokiLabelNamesRequest) Reset() { *m = LokiLabelNamesRequest{} } func (*LokiLabelNamesRequest) ProtoMessage() {} func (*LokiLabelNamesRequest) Descriptor() ([]byte, []int) { - return fileDescriptor_51b9d53b40d11902, []int{4} + return fileDescriptor_51b9d53b40d11902, []int{5} } func (m *LokiLabelNamesRequest) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -436,7 +519,7 @@ type LokiLabelNamesResponse struct { func (m *LokiLabelNamesResponse) Reset() { *m = LokiLabelNamesResponse{} } func (*LokiLabelNamesResponse) ProtoMessage() {} func (*LokiLabelNamesResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_51b9d53b40d11902, []int{5} + return fileDescriptor_51b9d53b40d11902, []int{6} } func (m *LokiLabelNamesResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -494,7 +577,7 @@ type LokiData struct { func (m *LokiData) Reset() { *m = LokiData{} } func (*LokiData) ProtoMessage() {} func (*LokiData) Descriptor() ([]byte, []int) { - return fileDescriptor_51b9d53b40d11902, []int{6} + return fileDescriptor_51b9d53b40d11902, []int{7} } func (m *LokiData) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -539,7 +622,7 @@ type LokiPromResponse struct { func (m *LokiPromResponse) Reset() { *m = LokiPromResponse{} } func (*LokiPromResponse) ProtoMessage() {} func (*LokiPromResponse) Descriptor() ([]byte, []int) { - return fileDescriptor_51b9d53b40d11902, []int{7} + return fileDescriptor_51b9d53b40d11902, []int{8} } func (m *LokiPromResponse) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -584,6 +667,7 @@ func (m *LokiPromResponse) GetStatistics() stats.Result { func init() { proto.RegisterType((*LokiRequest)(nil), "queryrange.LokiRequest") + proto.RegisterType((*LokiInstantRequest)(nil), "queryrange.LokiInstantRequest") proto.RegisterType((*LokiResponse)(nil), "queryrange.LokiResponse") proto.RegisterType((*LokiSeriesRequest)(nil), "queryrange.LokiSeriesRequest") proto.RegisterType((*LokiSeriesResponse)(nil), "queryrange.LokiSeriesResponse") @@ -598,62 +682,65 @@ func init() { } var fileDescriptor_51b9d53b40d11902 = []byte{ - // 874 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xdc, 0x56, 0x4f, 0x6f, 0xdc, 0x44, - 0x14, 0xdf, 0x59, 0xef, 0x9f, 0x78, 0x42, 0x03, 0x4c, 0x4a, 0x6b, 0x05, 0xc9, 0xb6, 0x56, 0x08, - 0x16, 0x41, 0xbd, 0x22, 0x85, 0x0b, 0x12, 0xa8, 0xb5, 0xca, 0x3f, 0xa9, 0x02, 0x34, 0xcd, 0x81, - 0xeb, 0x64, 0x3d, 0xf1, 0x9a, 0xd8, 0x3b, 0xce, 0xcc, 0x2c, 0x22, 0x37, 0xae, 0xdc, 0x7a, 0x03, - 0x3e, 0x01, 0x88, 0x33, 0x7c, 0x87, 0x1c, 0x73, 0xac, 0x2a, 0x61, 0xc8, 0xe6, 0x82, 0xf6, 0xd4, - 0x8f, 0x80, 0x66, 0xc6, 0xde, 0x9d, 0xa0, 0x16, 0xba, 0xcd, 0x05, 0x71, 0xd9, 0x7d, 0xef, 0xcd, - 0x7b, 0x33, 0xef, 0xfd, 0xde, 0xef, 0x3d, 0x19, 0xbe, 0x56, 0x1e, 0xa6, 0xa3, 0xa3, 0x19, 0xe5, - 0x19, 0xe5, 0xfa, 0xff, 0x98, 0x93, 0x69, 0x4a, 0x2d, 0x31, 0x2a, 0x39, 0x93, 0x0c, 0xc1, 0x95, - 0x65, 0xe7, 0x46, 0x9a, 0xc9, 0xc9, 0x6c, 0x3f, 0x1a, 0xb3, 0x62, 0x94, 0xb2, 0x94, 0x8d, 0xb4, - 0xcb, 0xfe, 0xec, 0x40, 0x6b, 0x5a, 0xd1, 0x92, 0x09, 0xdd, 0x79, 0x59, 0xbd, 0x91, 0xb3, 0xd4, - 0x1c, 0x34, 0x42, 0x7d, 0x18, 0xd6, 0x87, 0x47, 0x79, 0xc1, 0x12, 0x9a, 0x8f, 0x84, 0x24, 0x52, - 0x98, 0xdf, 0xda, 0xe3, 0x23, 0xe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c, 0x4e, 0x48, 0x29, 0x29, 0x8f, 0xdf, - 0x53, 0x6d, 0x7a, 0x58, 0x05, 0x6f, 0xd8, 0x1f, 0x1d, 0x9c, 0x1c, 0x90, 0x29, 0x19, 0xe5, 0xec, - 0x30, 0x1b, 0xd9, 0x5f, 0x17, 0x75, 0xac, 0xea, 0x84, 0x79, 0x17, 0xd7, 0xaf, 0x0c, 0xbe, 0x05, - 0xf0, 0x05, 0x95, 0xac, 0xaa, 0x6d, 0xd9, 0xc2, 0x5b, 0x70, 0x83, 0xd7, 0x72, 0x4d, 0x37, 0xff, - 0x9f, 0xc1, 0x8d, 0x3b, 0x27, 0x55, 0x00, 0xf0, 0x32, 0x0a, 0xdd, 0xbc, 0xb0, 0xbf, 0xdb, 0x8f, - 0xdb, 0xdf, 0x2a, 0xa4, 0x65, 0x6f, 0xec, 0xf8, 0xed, 0xd3, 0x33, 0xbf, 0xf5, 0xe0, 0xcc, 0x6f, - 0x3d, 0x3a, 0xf3, 0xc1, 0x37, 0x73, 0x1f, 0xfc, 0x34, 0xf7, 0xc1, 0xc9, 0xdc, 0x07, 0xa7, 0x73, - 0x1f, 0xfc, 0x31, 0xf7, 0xc1, 0x9f, 0x73, 0xbf, 0xf5, 0x68, 0xee, 0x83, 0xfb, 0xe7, 0x7e, 0xeb, - 0xf4, 0xdc, 0x6f, 0x3d, 0x38, 0xf7, 0x5b, 0xfb, 0x3d, 0x5d, 0xe1, 0xcd, 0xbf, 0x02, 0x00, 0x00, - 0xff, 0xff, 0xb2, 0xbc, 0xe5, 0x40, 0xb3, 0x09, 0x00, 0x00, + // 913 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xdc, 0x56, 0x4f, 0x6f, 0x23, 0x35, + 0x14, 0x8f, 0xf3, 0xb7, 0x71, 0xd9, 0x02, 0xee, 0xb2, 0x3b, 0x2a, 0xd2, 0x4c, 0x14, 0x21, 0x08, + 0x82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, 0xb5, + 0x7d, 0x2e, 0xd6, 0xef, 0x9a, 0xf0, 0x39, 0x23, 0x11, 0x91, 0xb3, 0x4c, 0x50, 0x95, 0xf4, 0x50, + 0x12, 0x39, 0x15, 0x06, 0x66, 0x99, 0xa4, 0x3d, 0xb8, 0xdc, 0x41, 0x77, 0x60, 0xf3, 0x1e, 0x91, + 0x44, 0x43, 0x5e, 0xdf, 0xbe, 0xee, 0x5b, 0xca, 0x54, 0x67, 0xa9, 0xbd, 0xe0, 0x86, 0x42, 0x35, + 0x2f, 0xbc, 0x8d, 0x90, 0x48, 0xf2, 0x26, 0x4b, 0x63, 0x49, 0xd3, 0x5c, 0x1e, 0x60, 0x9d, 0x89, + 0xde, 0x81, 0xdd, 0x0f, 0x39, 0x67, 0x7c, 0x74, 0x90, 0x53, 0x4d, 0x51, 0x37, 0xb8, 0x39, 0x2f, + 0xbc, 0x4d, 0x5a, 0x39, 0xad, 0x8c, 0x65, 0x24, 0x7a, 0x1d, 0xb6, 0xb4, 0xa1, 0x49, 0xe9, 0x06, + 0x9b, 0xf3, 0xc2, 0x7b, 0x5e, 0xa7, 0x58, 0xe1, 0x26, 0xe2, 0x34, 0x87, 0xad, 0x0b, 0x71, 0xb8, + 0x68, 0x65, 0xdb, 0x6e, 0xa5, 0x03, 0x3b, 0x5f, 0x51, 0x2e, 0xd4, 0x31, 0x1d, 0xed, 0xaf, 0x4c, + 0x74, 0x17, 0x42, 0x45, 0x4c, 0x2c, 0x64, 0x3c, 0x56, 0x7a, 0x52, 0x64, 0x5c, 0xf3, 0xcd, 0x53, + 0xc7, 0x54, 0x4c, 0x13, 0x19, 0xa0, 0x92, 0x05, 0x2b, 0x10, 0x5b, 0x6b, 0xf4, 0x3d, 0x80, 0x9d, + 0x4f, 0x28, 0x09, 0x29, 0x17, 0x4e, 0xb7, 0xd7, 0x18, 0xac, 0x6f, 0xbf, 0x62, 0xb3, 0xf9, 0x39, + 0x67, 0x29, 0x95, 0x13, 0x3a, 0x15, 0x55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x24, 0xa7, 0x24, + 0xbd, 0x1b, 0x92, 0x5c, 0x52, 0x1e, 0xbc, 0xaf, 0xda, 0xf4, 0xa4, 0xf0, 0xde, 0xb0, 0x3f, 0xb0, + 0x38, 0xd9, 0x25, 0x19, 0x19, 0x26, 0x6c, 0x2f, 0x1e, 0xda, 0x5f, 0x52, 0x65, 0xae, 0xea, 0x84, + 0xb9, 0x17, 0x97, 0xb7, 0xf4, 0xbf, 0x05, 0xf0, 0x05, 0x55, 0xac, 0xc2, 0xb6, 0x68, 0xe1, 0x1d, + 0xb8, 0xc6, 0xcb, 0x75, 0x29, 0x37, 0xf7, 0xdf, 0xc9, 0x0d, 0x9a, 0x87, 0x85, 0x07, 0xf0, 0x22, + 0x0b, 0xdd, 0x3e, 0x35, 0xbf, 0xeb, 0x67, 0xcd, 0x6f, 0x95, 0x52, 0xb3, 0x27, 0x76, 0xf0, 0xf6, + 0xd1, 0xb1, 0x5b, 0x7b, 0x7c, 0xec, 0xd6, 0x9e, 0x1e, 0xbb, 0xe0, 0x9b, 0x99, 0x0b, 0x7e, 0x9a, + 0xb9, 0xe0, 0x70, 0xe6, 0x82, 0xa3, 0x99, 0x0b, 0xfe, 0x98, 0xb9, 0xe0, 0xcf, 0x99, 0x5b, 0x7b, + 0x3a, 0x73, 0xc1, 0xa3, 0x13, 0xb7, 0x76, 0x74, 0xe2, 0xd6, 0x1e, 0x9f, 0xb8, 0xb5, 0x9d, 0xb6, + 0x46, 0x78, 0xfb, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0x52, 0x92, 0xbf, 0x59, 0x9f, 0x0a, 0x00, + 0x00, } func (this *LokiRequest) Equal(that interface{}) bool { @@ -706,6 +793,50 @@ func (this *LokiRequest) Equal(that interface{}) bool { } return true } +func (this *LokiInstantRequest) Equal(that interface{}) bool { + if that == nil { + return this == nil + } + + that1, ok := that.(*LokiInstantRequest) + if !ok { + that2, ok := that.(LokiInstantRequest) + if ok { + that1 = &that2 + } else { + return false + } + } + if that1 == nil { + return this == nil + } else if this == nil { + return false + } + if this.Query != that1.Query { + return false + } + if this.Limit != that1.Limit { + return false + } + if !this.TimeTs.Equal(that1.TimeTs) { + return false + } + if this.Direction != that1.Direction { + return false + } + if this.Path != that1.Path { + return false + } + if len(this.Shards) != len(that1.Shards) { + return false + } + for i := range this.Shards { + if this.Shards[i] != that1.Shards[i] { + return false + } + } + return true +} func (this *LokiResponse) Equal(that interface{}) bool { if that == nil { return this == nil @@ -997,6 +1128,21 @@ func (this *LokiRequest) GoString() string { s = append(s, "}") return strings.Join(s, "") } +func (this *LokiInstantRequest) GoString() string { + if this == nil { + return "nil" + } + s := make([]string, 0, 10) + s = append(s, "&queryrange.LokiInstantRequest{") + s = append(s, "Query: "+fmt.Sprintf("%#v", this.Query)+",\n") + s = append(s, "Limit: "+fmt.Sprintf("%#v", this.Limit)+",\n") + s = append(s, "TimeTs: "+fmt.Sprintf("%#v", this.TimeTs)+",\n") + s = append(s, "Direction: "+fmt.Sprintf("%#v", this.Direction)+",\n") + s = append(s, "Path: "+fmt.Sprintf("%#v", this.Path)+",\n") + s = append(s, "Shards: "+fmt.Sprintf("%#v", this.Shards)+",\n") + s = append(s, "}") + return strings.Join(s, "") +} func (this *LokiResponse) GoString() string { if this == nil { return "nil" @@ -1181,6 +1327,69 @@ func (m *LokiRequest) MarshalTo(dAtA []byte) (int, error) { return i, nil } +func (m *LokiInstantRequest) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalTo(dAtA) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *LokiInstantRequest) MarshalTo(dAtA []byte) (int, error) { + var i int + _ = i + var l int + _ = l + if len(m.Query) > 0 { + dAtA[i] = 0xa + i++ + i = encodeVarintQueryrange(dAtA, i, uint64(len(m.Query))) + i += copy(dAtA[i:], m.Query) + } + if m.Limit != 0 { + dAtA[i] = 0x10 + i++ + i = encodeVarintQueryrange(dAtA, i, uint64(m.Limit)) + } + dAtA[i] = 0x1a + i++ + i = encodeVarintQueryrange(dAtA, i, uint64(github_com_gogo_protobuf_types.SizeOfStdTime(m.TimeTs))) + n3, err := github_com_gogo_protobuf_types.StdTimeMarshalTo(m.TimeTs, dAtA[i:]) + if err != nil { + return 0, err + } + i += n3 + if m.Direction != 0 { + dAtA[i] = 0x20 + i++ + i = encodeVarintQueryrange(dAtA, i, uint64(m.Direction)) + } + if len(m.Path) > 0 { + dAtA[i] = 0x2a + i++ + i = encodeVarintQueryrange(dAtA, i, uint64(len(m.Path))) + i += copy(dAtA[i:], m.Path) + } + if len(m.Shards) > 0 { + for _, s := range m.Shards { + dAtA[i] = 0x32 + i++ + l = len(s) + for l >= 1<<7 { + dAtA[i] = uint8(uint64(l)&0x7f | 0x80) + l >>= 7 + i++ + } + dAtA[i] = uint8(l) + i++ + i += copy(dAtA[i:], s) + } + } + return i, nil +} + func (m *LokiResponse) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -1205,11 +1414,11 @@ func (m *LokiResponse) MarshalTo(dAtA []byte) (int, error) { dAtA[i] = 0x12 i++ i = encodeVarintQueryrange(dAtA, i, uint64(m.Data.Size())) - n3, err := m.Data.MarshalTo(dAtA[i:]) + n4, err := m.Data.MarshalTo(dAtA[i:]) if err != nil { return 0, err } - i += n3 + i += n4 if len(m.ErrorType) > 0 { dAtA[i] = 0x1a i++ @@ -1240,11 +1449,11 @@ func (m *LokiResponse) MarshalTo(dAtA []byte) (int, error) { dAtA[i] = 0x42 i++ i = encodeVarintQueryrange(dAtA, i, uint64(m.Statistics.Size())) - n4, err := m.Statistics.MarshalTo(dAtA[i:]) + n5, err := m.Statistics.MarshalTo(dAtA[i:]) if err != nil { return 0, err } - i += n4 + i += n5 if len(m.Headers) > 0 { for _, msg := range m.Headers { dAtA[i] = 0x4a @@ -1293,19 +1502,19 @@ func (m *LokiSeriesRequest) MarshalTo(dAtA []byte) (int, error) { dAtA[i] = 0x12 i++ i = encodeVarintQueryrange(dAtA, i, uint64(github_com_gogo_protobuf_types.SizeOfStdTime(m.StartTs))) - n5, err := github_com_gogo_protobuf_types.StdTimeMarshalTo(m.StartTs, dAtA[i:]) + n6, err := github_com_gogo_protobuf_types.StdTimeMarshalTo(m.StartTs, dAtA[i:]) if err != nil { return 0, err } - i += n5 + i += n6 dAtA[i] = 0x1a i++ i = encodeVarintQueryrange(dAtA, i, uint64(github_com_gogo_protobuf_types.SizeOfStdTime(m.EndTs))) - n6, err := github_com_gogo_protobuf_types.StdTimeMarshalTo(m.EndTs, dAtA[i:]) + n7, err := github_com_gogo_protobuf_types.StdTimeMarshalTo(m.EndTs, dAtA[i:]) if err != nil { return 0, err } - i += n6 + i += n7 if len(m.Path) > 0 { dAtA[i] = 0x22 i++ @@ -1401,19 +1610,19 @@ func (m *LokiLabelNamesRequest) MarshalTo(dAtA []byte) (int, error) { dAtA[i] = 0xa i++ i = encodeVarintQueryrange(dAtA, i, uint64(github_com_gogo_protobuf_types.SizeOfStdTime(m.StartTs))) - n7, err := github_com_gogo_protobuf_types.StdTimeMarshalTo(m.StartTs, dAtA[i:]) + n8, err := github_com_gogo_protobuf_types.StdTimeMarshalTo(m.StartTs, dAtA[i:]) if err != nil { return 0, err } - i += n7 + i += n8 dAtA[i] = 0x12 i++ i = encodeVarintQueryrange(dAtA, i, uint64(github_com_gogo_protobuf_types.SizeOfStdTime(m.EndTs))) - n8, err := github_com_gogo_protobuf_types.StdTimeMarshalTo(m.EndTs, dAtA[i:]) + n9, err := github_com_gogo_protobuf_types.StdTimeMarshalTo(m.EndTs, dAtA[i:]) if err != nil { return 0, err } - i += n8 + i += n9 if len(m.Path) > 0 { dAtA[i] = 0x1a i++ @@ -1534,20 +1743,20 @@ func (m *LokiPromResponse) MarshalTo(dAtA []byte) (int, error) { dAtA[i] = 0xa i++ i = encodeVarintQueryrange(dAtA, i, uint64(m.Response.Size())) - n9, err := m.Response.MarshalTo(dAtA[i:]) + n10, err := m.Response.MarshalTo(dAtA[i:]) if err != nil { return 0, err } - i += n9 + i += n10 } dAtA[i] = 0x12 i++ i = encodeVarintQueryrange(dAtA, i, uint64(m.Statistics.Size())) - n10, err := m.Statistics.MarshalTo(dAtA[i:]) + n11, err := m.Statistics.MarshalTo(dAtA[i:]) if err != nil { return 0, err } - i += n10 + i += n11 return i, nil } @@ -1596,6 +1805,37 @@ func (m *LokiRequest) Size() (n int) { return n } +func (m *LokiInstantRequest) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Query) + if l > 0 { + n += 1 + l + sovQueryrange(uint64(l)) + } + if m.Limit != 0 { + n += 1 + sovQueryrange(uint64(m.Limit)) + } + l = github_com_gogo_protobuf_types.SizeOfStdTime(m.TimeTs) + n += 1 + l + sovQueryrange(uint64(l)) + if m.Direction != 0 { + n += 1 + sovQueryrange(uint64(m.Direction)) + } + l = len(m.Path) + if l > 0 { + n += 1 + l + sovQueryrange(uint64(l)) + } + if len(m.Shards) > 0 { + for _, s := range m.Shards { + l = len(s) + n += 1 + l + sovQueryrange(uint64(l)) + } + } + return n +} + func (m *LokiResponse) Size() (n int) { if m == nil { return 0 @@ -1802,6 +2042,21 @@ func (this *LokiRequest) String() string { }, "") return s } +func (this *LokiInstantRequest) String() string { + if this == nil { + return "nil" + } + s := strings.Join([]string{`&LokiInstantRequest{`, + `Query:` + fmt.Sprintf("%v", this.Query) + `,`, + `Limit:` + fmt.Sprintf("%v", this.Limit) + `,`, + `TimeTs:` + strings.Replace(strings.Replace(this.TimeTs.String(), "Timestamp", "types.Timestamp", 1), `&`, ``, 1) + `,`, + `Direction:` + fmt.Sprintf("%v", this.Direction) + `,`, + `Path:` + fmt.Sprintf("%v", this.Path) + `,`, + `Shards:` + fmt.Sprintf("%v", this.Shards) + `,`, + `}`, + }, "") + return s +} func (this *LokiResponse) String() string { if this == nil { return "nil" @@ -2174,6 +2429,226 @@ func (m *LokiRequest) Unmarshal(dAtA []byte) error { } return nil } +func (m *LokiInstantRequest) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQueryrange + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: LokiInstantRequest: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: LokiInstantRequest: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Query", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQueryrange + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthQueryrange + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQueryrange + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Query = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Limit", wireType) + } + m.Limit = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQueryrange + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Limit |= uint32(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field TimeTs", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQueryrange + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthQueryrange + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthQueryrange + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := github_com_gogo_protobuf_types.StdTimeUnmarshal(&m.TimeTs, dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 4: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Direction", wireType) + } + m.Direction = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQueryrange + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Direction |= logproto.Direction(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 5: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Path", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQueryrange + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthQueryrange + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQueryrange + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Path = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 6: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Shards", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowQueryrange + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthQueryrange + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthQueryrange + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Shards = append(m.Shards, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipQueryrange(dAtA[iNdEx:]) + if err != nil { + return err + } + if skippy < 0 { + return ErrInvalidLengthQueryrange + } + if (iNdEx + skippy) < 0 { + return ErrInvalidLengthQueryrange + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func (m *LokiResponse) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 diff --git a/pkg/querier/queryrange/queryrange.proto b/pkg/querier/queryrange/queryrange.proto index 3513454867de3..88394b9ce2faa 100644 --- a/pkg/querier/queryrange/queryrange.proto +++ b/pkg/querier/queryrange/queryrange.proto @@ -22,6 +22,15 @@ message LokiRequest { repeated string shards = 8 [(gogoproto.jsontag) = "shards"]; } +message LokiInstantRequest { + string query = 1; + uint32 limit = 2; + google.protobuf.Timestamp timeTs = 3 [(gogoproto.stdtime) = true, (gogoproto.nullable) = false]; + logproto.Direction direction = 4; + string path = 5; + repeated string shards = 6 [(gogoproto.jsontag) = "shards"]; +} + message LokiResponse { string Status = 1 [(gogoproto.jsontag) = "status"]; LokiData Data = 2 [(gogoproto.nullable) = false, (gogoproto.jsontag) = "data,omitempty"]; diff --git a/pkg/querier/queryrange/querysharding.go b/pkg/querier/queryrange/querysharding.go index e4d5d6e09f8f4..c6fdc2b612c3f 100644 --- a/pkg/querier/queryrange/querysharding.go +++ b/pkg/querier/queryrange/querysharding.go @@ -23,7 +23,6 @@ import ( func NewQueryShardMiddleware( logger log.Logger, confs queryrange.ShardingConfigs, - minShardingLookback time.Duration, middlewareMetrics *queryrange.InstrumentMiddlewareMetrics, shardingMetrics *logql.ShardingMetrics, limits logql.Limits, diff --git a/pkg/querier/queryrange/querysharding_test.go b/pkg/querier/queryrange/querysharding_test.go index 5346dba795988..5205029f4c126 100644 --- a/pkg/querier/queryrange/querysharding_test.go +++ b/pkg/querier/queryrange/querysharding_test.go @@ -11,6 +11,7 @@ import ( "time" "github.com/cortexproject/cortex/pkg/chunk" + "github.com/cortexproject/cortex/pkg/cortexpb" "github.com/cortexproject/cortex/pkg/querier/queryrange" "github.com/cortexproject/cortex/pkg/util" "github.com/go-kit/kit/log" @@ -234,6 +235,54 @@ func mockHandler(resp queryrange.Response, err error) queryrange.Handler { }) } +func Test_InstantSharding(t *testing.T) { + ctx := user.InjectOrgID(context.Background(), "1") + + sharding := NewQueryShardMiddleware(log.NewNopLogger(), queryrange.ShardingConfigs{ + chunk.PeriodConfig{ + RowShards: 3, + }, + }, queryrange.NewInstrumentMiddlewareMetrics(nil), + nilShardingMetrics, + fakeLimits{ + maxQueryParallelism: 10, + }) + response, err := sharding.Wrap(queryrange.HandlerFunc(func(c context.Context, r queryrange.Request) (queryrange.Response, error) { + t.Log(r) + return &LokiPromResponse{Response: &queryrange.PrometheusResponse{ + Data: queryrange.PrometheusData{ + ResultType: loghttp.ResultTypeVector, + Result: []queryrange.SampleStream{ + { + Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "bar"}}, + Samples: []cortexpb.Sample{{Value: 10, TimestampMs: 10}}, + }, + }, + }, + }}, nil + })).Do(ctx, &LokiInstantRequest{ + Query: `rate({app="foo"}[1m])`, + TimeTs: time.Now(), + Path: "/v1/query", + }) + require.Nil(t, err) + require.Equal(t, &LokiPromResponse{Response: &queryrange.PrometheusResponse{ + Data: queryrange.PrometheusData{ + ResultType: loghttp.ResultTypeVector, + Result: []queryrange.SampleStream{ + { + Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "bar"}}, + Samples: []cortexpb.Sample{{Value: 10, TimestampMs: 10}}, + }, + { + Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "buzz"}}, + Samples: []cortexpb.Sample{{Value: 10, TimestampMs: 10}}, + }, + }, + }, + }}, response) +} + func Test_SeriesShardingHandler(t *testing.T) { sharding := NewSeriesQueryShardMiddleware(log.NewNopLogger(), queryrange.ShardingConfigs{ chunk.PeriodConfig{ diff --git a/pkg/querier/queryrange/roundtrip.go b/pkg/querier/queryrange/roundtrip.go index 16536f949d5c1..d35556f5f41d7 100644 --- a/pkg/querier/queryrange/roundtrip.go +++ b/pkg/querier/queryrange/roundtrip.go @@ -76,30 +76,36 @@ func NewTripperware( return nil, nil, err } + instantMetricTripperware, err := NewInstantMetricTripperware(cfg, log, limits, schema, LokiCodec, instrumentMetrics, retryMetrics, shardingMetrics, splitByMetrics) + if err != nil { + return nil, nil, err + } return func(next http.RoundTripper) http.RoundTripper { metricRT := metricsTripperware(next) logFilterRT := logFilterTripperware(next) seriesRT := seriesTripperware(next) labelsRT := labelsTripperware(next) - return newRoundTripper(next, logFilterRT, metricRT, seriesRT, labelsRT, limits) + instantRT := instantMetricTripperware(next) + return newRoundTripper(next, logFilterRT, metricRT, seriesRT, labelsRT, instantRT, limits) }, cache, nil } type roundTripper struct { - next, log, metric, series, labels http.RoundTripper + next, log, metric, series, labels, instantMetrics http.RoundTripper limits Limits } // newRoundTripper creates a new queryrange roundtripper -func newRoundTripper(next, log, metric, series, labels http.RoundTripper, limits Limits) roundTripper { +func newRoundTripper(next, log, metric, series, labels, instantMetrics http.RoundTripper, limits Limits) roundTripper { return roundTripper{ - log: log, - limits: limits, - metric: metric, - series: series, - labels: labels, - next: next, + log: log, + limits: limits, + metric: metric, + series: series, + labels: labels, + instantMetrics: instantMetrics, + next: next, } } @@ -150,8 +156,21 @@ func (r roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) } return r.labels.RoundTrip(req) - case InstanteQueryOp: - return r.log.RoundTrip(req) + case InstantQueryOp: + instantQuery, err := loghttp.ParseInstantQuery(req) + if err != nil { + return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + } + expr, err := logql.ParseExpr(instantQuery.Query) + if err != nil { + return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) + } + switch expr.(type) { + case logql.SampleExpr: + return r.instantMetrics.RoundTrip(req) + default: + return r.next.RoundTrip(req) + } default: return r.next.RoundTrip(req) } @@ -192,10 +211,10 @@ func validateLimits(req *http.Request, reqLimit uint32, limits Limits) error { } const ( - InstanteQueryOp = "instant_query" - QueryRangeOp = "query_range" - SeriesOp = "series" - LabelNamesOp = "labels" + InstantQueryOp = "instant_query" + QueryRangeOp = "query_range" + SeriesOp = "series" + LabelNamesOp = "labels" ) func getOperation(path string) string { @@ -207,7 +226,7 @@ func getOperation(path string) string { case strings.HasSuffix(path, "/labels") || strings.HasSuffix(path, "/label"): return LabelNamesOp case strings.HasSuffix(path, "/v1/query"): - return InstanteQueryOp + return InstantQueryOp default: return "" } @@ -239,7 +258,6 @@ func NewLogFilterTripperware( NewQueryShardMiddleware( log, schema.Configs, - minShardingLookback, instrumentMetrics, // instrumentation is included in the sharding middleware shardingMetrics, limits, @@ -401,7 +419,6 @@ func NewMetricTripperware( NewQueryShardMiddleware( log, schema.Configs, - minShardingLookback, instrumentMetrics, // instrumentation is included in the sharding middleware shardingMetrics, limits, @@ -431,3 +448,49 @@ func NewMetricTripperware( return next }, c, nil } + +// NewInstantMetricTripperware creates a new frontend tripperware responsible for handling metric queries +func NewInstantMetricTripperware( + cfg Config, + log log.Logger, + limits Limits, + schema chunk.SchemaConfig, + codec queryrange.Codec, + instrumentMetrics *queryrange.InstrumentMiddlewareMetrics, + retryMiddlewareMetrics *queryrange.RetryMiddlewareMetrics, + shardingMetrics *logql.ShardingMetrics, + splitByMetrics *SplitByMetrics, +) (queryrange.Tripperware, error) { + queryRangeMiddleware := []queryrange.Middleware{StatsCollectorMiddleware(), nil} + + if cfg.CacheResults { + // think about caching + } + + if cfg.ShardedQueries { + queryRangeMiddleware = append(queryRangeMiddleware, + NewQueryShardMiddleware( + log, + schema.Configs, + instrumentMetrics, // instrumentation is included in the sharding middleware + shardingMetrics, + limits, + ), + ) + } + + if cfg.MaxRetries > 0 { + queryRangeMiddleware = append( + queryRangeMiddleware, + queryrange.InstrumentMiddleware("retry", instrumentMetrics), + queryrange.NewRetryMiddleware(log, cfg.MaxRetries, retryMiddlewareMetrics), + ) + } + + return func(next http.RoundTripper) http.RoundTripper { + if len(queryRangeMiddleware) > 0 { + return NewLimitedRoundTripper(next, codec, limits, queryRangeMiddleware...) + } + return next + }, nil +} diff --git a/pkg/querier/queryrange/roundtrip_test.go b/pkg/querier/queryrange/roundtrip_test.go index a9bc1a9e58e65..ad46637f4e242 100644 --- a/pkg/querier/queryrange/roundtrip_test.go +++ b/pkg/querier/queryrange/roundtrip_test.go @@ -423,6 +423,10 @@ func TestPostQueries(t *testing.T) { t.Error("unexpected labels roundtripper called") return nil, nil }), + queryrange.RoundTripFunc(func(*http.Request) (*http.Response, error) { + t.Error("unexpected labels roundtripper called") + return nil, nil + }), fakeLimits{}, ).RoundTrip(req) require.NoError(t, err) From b7a7a01ec23a45ce72bf6da6405465806e6557c7 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Mon, 12 Jul 2021 19:44:43 +0200 Subject: [PATCH 03/27] Trace casting error. --- pkg/querier/queryrange/codec.go | 12 ++++++------ pkg/querier/queryrange/querysharding.go | 1 + 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index d8274d5655ae0..9cee1da5dc66a 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -707,27 +707,27 @@ func paramsFromRequest(req queryrange.Request) *paramsWrapper { } func (p paramsWrapper) Query() string { - return p.LokiRequest.Query + return p.GetQuery() } func (p paramsWrapper) Start() time.Time { - return p.StartTs + return p.GetStartTs() } func (p paramsWrapper) End() time.Time { - return p.EndTs + return p.GetEndTs() } func (p paramsWrapper) Step() time.Duration { - return time.Duration(p.LokiRequest.Step * 1e6) + return time.Duration(p.GetStep() * 1e6) } func (p paramsWrapper) Interval() time.Duration { return 0 } func (p paramsWrapper) Direction() logproto.Direction { - return p.LokiRequest.Direction + return p.GetDirection() } func (p paramsWrapper) Limit() uint32 { return p.LokiRequest.Limit } func (p paramsWrapper) Shards() []string { - return p.LokiRequest.Shards + return p.GetShards() } func httpResponseHeadersToPromResponseHeaders(httpHeaders http.Header) []queryrange.PrometheusResponseHeader { diff --git a/pkg/querier/queryrange/querysharding.go b/pkg/querier/queryrange/querysharding.go index c6fdc2b612c3f..1dc1845a3fb66 100644 --- a/pkg/querier/queryrange/querysharding.go +++ b/pkg/querier/queryrange/querysharding.go @@ -87,6 +87,7 @@ func (ast *astMapperware) Do(ctx context.Context, r queryrange.Request) (queryra shardedLog, ctx := spanlogger.New(ctx, "shardedEngine") defer shardedLog.Finish() + // todo suppoer LokiInstantRequest as well req, ok := r.(*LokiRequest) if !ok { return nil, fmt.Errorf("expected *LokiRequest, got (%T)", r) From f6a8f29e85c25e885a6494b58af2d570ccd0c081 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 13 Jul 2021 10:42:30 +0200 Subject: [PATCH 04/27] WRap LokiInstantRequest for Params interface. --- pkg/querier/queryrange/codec.go | 34 +++++++++++++++++++++++++ pkg/querier/queryrange/querysharding.go | 26 +++++++++++-------- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index 9cee1da5dc66a..d1d1676803223 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -730,6 +730,40 @@ func (p paramsWrapper) Shards() []string { return p.GetShards() } +type paramsInstantWrapper struct { + *LokiInstantRequest +} + +func paramsFromInstantRequest(req queryrange.Request) *paramsInstantWrapper { + return ¶msInstantWrapper{ + LokiInstantRequest: req.(*LokiInstantRequest), + } +} + +func (p paramsInstantWrapper) Query() string { + return p.GetQuery() +} + +func (p paramsInstantWrapper) Start() time.Time { + return p.LokiInstantRequest.GetTimeTs() +} + +func (p paramsInstantWrapper) End() time.Time { + return p.LokiInstantRequest.GetTimeTs() +} + +func (p paramsInstantWrapper) Step() time.Duration { + return time.Duration(p.GetStep() * 1e6) +} +func (p paramsInstantWrapper) Interval() time.Duration { return 0 } +func (p paramsInstantWrapper) Direction() logproto.Direction { + return p.GetDirection() +} +func (p paramsInstantWrapper) Limit() uint32 { return p.LokiInstantRequest.Limit } +func (p paramsInstantWrapper) Shards() []string { + return p.GetShards() +} + func httpResponseHeadersToPromResponseHeaders(httpHeaders http.Header) []queryrange.PrometheusResponseHeader { var promHeaders []queryrange.PrometheusResponseHeader for h, hv := range httpHeaders { diff --git a/pkg/querier/queryrange/querysharding.go b/pkg/querier/queryrange/querysharding.go index 1dc1845a3fb66..bb39d58609bff 100644 --- a/pkg/querier/queryrange/querysharding.go +++ b/pkg/querier/queryrange/querysharding.go @@ -87,12 +87,6 @@ func (ast *astMapperware) Do(ctx context.Context, r queryrange.Request) (queryra shardedLog, ctx := spanlogger.New(ctx, "shardedEngine") defer shardedLog.Finish() - // todo suppoer LokiInstantRequest as well - req, ok := r.(*LokiRequest) - if !ok { - return nil, fmt.Errorf("expected *LokiRequest, got (%T)", r) - } - mapper, err := logql.NewShardMapper(int(conf.RowShards), ast.metrics) if err != nil { return nil, err @@ -111,7 +105,19 @@ func (ast *astMapperware) Do(ctx context.Context, r queryrange.Request) (queryra return ast.next.Do(ctx, r) } - query := ast.ng.Query(paramsFromRequest(req), parsed) + var params logql.Params + var path string + switch r.(type) { + case *LokiRequest: + params = paramsFromRequest(r) + path = r.(*LokiRequest).GetPath() + case *LokiInstantRequest: + params = paramsFromInstantRequest(r) + path = r.(*LokiInstantRequest).GetPath() + default: + return nil, fmt.Errorf("expected *LokiRequest or *LokiInstantRequest, got (%T)", r) + } + query := ast.ng.Query(params, parsed) res, err := query.Exec(ctx) if err != nil { @@ -138,9 +144,9 @@ func (ast *astMapperware) Do(ctx context.Context, r queryrange.Request) (queryra case logqlmodel.ValueTypeStreams: return &LokiResponse{ Status: loghttp.QueryStatusSuccess, - Direction: req.Direction, - Limit: req.Limit, - Version: uint32(loghttp.GetVersion(req.Path)), + Direction: params.Direction(), + Limit: params.Limit(), + Version: uint32(loghttp.GetVersion(path)), Statistics: res.Statistics, Data: LokiData{ ResultType: loghttp.ResultTypeStream, From 993e3985d81e787116e81bd59664b365f7d853aa Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 13 Jul 2021 10:53:20 +0200 Subject: [PATCH 05/27] Convert paras to request. --- pkg/querier/queryrange/downstreamer.go | 12 ++++++------ pkg/querier/queryrange/downstreamer_test.go | 2 +- pkg/querier/queryrange/querysharding_test.go | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/querier/queryrange/downstreamer.go b/pkg/querier/queryrange/downstreamer.go index 7ee4e9b9e5b7a..a374f29da3b6b 100644 --- a/pkg/querier/queryrange/downstreamer.go +++ b/pkg/querier/queryrange/downstreamer.go @@ -24,16 +24,15 @@ type DownstreamHandler struct { next queryrange.Handler } -func ParamsToLokiRequest(params logql.Params) *LokiRequest { +func ParamsToLokiRequest(params logql.Params) queryrange.Request { if params.Start() == params.End() { - return &LokiRequest{ + return &LokiInstantRequest{ Query: params.Query(), Limit: params.Limit(), - Step: int64(params.Step() / time.Millisecond), - StartTs: params.Start(), - EndTs: params.End(), + TimeTs: params.Start(), Direction: params.Direction(), Path: "/loki/api/v1/query", // TODO(owen-d): make this derivable + Shards: params.Shards(), } } return &LokiRequest{ @@ -44,6 +43,7 @@ func ParamsToLokiRequest(params logql.Params) *LokiRequest { EndTs: params.End(), Direction: params.Direction(), Path: "/loki/api/v1/query_range", // TODO(owen-d): make this derivable + Shards: params.Shards(), } } @@ -70,7 +70,7 @@ type instance struct { func (in instance) Downstream(ctx context.Context, queries []logql.DownstreamQuery) ([]logqlmodel.Result, error) { return in.For(ctx, queries, func(qry logql.DownstreamQuery) (logqlmodel.Result, error) { // todo handle instant queries - req := ParamsToLokiRequest(qry.Params).WithShards(qry.Shards).WithQuery(qry.Expr.String()).(*LokiRequest) + req := ParamsToLokiRequest(qry.Params).WithQuery(qry.Expr.String()).(*LokiRequest) logger, ctx := spanlogger.New(ctx, "DownstreamHandler.instance") defer logger.Finish() level.Debug(logger).Log("shards", fmt.Sprintf("%+v", req.Shards), "query", req.Query, "step", req.GetStep()) diff --git a/pkg/querier/queryrange/downstreamer_test.go b/pkg/querier/queryrange/downstreamer_test.go index 29408982be5c2..af3697eb37b5a 100644 --- a/pkg/querier/queryrange/downstreamer_test.go +++ b/pkg/querier/queryrange/downstreamer_test.go @@ -329,7 +329,7 @@ func TestInstanceDownstream(t *testing.T) { // for some reason these seemingly can't be checked in their own goroutines, // so we assign them to scoped variables for later comparison. got = req - want = ParamsToLokiRequest(params).WithShards(logql.Shards{{Shard: 0, Of: 2}}).WithQuery(expr.String()) + want = ParamsToLokiRequest(params).WithQuery(expr.String()) return expectedResp(), nil }, diff --git a/pkg/querier/queryrange/querysharding_test.go b/pkg/querier/queryrange/querysharding_test.go index 5205029f4c126..60ded164a3dc5 100644 --- a/pkg/querier/queryrange/querysharding_test.go +++ b/pkg/querier/queryrange/querysharding_test.go @@ -265,7 +265,7 @@ func Test_InstantSharding(t *testing.T) { TimeTs: time.Now(), Path: "/v1/query", }) - require.Nil(t, err) + require.NoError(t, err) require.Equal(t, &LokiPromResponse{Response: &queryrange.PrometheusResponse{ Data: queryrange.PrometheusData{ ResultType: loghttp.ResultTypeVector, From d4190f63b179b65f666f2c6be1350eb2b57642a1 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 13 Jul 2021 12:48:19 +0200 Subject: [PATCH 06/27] Convert vector. --- pkg/querier/queryrange/codec.go | 19 +++++++++++++++++++ pkg/querier/queryrange/downstreamer.go | 5 ++--- pkg/querier/queryrange/querysharding.go | 11 ++++++++++- pkg/querier/queryrange/querysharding_test.go | 1 + 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index d1d1676803223..e2e5c1134e3d4 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -688,6 +688,25 @@ func toProto(m loghttp.Matrix) []queryrange.SampleStream { return res } +func toProtoVector(v loghttp.Vector) []queryrange.SampleStream { + if len(v) == 0 { + return nil + } + res := make([]queryrange.SampleStream, 0, 1) + samples := make([]cortexpb.Sample, 0, len(v)) + for _, s := range v { + samples = append(samples, cortexpb.Sample{ + Value: float64(s.Value), + TimestampMs: int64(s.Timestamp), + }) + // todo add labels + } + res = append(res, queryrange.SampleStream{ + Samples: samples, + }) + return res +} + func (res LokiResponse) Count() int64 { var result int64 for _, s := range res.Data.Result { diff --git a/pkg/querier/queryrange/downstreamer.go b/pkg/querier/queryrange/downstreamer.go index a374f29da3b6b..bbb848d1bf895 100644 --- a/pkg/querier/queryrange/downstreamer.go +++ b/pkg/querier/queryrange/downstreamer.go @@ -69,11 +69,10 @@ type instance struct { func (in instance) Downstream(ctx context.Context, queries []logql.DownstreamQuery) ([]logqlmodel.Result, error) { return in.For(ctx, queries, func(qry logql.DownstreamQuery) (logqlmodel.Result, error) { - // todo handle instant queries - req := ParamsToLokiRequest(qry.Params).WithQuery(qry.Expr.String()).(*LokiRequest) + req := ParamsToLokiRequest(qry.Params).WithQuery(qry.Expr.String()) logger, ctx := spanlogger.New(ctx, "DownstreamHandler.instance") defer logger.Finish() - level.Debug(logger).Log("shards", fmt.Sprintf("%+v", req.Shards), "query", req.Query, "step", req.GetStep()) + level.Debug(logger).Log("shards", fmt.Sprintf("%+v", qry.Shards), "query", req.GetQuery(), "step", req.GetStep()) res, err := in.handler.Do(ctx, req) if err != nil { diff --git a/pkg/querier/queryrange/querysharding.go b/pkg/querier/queryrange/querysharding.go index bb39d58609bff..5a4e54e2ac58a 100644 --- a/pkg/querier/queryrange/querysharding.go +++ b/pkg/querier/queryrange/querysharding.go @@ -153,8 +153,17 @@ func (ast *astMapperware) Do(ctx context.Context, r queryrange.Request) (queryra Result: value.(loghttp.Streams).ToProto(), }, }, nil + case parser.ValueTypeVector: + return &LokiPromResponse{Response: &queryrange.PrometheusResponse{ + Status: loghttp.QueryStatusSuccess, + Data: queryrange.PrometheusData{ + ResultType: loghttp.ResultTypeVector, + Result: toProtoVector(value.(loghttp.Vector)), + }, + }, + }, nil default: - return nil, fmt.Errorf("unexpected downstream response type (%T)", res.Data) + return nil, fmt.Errorf("unexpected downstream response type (%T)", res.Data.Type()) } } diff --git a/pkg/querier/queryrange/querysharding_test.go b/pkg/querier/queryrange/querysharding_test.go index 60ded164a3dc5..de2d03ef61a02 100644 --- a/pkg/querier/queryrange/querysharding_test.go +++ b/pkg/querier/queryrange/querysharding_test.go @@ -245,6 +245,7 @@ func Test_InstantSharding(t *testing.T) { }, queryrange.NewInstrumentMiddlewareMetrics(nil), nilShardingMetrics, fakeLimits{ + maxSeries: math.MaxInt32, maxQueryParallelism: 10, }) response, err := sharding.Wrap(queryrange.HandlerFunc(func(c context.Context, r queryrange.Request) (queryrange.Response, error) { From 2dc6ae809eb83378392018ccaa0596b6608fae89 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 13 Jul 2021 13:21:47 +0200 Subject: [PATCH 07/27] Use proper query time stamp. --- pkg/querier/queryrange/codec.go | 4 +++- pkg/querier/queryrange/querysharding_test.go | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index e2e5c1134e3d4..c8b7d271ffdb4 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -694,15 +694,17 @@ func toProtoVector(v loghttp.Vector) []queryrange.SampleStream { } res := make([]queryrange.SampleStream, 0, 1) samples := make([]cortexpb.Sample, 0, len(v)) + labels := make([]cortexpb.LabelAdapter, 0, len(v)) for _, s := range v { samples = append(samples, cortexpb.Sample{ Value: float64(s.Value), TimestampMs: int64(s.Timestamp), }) - // todo add labels + labels = append(labels, cortexpb.FromMetricsToLabelAdapters(s.Metric)...) } res = append(res, queryrange.SampleStream{ Samples: samples, + Labels: labels, }) return res } diff --git a/pkg/querier/queryrange/querysharding_test.go b/pkg/querier/queryrange/querysharding_test.go index de2d03ef61a02..f67001f63862c 100644 --- a/pkg/querier/queryrange/querysharding_test.go +++ b/pkg/querier/queryrange/querysharding_test.go @@ -249,7 +249,6 @@ func Test_InstantSharding(t *testing.T) { maxQueryParallelism: 10, }) response, err := sharding.Wrap(queryrange.HandlerFunc(func(c context.Context, r queryrange.Request) (queryrange.Response, error) { - t.Log(r) return &LokiPromResponse{Response: &queryrange.PrometheusResponse{ Data: queryrange.PrometheusData{ ResultType: loghttp.ResultTypeVector, @@ -263,11 +262,12 @@ func Test_InstantSharding(t *testing.T) { }}, nil })).Do(ctx, &LokiInstantRequest{ Query: `rate({app="foo"}[1m])`, - TimeTs: time.Now(), + TimeTs: util.TimeFromMillis(10), Path: "/v1/query", }) require.NoError(t, err) require.Equal(t, &LokiPromResponse{Response: &queryrange.PrometheusResponse{ + Status: "success", Data: queryrange.PrometheusData{ ResultType: loghttp.ResultTypeVector, Result: []queryrange.SampleStream{ From 4ec4acac49085bab128506482710b09e853f5195 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 13 Jul 2021 13:38:06 +0200 Subject: [PATCH 08/27] Assert number of calls. --- pkg/querier/queryrange/querysharding_test.go | 24 +++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/pkg/querier/queryrange/querysharding_test.go b/pkg/querier/queryrange/querysharding_test.go index f67001f63862c..7df9db0c5f5d4 100644 --- a/pkg/querier/queryrange/querysharding_test.go +++ b/pkg/querier/queryrange/querysharding_test.go @@ -238,6 +238,9 @@ func mockHandler(resp queryrange.Response, err error) queryrange.Handler { func Test_InstantSharding(t *testing.T) { ctx := user.InjectOrgID(context.Background(), "1") + var lock sync.Mutex + called := 0 + sharding := NewQueryShardMiddleware(log.NewNopLogger(), queryrange.ShardingConfigs{ chunk.PeriodConfig{ RowShards: 3, @@ -249,6 +252,9 @@ func Test_InstantSharding(t *testing.T) { maxQueryParallelism: 10, }) response, err := sharding.Wrap(queryrange.HandlerFunc(func(c context.Context, r queryrange.Request) (queryrange.Response, error) { + lock.Lock() + defer lock.Unlock() + called++ return &LokiPromResponse{Response: &queryrange.PrometheusResponse{ Data: queryrange.PrometheusData{ ResultType: loghttp.ResultTypeVector, @@ -266,18 +272,24 @@ func Test_InstantSharding(t *testing.T) { Path: "/v1/query", }) require.NoError(t, err) + require.Equal(t, 3, called, "expected 3 calls but got {}", called) + require.Len(t, response.(*LokiPromResponse).Response.Data.Result[0].Labels, 3) require.Equal(t, &LokiPromResponse{Response: &queryrange.PrometheusResponse{ Status: "success", Data: queryrange.PrometheusData{ ResultType: loghttp.ResultTypeVector, Result: []queryrange.SampleStream{ { - Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "bar"}}, - Samples: []cortexpb.Sample{{Value: 10, TimestampMs: 10}}, - }, - { - Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "buzz"}}, - Samples: []cortexpb.Sample{{Value: 10, TimestampMs: 10}}, + Labels: []cortexpb.LabelAdapter{ + {Name: "foo", Value: "bar"}, + {Name: "foo", Value: "bar"}, + {Name: "foo", Value: "bar"}, + }, + Samples: []cortexpb.Sample{ + {Value: 10, TimestampMs: 10}, + {Value: 10, TimestampMs: 10}, + {Value: 10, TimestampMs: 10}, + }, }, }, }, From 41695f38e8627e9e1f41af18f86775d6a88b49ba Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 13 Jul 2021 13:45:18 +0200 Subject: [PATCH 09/27] Convert vector to multiple samples. --- pkg/querier/queryrange/codec.go | 18 +++++++--------- pkg/querier/queryrange/querysharding_test.go | 22 ++++++++++---------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index c8b7d271ffdb4..28f3066d38d3d 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -692,20 +692,16 @@ func toProtoVector(v loghttp.Vector) []queryrange.SampleStream { if len(v) == 0 { return nil } - res := make([]queryrange.SampleStream, 0, 1) - samples := make([]cortexpb.Sample, 0, len(v)) - labels := make([]cortexpb.LabelAdapter, 0, len(v)) + res := make([]queryrange.SampleStream, 0, len(v)) for _, s := range v { - samples = append(samples, cortexpb.Sample{ - Value: float64(s.Value), - TimestampMs: int64(s.Timestamp), + res = append(res, queryrange.SampleStream{ + Samples: []cortexpb.Sample{{ + Value: float64(s.Value), + TimestampMs: int64(s.Timestamp), + }}, + Labels: cortexpb.FromMetricsToLabelAdapters(s.Metric), }) - labels = append(labels, cortexpb.FromMetricsToLabelAdapters(s.Metric)...) } - res = append(res, queryrange.SampleStream{ - Samples: samples, - Labels: labels, - }) return res } diff --git a/pkg/querier/queryrange/querysharding_test.go b/pkg/querier/queryrange/querysharding_test.go index 7df9db0c5f5d4..5e59bd6f7f2a2 100644 --- a/pkg/querier/queryrange/querysharding_test.go +++ b/pkg/querier/queryrange/querysharding_test.go @@ -273,23 +273,23 @@ func Test_InstantSharding(t *testing.T) { }) require.NoError(t, err) require.Equal(t, 3, called, "expected 3 calls but got {}", called) - require.Len(t, response.(*LokiPromResponse).Response.Data.Result[0].Labels, 3) + require.Len(t, response.(*LokiPromResponse).Response.Data.Result, 3) require.Equal(t, &LokiPromResponse{Response: &queryrange.PrometheusResponse{ Status: "success", Data: queryrange.PrometheusData{ ResultType: loghttp.ResultTypeVector, Result: []queryrange.SampleStream{ { - Labels: []cortexpb.LabelAdapter{ - {Name: "foo", Value: "bar"}, - {Name: "foo", Value: "bar"}, - {Name: "foo", Value: "bar"}, - }, - Samples: []cortexpb.Sample{ - {Value: 10, TimestampMs: 10}, - {Value: 10, TimestampMs: 10}, - {Value: 10, TimestampMs: 10}, - }, + Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "bar"}}, + Samples: []cortexpb.Sample{{Value: 10, TimestampMs: 10}}, + }, + { + Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "bar"}}, + Samples: []cortexpb.Sample{{Value: 10, TimestampMs: 10}}, + }, + { + Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "bar"}}, + Samples: []cortexpb.Sample{{Value: 10, TimestampMs: 10}}, }, }, }, From 857f0d48fd751646fd3a3648255a4c2f3da98339 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 13 Jul 2021 13:46:51 +0200 Subject: [PATCH 10/27] Format code. --- pkg/querier/queryrange/codec.go | 2 +- pkg/querier/queryrange/querysharding.go | 4 ++-- pkg/querier/queryrange/querysharding_test.go | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index 28f3066d38d3d..53011c7261347 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -699,7 +699,7 @@ func toProtoVector(v loghttp.Vector) []queryrange.SampleStream { Value: float64(s.Value), TimestampMs: int64(s.Timestamp), }}, - Labels: cortexpb.FromMetricsToLabelAdapters(s.Metric), + Labels: cortexpb.FromMetricsToLabelAdapters(s.Metric), }) } return res diff --git a/pkg/querier/queryrange/querysharding.go b/pkg/querier/queryrange/querysharding.go index 5a4e54e2ac58a..10f4bf5ec22fb 100644 --- a/pkg/querier/queryrange/querysharding.go +++ b/pkg/querier/queryrange/querysharding.go @@ -158,9 +158,9 @@ func (ast *astMapperware) Do(ctx context.Context, r queryrange.Request) (queryra Status: loghttp.QueryStatusSuccess, Data: queryrange.PrometheusData{ ResultType: loghttp.ResultTypeVector, - Result: toProtoVector(value.(loghttp.Vector)), - }, + Result: toProtoVector(value.(loghttp.Vector)), }, + }, }, nil default: return nil, fmt.Errorf("unexpected downstream response type (%T)", res.Data.Type()) diff --git a/pkg/querier/queryrange/querysharding_test.go b/pkg/querier/queryrange/querysharding_test.go index 5e59bd6f7f2a2..b739c14a82d6f 100644 --- a/pkg/querier/queryrange/querysharding_test.go +++ b/pkg/querier/queryrange/querysharding_test.go @@ -248,7 +248,7 @@ func Test_InstantSharding(t *testing.T) { }, queryrange.NewInstrumentMiddlewareMetrics(nil), nilShardingMetrics, fakeLimits{ - maxSeries: math.MaxInt32, + maxSeries: math.MaxInt32, maxQueryParallelism: 10, }) response, err := sharding.Wrap(queryrange.HandlerFunc(func(c context.Context, r queryrange.Request) (queryrange.Response, error) { @@ -280,15 +280,15 @@ func Test_InstantSharding(t *testing.T) { ResultType: loghttp.ResultTypeVector, Result: []queryrange.SampleStream{ { - Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "bar"}}, + Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "bar"}}, Samples: []cortexpb.Sample{{Value: 10, TimestampMs: 10}}, }, { - Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "bar"}}, + Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "bar"}}, Samples: []cortexpb.Sample{{Value: 10, TimestampMs: 10}}, }, { - Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "bar"}}, + Labels: []cortexpb.LabelAdapter{{Name: "foo", Value: "bar"}}, Samples: []cortexpb.Sample{{Value: 10, TimestampMs: 10}}, }, }, From 91cefd2c499284d9cafc6176ab9bf96e9425d81d Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 13 Jul 2021 15:10:25 +0200 Subject: [PATCH 11/27] Use type switch. --- pkg/querier/queryrange/querysharding.go | 6 +++--- pkg/querier/queryrange/roundtrip.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/querier/queryrange/querysharding.go b/pkg/querier/queryrange/querysharding.go index 10f4bf5ec22fb..3bc1c6c45676f 100644 --- a/pkg/querier/queryrange/querysharding.go +++ b/pkg/querier/queryrange/querysharding.go @@ -107,13 +107,13 @@ func (ast *astMapperware) Do(ctx context.Context, r queryrange.Request) (queryra var params logql.Params var path string - switch r.(type) { + switch r := r.(type) { case *LokiRequest: params = paramsFromRequest(r) - path = r.(*LokiRequest).GetPath() + path = r.GetPath() case *LokiInstantRequest: params = paramsFromInstantRequest(r) - path = r.(*LokiInstantRequest).GetPath() + path = r.GetPath() default: return nil, fmt.Errorf("expected *LokiRequest or *LokiInstantRequest, got (%T)", r) } diff --git a/pkg/querier/queryrange/roundtrip.go b/pkg/querier/queryrange/roundtrip.go index d35556f5f41d7..5aaad4689262d 100644 --- a/pkg/querier/queryrange/roundtrip.go +++ b/pkg/querier/queryrange/roundtrip.go @@ -463,9 +463,9 @@ func NewInstantMetricTripperware( ) (queryrange.Tripperware, error) { queryRangeMiddleware := []queryrange.Middleware{StatsCollectorMiddleware(), nil} - if cfg.CacheResults { + //if cfg.CacheResults { // think about caching - } + //} if cfg.ShardedQueries { queryRangeMiddleware = append(queryRangeMiddleware, From 72f5b3970c59de51f891e102068c2ce7d82ecd34 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 13 Jul 2021 15:17:22 +0200 Subject: [PATCH 12/27] Format code. --- pkg/querier/queryrange/roundtrip.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/querier/queryrange/roundtrip.go b/pkg/querier/queryrange/roundtrip.go index 5aaad4689262d..d8471fe054ef8 100644 --- a/pkg/querier/queryrange/roundtrip.go +++ b/pkg/querier/queryrange/roundtrip.go @@ -464,7 +464,7 @@ func NewInstantMetricTripperware( queryRangeMiddleware := []queryrange.Middleware{StatsCollectorMiddleware(), nil} //if cfg.CacheResults { - // think about caching + // think about caching //} if cfg.ShardedQueries { From 91c08e7b196baf35968b7bf1d074821fac56f996 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Thu, 15 Jul 2021 12:03:33 +0200 Subject: [PATCH 13/27] Rename Matrix to proto conversion. --- pkg/querier/queryrange/codec.go | 4 ++-- pkg/querier/queryrange/querysharding.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index 53011c7261347..af3a46d5c17ed 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -389,7 +389,7 @@ func (Codec) DecodeResponse(ctx context.Context, r *http.Response, req queryrang Status: resp.Status, Data: queryrange.PrometheusData{ ResultType: loghttp.ResultTypeMatrix, - Result: toProto(resp.Data.Result.(loghttp.Matrix)), + Result: toProtoMatrix(resp.Data.Result.(loghttp.Matrix)), }, Headers: convertPrometheusResponseHeadersToPointers(httpResponseHeadersToPromResponseHeaders(r.Header)), }, @@ -667,7 +667,7 @@ func mergeOrderedNonOverlappingStreams(resps []*LokiResponse, limit uint32, dire return results } -func toProto(m loghttp.Matrix) []queryrange.SampleStream { +func toProtoMatrix(m loghttp.Matrix) []queryrange.SampleStream { if len(m) == 0 { return nil } diff --git a/pkg/querier/queryrange/querysharding.go b/pkg/querier/queryrange/querysharding.go index 3bc1c6c45676f..3a7abcf20e436 100644 --- a/pkg/querier/queryrange/querysharding.go +++ b/pkg/querier/queryrange/querysharding.go @@ -136,7 +136,7 @@ func (ast *astMapperware) Do(ctx context.Context, r queryrange.Request) (queryra Status: loghttp.QueryStatusSuccess, Data: queryrange.PrometheusData{ ResultType: loghttp.ResultTypeMatrix, - Result: toProto(value.(loghttp.Matrix)), + Result: toProtoMatrix(value.(loghttp.Matrix)), }, }, Statistics: res.Statistics, From b0745678bb4c9dbc15275090064041146b75247d Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Mon, 19 Jul 2021 15:01:15 +0200 Subject: [PATCH 14/27] Remvoe comment on caching. --- pkg/querier/queryrange/roundtrip.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pkg/querier/queryrange/roundtrip.go b/pkg/querier/queryrange/roundtrip.go index d8471fe054ef8..c7acf5f68ee51 100644 --- a/pkg/querier/queryrange/roundtrip.go +++ b/pkg/querier/queryrange/roundtrip.go @@ -463,10 +463,6 @@ func NewInstantMetricTripperware( ) (queryrange.Tripperware, error) { queryRangeMiddleware := []queryrange.Middleware{StatsCollectorMiddleware(), nil} - //if cfg.CacheResults { - // think about caching - //} - if cfg.ShardedQueries { queryRangeMiddleware = append(queryRangeMiddleware, NewQueryShardMiddleware( From b56c00ffb930bad528bf60c1af9e75901f81974a Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Mon, 19 Jul 2021 15:01:50 +0200 Subject: [PATCH 15/27] Update pkg/querier/queryrange/roundtrip_test.go Co-authored-by: Cyril Tovena --- pkg/querier/queryrange/roundtrip_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/querier/queryrange/roundtrip_test.go b/pkg/querier/queryrange/roundtrip_test.go index ad46637f4e242..9b75a7d313fee 100644 --- a/pkg/querier/queryrange/roundtrip_test.go +++ b/pkg/querier/queryrange/roundtrip_test.go @@ -424,7 +424,7 @@ func TestPostQueries(t *testing.T) { return nil, nil }), queryrange.RoundTripFunc(func(*http.Request) (*http.Response, error) { - t.Error("unexpected labels roundtripper called") + t.Error("unexpected instant roundtripper called") return nil, nil }), fakeLimits{}, From b0120e14c3771ca2208cfeec8586fa90f5dd8e74 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 20 Jul 2021 11:40:39 +0200 Subject: [PATCH 16/27] Remove nil middlware. --- pkg/querier/queryrange/roundtrip.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/querier/queryrange/roundtrip.go b/pkg/querier/queryrange/roundtrip.go index c7acf5f68ee51..4366cd5929825 100644 --- a/pkg/querier/queryrange/roundtrip.go +++ b/pkg/querier/queryrange/roundtrip.go @@ -461,7 +461,7 @@ func NewInstantMetricTripperware( shardingMetrics *logql.ShardingMetrics, splitByMetrics *SplitByMetrics, ) (queryrange.Tripperware, error) { - queryRangeMiddleware := []queryrange.Middleware{StatsCollectorMiddleware(), nil} + queryRangeMiddleware := []queryrange.Middleware{StatsCollectorMiddleware()} if cfg.ShardedQueries { queryRangeMiddleware = append(queryRangeMiddleware, From 810a0a9b4e1e04a00012e863e607e9f7a91ca63f Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 20 Jul 2021 13:59:46 +0200 Subject: [PATCH 17/27] Handle LokiInstantRequest correctly. --- pkg/querier/queryrange/codec.go | 44 +++++++++++++++++++++--- pkg/querier/queryrange/querysharding.go | 2 +- pkg/querier/queryrange/roundtrip_test.go | 34 ++++++++++++++++++ pkg/querier/queryrange/stats.go | 11 +++++- pkg/querier/queryrange/stats_test.go | 6 ++-- 5 files changed, 88 insertions(+), 9 deletions(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index af3a46d5c17ed..755eb90611ac8 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -322,6 +322,29 @@ func (Codec) EncodeRequest(ctx context.Context, r queryrange.Request) (*http.Req Body: http.NoBody, Header: http.Header{}, } + return req.WithContext(ctx), nil + case *LokiInstantRequest: + params := url.Values{ + "query": []string{request.Query}, + "direction": []string{request.Direction.String()}, + "limit": []string{fmt.Sprintf("%d", request.Limit)}, + } + if len(request.Shards) > 0 { + params["shards"] = request.Shards + } + u := &url.URL{ + // the request could come /api/prom/query but we want to only use the new api. + Path: "/loki/api/v1/query", + RawQuery: params.Encode(), + } + req := &http.Request{ + Method: "GET", + RequestURI: u.String(), // This is what the httpgrpc code looks at. + URL: u, + Body: http.NoBody, + Header: http.Header{}, + } + return req.WithContext(ctx), nil default: return nil, httpgrpc.Errorf(http.StatusInternalServerError, "invalid request format") @@ -396,11 +419,24 @@ func (Codec) DecodeResponse(ctx context.Context, r *http.Response, req queryrang Statistics: resp.Data.Statistics, }, nil case loghttp.ResultTypeStream: + // This is the same as in querysharding.go + var params logql.Params + var path string + switch r := req.(type) { + case *LokiRequest: + params = paramsFromRangeRequest(r) + path = r.GetPath() + case *LokiInstantRequest: + params = paramsFromInstantRequest(r) + path = r.GetPath() + default: + return nil, fmt.Errorf("expected *LokiRequest or *LokiInstantRequest, got (%T)", r) + } return &LokiResponse{ Status: resp.Status, - Direction: req.(*LokiRequest).Direction, - Limit: req.(*LokiRequest).Limit, - Version: uint32(loghttp.GetVersion(req.(*LokiRequest).Path)), + Direction: params.Direction(), + Limit: params.Limit(), + Version: uint32(loghttp.GetVersion(path)), Statistics: resp.Data.Statistics, Data: LokiData{ ResultType: loghttp.ResultTypeStream, @@ -717,7 +753,7 @@ type paramsWrapper struct { *LokiRequest } -func paramsFromRequest(req queryrange.Request) *paramsWrapper { +func paramsFromRangeRequest(req queryrange.Request) *paramsWrapper { return ¶msWrapper{ LokiRequest: req.(*LokiRequest), } diff --git a/pkg/querier/queryrange/querysharding.go b/pkg/querier/queryrange/querysharding.go index 3a7abcf20e436..5baad575793d0 100644 --- a/pkg/querier/queryrange/querysharding.go +++ b/pkg/querier/queryrange/querysharding.go @@ -109,7 +109,7 @@ func (ast *astMapperware) Do(ctx context.Context, r queryrange.Request) (queryra var path string switch r := r.(type) { case *LokiRequest: - params = paramsFromRequest(r) + params = paramsFromRangeRequest(r) path = r.GetPath() case *LokiInstantRequest: params = paramsFromInstantRequest(r) diff --git a/pkg/querier/queryrange/roundtrip_test.go b/pkg/querier/queryrange/roundtrip_test.go index 9b75a7d313fee..44981a7cba0df 100644 --- a/pkg/querier/queryrange/roundtrip_test.go +++ b/pkg/querier/queryrange/roundtrip_test.go @@ -202,6 +202,40 @@ func TestLogFilterTripperware(t *testing.T) { require.Error(t, err) } +func TestInstantQueryTripperware(t *testing.T) { + + tpw, stopper, err := NewTripperware(testConfig, util_log.Logger, fakeLimits{}, chunk.SchemaConfig{}, 0, nil) + if stopper != nil { + defer stopper.Stop() + } + require.NoError(t, err) + rt, err := newfakeRoundTripper() + require.NoError(t, err) + defer rt.Close() + + lreq := &LokiInstantRequest{ + Query: `sum by (job) (bytes_rate({cluster="dev-us-central-0"}[15m]))`, + Limit: 1000, + Direction: logproto.FORWARD, + Path: "/loki/api/v1/query", + } + + ctx := user.InjectOrgID(context.Background(), "1") + req, err := LokiCodec.EncodeRequest(ctx, lreq) + require.NoError(t, err) + + req = req.WithContext(ctx) + err = user.InjectOrgIDIntoHTTPRequest(ctx, req) + require.NoError(t, err) + + //testing limit + count, h := promqlResult(streams) + rt.setHandler(h) + _, err = tpw(rt).RoundTrip(req) + require.Equal(t, 1, *count) + require.NoError(t, err) +} + func TestSeriesTripperware(t *testing.T) { tpw, stopper, err := NewTripperware(testConfig, util_log.Logger, fakeLimits{}, chunk.SchemaConfig{}, 0, nil) diff --git a/pkg/querier/queryrange/stats.go b/pkg/querier/queryrange/stats.go index c9e4b187d29a1..641bc2e84f814 100644 --- a/pkg/querier/queryrange/stats.go +++ b/pkg/querier/queryrange/stats.go @@ -107,9 +107,18 @@ func StatsCollectorMiddleware() queryrange.Middleware { } ctxValue := ctx.Value(ctxKey) if data, ok := ctxValue.(*queryData); ok { + + switch r := req.(type) { + case *LokiRequest: + data.params = paramsFromRangeRequest(r) + case *LokiInstantRequest: + data.params = paramsFromInstantRequest(r) + default: + level.Warn(logger).Log("msg", fmt.Sprintf("cannot compute params, unexpected type: %T", resp)) + } + data.recorded = true data.statistics = statistics - data.params = paramsFromRequest(req) data.result = res } return resp, err diff --git a/pkg/querier/queryrange/stats_test.go b/pkg/querier/queryrange/stats_test.go index e5e94c90cd02f..d18e3626bb8c5 100644 --- a/pkg/querier/queryrange/stats_test.go +++ b/pkg/querier/queryrange/stats_test.go @@ -86,7 +86,7 @@ func Test_StatsHTTP(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { data := r.Context().Value(ctxKey).(*queryData) data.recorded = true - data.params = paramsFromRequest(&LokiRequest{ + data.params = paramsFromRangeRequest(&LokiRequest{ Query: "foo", Direction: logproto.BACKWARD, Limit: 100, @@ -106,7 +106,7 @@ func Test_StatsHTTP(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { data := r.Context().Value(ctxKey).(*queryData) data.recorded = true - data.params = paramsFromRequest(&LokiRequest{ + data.params = paramsFromRangeRequest(&LokiRequest{ Query: "foo", Direction: logproto.BACKWARD, Limit: 100, @@ -127,7 +127,7 @@ func Test_StatsHTTP(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { data := r.Context().Value(ctxKey).(*queryData) data.recorded = true - data.params = paramsFromRequest(&LokiRequest{ + data.params = paramsFromRangeRequest(&LokiRequest{ Query: "foo", Direction: logproto.BACKWARD, Limit: 100, From f640c10e1fabf0387f4436dc0b1c58393b163f47 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 20 Jul 2021 15:21:28 +0200 Subject: [PATCH 18/27] Assert reponse type. --- pkg/querier/queryrange/codec.go | 2 +- pkg/querier/queryrange/roundtrip_test.go | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index 755eb90611ac8..4b992443b6a04 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -445,7 +445,7 @@ func (Codec) DecodeResponse(ctx context.Context, r *http.Response, req queryrang Headers: httpResponseHeadersToPromResponseHeaders(r.Header), }, nil default: - return nil, httpgrpc.Errorf(http.StatusBadRequest, "unsupported response type") + return nil, httpgrpc.Errorf(http.StatusBadRequest, "unsupported response type, got (%s)", string(resp.Data.ResultType)) } } } diff --git a/pkg/querier/queryrange/roundtrip_test.go b/pkg/querier/queryrange/roundtrip_test.go index 44981a7cba0df..bf17dbc96496f 100644 --- a/pkg/querier/queryrange/roundtrip_test.go +++ b/pkg/querier/queryrange/roundtrip_test.go @@ -228,12 +228,14 @@ func TestInstantQueryTripperware(t *testing.T) { err = user.InjectOrgIDIntoHTTPRequest(ctx, req) require.NoError(t, err) - //testing limit count, h := promqlResult(streams) rt.setHandler(h) - _, err = tpw(rt).RoundTrip(req) + resp, err := tpw(rt).RoundTrip(req) require.Equal(t, 1, *count) require.NoError(t, err) + + lokiResponse, err := LokiCodec.DecodeResponse(ctx, resp, lreq) + require.IsType(t, &LokiResponse{}, lokiResponse) } func TestSeriesTripperware(t *testing.T) { From 886bbca0d28d51c712c2578db0f03147bb5273cf Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 20 Jul 2021 15:53:08 +0200 Subject: [PATCH 19/27] Return LokiPromResponse. --- pkg/querier/queryrange/codec.go | 12 ++++++++++++ pkg/querier/queryrange/roundtrip_test.go | 2 +- tools/dev/loki-boltdb-storage-s3/compose-up.sh | 4 ++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index 4b992443b6a04..0a98c2e4ab0d0 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -444,6 +444,18 @@ func (Codec) DecodeResponse(ctx context.Context, r *http.Response, req queryrang }, Headers: httpResponseHeadersToPromResponseHeaders(r.Header), }, nil + case loghttp.ResultTypeVector: + return &LokiPromResponse{ + Response: &queryrange.PrometheusResponse{ + Status: resp.Status, + Data: queryrange.PrometheusData{ + ResultType: loghttp.ResultTypeMatrix, + Result: toProtoVector(resp.Data.Result.(loghttp.Vector)), + }, + Headers: convertPrometheusResponseHeadersToPointers(httpResponseHeadersToPromResponseHeaders(r.Header)), + }, + Statistics: resp.Data.Statistics, + }, nil default: return nil, httpgrpc.Errorf(http.StatusBadRequest, "unsupported response type, got (%s)", string(resp.Data.ResultType)) } diff --git a/pkg/querier/queryrange/roundtrip_test.go b/pkg/querier/queryrange/roundtrip_test.go index bf17dbc96496f..53168d4d93b7e 100644 --- a/pkg/querier/queryrange/roundtrip_test.go +++ b/pkg/querier/queryrange/roundtrip_test.go @@ -235,7 +235,7 @@ func TestInstantQueryTripperware(t *testing.T) { require.NoError(t, err) lokiResponse, err := LokiCodec.DecodeResponse(ctx, resp, lreq) - require.IsType(t, &LokiResponse{}, lokiResponse) + require.IsType(t, &LokiResponse{}, lokiResponse) // Is LokiPromResponse in Docker compose } func TestSeriesTripperware(t *testing.T) { diff --git a/tools/dev/loki-boltdb-storage-s3/compose-up.sh b/tools/dev/loki-boltdb-storage-s3/compose-up.sh index df1077c4a0def..1841f312ca33f 100755 --- a/tools/dev/loki-boltdb-storage-s3/compose-up.sh +++ b/tools/dev/loki-boltdb-storage-s3/compose-up.sh @@ -18,7 +18,7 @@ CGO_ENABLED=0 GOOS=linux go build -mod=vendor -gcflags "all=-N -l" -o "${SCRIPT_ # ## install loki driver to send logs docker plugin install grafana/loki-docker-driver:latest --alias loki-compose --grant-all-permissions || true # build the compose image -docker compose -f "${SCRIPT_DIR}"/docker-compose.yml build distributor +docker-compose -f "${SCRIPT_DIR}"/docker-compose.yml build distributor # cleanup sources rm -Rf "${SRC_DEST}" -docker compose -f "${SCRIPT_DIR}"/docker-compose.yml up "$@" +docker-compose -f "${SCRIPT_DIR}"/docker-compose.yml up "$@" From d0f08730f13a03c377e386d51eb43615e38f1825 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 20 Jul 2021 16:03:23 +0200 Subject: [PATCH 20/27] Verify err in tests. --- pkg/querier/queryrange/roundtrip_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/querier/queryrange/roundtrip_test.go b/pkg/querier/queryrange/roundtrip_test.go index 53168d4d93b7e..e173dee0caf3f 100644 --- a/pkg/querier/queryrange/roundtrip_test.go +++ b/pkg/querier/queryrange/roundtrip_test.go @@ -235,6 +235,7 @@ func TestInstantQueryTripperware(t *testing.T) { require.NoError(t, err) lokiResponse, err := LokiCodec.DecodeResponse(ctx, resp, lreq) + require.NoError(t, err) require.IsType(t, &LokiResponse{}, lokiResponse) // Is LokiPromResponse in Docker compose } From dc6fa0942d48f264150e2f81f972acc7ce8b27fa Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Tue, 20 Jul 2021 17:20:33 +0200 Subject: [PATCH 21/27] Set shards. --- pkg/querier/queryrange/downstreamer.go | 8 ++++---- pkg/querier/queryrange/downstreamer_test.go | 2 +- pkg/querier/queryrange/querysharding_test.go | 3 +++ 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/querier/queryrange/downstreamer.go b/pkg/querier/queryrange/downstreamer.go index bbb848d1bf895..f94be557000a4 100644 --- a/pkg/querier/queryrange/downstreamer.go +++ b/pkg/querier/queryrange/downstreamer.go @@ -24,7 +24,7 @@ type DownstreamHandler struct { next queryrange.Handler } -func ParamsToLokiRequest(params logql.Params) queryrange.Request { +func ParamsToLokiRequest(params logql.Params, shards []string) queryrange.Request { if params.Start() == params.End() { return &LokiInstantRequest{ Query: params.Query(), @@ -32,7 +32,7 @@ func ParamsToLokiRequest(params logql.Params) queryrange.Request { TimeTs: params.Start(), Direction: params.Direction(), Path: "/loki/api/v1/query", // TODO(owen-d): make this derivable - Shards: params.Shards(), + Shards: shards, } } return &LokiRequest{ @@ -43,7 +43,7 @@ func ParamsToLokiRequest(params logql.Params) queryrange.Request { EndTs: params.End(), Direction: params.Direction(), Path: "/loki/api/v1/query_range", // TODO(owen-d): make this derivable - Shards: params.Shards(), + Shards: shards, } } @@ -69,7 +69,7 @@ type instance struct { func (in instance) Downstream(ctx context.Context, queries []logql.DownstreamQuery) ([]logqlmodel.Result, error) { return in.For(ctx, queries, func(qry logql.DownstreamQuery) (logqlmodel.Result, error) { - req := ParamsToLokiRequest(qry.Params).WithQuery(qry.Expr.String()) + req := ParamsToLokiRequest(qry.Params, qry.Shards.Encode()).WithQuery(qry.Expr.String()) logger, ctx := spanlogger.New(ctx, "DownstreamHandler.instance") defer logger.Finish() level.Debug(logger).Log("shards", fmt.Sprintf("%+v", qry.Shards), "query", req.GetQuery(), "step", req.GetStep()) diff --git a/pkg/querier/queryrange/downstreamer_test.go b/pkg/querier/queryrange/downstreamer_test.go index af3697eb37b5a..24877aefd674f 100644 --- a/pkg/querier/queryrange/downstreamer_test.go +++ b/pkg/querier/queryrange/downstreamer_test.go @@ -329,7 +329,7 @@ func TestInstanceDownstream(t *testing.T) { // for some reason these seemingly can't be checked in their own goroutines, // so we assign them to scoped variables for later comparison. got = req - want = ParamsToLokiRequest(params).WithQuery(expr.String()) + want = ParamsToLokiRequest(params, queries[0].Shards.Encode()).WithQuery(expr.String()) return expectedResp(), nil }, diff --git a/pkg/querier/queryrange/querysharding_test.go b/pkg/querier/queryrange/querysharding_test.go index b739c14a82d6f..72463d6c643b9 100644 --- a/pkg/querier/queryrange/querysharding_test.go +++ b/pkg/querier/queryrange/querysharding_test.go @@ -240,6 +240,7 @@ func Test_InstantSharding(t *testing.T) { var lock sync.Mutex called := 0 + shards := []string{} sharding := NewQueryShardMiddleware(log.NewNopLogger(), queryrange.ShardingConfigs{ chunk.PeriodConfig{ @@ -255,6 +256,7 @@ func Test_InstantSharding(t *testing.T) { lock.Lock() defer lock.Unlock() called++ + shards = append(shards, r.(*LokiInstantRequest).Shards...) return &LokiPromResponse{Response: &queryrange.PrometheusResponse{ Data: queryrange.PrometheusData{ ResultType: loghttp.ResultTypeVector, @@ -274,6 +276,7 @@ func Test_InstantSharding(t *testing.T) { require.NoError(t, err) require.Equal(t, 3, called, "expected 3 calls but got {}", called) require.Len(t, response.(*LokiPromResponse).Response.Data.Result, 3) + require.ElementsMatch(t, []string{"0_of_3", "1_of_3", "2_of_3"}, shards) require.Equal(t, &LokiPromResponse{Response: &queryrange.PrometheusResponse{ Status: "success", Data: queryrange.PrometheusData{ From 768acb1d7ae86f07ccc0b59aea0f8012486a48a3 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Wed, 21 Jul 2021 10:18:38 +0200 Subject: [PATCH 22/27] Correct result type. --- pkg/querier/queryrange/codec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index 0a98c2e4ab0d0..f63b8d2778441 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -449,7 +449,7 @@ func (Codec) DecodeResponse(ctx context.Context, r *http.Response, req queryrang Response: &queryrange.PrometheusResponse{ Status: resp.Status, Data: queryrange.PrometheusData{ - ResultType: loghttp.ResultTypeMatrix, + ResultType: loghttp.ResultTypeVector, Result: toProtoVector(resp.Data.Result.(loghttp.Vector)), }, Headers: convertPrometheusResponseHeadersToPointers(httpResponseHeadersToPromResponseHeaders(r.Header)), From cf4a29f42513ba054902c417e7a6535bde9bb459 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Wed, 21 Jul 2021 14:53:27 +0200 Subject: [PATCH 23/27] Use limits middleware. --- pkg/querier/queryrange/downstreamer.go | 8 ++++---- pkg/querier/queryrange/downstreamer_test.go | 2 +- pkg/querier/queryrange/roundtrip.go | 22 ++++++++++----------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/pkg/querier/queryrange/downstreamer.go b/pkg/querier/queryrange/downstreamer.go index f94be557000a4..d532ef24961bb 100644 --- a/pkg/querier/queryrange/downstreamer.go +++ b/pkg/querier/queryrange/downstreamer.go @@ -24,7 +24,7 @@ type DownstreamHandler struct { next queryrange.Handler } -func ParamsToLokiRequest(params logql.Params, shards []string) queryrange.Request { +func ParamsToLokiRequest(params logql.Params, shards logql.Shards) queryrange.Request { if params.Start() == params.End() { return &LokiInstantRequest{ Query: params.Query(), @@ -32,7 +32,7 @@ func ParamsToLokiRequest(params logql.Params, shards []string) queryrange.Reques TimeTs: params.Start(), Direction: params.Direction(), Path: "/loki/api/v1/query", // TODO(owen-d): make this derivable - Shards: shards, + Shards: shards.Encode(), } } return &LokiRequest{ @@ -43,7 +43,7 @@ func ParamsToLokiRequest(params logql.Params, shards []string) queryrange.Reques EndTs: params.End(), Direction: params.Direction(), Path: "/loki/api/v1/query_range", // TODO(owen-d): make this derivable - Shards: shards, + Shards: shards.Encode(), } } @@ -69,7 +69,7 @@ type instance struct { func (in instance) Downstream(ctx context.Context, queries []logql.DownstreamQuery) ([]logqlmodel.Result, error) { return in.For(ctx, queries, func(qry logql.DownstreamQuery) (logqlmodel.Result, error) { - req := ParamsToLokiRequest(qry.Params, qry.Shards.Encode()).WithQuery(qry.Expr.String()) + req := ParamsToLokiRequest(qry.Params, qry.Shards).WithQuery(qry.Expr.String()) logger, ctx := spanlogger.New(ctx, "DownstreamHandler.instance") defer logger.Finish() level.Debug(logger).Log("shards", fmt.Sprintf("%+v", qry.Shards), "query", req.GetQuery(), "step", req.GetStep()) diff --git a/pkg/querier/queryrange/downstreamer_test.go b/pkg/querier/queryrange/downstreamer_test.go index 24877aefd674f..a2fd85434723c 100644 --- a/pkg/querier/queryrange/downstreamer_test.go +++ b/pkg/querier/queryrange/downstreamer_test.go @@ -329,7 +329,7 @@ func TestInstanceDownstream(t *testing.T) { // for some reason these seemingly can't be checked in their own goroutines, // so we assign them to scoped variables for later comparison. got = req - want = ParamsToLokiRequest(params, queries[0].Shards.Encode()).WithQuery(expr.String()) + want = ParamsToLokiRequest(params, queries[0].Shards).WithQuery(expr.String()) return expectedResp(), nil }, diff --git a/pkg/querier/queryrange/roundtrip.go b/pkg/querier/queryrange/roundtrip.go index 4366cd5929825..70b0acf5a3bd5 100644 --- a/pkg/querier/queryrange/roundtrip.go +++ b/pkg/querier/queryrange/roundtrip.go @@ -91,21 +91,21 @@ func NewTripperware( } type roundTripper struct { - next, log, metric, series, labels, instantMetrics http.RoundTripper + next, log, metric, series, labels, instantMetric http.RoundTripper limits Limits } // newRoundTripper creates a new queryrange roundtripper -func newRoundTripper(next, log, metric, series, labels, instantMetrics http.RoundTripper, limits Limits) roundTripper { +func newRoundTripper(next, log, metric, series, labels, instantMetric http.RoundTripper, limits Limits) roundTripper { return roundTripper{ - log: log, - limits: limits, - metric: metric, - series: series, - labels: labels, - instantMetrics: instantMetrics, - next: next, + log: log, + limits: limits, + metric: metric, + series: series, + labels: labels, + instantMetric: instantMetric, + next: next, } } @@ -167,7 +167,7 @@ func (r roundTripper) RoundTrip(req *http.Request) (*http.Response, error) { } switch expr.(type) { case logql.SampleExpr: - return r.instantMetrics.RoundTrip(req) + return r.instantMetric.RoundTrip(req) default: return r.next.RoundTrip(req) } @@ -461,7 +461,7 @@ func NewInstantMetricTripperware( shardingMetrics *logql.ShardingMetrics, splitByMetrics *SplitByMetrics, ) (queryrange.Tripperware, error) { - queryRangeMiddleware := []queryrange.Middleware{StatsCollectorMiddleware()} + queryRangeMiddleware := []queryrange.Middleware{StatsCollectorMiddleware(), queryrange.NewLimitsMiddleware(limits)} if cfg.ShardedQueries { queryRangeMiddleware = append(queryRangeMiddleware, From b8e4f05f42fa613121764053c517ef8822ea52a2 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Wed, 21 Jul 2021 16:16:00 +0200 Subject: [PATCH 24/27] Use shars parameter in querier. --- pkg/querier/http.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/querier/http.go b/pkg/querier/http.go index ccb572ea9636a..3a5e85b875050 100644 --- a/pkg/querier/http.go +++ b/pkg/querier/http.go @@ -96,7 +96,7 @@ func (q *Querier) InstantQueryHandler(w http.ResponseWriter, r *http.Request) { 0, request.Direction, request.Limit, - nil, + request.Shards, ) query := q.engine.Query(params) result, err := query.Exec(ctx) From 060ba660d8254797f96daa651fd03e961dc78edd Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Thu, 22 Jul 2021 15:08:14 +0200 Subject: [PATCH 25/27] Return vector. --- pkg/querier/queryrange/roundtrip_test.go | 28 ++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/querier/queryrange/roundtrip_test.go b/pkg/querier/queryrange/roundtrip_test.go index e173dee0caf3f..d235bc14ff29b 100644 --- a/pkg/querier/queryrange/roundtrip_test.go +++ b/pkg/querier/queryrange/roundtrip_test.go @@ -67,6 +67,24 @@ var ( }, }, } + vector = promql.Vector{ + { + Point: promql.Point{ + T: toMs(testTime.Add(-4 * time.Hour)), + V: 0.013333333333333334, + }, + Metric: []labels.Label{ + { + Name: "filename", + Value: `/var/hostlog/apport.log`, + }, + { + Name: "job", + Value: "varlogs", + }, + }, + }, + } streams = logqlmodel.Streams{ { Entries: []logproto.Entry{ @@ -204,7 +222,9 @@ func TestLogFilterTripperware(t *testing.T) { func TestInstantQueryTripperware(t *testing.T) { - tpw, stopper, err := NewTripperware(testConfig, util_log.Logger, fakeLimits{}, chunk.SchemaConfig{}, 0, nil) + testShardingConfig := testConfig + testShardingConfig.ShardedQueries = true + tpw, stopper, err := NewTripperware(testShardingConfig, util_log.Logger, fakeLimits{}, chunk.SchemaConfig{}, 1*time.Second, nil) if stopper != nil { defer stopper.Stop() } @@ -216,7 +236,7 @@ func TestInstantQueryTripperware(t *testing.T) { lreq := &LokiInstantRequest{ Query: `sum by (job) (bytes_rate({cluster="dev-us-central-0"}[15m]))`, Limit: 1000, - Direction: logproto.FORWARD, + Direction: logproto.BACKWARD, Path: "/loki/api/v1/query", } @@ -228,7 +248,7 @@ func TestInstantQueryTripperware(t *testing.T) { err = user.InjectOrgIDIntoHTTPRequest(ctx, req) require.NoError(t, err) - count, h := promqlResult(streams) + count, h := promqlResult(vector) rt.setHandler(h) resp, err := tpw(rt).RoundTrip(req) require.Equal(t, 1, *count) @@ -236,7 +256,7 @@ func TestInstantQueryTripperware(t *testing.T) { lokiResponse, err := LokiCodec.DecodeResponse(ctx, resp, lreq) require.NoError(t, err) - require.IsType(t, &LokiResponse{}, lokiResponse) // Is LokiPromResponse in Docker compose + require.IsType(t, &LokiPromResponse{}, lokiResponse) } func TestSeriesTripperware(t *testing.T) { From baf7f0b14f9916708ee5f4050e255539ac0802d5 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Thu, 22 Jul 2021 15:25:08 +0200 Subject: [PATCH 26/27] Unify params. --- pkg/querier/queryrange/codec.go | 51 ++++++++++++++----------- pkg/querier/queryrange/querysharding.go | 8 ++-- pkg/querier/queryrange/stats.go | 14 ++----- pkg/querier/queryrange/stats_test.go | 6 +-- 4 files changed, 40 insertions(+), 39 deletions(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index f63b8d2778441..06d8315562c99 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -420,14 +420,16 @@ func (Codec) DecodeResponse(ctx context.Context, r *http.Response, req queryrang }, nil case loghttp.ResultTypeStream: // This is the same as in querysharding.go - var params logql.Params + params, err := paramsFromRequest(req) + if err != nil { + return nil, err + } + var path string switch r := req.(type) { case *LokiRequest: - params = paramsFromRangeRequest(r) path = r.GetPath() case *LokiInstantRequest: - params = paramsFromInstantRequest(r) path = r.GetPath() default: return nil, fmt.Errorf("expected *LokiRequest or *LokiInstantRequest, got (%T)", r) @@ -761,37 +763,46 @@ func (res LokiResponse) Count() int64 { return result } -type paramsWrapper struct { - *LokiRequest +func paramsFromRequest(req queryrange.Request) (logql.Params, error) { + switch r := req.(type) { + case *LokiRequest: + return ¶msRangeWrapper{ + LokiRequest: r, + }, nil + case *LokiInstantRequest: + return ¶msInstantWrapper{ + LokiInstantRequest: r, + }, nil + default: + return nil, fmt.Errorf("expected *LokiRequest or *LokiInstantRequest, got (%T)", r) + } } -func paramsFromRangeRequest(req queryrange.Request) *paramsWrapper { - return ¶msWrapper{ - LokiRequest: req.(*LokiRequest), - } +type paramsRangeWrapper struct { + *LokiRequest } -func (p paramsWrapper) Query() string { +func (p paramsRangeWrapper) Query() string { return p.GetQuery() } -func (p paramsWrapper) Start() time.Time { +func (p paramsRangeWrapper) Start() time.Time { return p.GetStartTs() } -func (p paramsWrapper) End() time.Time { +func (p paramsRangeWrapper) End() time.Time { return p.GetEndTs() } -func (p paramsWrapper) Step() time.Duration { +func (p paramsRangeWrapper) Step() time.Duration { return time.Duration(p.GetStep() * 1e6) } -func (p paramsWrapper) Interval() time.Duration { return 0 } -func (p paramsWrapper) Direction() logproto.Direction { +func (p paramsRangeWrapper) Interval() time.Duration { return 0 } +func (p paramsRangeWrapper) Direction() logproto.Direction { return p.GetDirection() } -func (p paramsWrapper) Limit() uint32 { return p.LokiRequest.Limit } -func (p paramsWrapper) Shards() []string { +func (p paramsRangeWrapper) Limit() uint32 { return p.LokiRequest.Limit } +func (p paramsRangeWrapper) Shards() []string { return p.GetShards() } @@ -799,12 +810,6 @@ type paramsInstantWrapper struct { *LokiInstantRequest } -func paramsFromInstantRequest(req queryrange.Request) *paramsInstantWrapper { - return ¶msInstantWrapper{ - LokiInstantRequest: req.(*LokiInstantRequest), - } -} - func (p paramsInstantWrapper) Query() string { return p.GetQuery() } diff --git a/pkg/querier/queryrange/querysharding.go b/pkg/querier/queryrange/querysharding.go index 5baad575793d0..7295ecb80e815 100644 --- a/pkg/querier/queryrange/querysharding.go +++ b/pkg/querier/queryrange/querysharding.go @@ -105,14 +105,16 @@ func (ast *astMapperware) Do(ctx context.Context, r queryrange.Request) (queryra return ast.next.Do(ctx, r) } - var params logql.Params + params, err := paramsFromRequest(r) + if err != nil { + return nil, err + } + var path string switch r := r.(type) { case *LokiRequest: - params = paramsFromRangeRequest(r) path = r.GetPath() case *LokiInstantRequest: - params = paramsFromInstantRequest(r) path = r.GetPath() default: return nil, fmt.Errorf("expected *LokiRequest or *LokiInstantRequest, got (%T)", r) diff --git a/pkg/querier/queryrange/stats.go b/pkg/querier/queryrange/stats.go index 641bc2e84f814..cda6cc3273430 100644 --- a/pkg/querier/queryrange/stats.go +++ b/pkg/querier/queryrange/stats.go @@ -107,19 +107,13 @@ func StatsCollectorMiddleware() queryrange.Middleware { } ctxValue := ctx.Value(ctxKey) if data, ok := ctxValue.(*queryData); ok { - - switch r := req.(type) { - case *LokiRequest: - data.params = paramsFromRangeRequest(r) - case *LokiInstantRequest: - data.params = paramsFromInstantRequest(r) - default: - level.Warn(logger).Log("msg", fmt.Sprintf("cannot compute params, unexpected type: %T", resp)) - } - data.recorded = true data.statistics = statistics data.result = res + data.params, err = paramsFromRequest(req) + if err != nil { + return nil, err + } } return resp, err }) diff --git a/pkg/querier/queryrange/stats_test.go b/pkg/querier/queryrange/stats_test.go index d18e3626bb8c5..61603f0dc2a28 100644 --- a/pkg/querier/queryrange/stats_test.go +++ b/pkg/querier/queryrange/stats_test.go @@ -86,7 +86,7 @@ func Test_StatsHTTP(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { data := r.Context().Value(ctxKey).(*queryData) data.recorded = true - data.params = paramsFromRangeRequest(&LokiRequest{ + data.params, _ = paramsFromRequest(&LokiRequest{ Query: "foo", Direction: logproto.BACKWARD, Limit: 100, @@ -106,7 +106,7 @@ func Test_StatsHTTP(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { data := r.Context().Value(ctxKey).(*queryData) data.recorded = true - data.params = paramsFromRangeRequest(&LokiRequest{ + data.params, _ = paramsFromRequest(&LokiRequest{ Query: "foo", Direction: logproto.BACKWARD, Limit: 100, @@ -127,7 +127,7 @@ func Test_StatsHTTP(t *testing.T) { http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { data := r.Context().Value(ctxKey).(*queryData) data.recorded = true - data.params = paramsFromRangeRequest(&LokiRequest{ + data.params, _ = paramsFromRequest(&LokiRequest{ Query: "foo", Direction: logproto.BACKWARD, Limit: 100, From 10f53b61ccb92064232311778b58177652d3e23a Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Fri, 23 Jul 2021 09:40:00 +0200 Subject: [PATCH 27/27] Change direction to FORWARD. --- pkg/querier/queryrange/roundtrip_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/querier/queryrange/roundtrip_test.go b/pkg/querier/queryrange/roundtrip_test.go index a236ecdcd0184..c49f74de13402 100644 --- a/pkg/querier/queryrange/roundtrip_test.go +++ b/pkg/querier/queryrange/roundtrip_test.go @@ -234,7 +234,7 @@ func TestInstantQueryTripperware(t *testing.T) { lreq := &LokiInstantRequest{ Query: `sum by (job) (bytes_rate({cluster="dev-us-central-0"}[15m]))`, Limit: 1000, - Direction: logproto.BACKWARD, + Direction: logproto.FORWARD, Path: "/loki/api/v1/query", }