sort_by_label: Switch to feature flag

Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
This commit is contained in:
Julien Pivotto 2023-11-28 15:10:12 +01:00
parent 5051a993ab
commit c1ec6ae851
7 changed files with 15 additions and 33 deletions

View file

@ -154,7 +154,6 @@ type flagConfig struct {
enableNewSDManager bool enableNewSDManager bool
enablePerStepStats bool enablePerStepStats bool
enableAutoGOMAXPROCS bool enableAutoGOMAXPROCS bool
enablePromQLSortByLabel bool
prometheusURL string prometheusURL string
corsRegexString string corsRegexString string
@ -210,8 +209,6 @@ func (c *flagConfig) setFeatureListOptions(logger log.Logger) error {
config.DefaultConfig.GlobalConfig.ScrapeProtocols = config.DefaultNativeHistogramScrapeProtocols config.DefaultConfig.GlobalConfig.ScrapeProtocols = config.DefaultNativeHistogramScrapeProtocols
config.DefaultGlobalConfig.ScrapeProtocols = config.DefaultNativeHistogramScrapeProtocols config.DefaultGlobalConfig.ScrapeProtocols = config.DefaultNativeHistogramScrapeProtocols
level.Info(logger).Log("msg", "Experimental native histogram support enabled. Changed default scrape_protocols to prefer PrometheusProto format.", "global.scrape_protocols", fmt.Sprintf("%v", config.DefaultGlobalConfig.ScrapeProtocols)) level.Info(logger).Log("msg", "Experimental native histogram support enabled. Changed default scrape_protocols to prefer PrometheusProto format.", "global.scrape_protocols", fmt.Sprintf("%v", config.DefaultGlobalConfig.ScrapeProtocols))
case "promql-sort-by-label":
c.enablePromQLSortByLabel = true
case "": case "":
continue continue
case "promql-at-modifier", "promql-negative-offset": case "promql-at-modifier", "promql-negative-offset":
@ -668,7 +665,6 @@ func main() {
EnableAtModifier: true, EnableAtModifier: true,
EnableNegativeOffset: true, EnableNegativeOffset: true,
EnablePerStepStats: cfg.enablePerStepStats, EnablePerStepStats: cfg.enablePerStepStats,
EnableSortByLabel: cfg.enablePromQLSortByLabel,
} }
queryEngine = promql.NewEngine(opts) queryEngine = promql.NewEngine(opts)

View file

@ -195,10 +195,3 @@ won't work when you push OTLP metrics.
Enables PromQL functions that are considered experimental and whose name or Enables PromQL functions that are considered experimental and whose name or
semantics could change. semantics could change.
## PromQL: Sort By Label Function
`--enable-feature=promql-sort-by-label`
When enabled, the `sort_by_label` and `sort_by_label_desc` functions can be used
to sort instant query results by their label values.

View file

@ -588,7 +588,7 @@ Same as `sort`, but sorts in descending order.
## `sort_by_label()` ## `sort_by_label()`
**This function has to be enabled via a [feature flag](../feature_flags/#promql-sort-by-label-function).** **This function has to be enabled via the [feature flag](../feature_flags/) `--enable-feature=promql-experimental-functions`.**
`sort_by_label(v instant-vector, label string, ...)` returns vector elements sorted by their label values and sample value in case of label values being equal, in ascending order. `sort_by_label(v instant-vector, label string, ...)` returns vector elements sorted by their label values and sample value in case of label values being equal, in ascending order.
@ -596,7 +596,7 @@ Please note that the sort by label functions only affect the results of instant
## `sort_by_label_desc()` ## `sort_by_label_desc()`
**This function has to be enabled via a [feature flag](../feature_flags/#promql-sort-by-label-function).** **This function has to be enabled via the [feature flag](../feature_flags/) `--enable-feature=promql-experimental-functions`.**
Same as `sort_by_label`, but sorts in descending order. Same as `sort_by_label`, but sorts in descending order.

View file

@ -304,9 +304,6 @@ type EngineOpts struct {
// EnablePerStepStats if true allows for per-step stats to be computed on request. Disabled otherwise. // EnablePerStepStats if true allows for per-step stats to be computed on request. Disabled otherwise.
EnablePerStepStats bool EnablePerStepStats bool
// EnableSortByLabel if true enables sort_by_label/sort_by_label_desc query functions.
EnableSortByLabel bool
} }
// Engine handles the lifetime of queries from beginning to end. // Engine handles the lifetime of queries from beginning to end.
@ -324,7 +321,6 @@ type Engine struct {
enableAtModifier bool enableAtModifier bool
enableNegativeOffset bool enableNegativeOffset bool
enablePerStepStats bool enablePerStepStats bool
enableSortByLabel bool
} }
// NewEngine returns a new engine. // NewEngine returns a new engine.
@ -415,7 +411,6 @@ func NewEngine(opts EngineOpts) *Engine {
enableAtModifier: opts.EnableAtModifier, enableAtModifier: opts.EnableAtModifier,
enableNegativeOffset: opts.EnableNegativeOffset, enableNegativeOffset: opts.EnableNegativeOffset,
enablePerStepStats: opts.EnablePerStepStats, enablePerStepStats: opts.EnablePerStepStats,
enableSortByLabel: opts.EnableSortByLabel,
} }
} }
@ -707,7 +702,6 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *parser.Eval
lookbackDelta: s.LookbackDelta, lookbackDelta: s.LookbackDelta,
samplesStats: query.sampleStats, samplesStats: query.sampleStats,
noStepSubqueryIntervalFn: ng.noStepSubqueryIntervalFn, noStepSubqueryIntervalFn: ng.noStepSubqueryIntervalFn,
enableSortByLabel: ng.enableSortByLabel,
} }
query.sampleStats.InitStepTracking(start, start, 1) query.sampleStats.InitStepTracking(start, start, 1)
@ -765,7 +759,6 @@ func (ng *Engine) execEvalStmt(ctx context.Context, query *query, s *parser.Eval
lookbackDelta: s.LookbackDelta, lookbackDelta: s.LookbackDelta,
samplesStats: query.sampleStats, samplesStats: query.sampleStats,
noStepSubqueryIntervalFn: ng.noStepSubqueryIntervalFn, noStepSubqueryIntervalFn: ng.noStepSubqueryIntervalFn,
enableSortByLabel: ng.enableSortByLabel,
} }
query.sampleStats.InitStepTracking(evaluator.startTimestamp, evaluator.endTimestamp, evaluator.interval) query.sampleStats.InitStepTracking(evaluator.startTimestamp, evaluator.endTimestamp, evaluator.interval)
val, warnings, err := evaluator.Eval(s.Expr) val, warnings, err := evaluator.Eval(s.Expr)
@ -1019,7 +1012,6 @@ type evaluator struct {
lookbackDelta time.Duration lookbackDelta time.Duration
samplesStats *stats.QuerySamples samplesStats *stats.QuerySamples
noStepSubqueryIntervalFn func(rangeMillis int64) int64 noStepSubqueryIntervalFn func(rangeMillis int64) int64
enableSortByLabel bool
} }
// errorf causes a panic with the input formatted into an error. // errorf causes a panic with the input formatted into an error.
@ -1395,8 +1387,6 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, annotations.Annotatio
if ok { if ok {
return ev.rangeEvalTimestampFunctionOverVectorSelector(vs, call, e) return ev.rangeEvalTimestampFunctionOverVectorSelector(vs, call, e)
} }
} else if (e.Func.Name == "sort_by_label" || e.Func.Name == "sort_by_label_desc") && !ev.enableSortByLabel {
ev.error(fmt.Errorf("sort_by_label() and sort_by_label_desc() require the \"promql-sort-by-label\" feature flag to be set"))
} }
// Check if the function has a matrix argument. // Check if the function has a matrix argument.
@ -1790,7 +1780,6 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, annotations.Annotatio
lookbackDelta: ev.lookbackDelta, lookbackDelta: ev.lookbackDelta,
samplesStats: ev.samplesStats.NewChild(), samplesStats: ev.samplesStats.NewChild(),
noStepSubqueryIntervalFn: ev.noStepSubqueryIntervalFn, noStepSubqueryIntervalFn: ev.noStepSubqueryIntervalFn,
enableSortByLabel: ev.enableSortByLabel,
} }
res, ws := newEv.eval(e.Expr) res, ws := newEv.eval(e.Expr)
ev.currentSamples = newEv.currentSamples ev.currentSamples = newEv.currentSamples

