Repository navigation
promql/parser: allow custom functions via parser.Options - #19830
Merged
roidelapluie merged 1 commit intoSep 25, 2026
Merged
Conversation
prometheus#17977 replaced the package-level parser flags with parser.Options and, in doing so, removed parser.WithFunctions (added in prometheus#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]>
sandy2008
requested review from
a team,
roidelapluie and
vpranckaitis
as code owners
September 25, 2026 04:54
roidelapluie
approved these changes
Sep 25, 2026
This was referenced Sep 25, 2026
1 of 2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
This adds a
Functionsfield toparser.Options, so a parser can be created with its own set of functions:Functionskeeps using the defaultparser.Functionsset.parser.WithFunctionshad. An empty map accepts no functions.NewParsercopies the map. Later changes to the caller's map cannot affect parsing or race with it.RegisterFeaturesreports what the parser actually accepts. It lists the functions from this parser's set, not the global one.Why
#12170 added
parser.WithFunctionsfor downstream PromQL engines. The Thanos engine, thanos-io/promql-engine, uses it to parse its extraxrate,xincreaseandxdeltafunctions without adding them to the globalparser.Functionsmap. Adding them there would make them parseable by every other parser in the process, including the Prometheus engine's.#17977 moved parser configuration into
parser.Optionsand droppedWithFunctions. I couldn't find any discussion of the removal in that PR, so it looks incidental. Since then, the only way left is to mutate the global map. @GiedriusS asked about this on #17977 (comment). It is what currently keeps thanos-io/promql-engine and Thanos, and in turn Cortex, from moving past Prometheus 3.10.The internal
newParserWithFunctionshelper survived the refactor, so this change only exposes that capability through the options. The helper, which was left with no callers outside a test, is removed.Test
TestParseCustomFunctionsnow uses the public API. It checks that:Functionsmap is untouched;EnableExperimentalFunctions;NewParserhas no effect.The previous version of this test did
funcs := Functions; funcs["custom_func"] = ..., which addedcustom_functo the global map for the rest of the package's tests. It no longer touches global state.TestRegisterFeaturesCustomFunctionschecks that the features API reports the custom set for such a parser and the default set otherwise.Downstream check. I built thanos-io/promql-engine against this branch, parsing with
parser.NewParser(parser.Options{Functions: e.functions}).ParseExpr(qs):xrateis evaluated whenEnableXFunctionsis set, and rejected otherwise;parser.Functionsnever contains it;promql.Enginein the same process still rejects it.CI-equivalent checks:
go test ./promql/... ./rules/... ./model/rulefmt/... ./web/api/...passes,-raceon./promql/parser/passes, andgolangci-lintv2.13.1 reports no issues.Which issue(s) does the PR fix:
There's no separate issue; this answers the question on #17977 and restores what #12170 added.
@vpranckaitis, could you take a look? Since this restores a capability that library users lost in 3.11, would a backport to
release-3.15be welcome? The patch doesn't apply as-is there, becauseOptionsstill has the fieldsmainhas since removed, so I'd open a separate backport PR. Otherwise, 3.16 is fine.Does this PR introduce a user-facing change?
No. This only affects code that uses the
promql/parserpackage as a library.🤖 Generated with Claude Code