Skip to content

parser: Allow parsing arbitrary functions - #12170

Merged
roidelapluie merged 2 commits into
prometheus:mainfrom
fpetkovski:parser-inject-functions
Jun 27, 2023
Merged

roidelapluie merged 2 commits into
prometheus:mainfrom
fpetkovski:parser-inject-functions

Conversation

@fpetkovski

Copy link
Copy Markdown
Contributor

In Thanos we would like to start experimenting with custom functions that are currently not part of the PromQL spec. We would do this by adding an implementation for those functions to the Thanos engine and allow users to decide which engine they want to use on a per-query basis.

Since we use the PromQL parser from Prometheus, injecting functions in the global Functions variable would mean they also become available for the Prometheus engine. To avoid this side-effect, this commit exposes a Parser interface in which the supported functions can be injected as an option. If no functions are injected, the parser implementation will default to the functions defined in the global Functions variable.

In Thanos we would like to start experimenting with custom functions that are
currently not part of the PromQL spec. We would do this by adding an implementation
for those functions in the Thanos engine: https://github.com/thanos-community/promql-engine and allow
users to decide which engine they want to use on a per-query basis.

Since we use the PromQL parser from Prometheus, injecting functions in the global `Functions` variable
would mean they also become available for the Prometheus engine. To avoid this side-effect, this commit
exposes a Parser interface in which the supported functions can be injected as an option. If not functions
are injected, the parser implementation will default to the functions defined in the global Functions variable.

Signed-off-by: Filip Petkovski <[email protected]>
@fpetkovski
fpetkovski force-pushed the parser-inject-functions branch from 75d0b4b to 97c7fff Compare March 22, 2023 09:15
Signed-off-by: Filip Petkovski <[email protected]>
@roidelapluie

Copy link
Copy Markdown
Member

While I appreciate your efforts, I cannot accept this pull request as we aim to maintain a consistent PromQL experience across the ecosystem. I would like to inquire about the types of functions that are not suitable for Prometheus upstream and whether they can be implemented (maybe even behind a feature flag).

(Additionally, some users have already been overriding the exposed Function.)

@fpetkovski

Copy link
Copy Markdown
Contributor Author

Thank you for the feedback. We wanted to experiment with the xrate/xincrease functions which have been discussed extensively in this issue: #3806.

There are several forks out there that already add support for these functions, but I don't think Prometheus has made a decision yet on whether they make sense in prometheus/prometheus.

@roidelapluie

Copy link
Copy Markdown
Member

In my opinion, we should move forward with implementing them in Prometheus behind a feature flag. Since the design document has stalled, with Alin commenting last, this is definitely something worth implementing and discussing upstream.

@fpetkovski

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback, and I am glad that Prometheus is willing to consider adding these functions under a feature flag.

I will close this PR now and we will try to get ourselves unblocked in the short term. Maybe it's worth starting a discussion on this topic during a dev summit?

@beorn7

beorn7 commented Mar 27, 2023

Copy link
Copy Markdown
Contributor

Personally, I'm still not convinced adding xrate and friends to standard PromQL is a net gain for the community.

However, coming from a very different angle, I am pretty convinced that we need to offer some way of injecting non-standard functions or aggregators into PromQL. This is mostly coming from the many discussions we had about adding specific statistical functions. There are just so many of them that we cannot simply add all, and it's really hard to make the call which ones are generally useful enough to make them "built-ins". We had discussed this at a dev-summit, and even contemplated adding some kind of scripting interface (embedded Lua, Python, R? 🤯 ), but maybe it has to be a simple way of adding functions at compile time. In any case, once there is a way of "loading custom libraries of PromQL functions", it would also be a way to offer something like xrate as a custom thing while still clearly communicating that it is not a standard "built-in" function yet.

@roidelapluie

roidelapluie commented Mar 27, 2023 via email

Copy link
Copy Markdown
Member

@codesome
codesome removed their request for review March 30, 2023 13:53
@roidelapluie

Copy link
Copy Markdown
Member

Thanks for your contribution, we have decided during our bug scrub to enable this change.

@roidelapluie
roidelapluie merged commit a605b81 into prometheus:main Jun 27, 2023
@fpetkovski

Copy link
Copy Markdown
Contributor Author

Thank you, much appreciated !

roidelapluie pushed a commit that referenced this pull request Sep 25, 2026
#17977 replaced the package-level parser flags with parser.Options and,
in doing so, removed parser.WithFunctions (added in #12170). Since then
the only way for a downstream PromQL engine to parse its own functions
is to add them to the global parser.Functions map, which also makes
them parseable by every other parser in the process, including the one
used by the Prometheus engine.

Add a Functions field to parser.Options. When set, it replaces the
default function set for that parser, as WithFunctions did; when nil,
the default parser.Functions set is used. NewParser copies the map so
later changes by the caller cannot affect or race with parsing, and
RegisterFeatures now reports the functions the parser actually accepts.

TestParseCustomFunctions used the internal newParserWithFunctions
helper and added its function to the global Functions map, leaking it
into the other tests of the package. It now uses the public API with
its own map, and the helper is removed.

Signed-off-by: Sandy Chen <[email protected]>
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.

3 participants