View file

@ -348,16 +348,18 @@ var Functions = map[string]*Function{
ReturnType: ValueTypeVector, ReturnType: ValueTypeVector,
}, },
"sort_by_label": { "sort_by_label": {
Name: "sort_by_label", Name: "sort_by_label",
ArgTypes: []ValueType{ValueTypeVector, ValueTypeString}, ArgTypes: []ValueType{ValueTypeVector, ValueTypeString},
Variadic: -1, Variadic: -1,
ReturnType: ValueTypeVector, ReturnType: ValueTypeVector,
Experimental: true,
}, },
"sort_by_label_desc": { "sort_by_label_desc": {
Name: "sort_by_label_desc", Name: "sort_by_label_desc",
ArgTypes: []ValueType{ValueTypeVector, ValueTypeString}, ArgTypes: []ValueType{ValueTypeVector, ValueTypeString},
Variadic: -1, Variadic: -1,
ReturnType: ValueTypeVector, ReturnType: ValueTypeVector,
Experimental: true,
}, },
"sqrt": { "sqrt": {
Name: "sqrt", Name: "sqrt",

View file

@ -35,7 +35,6 @@ func newTestEngine() *Engine {
EnableAtModifier: true, EnableAtModifier: true,
EnableNegativeOffset: true, EnableNegativeOffset: true,
EnablePerStepStats: true, EnablePerStepStats: true,
EnableSortByLabel: true,
}) })
} }

View file

@ -73,6 +73,9 @@ func LoadedStorage(t testutil.T, input string) *teststorage.TestStorage {
// RunBuiltinTests runs an acceptance test suite against the provided engine. // RunBuiltinTests runs an acceptance test suite against the provided engine.
func RunBuiltinTests(t *testing.T, engine engineQuerier) { func RunBuiltinTests(t *testing.T, engine engineQuerier) {
t.Cleanup(func() { parser.EnableExperimentalFunctions = false })
parser.EnableExperimentalFunctions = true
files, err := fs.Glob(testsFs, "*/*.test") files, err := fs.Glob(testsFs, "*/*.test")
require.NoError(t, err) require.NoError(t, err)