Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add step param to Patterns Query API #12703

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/loghttp/patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package loghttp

import (
"net/http"
"time"

"github.com/grafana/loki/v3/pkg/logproto"
)
Expand All @@ -15,6 +16,7 @@ func ParsePatternsQuery(r *http.Request) (*logproto.QueryPatternsRequest, error)
}
req.Start = start
req.End = end
req.Step = (time.Duration(defaultQueryRangeStep(start, end)) * time.Second).Milliseconds()

req.Query = query(r)
return req, nil
Expand Down
4 changes: 3 additions & 1 deletion pkg/logproto/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ func (m *VolumeRequest) LogToSpan(sp opentracing.Span) {
otlog.String("query", m.GetQuery()),
otlog.String("start", timestamp.Time(int64(m.From)).String()),
otlog.String("end", timestamp.Time(int64(m.Through)).String()),
otlog.String("step", time.Duration(m.Step).String()),
)
}

Expand Down Expand Up @@ -467,9 +468,10 @@ func (m *QueryPatternsRequest) WithStartEndForCache(start, end time.Time) result

func (m *QueryPatternsRequest) LogToSpan(sp opentracing.Span) {
fields := []otlog.Field{
otlog.String("query", m.GetQuery()),
otlog.String("start", m.Start.String()),
otlog.String("end", m.End.String()),
otlog.String("query", m.GetQuery()),
otlog.String("step", time.Duration(m.Step).String()),
}
sp.LogFields(fields...)
}
19 changes: 9 additions & 10 deletions pkg/pattern/drain/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
timeResolution = model.Time(int64(time.Second*10) / 1e6)
TimeResolution = model.Time(int64(time.Second*10) / 1e6)

defaultVolumeSize = 500

Expand All @@ -25,7 +25,7 @@ type Chunk struct {
}

func newChunk(ts model.Time) Chunk {
maxSize := int(maxChunkTime.Nanoseconds()/timeResolution.UnixNano()) + 1
maxSize := int(maxChunkTime.Nanoseconds()/TimeResolution.UnixNano()) + 1
v := Chunk{Samples: make([]logproto.PatternSample, 1, maxSize)}
v.Samples[0] = logproto.PatternSample{
Timestamp: ts,
Expand Down Expand Up @@ -66,18 +66,17 @@ func (c Chunk) ForRange(start, end, step model.Time) []logproto.PatternSample {
return c.Samples[i].Timestamp >= end
})
}
if step == timeResolution {
if step == TimeResolution {
return c.Samples[lo:hi]
}

// Re-scale samples into step-sized buckets
currentStep := truncateTimestamp(c.Samples[lo].Timestamp, step)
aggregatedSamples := []logproto.PatternSample{
{
Timestamp: currentStep,
Value: 0,
},
}
aggregatedSamples := make([]logproto.PatternSample, 0, ((c.Samples[hi-1].Timestamp-currentStep)/step)+1)
aggregatedSamples = append(aggregatedSamples, logproto.PatternSample{
Timestamp: currentStep,
Value: 0,
})
for _, sample := range c.Samples[lo:hi] {
if sample.Timestamp >= currentStep+step {
stepForSample := truncateTimestamp(sample.Timestamp, step)
Expand All @@ -96,7 +95,7 @@ func (c Chunk) ForRange(start, end, step model.Time) []logproto.PatternSample {
}

func (c *Chunks) Add(ts model.Time) {
t := truncateTimestamp(ts, timeResolution)
t := truncateTimestamp(ts, TimeResolution)

if len(*c) == 0 {
*c = append(*c, newChunk(t))
Expand Down
19 changes: 10 additions & 9 deletions pkg/pattern/drain/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ import (

func TestAdd(t *testing.T) {
cks := Chunks{}
cks.Add(timeResolution + 1)
cks.Add(timeResolution + 2)
cks.Add(2*timeResolution + 1)
cks.Add(TimeResolution + 1)
cks.Add(TimeResolution + 2)
cks.Add(2*TimeResolution + 1)
require.Equal(t, 1, len(cks))
require.Equal(t, 2, len(cks[0].Samples))
cks.Add(model.TimeFromUnixNano(time.Hour.Nanoseconds()) + timeResolution + 1)
cks.Add(model.TimeFromUnixNano(time.Hour.Nanoseconds()) + TimeResolution + 1)
require.Equal(t, 2, len(cks))
require.Equal(t, 1, len(cks[1].Samples))
}

func TestIterator(t *testing.T) {
cks := Chunks{}
cks.Add(timeResolution + 1)
cks.Add(timeResolution + 2)
cks.Add(2*timeResolution + 1)
cks.Add(model.TimeFromUnixNano(time.Hour.Nanoseconds()) + timeResolution + 1)
cks.Add(TimeResolution + 1)
cks.Add(TimeResolution + 2)
cks.Add(2*TimeResolution + 1)
cks.Add(model.TimeFromUnixNano(time.Hour.Nanoseconds()) + TimeResolution + 1)

it := cks.Iterator("test", model.Time(0), model.Time(time.Hour.Nanoseconds()), timeResolution)
it := cks.Iterator("test", model.Time(0), model.Time(time.Hour.Nanoseconds()), TimeResolution)
require.NotNil(t, it)

var samples []logproto.PatternSample
Expand Down Expand Up @@ -188,6 +188,7 @@ func TestForRange(t *testing.T) {
if !reflect.DeepEqual(result, tc.expected) {
t.Errorf("Expected %v, got %v", tc.expected, result)
}
require.Equal(t, len(result), cap(result), "Returned slice wasn't created at the correct capacity")
})
}
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/pattern/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"net/http"
"time"

"github.com/go-kit/log"
"github.com/grafana/dskit/httpgrpc"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/grafana/loki/v3/pkg/ingester/index"
"github.com/grafana/loki/v3/pkg/logproto"
"github.com/grafana/loki/v3/pkg/logql/syntax"
"github.com/grafana/loki/v3/pkg/pattern/drain"
"github.com/grafana/loki/v3/pkg/pattern/iter"
"github.com/grafana/loki/v3/pkg/util"
)
Expand Down Expand Up @@ -79,14 +79,14 @@ func (i *instance) Iterator(ctx context.Context, req *logproto.QueryPatternsRequ
return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error())
}
from, through := util.RoundToMilliseconds(req.Start, req.End)
step := req.Step
if step < 10*time.Second.Milliseconds() {
step = 10 * time.Second.Milliseconds()
step := model.Time(req.Step)
if step < drain.TimeResolution {
step = drain.TimeResolution
}

var iters []iter.Iterator
err = i.forMatchingStreams(matchers, func(s *stream) error {
iter, err := s.Iterator(ctx, from, through, model.Time(step))
iter, err := s.Iterator(ctx, from, through, step)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/querier/queryrange/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,9 +996,10 @@ func (c Codec) EncodeRequest(ctx context.Context, r queryrangebase.Request) (*ht
return req.WithContext(ctx), nil
case *logproto.QueryPatternsRequest:
params := url.Values{
"query": []string{request.GetQuery()},
"start": []string{fmt.Sprintf("%d", request.Start.UnixNano())},
"end": []string{fmt.Sprintf("%d", request.End.UnixNano())},
"query": []string{request.GetQuery()},
"step": []string{fmt.Sprintf("%d", request.GetStep())},
}

u := &url.URL{
Expand Down
Loading