Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [FEATURE] Querier: Add timeout classification to classify query timeouts as 4XX (user error) or 5XX (system error) based on phase timing. When enabled, queries that spend most of their time in PromQL evaluation return `422 Unprocessable Entity` instead of `503 Service Unavailable`. #7374
* [FEATURE] Querier: Implement Resource Based Throttling in Querier. #7442
* [FEATURE] Querier: Add resource-based query eviction that automatically cancels the heaviest running query when CPU or heap utilization exceeds configured thresholds. #7488
* [BUGFIX] Ruler: Register xfunctions (xincrease, xrate, xdelta) in the global parser before loading rule files. #7621

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changelog entry needs to be properly sorted

* [ENHANCEMENT] Upgrade prometheus alertmanager version to v0.32.1. #7462
* [ENHANCEMENT] Tenant Federation: Avoid purging the regex resolver LRU cache on user-sync ticks when the set of known users has not changed. #7489
* [ENHANCEMENT] Memberlist: Add `-memberlist.packet-read-timeout`, `-memberlist.max-packet-size`, and `-memberlist.max-concurrent-connections` flags to bound inbound gossip TCP connections, preventing slow-read, OOM, and connection-flood attacks on the gossip port. #7518
Expand Down
8 changes: 8 additions & 0 deletions pkg/cortex/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"log/slog"
"maps"
"net/http"
"runtime"
"runtime/debug"
Expand All @@ -18,9 +19,11 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/rules"
prom_storage "github.com/prometheus/prometheus/storage"
"github.com/thanos-io/objstore"
"github.com/thanos-io/promql-engine/execution/parse"
"github.com/thanos-io/thanos/pkg/discovery/dns"
"github.com/thanos-io/thanos/pkg/querysharding"
httpgrpc_server "github.com/weaveworks/common/httpgrpc/server"
Expand Down Expand Up @@ -661,6 +664,11 @@ func (t *Cortex) initRulerStorage() (serv services.Service, err error) {
return
}

// Register xfunctions (xincrease, xrate, xdelta) in the global parser
if t.Cfg.Querier.ThanosEngine.EnableXFunctions {
maps.Copy(parser.Functions, parse.XFunctions)
}
Comment on lines +667 to +670

@SungJin1212 SungJin1212 Jun 12, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Register xfunctions (xincrease, xrate, xdelta) in the global parser
if t.Cfg.Querier.ThanosEngine.EnableXFunctions {
maps.Copy(parser.Functions, parse.XFunctions)
}
// Register xfunctions (xincrease, xrate, xdelta) in the global parser
if t.Cfg.Querier.ThanosEngine.Enabled && t.Cfg.Querier.ThanosEngine.EnableXFunctions {
maps.Copy(parser.Functions, parse.XFunctions)
}

nit: should we add an enabled condition?


t.RulerStorage, err = ruler.NewRuleStore(context.Background(), t.Cfg.RulerStorage, t.OverridesConfig, rules.FileLoader{}, util_log.Logger, prometheus.DefaultRegisterer, t.Cfg.NameValidationScheme)
return
}
Expand Down
62 changes: 62 additions & 0 deletions pkg/cortex/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cortex

import (
"context"
"maps"
"net/http/httptest"
"os"
"reflect"
Expand All @@ -10,6 +11,7 @@ import (
"testing"

"github.com/gorilla/mux"
"github.com/prometheus/prometheus/promql/parser"
prom_storage "github.com/prometheus/prometheus/storage"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -169,6 +171,66 @@ func (p *myPusher) Push(ctx context.Context, req *cortexpb.WriteRequest) (*corte
return nil, nil
}

func TestCortex_InitRulerStorage_RegistersXFunctions(t *testing.T) {
tests := map[string]struct {
enableXFunctions bool
expectRegistered bool
}{
"should register xfunctions when EnableXFunctions is true": {
enableXFunctions: true,
expectRegistered: true,
},
"should not register xfunctions when EnableXFunctions is false": {
enableXFunctions: false,
expectRegistered: false,
},
}

for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
// Clean up global state after each test
originalFunctions := make(map[string]*parser.Function, len(parser.Functions))
maps.Copy(originalFunctions, parser.Functions)
defer func() {
// Restore original parser.Functions
for k := range parser.Functions {
if _, ok := originalFunctions[k]; !ok {
delete(parser.Functions, k)
}
}
}()

cfg := newDefaultConfig()
cfg.Target = []string{"ruler"}
cfg.RulerStorage.Backend = "local"
cfg.RulerStorage.Local.Directory = os.TempDir()
cfg.Querier.ThanosEngine.EnableXFunctions = testData.enableXFunctions

cortex := &Cortex{
Server: &server.Server{},
Cfg: *cfg,
}

_, err := cortex.initRulerStorage()
require.NoError(t, err)

_, hasXincrease := parser.Functions["xincrease"]
_, hasXrate := parser.Functions["xrate"]
_, hasXdelta := parser.Functions["xdelta"]

if testData.expectRegistered {
assert.True(t, hasXincrease, "xincrease should be registered")
assert.True(t, hasXrate, "xrate should be registered")
assert.True(t, hasXdelta, "xdelta should be registered")
} else {
assert.False(t, hasXincrease, "xincrease should not be registered")
assert.False(t, hasXrate, "xrate should not be registered")
assert.False(t, hasXdelta, "xdelta should not be registered")
}
})
}
}

type myQueryable struct{}

func (q *myQueryable) Querier(mint, maxt int64) (prom_storage.Querier, error) {
Expand Down
Loading