Skip to content

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

Open
sandy2008 wants to merge 1 commit into
prometheus:release-3.15from
sandy2008:backport-3.15-custom-functions
Open

sandy2008 wants to merge 1 commit into
prometheus:release-3.15from
sandy2008:backport-3.15-custom-functions

Conversation

@sandy2008

Copy link
Copy Markdown
Contributor

Backport of #19830 to release-3.15, for the next 3.15 patch release. #19830 was approved by roidelapluie and merged to main.

Why backport this

  • It restores a capability that downstream engines depend on. PromQL: Refactor parser to use instance configuration instead of global flags #17977 (v3.11) removed parser.WithFunctions without deprecation. Downstream PromQL engines used it to parse their own functions without adding them to the global parser.Functions map. promql/parser: allow custom functions via parser.Options #19830 adds the replacement, parser.Options.Functions, so for library users this is a regression fix rather than a new feature. Until a release contains it, thanos-io/promql-engine can only move past Prometheus 3.10 by mutating the global parser.Functions map. The same goes for Thanos and Cortex, which depend on promql-engine. That is what the open upgrade PRs, Prometheus 3.11 upstream thanos-io/promql-engine#706 and Prometheus 3.11 upstream thanos-io/thanos#8774, currently do.
  • Requiring a main commit doesn't work in the meantime. The highest tag reachable from main is currently v0.314.0-rc.0. A main commit therefore resolves to a pseudo-version such as v0.314.0-rc.0.0.20260925142636-270db2915054, which sorts below v0.314.0. As soon as any other dependency requires v0.314.0 or later, minimal version selection picks that release, and it doesn't have the change. For example, the current prom-label-proxy release, v0.15.1, requires v0.314.0, and Thanos will need a newer prom-label-proxy once it moves past 3.10. A replace directive only takes effect in the top-level module, and it means building against unreleased code. By contrast, a commit on release-3.15 resolves to v0.315.1-0.<timestamp>-<hash>, and v3.15.1 to module version v0.315.1. Both sort above v0.315.0, so downstream can use them right away.
  • The alternative is waiting for v3.16.0. Its cycle starts with rc.0 on 2026-10-21, and it has no shepherd yet, so the final release would land around early November at the earliest.

Risk

  • Additive and opt-in. The change adds an optional field to parser.Options. A nil Functions keeps the current behavior. Nothing in Prometheus sets it, so the Prometheus binary behaves exactly as before.
  • parser.Options is no longer comparable with ==. The new field is a map. Nothing in the repository compares Options or uses it as a map key, and Go APIs are outside the stability guarantees.
  • Same change as promql/parser: allow custom functions via parser.Options #19830. The only difference is a context conflict in the Options struct: on release-3.15 it still has ExperimentalDurationExpr and EnableExtendedRangeSelectors, and both are kept.
  • Merge-back conflict to expect. When release-3.15 is merged back into main, that same struct will conflict (two fields on main, four here). Keep main's side.
  • Tested on this branch: go test ./promql/... ./rules/... ./model/rulefmt/... ./web/api/... and go test -race ./promql/parser/ pass, and golangci-lint reports no issues.

cc @jan--f as the 3.15 release shepherd.

[ENHANCEMENT] PromQL: Add `parser.Options.Functions` so that library users can parse custom functions without modifying the global `parser.Functions` map (library use only).

🤖 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]>

(cherry picked from commit f57e5af)
@bboreham

Copy link
Copy Markdown
Member

Hi, thanks for raising this; given it takes a certain amount of work to do a patch release I expect it will wait for the 3.16.

Unless you can explain better why using a commit off main is unusable for you.

@roidelapluie

Copy link
Copy Markdown
Member

I think the tagging issue is because 3.15.0 release branch has not been merged to main yet.

@Naman-B-Parlecha

Copy link
Copy Markdown
Contributor

@bboreham @roidelapluie would it be possible to make a minor patch release for this?

i was trying to work on XOR2 adoption in thanos but bumping the prometheus dep to 0.315.0 changes how we interact with the parser since we have Options now and #19830 didn't make that release.

so right now we will have to register our custom functions (xrate/xincrease/xdelta) by mutating the global parser.Functions map :(((

if we can make a release and bump dependency to minor release it would make thing much cleaner

thanks!

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.

4 participants