-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Implement offset modifier for range vector aggregation in LogQL #3455
Conversation
implementation for grafana#2785, the offset modifier allows changing the time offset for range vectors in a query to support e.g. selective timeshift in Grafana Signed-off-by: garrettlish <[email protected]>
cc @wardbekker FYI |
the major use case for offset modifier is comparing current data with historical data. For example (generate an alert if log count increased 50% compares to 3 hours ago):
|
@owen-d WDYT? |
Looks interesting! I'll find some time to give this a proper review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR looks great - I'm always impressed when people dive into the logql code. Nice work!
I'd like a test or two in engine_test.go
that verifies we're only querying data at the correct bounds (code looks good, but would like tests to mirror that). Other than that and some small suggestions, it looks good.
pkg/logql/ast.go
Outdated
@@ -499,6 +499,7 @@ func newUnwrapExpr(id string, operation string) *unwrapExpr { | |||
type logRange struct { | |||
left LogSelectorExpr | |||
interval time.Duration | |||
offset *offsetExpr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small suggestion: it might be easier to simply have offset as time.Duration
. since offset=0
is the same as a nil offset, we could easily do
start := q.Start().Add(-rangExpr.left.interval).Add(-rangExpr.left.offset.offset)
rather than using the more clunky if statements:
if rangExpr.left.offset != nil {
start := start.Add(-rangExpr.left.offset.offset)
end = end.Add(-rangExpr.left.offset.offset)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea, will make the change
@@ -133,19 +135,31 @@ logExpr: | |||
; | |||
|
|||
logRangeExpr: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love how we're duplicating all paths here. I know we've done this in the past, but it's getting out of hand (not your fault).
@cyriltovena we should consider refactoring this a bit, but I'm fine with this PR.
pkg/logql/evaluator.go
Outdated
iter := newRangeVectorIterator( | ||
it, | ||
expr.left.interval.Nanoseconds(), | ||
q.Step().Nanoseconds(), | ||
q.Start().UnixNano(), q.End().UnixNano(), | ||
q.Start().UnixNano(), q.End().UnixNano(), offset, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can simplify this as well. There's no need to make the iterator offset
aware if we just pass (start-offset, end-offset)
to it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@owen-d thanks for your comments. I am passing the offset to iterator is because I'd like to shift the returned samples timestamp from history timestamp to current timestamp, otherwise, the returned samples timestamp are historical timestamp which is not meaningful. Any thoughts? Please advise, thanks!
For example:
Assume current time is 17:00, expression - count_over_time({job="varlogs"} [1m] offset 3h)
returns 14:00 samples, but count_over_time({job="varlogs"} [1m])
returns 17:00 samples, then the expression - count_over_time({job="varlogs"} [1m])/count_over_time({job="varlogs"} [1m] offset 3h) > 1.5
cannot work since these sub-expressions are returning inconsistent timestamp.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally had the same assumption as Owen, eg. you could just pass in the modified start/end.
But now with your explanation I understand why it's like this.
I think you could only offset back L148 only and that would be fine.
loki/pkg/logql/range_vector.go
Line 148 in 05e14f9
ts := r.current / 1e+6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @cyriltovena for the correction, I noticed the same thing and made changed in https://github.com/grafana/loki/pull/3455/files#diff-f4c7f355512099579b5ea8fa4ceceb6d1eeb2b99b10233b22e0e12ce571b25d1R152 already
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should add a test with an offset different than zero for the range vector iterator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise LGTM for me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sense, let me add test, thanks @cyriltovena
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cyriltovena added non-zero offset test - 1b8f454, please help review again, thanks!
} | ||
|
||
func newRangeVectorIterator( | ||
it iter.PeekingSampleIterator, | ||
selRange, step, start, end int64) *rangeVectorIterator { | ||
selRange, step, start, end, offset int64) *rangeVectorIterator { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mirroring previous comment, I don't think we need to make the iterator offset
aware.
@owen-d thanks for your comments, I had resolved your comments except making the iterator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks @garrettlish for your contribution, awesome work ! |
Bring back offset modifier doc section from grafana#3455 and lost in grafana#4012
Bring back offset modifier doc section from #3455 and lost in #4012 **What this PR does / why we need it**: **Which issue(s) this PR fixes**: Fixes regression from #2785 **Special notes for your reviewer**: **Checklist** - [x] Reviewed the [`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md) guide (**required**) - [x] Documentation added - [ ] Tests updated **N/A** - [ ] `CHANGELOG.md` updated **N/A** - [ ] If the change is worth mentioning in the release notes, add `add-to-release-notes` label **N/A** - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/setup/upgrade/_index.md` **N/A** - [ ] For Helm chart changes bump the Helm chart version in `production/helm/loki/Chart.yaml` and update `production/helm/loki/CHANGELOG.md` and `production/helm/loki/README.md`. [Example PR](d10549e) **N/A**
Bring back offset modifier doc section from #3455 and lost in #4012 **What this PR does / why we need it**: **Which issue(s) this PR fixes**: Fixes regression from #2785 **Special notes for your reviewer**: **Checklist** - [x] Reviewed the [`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md) guide (**required**) - [x] Documentation added - [ ] Tests updated **N/A** - [ ] `CHANGELOG.md` updated **N/A** - [ ] If the change is worth mentioning in the release notes, add `add-to-release-notes` label **N/A** - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/setup/upgrade/_index.md` **N/A** - [ ] For Helm chart changes bump the Helm chart version in `production/helm/loki/Chart.yaml` and update `production/helm/loki/CHANGELOG.md` and `production/helm/loki/README.md`. [Example PR](d10549e) **N/A** (cherry picked from commit 8645519)
Backport 8645519 from #10960 --- Bring back offset modifier doc section from #3455 and lost in #4012 **What this PR does / why we need it**: **Which issue(s) this PR fixes**: Fixes regression from #2785 **Special notes for your reviewer**: **Checklist** - [x] Reviewed the [`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md) guide (**required**) - [x] Documentation added - [ ] Tests updated **N/A** - [ ] `CHANGELOG.md` updated **N/A** - [ ] If the change is worth mentioning in the release notes, add `add-to-release-notes` label **N/A** - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/setup/upgrade/_index.md` **N/A** - [ ] For Helm chart changes bump the Helm chart version in `production/helm/loki/Chart.yaml` and update `production/helm/loki/CHANGELOG.md` and `production/helm/loki/README.md`. [Example PR](d10549e) **N/A** Co-authored-by: Kevin Burek <[email protected]>
Bring back offset modifier doc section from grafana#3455 and lost in grafana#4012 **What this PR does / why we need it**: **Which issue(s) this PR fixes**: Fixes regression from grafana#2785 **Special notes for your reviewer**: **Checklist** - [x] Reviewed the [`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md) guide (**required**) - [x] Documentation added - [ ] Tests updated **N/A** - [ ] `CHANGELOG.md` updated **N/A** - [ ] If the change is worth mentioning in the release notes, add `add-to-release-notes` label **N/A** - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/setup/upgrade/_index.md` **N/A** - [ ] For Helm chart changes bump the Helm chart version in `production/helm/loki/Chart.yaml` and update `production/helm/loki/CHANGELOG.md` and `production/helm/loki/README.md`. [Example PR](grafana@d10549e) **N/A**
What this PR does / why we need it:
Enable offset modifier for range vector aggregation in LogQL. The offset modifier allows changing the time offset for range vectors in a query to support e.g. selective timeshift in Grafana.
Which issue(s) this PR fixes:
Fixes #2785
Special notes for your reviewer:
Checklist