Skip to content

promql/parser: allow custom functions via parser.Options - #19830

Merged
roidelapluie merged 1 commit into
prometheus:mainfrom
sandy2008:promql-parser-custom-functions
Sep 25, 2026
Merged

roidelapluie merged 1 commit into
prometheus:mainfrom
sandy2008:promql-parser-custom-functions

Conversation

@sandy2008

Copy link
Copy Markdown
Contributor

What

This adds a Functions field to parser.Options, so a parser can be created with its own set of functions:

funcs := maps.Clone(parser.Functions)
maps.Copy(funcs, engineFunctions)
p := parser.NewParser(parser.Options{Functions: funcs})
  • Existing callers are unaffected. A nil Functions keeps using the default parser.Functions set.
  • A non-nil map replaces the default set. These are the same semantics parser.WithFunctions had. An empty map accepts no functions.
  • NewParser copies the map. Later changes to the caller's map cannot affect parsing or race with it.
  • RegisterFeatures reports what the parser actually accepts. It lists the functions from this parser's set, not the global one.

Why

#12170 added parser.WithFunctions for downstream PromQL engines. The Thanos engine, thanos-io/promql-engine, uses it to parse its extra xrate, xincrease and xdelta functions without adding them to the global parser.Functions map. 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.Options and dropped WithFunctions. 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 newParserWithFunctions helper 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

  • TestParseCustomFunctions now uses the public API. It checks that:

    • a custom function is parsed;
    • the default parser still rejects it, and the global Functions map is untouched;
    • the given map replaces the default set, and an empty map accepts no functions;
    • an experimental custom function still requires EnableExperimentalFunctions;
    • changing the caller's map after NewParser has no effect.

    The previous version of this test did funcs := Functions; funcs["custom_func"] = ..., which added custom_func to the global map for the rest of the package's tests. It no longer touches global state.

  • TestRegisterFeaturesCustomFunctions checks 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):

    • xrate is evaluated when EnableXFunctions is set, and rejected otherwise;
    • parser.Functions never contains it;
    • a stock promql.Engine in the same process still rejects it.
  • CI-equivalent checks: go test ./promql/... ./rules/... ./model/rulefmt/... ./web/api/... passes, -race on ./promql/parser/ passes, and golangci-lint v2.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.15 be welcome? The patch doesn't apply as-is there, because Options still has the fields main has 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/parser package as a library.

NONE

🤖 Generated with Claude Code

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]>
@roidelapluie
roidelapluie merged commit 270db29 into prometheus:main Sep 25, 2026
38 checks passed
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.

2 participants