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

Ensure that TimeFilter's adjustments to a QueryRange Range are valid #663

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/promclient/multi_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type stubAPI struct {
labelNames func() []string
labelValues func(label string) model.LabelValues
query func() model.Value
queryRange func() model.Value
queryRange func(q string, r v1.Range) model.Value
series func() []model.LabelSet
getValue func() model.Value
metadata func() map[string][]v1.Metadata
Expand Down Expand Up @@ -51,7 +51,7 @@ func (s *stubAPI) QueryRange(ctx context.Context, query string, r v1.Range) (mod
if s.queryRange == nil {
return nil, nil, nil
}
return s.queryRange(), nil, nil
return s.queryRange(query, r), nil, nil
}

// Series finds series by label matchers.
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestMultiAPIMerging(t *testing.T) {
getSample(model.LabelSet{model.MetricNameLabel: "testmetric"}),
}
},
queryRange: func() model.Value {
queryRange: func(_ string, _ v1.Range) model.Value {
return model.Vector{
getSample(model.LabelSet{model.MetricNameLabel: "testmetric"}),
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/promclient/timefilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ func (tf *AbsoluteTimeFilter) QueryRange(ctx context.Context, query string, r v1

if tf.Truncate {
if !tf.Start.IsZero() && r.Start.Before(tf.Start) {
r.Start = tf.Start
remainder := tf.Start.Sub(r.Start) % r.Step
if remainder > 0 {
r.Start = tf.Start.Add(r.Step - remainder)
} else {
r.Start = tf.Start
}
}
if !tf.End.IsZero() && r.End.After(tf.End) {
r.End = tf.End
Expand Down Expand Up @@ -193,7 +198,12 @@ func (tf *RelativeTimeFilter) QueryRange(ctx context.Context, query string, r v1

if tf.Truncate {
if !tfStart.IsZero() && r.Start.Before(tfStart) {
r.Start = tfStart
remainder := tfStart.Sub(r.Start) % r.Step
if remainder > 0 {
r.Start = tfStart.Add(r.Step - remainder)
} else {
r.Start = tfStart
}
}
if !tfEnd.IsZero() && r.End.After(tfEnd) {
r.End = tfEnd
Expand Down
66 changes: 66 additions & 0 deletions pkg/promclient/timefilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
)

type timeFilterTestCase struct {
Expand Down Expand Up @@ -200,3 +201,68 @@ func TestRelativeTimeFilter(t *testing.T) {
})

}

// For time truncation we need to ensure that any new range's start aligns with a multiple of the step from the overall query start
// otherwise we get into a LOT of trouble with LookbackDelta as the timestamps of the result won't align properly
func TestAbsoluteTimeFilterStepAlignment(t *testing.T) {
var r v1.Range
stub := &stubAPI{
queryRange: func(_ string, rng v1.Range) model.Value {
r = rng
return nil
},
}
filterStart, _ := time.Parse(time.RFC3339, "2024-07-01T00:00:00Z")
api := &AbsoluteTimeFilter{
API: stub,
Start: filterStart,
Truncate: true,
}

start := time.Unix(1719738435, 0)
end := time.Unix(1719879274, 0)

api.QueryRange(context.TODO(), "foo", v1.Range{
Start: start,
End: end,
Step: 563 * time.Second,
})

remainder := r.Start.Sub(start) % r.Step
if remainder > 0 {
t.Fatalf("unexpected step misalignment!")
}
}

// For time truncation we need to ensure that any new range's start aligns with a multiple of the step from the overall query start
// otherwise we get into a LOT of trouble with LookbackDelta as the timestamps of the result won't align properly
func TestRelativeTimeFilterStepAlignment(t *testing.T) {
var r v1.Range
stub := &stubAPI{
queryRange: func(_ string, rng v1.Range) model.Value {
r = rng
return nil
},
}
dur, _ := time.ParseDuration("-2h")
api := &RelativeTimeFilter{
API: stub,
Start: &dur,
Truncate: true,
}

now := time.Now()
start := now.Add(-1 * time.Hour * 24)
end := now

api.QueryRange(context.TODO(), "foo", v1.Range{
Start: start,
End: end,
Step: 563 * time.Second,
})

remainder := r.Start.Sub(start) % r.Step
if remainder > 0 {
t.Fatalf("unexpected step misalignment!")
}
}
Loading