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

Implement offset modifier for range vector aggregation in LogQL #3455

Merged
merged 6 commits into from
Mar 22, 2021

Conversation

garrettlish
Copy link
Contributor

@garrettlish garrettlish commented Mar 9, 2021

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

  • Documentation added
  • Tests updated

@CLAassistant
Copy link

CLAassistant commented Mar 9, 2021

CLA assistant check
All committers have signed the CLA.

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 <garrett.li.sh@gmail.com>
@garrettlish
Copy link
Contributor Author

cc @wardbekker FYI

@garrettlish
Copy link
Contributor Author

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):

count_over_time({job="varlogs"} [5m])/count_over_time({job="varlogs"} [5m] offset 3h) > 1.5

@garrettlish
Copy link
Contributor Author

@owen-d WDYT?

@owen-d
Copy link
Member

owen-d commented Mar 18, 2021

Looks interesting! I'll find some time to give this a proper review.

Copy link
Member

@owen-d owen-d left a 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
Copy link
Member

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)
				}

Copy link
Contributor Author

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:
Copy link
Member

@owen-d owen-d Mar 19, 2021

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.

iter := newRangeVectorIterator(
it,
expr.left.interval.Nanoseconds(),
q.Step().Nanoseconds(),
q.Start().UnixNano(), q.End().UnixNano(),
q.Start().UnixNano(), q.End().UnixNano(), offset,
Copy link
Member

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.

Copy link
Contributor Author

@garrettlish garrettlish Mar 22, 2021

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.

Copy link
Contributor

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.

ts := r.current / 1e+6

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM for me.

Copy link
Contributor Author

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

Copy link
Contributor Author

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 {
Copy link
Member

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 owen-d added advice Issue that illustrates a problem and possible resolution. and removed advice Issue that illustrates a problem and possible resolution. labels Mar 21, 2021
@garrettlish
Copy link
Contributor Author

@owen-d thanks for your comments, I had resolved your comments except making the iterator offset aware (since I'd like to shift the returned samples timestamp from history timestamp to current timestamp), please help review the PR again, thanks!

Copy link
Contributor

@cyriltovena cyriltovena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cyriltovena
Copy link
Contributor

Thanks @garrettlish for your contribution, awesome work !

@cyriltovena cyriltovena merged commit f70c7ea into grafana:master Mar 22, 2021
kb000 added a commit to kb000/loki that referenced this pull request Oct 18, 2023
Bring back offset modifier doc section from grafana#3455 and lost in  grafana#4012
@kb000 kb000 mentioned this pull request Oct 18, 2023
7 tasks
JStickler pushed a commit that referenced this pull request Oct 19, 2023
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**
grafanabot pushed a commit that referenced this pull request Oct 19, 2023
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)
JStickler pushed a commit that referenced this pull request Oct 19, 2023
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 <kb000@users.noreply.github.com>
rhnasc pushed a commit to inloco/loki that referenced this pull request Apr 12, 2024
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**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement PromQL offset modifier for range vector in LogQL
4 participants