Skip to content

Commit

Permalink
Add support for extended promql functions in rule (#7105)
Browse files Browse the repository at this point in the history
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 <samuel.dufel@shopify.com>
  • Loading branch information
sdufel committed Feb 21, 2024
1 parent fc3b360 commit 1723d1d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
- [#7078](https://github.com/thanos-io/thanos/pull/7078) *: Bump gRPC to 1.57.2

### 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

Expand Down
12 changes: 12 additions & 0 deletions cmd/thanos/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -105,6 +107,8 @@ type ruleConfig struct {
lset labels.Labels
ignoredLabelNames []string
storeRateLimits store.SeriesSelectLimits

extendedFunctionsEnabled bool
}

type Expression struct {
Expand Down Expand Up @@ -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("<endpoint>").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)
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 1723d1d

Please sign in to comment.