-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
promql: add sort_by_label and sort_by_label_desc functions #11299
Conversation
704437c
to
298497e
Compare
How difficult would it be to make this a vararg function to be able to sort by multiple labels?
|
@roidelapluie I can look into making it into a vararg function to be able to sort by multiple labels. Is there an existing function that already accepts a list of strings as an argument that I can look at? |
|
1a1be49
to
84d2e6d
Compare
@roidelapluie The |
@@ -401,6 +401,14 @@ in ascending order. | |||
|
|||
Same as `sort`, but sorts in descending order. | |||
|
|||
## `sort_by_label()` | |||
|
|||
`sort_by_label(v instant-vector, label string, ...)` returns vector elements sorted by their label values and sample value in case of label values being equal, in ascending order. |
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.
Hmm, I wonder if we shouldn't keep this completely independent from the sample value and instead use other label values to break the sorting tie? That way, sort results would be stable over time, given the same set of time series.
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 agree with Julius. We could also decide not to sort at all in that case, so you can do sort_by_label(sort(up),"job")
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.
Yeah, that sounds even better.
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.
So I should remove the "fallback" to comparing the value in the sort_by_label
functions? This might make the test unstable, though I have to re-check if that happens without this "fallback" compare.
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 tests should be stable even without this
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.
we should make it clear that labels are sorted lexicographically
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.
@roidelapluie Without the value comparison the test fails no matter in what order the result in the test(s) are placed.
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.
That is strange. The order should be predictible.
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.
It seems that it is not predictable as every X run the test fails and complains about the order of the result.
How should we approach this that we can hopefully get this functionality integrated?
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.
From my perspective, the fix here is to sort the values using the sort
/sort_desc
functions and then do the sort by label(s).
promql/functions.go
Outdated
continue | ||
case 1: | ||
return false | ||
} |
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.
we need a return at the end
Note that range queries are always sorted by label sets, right at the end: Line 722 in 8b863c4
Perhaps you could mention in the documentation this is intended for instant queries. |
That same comment would apply to I think we can either omit that comment on the new functions as well, or add a comment to the existing sort functions too. Something like:
|
I think it would be easy to work out that Whereas |
Ok yeah, that makes sense. Or we could actually make it work for range queries as well, but I doubt that's worth it. So then let's just add a comment for the new functions. |
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.
Hi, we discussed this PR at the Prometheus dev summit today; we would like to move forward, but would like to ask you to put these functions behind a feature flag.
I had a couple of comments about the implementation too.
promql/functions.go
Outdated
@@ -1218,6 +1238,78 @@ func (s *vectorByReverseValueHeap) Pop() interface{} { | |||
return el | |||
} | |||
|
|||
type vectorByLabelHeap struct { |
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.
What does the word "Heap" denote here?
promql/functions.go
Outdated
// Incase the labels are the same, NaN should sort to the bottom, so take | ||
// ascending sort with NaN first and reverse it. | ||
byLabelSorter := vectorByLabelHeap{vector: vals[0].(Vector), labels: stringSliceFromArgs(args[1:])} | ||
sort.Sort(sort.Reverse(byLabelSorter)) |
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.
slices.SortFunc
is probably neater now.
cc @galexrt are you still willing to work on this, rebase this pull request and put the functions behind a feature flag? Thanks! |
Oh that totally fell of my radar, yes I am willing to work on that. Do you have a link to a feature flag example somewhere that I can use as an example? |
03b3ecb
to
b14c014
Compare
You can look at the engine ops EnableNegativeOffset which used to be a feature flag |
So should I dynamically add the |
I think a proper error saying that the feature needs to be enabled if the feature flag is not set would be better. However, I don't know if we can properly error from promql functions so we might need to be creative. |
a054cc4
to
66ff46b
Compare
@roidelapluie I think I have found a way to check the feature flag in the evaluator. Please take another look at the latest code changes, thanks! |
bff4451
to
ae874fd
Compare
@roidelapluie I have updated the PR to resolve the conflicts. Any news on this? |
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.
Thank you, looks pretty good to me now!
It seems like the main outstanding question is around sort stability and whether we should resort to:
- Sorting by sample value
- Sorting by the remaining label set
- Do nothing
I would be for sorting by the remaining label set if that is easily doable (hopefully just a small extension of the SortFunc for the 0
string comparison case?). Sorting by sample value seems too unrelated to the label sorting and is also not necessarily always stable, as two series can and often do have the same sample value.
ae874fd
to
c102153
Compare
|
||
# metrics_path defaults to '/metrics' | ||
# scheme defaults to 'http'. | ||
|
||
static_configs: | ||
- targets: ["localhost:9090"] | ||
- targets: ["localhost:9138"] |
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.
Looks like some other unrelated changes crept into the PR here now?
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 have removed this unrelated change, PTAL
a9160ce
to
224e642
Compare
Thanks! I see the tests failing with this error currently:
|
@@ -20,7 +20,7 @@ rule_files: | |||
# Here it's Prometheus itself. | |||
scrape_configs: | |||
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. | |||
- job_name: "prometheus" | |||
- job_name: "extended-ceph-exporter" |
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 still see this unrelated change in here?
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.
Fixed, all unrelated changes should be gone now
224e642
to
dbeef3d
Compare
I have changed the sorting logic slightly, and it should now work better when more than 2 labels are involved. PTAL |
This adds functions to sort a vector by its label value. Based on prometheus#1533 Signed-off-by: Alexander Trost <galexrt@googlemail.com>
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
dbeef3d
to
c1ec6ae
Compare
Thanks! |
Thank you @galexrt ! This is a great achievement! |
@bobrik Agreed, good point! It's not yet too late to change things, since the new sorting functions are added under an experimental feature flag. Do you want to send a PR? |
Here's my attempt: #13411. |
This adds functions to sort a vector by its label value.
Based on #1533
Signed-off-by: Alexander Trost galexrt@googlemail.com
There was some discussion around adding this functionality to PromQL/ Prometheus in the original PR #1533, though based on the various comments from users and now myself stumbling upon this while trying to visualize certain metrics sorted by label value, I would gladly see this topic revisited.