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

Workaround for __name__ grouping in avg() #280

Merged
merged 2 commits into from
Mar 6, 2020
Merged

Workaround for __name__ grouping in avg() #280

merged 2 commits into from
Mar 6, 2020

Conversation

jacksontj
Copy link
Owner

@jacksontj jacksontj commented Mar 6, 2020

Right now promql's BinaryExpr strips the name label -- which breaks
the avg() optimizations in the NodeReplacer. This patch implements a
relabel workaround (which is nasty, but works) to relabel __name__ ->
__name and then back.

Fixes #274

Special thanks to @john-delivuk for the debugging help!

TODO:

  • Investigate mechanism without string() and parse

@@ -115,3 +115,9 @@ func CloneExpr(expr promql.Expr) (newExpr promql.Expr) {
newExpr, _ = promql.ParseExpr(expr.String())
return
}

// PreserveLabel wraps the input expression with a label replace in order to preserve the metadata through binary expressions
func PreserveLabel(expr promql.Expr, srcLabel string, dstLabel string) (relabelExpress promql.Expr) {
Copy link
Owner Author

Choose a reason for hiding this comment

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

I'd like to find a way to create this object without having to string and unstring everything if possible (this works, just feels nasty)

Copy link
Owner Author

Choose a reason for hiding this comment

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

I've looked a bit into this and the promql package doesn't make this easy. I also ran a benchmark and it seems that the perf hit is non-existant:

benchmark                                                               old ns/op     new ns/op     delta
BenchmarkEvaluations/benchdata/aggregators.test/direct-4                20803         20102         -3.37%
BenchmarkEvaluations/benchdata/aggregators.test/promxy-4                232304        226409        -2.54%
BenchmarkEvaluations/benchdata/aggregators.test/promxy_remoteread-4     233642        231085        -1.09%

benchmark                                                               old allocs     new allocs     delta
BenchmarkEvaluations/benchdata/aggregators.test/direct-4                143            143            +0.00%
BenchmarkEvaluations/benchdata/aggregators.test/promxy-4                572            571            -0.17%
BenchmarkEvaluations/benchdata/aggregators.test/promxy_remoteread-4     569            569            +0.00%

benchmark                                                               old bytes     new bytes     delta
BenchmarkEvaluations/benchdata/aggregators.test/direct-4                7517          7517          +0.00%
BenchmarkEvaluations/benchdata/aggregators.test/promxy-4                37130         36696         -1.17%
BenchmarkEvaluations/benchdata/aggregators.test/promxy_remoteread-4     36737         36776         +0.11%

So assuming tests pass, I'll rebase/squash and we should be good to go.

Choose a reason for hiding this comment

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

I'm new to these libraries so I'll continue to dig for a better option. I'm not sure if there is a better way to run the label_replace function as it has to be outside of the aggregate and not inside.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Besides the fact that this is ugly (String-ing and re-parsing) this seems fine; I was worried that the re-parse would be dramatically slower, but it seems its not noticeable in the scheme of things :)

Right now promql's BinaryExpr strips the __name__ label -- which breaks
the avg() optimizations in the NodeReplacer. This patch implements a
relabel workaround (which is nasty, but works) to relabel __name__ ->
__name and then back.

Fixes #274

Special thanks to @john-delivuk for the debugging help!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Metric names not preserved in query
2 participants