Skip to content

proposal: trim query_range response to user-requested time window#7288

Open
venkatchinmay wants to merge 1 commit intocortexproject:masterfrom
venkatchinmay:proposal/query-range-response-trimming
Open

proposal: trim query_range response to user-requested time window#7288
venkatchinmay wants to merge 1 commit intocortexproject:masterfrom
venkatchinmay:proposal/query-range-response-trimming

Conversation

@venkatchinmay
Copy link
Copy Markdown
Contributor

@venkatchinmay venkatchinmay commented Feb 21, 2026

Fixes #7289

What

Adds docs/proposals/query-range-response-trimming.md — a proposal for a new opt-in middleware that clips the query_range response to the exact [start, end] window requested by the user.

Problem

When querier.align-querier-with-step: true is enabled, StepAlignMiddleware floors the user's start time to the nearest step boundary before the rest of the middleware chain runs:

User requests:   start=09:01, end=10:00, step=10m
After alignment: start=09:00, end=10:00

The original start=09:01 is permanently lost — no downstream middleware can recover it to trim the response. The user receives data from 09:00 even though they asked for 09:01.

The same drift can occur from SplitByIntervalMiddleware's nextIntervalBoundary rounding.

Proposed Fix

A new RangeTrimMiddleware as the outermost middleware, enabled via:

querier:
  trim_response_to_requested_range: true  # default: false

It captures the original start/end before any mutation, lets the full stack run internally (alignment, splitting, caching, sharding), then trims the final response using the existing Extractor.Extract() interface — which already handles per-sample and per-step-stats trimming correctly.

Key Points

  • Opt-in — default false, no behaviour change for existing deployments
  • No cache impact — cache continues to operate with aligned times internally
  • Reuses existing code — extractSampleStreams() + extractStats() already handle the trimming logic
  • Implementation PR will follow once the proposal is approved

Related

  • pkg/querier/tripperware/queryrange/step_align.go
  • pkg/querier/tripperware/queryrange/results_cache.go

Add docs/proposals/query-range-response-trimming.md proposing
a new opt-in RangeTrimMiddleware (querier.trim-response-to-requested-range)
that clips the final query_range response back to the exact [start, end]
window requested by the user.

This fixes data leakage outside the requested range caused by
StepAlignMiddleware flooring start to the nearest step boundary, and
SplitByIntervalMiddleware boundary rounding.

Author: Chinmay Venkat
Signed-off-by: Your Name <you@example.com>
@dosubot
Copy link
Copy Markdown

dosubot bot commented Feb 21, 2026

Related Documentation

Checked 0 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

@SungJin1212
Copy link
Copy Markdown
Member

@venkatchinmay
Thanks for the proposal.
I have a question. The response contains [9:10, 9:20,... 10:00] in the above example, if we enable the trim option, right?

@venkatchinmay
Copy link
Copy Markdown
Contributor Author

@venkatchinmay Thanks for the proposal. I have a question. The response contains [9:10, 9:20,... 10:00] in the above example, if we enable the trim option, right?

yes @SungJin1212

Copy link
Copy Markdown
Member

@friedrichg friedrichg left a comment

Choose a reason for hiding this comment

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

Sorry for the late response. I appreciate the detail


## Proposed Solution

Add a `RangeTrimMiddleware` as the **outermost** middleware in the chain, controlled by a new opt-in configuration flag `querier.trim-response-to-requested-range`.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I prefer if we don't add any flag. It's a bugfix, if we are returning more samples than we should.


When `querier.align-querier-with-step: true` is configured, the query frontend modifies the user's requested `start` time by flooring it to the nearest `step` boundary **before** passing the request to the results cache and downstream querier.

This causes the response to contain data points **outside** the user's originally requested time range.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks like a bug to me. can you create a test case to trigger this behavior ?.

If you can trigger the behavior and is buggy, you don't need a proposal. You just need a unit test that triggers it and the fix.

The only thing though is what happens if we have increased CPU usage or less cache efficiency. Those things will matter in this case too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

query_range response contains samples outside requested [start, end] when align_queries_with_step=true

3 participants