From 77bcd00bf703ad79df118e143e8f773bdfa924ff Mon Sep 17 00:00:00 2001 From: Samuel Dufel Date: Wed, 6 Dec 2023 15:02:58 -0800 Subject: [PATCH] Add support for extended promql functions in rule Adds a flag to register the extended promql functions supported by the thanos query engine when running the rule component. This will allow rule config files containing query expressions with (xrate / xincrease / xdelta) to pass validation. This will only work if the query endpoint in use is running the thanos engine. Signed-off-by: Samuel Dufel --- CHANGELOG.md | 1 + cmd/thanos/rule.go | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6abefe263c..914230f3c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re - [#7080](https://github.com/thanos-io/thanos/pull/7080) Receive: race condition in handler Close() when stopped early ### Added +- [#7105](https://github.com/thanos-io/thanos/pull/7105) Rule: add flag `--query.enable-x-functions` to allow usage of extended promql functions (xrate, xincrease, xdelta) in loaded rules ### Changed diff --git a/cmd/thanos/rule.go b/cmd/thanos/rule.go index dce39cbf97..6b76359042 100644 --- a/cmd/thanos/rule.go +++ b/cmd/thanos/rule.go @@ -34,6 +34,7 @@ import ( "github.com/prometheus/prometheus/model/relabel" "github.com/prometheus/prometheus/notifier" "github.com/prometheus/prometheus/promql" + "github.com/prometheus/prometheus/promql/parser" "github.com/prometheus/prometheus/rules" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/storage/remote" @@ -44,6 +45,7 @@ import ( "github.com/thanos-io/objstore" "github.com/thanos-io/objstore/client" objstoretracing "github.com/thanos-io/objstore/tracing/opentracing" + "github.com/thanos-io/promql-engine/execution/parse" "gopkg.in/yaml.v2" "github.com/thanos-io/thanos/pkg/alert" @@ -105,6 +107,8 @@ type ruleConfig struct { lset labels.Labels ignoredLabelNames []string storeRateLimits store.SeriesSelectLimits + + extendedFunctionsEnabled bool } type Expression struct { @@ -155,6 +159,8 @@ func registerRule(app *extkingpin.App) { cmd.Flag("grpc-query-endpoint", "Addresses of Thanos gRPC query API servers (repeatable). The scheme may be prefixed with 'dns+' or 'dnssrv+' to detect Thanos API servers through respective DNS lookups."). PlaceHolder("").StringsVar(&conf.grpcQueryEndpoints) + cmd.Flag("query.enable-x-functions", "Whether to enable extended rate functions (xrate, xincrease and xdelta). Only has effect when used with Thanos engine.").Default("false").BoolVar(&conf.extendedFunctionsEnabled) + conf.rwConfig = extflag.RegisterPathOrContent(cmd, "remote-write.config", "YAML config for the remote-write configurations, that specify servers where samples should be sent to (see https://prometheus.io/docs/prometheus/latest/configuration/configuration/#remote_write). This automatically enables stateless mode for ruler and no series will be stored in the ruler's TSDB. If an empty config (or file) is provided, the flag is ignored and ruler is run with its own TSDB.", extflag.WithEnvSubstitution()) conf.objStoreConfig = extkingpin.RegisterCommonObjStoreFlags(cmd, "", false) @@ -582,6 +588,12 @@ func runRule( alertQ = alert.NewQueue(logger, reg, 10000, 100, labelsTSDBToProm(conf.lset), conf.alertmgr.alertExcludeLabels, alertRelabelConfigs) ) { + if conf.extendedFunctionsEnabled { + for k, fn := range parse.XFunctions { + parser.Functions[k] = fn + } + } + // Run rule evaluation and alert notifications. notifyFunc := func(ctx context.Context, expr string, alerts ...*rules.Alert) { res := make([]*notifier.Alert, 0, len(alerts))