-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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
Add User Defined Functions to PromQL #12851
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
…e config Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
Signed-off-by: Jeanette Tan <jeanette.tan@grafana.com>
The current test failure seems transient. |
Thank you very much @zenador . That looks really cool. We had discussed embedding a scripting engine before at the dev-summit.
|
I think you should set out your requirements and proposal in a GitHub issue, then link this PR to it. Also, would this help with #11609? |
I think #11609 is aiming for adding new concepts to PromQL itself, while this PR uses a different language (Tengo) to add new functions to PromQL (which you could already do easily in Go if you are fine with compiling your own Prometheus code – this PR just allows to "script" functions in a Go-like language and run them within an already compiled standard Prometheus.) |
@beorn7 Sure, do you mean discuss in a meeting, on a Github issue/PR or at the dev summit? I chose Tengo for this implementation as their benchmarks indicate that it is faster than the alternatives (including gpython), though I didn't check the benchmarks closely. @bboreham Embedding a scripting language is something that has been discussed previously (it's also a dev summit item) and I think others have more context on this. I couldn't find an issue though, so please let me know if I should create one @beorn7. I think it can help with #11609 but not directly, in that it doesn't do exactly as requested but it should be able to achieve the goals in another way. It doesn't allow defining variables in PromQL as requested there, but you can define variables in Tengo for a new UDF (so the UDF code should be more readable), and you can reuse the UDF in multiple places, so queries should be shorter and more understandable overall. As mentioned in the dev summit item, it would help with issues like #5514 #12497 and #9016 where users want to use new functions, but Prometheus wants to be selective about the officially supported functions that need to be maintained. This way, users can add any functions they like without needing it to be accepted by Prometheus first (and it seems very difficult to push for new functions to be accepted), and functions can have many varieties (see MAD and the discussion on holt winters), so users can choose which variety they want to use for themselves. |
Since the dev summit is literally next week, I would say we can just wait to see the topic discussed and take it from there. |
Unfortunately, the dev summit didn't manage to discuss the topic relevant to this PR. It will still happen eventually on the monthly online summits, but maybe we can at least get an idea how to proceed by discussing it here. @juliusv @roidelapluie you two might be the most relevant custodians of PromQL, so flagging you (but I don't want to exclude anyone else, please speak up). What do you generally think about this approach? Personally, I think this is probably worth a design doc, but your input would good to find out about the direction to take in the doc. Or worst case, if you are fundamentally against this, we could discuss the reasons before investing a lot of effort into writing a design doc. |
We discussed this PR and the topics around it at the recent dev summit. I would like to summarize the relevant outcome here. Generally, we didn't come to a consensus that we want this. However, there was a fairly easy consensus that more functions should be accepted into PromQL behind a feature flag. The bar was set to "at least somewhat generally useful" and "cannot be easily modeled by existing PromQL primitives". Since the new functions will be behind a feature flag, there is no danger of a "second holt_winters" (where a function was added under that name that isn't the "real" Holt Winters and was also of limited use, but now we have to maintain it forever). New functions can then be graduated to stable (or removed again) depending on the experience made in the wild. Adding native functions more easily would reduce the weight of one of the basic motivations behind this PR. Which in turn would increase the relative weight of the concerns. While performance concerns were mentioned, the main concerns were more about operational aspects, like the following:
In favor of this approach, people mentioned the ease of adding new functions for problems at hand and enabling quick experimentation. This could also be used to see what kind of functions users implement most often, so that we can take that as an inspiration for future built-in PromQL functions. To avoid name collisions, UDFs should come with some unique prefix (so that the later introduction of built-in functions of the same name doesn't cause an error) or maybe UDFs can be called through a special built-in PromQL function (e.g. Overall, I would recommend to wait how more openness towards adding native functions plays out. Right now, it will probably be a lot of effort to get sufficient buy-in for UDFs via an embedded scripting engine. |
Noted, thanks for the summary! Just an FYI, this PR already avoids name collisions by enforcing a |
This PR adds User Defined Functions (UDFs) to PromQL.
Why use UDFs?
Limitations of the current implementation:
Example YML (add to main Prometheus config):
Benchmark results (before is native function, after is UDF):
BenchmarkStdDevOverTime is a better comparison as the native function and UDF are implemented in very similar ways. For BenchmarkMadOverTime, the implementations are very different, and there is room to optimise the UDF performance by using more efficient methods to calculate the median.