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

Alerting: Make time range query parameters not required when querying Loki #62985

Merged
merged 2 commits into from
Feb 7, 2023

Conversation

alexweav
Copy link
Contributor

@alexweav alexweav commented Feb 6, 2023

What is this feature?

Now we only forward the from/to query parameters if they are supplied, rather than all the time. It will fall back to Loki's default time range, which is usually the last hour. Although most of our clients will need to specify a longer time range, this still improves basic usability of the API. Also, such clients only need to specify from and can omit to most of the time.

Also renames the client method query to rangeQuery to be clear as to which query type we're using. For some queries, it might be advantageous to use an instant query instead. We can evaluate that in the future.

Why do we need this feature?

Nice quality-of-life for clients. UI can omit the to parameter when it rolls out. Doesn't affect datasource-based clients. Not compat breaking.

Which issue(s) does this PR fix?:

contrib #48359

Special notes for your reviewer:

@alexweav alexweav added this to the 9.5.0 milestone Feb 6, 2023
@alexweav alexweav requested a review from a team February 6, 2023 21:40
Copy link
Contributor

@gotjosh gotjosh left a comment

Choose a reason for hiding this comment

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

LGTM, but please address my comment.

Comment on lines 202 to 207
if start != 0 {
values.Set("start", fmt.Sprintf("%d", start))
}
if end != 0 {
values.Set("end", fmt.Sprintf("%d", end))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd document the behaviour of range query defaults here - When no start or end timestamps are sent, Loki will default to 1h for the start timestamp and now for the end time.

One more thing that I don't like is that start and end are kind-of like options parameters but you must specify something at the function call - can we at least document the function, it so that users of the method understand that behaviour?

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps it might make sense to use a QueryOptions struct instead?

type QueryOptions struct {
    Selectors []Selector
    Start     int64 // an optional start time
    End       int64 // an optional end time
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at this with fresh eyes, I took a new approach that I think addresses all these.

Now, we move this up a layer - the client still requires timestamps, but we calculate our own defaults instead.

  1. The layer above already uses a query options struct like in @grobinson-grafana's suggestion.
  2. No need for docs to explain weird edge case behavior at the client level. Code is more clear.
  3. 1 hour is honestly not a good default for state history. It's too short. In the new approach, we can add our own default.
  4. Using Grafana's time.Now() rather than Loki's is generally more correct in the event that system clocks do not align.
  5. Better single responsibility principle - let's keep loki_http.go focused on the actual HTTP communication, the default range is app logic that's agnostic of transport.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm with George - QueryOptions is what I would expect. Aligns with open/close and SRP.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, nvm - I see what you mean with "moving up a layer". LGTM.

@alexweav alexweav merged commit f80bf11 into main Feb 7, 2023
@alexweav alexweav deleted the alexweav/embed-state-transition branch February 7, 2023 20:26
ryantxu pushed a commit that referenced this pull request Mar 2, 2023
… Loki (#62985)

* Make from and to not required

* Move default range calculation up to loki.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants