feat(offset): implement LogQL offset directive — shift eval window for range and instant queries#357
Merged
Merged
Conversation
Contributor
PR Quality ReportCompared against base branch Coverage and tests
Compatibility
Performance smokeLower CPU cost (
State
|
…lause Adds logqlOffsetRE and extractLogQLOffset() to query_translation.go. The function finds "] offset <dur>" clauses, strips all occurrences, and returns the parsed duration. Errors on multiple distinct offsets. Unit-tested with 8 cases covering no-offset, simple, negative, 1d, outer aggregation, error, and repeated-same-offset scenarios.
… mutation safety Move the LogQL offset extraction block to run after resolveGrafanaRangeTemplateTokens but before preferWorkingParser so that parser probing uses the shifted historical window where offset data actually lives. Add Form map alias safety comment.
…r instant queries Extends the offset directive support to instant queries (GET /loki/api/v1/query). Strips the offset clause from the LogQL query and shifts the eval `time` parameter backward by `offsetDur` before calling `preferWorkingParser`, matching the same pattern already applied in `handleQueryRange` for range queries. Adds three integration tests: offset shifts time, no-offset leaves time untouched, and multiple distinct offsets return HTTP 400.
…hift
When a client omits the `time` parameter (Loki allows this, defaulting
to now), parseLokiTimeToUnixNano("") returned (0, false) and the offset
shift was silently skipped. Materialise the default before parsing so
the offset is always applied.
d05aa87 to
7a1f55c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
offsetdirective gap tracked in KNOWN_ISSUES.md: queries likerate({app="nginx"}[5m] offset 1h)previously silently stripped the offset; they now correctly shift the evaluation windowextractLogQLOffset()to parse and strip theoffsetclause from LogQL; multiple distinct offsets in the same query return HTTP 400handleQueryRangeshiftsstart/endbackward by the offset duration before dispatching to VictoriaLogs;handleQueryshiftstime(defaulting to now when omitted)preferWorkingParserso the parser probe targets the shifted historical windowTest Plan
go test ./internal/proxy/... -run 'Offset|offset' -v— 18 offset unit + integration tests passgo test ./...— 2518 tests, no regressionsoffset 1hreturns data from 1 hour ago (not current window)offset 1h+offset 2h) returns HTTP 400/loki/api/v1/query) withoffset 30mand notimeparam shifts correctly to now-30m