Skip to content

Commit

Permalink
Remove need for separate remote-write flag for stateless ruler
Browse files Browse the repository at this point in the history
This removes the need to pass a separate `remote-write` flag to ruler
to enable stateless mode. Instead, we now check if a remote-write config is provided
and automatically enables stateless mode based off that.

Ruler test is also cleaned up to remove unnecessary
tests (i.e those that have been performed by other e2e suites).

Signed-off-by: Michael Okoko <okokomichaels@outlook.com>
  • Loading branch information
idoqo committed Jun 1, 2021
1 parent 5e98c72 commit 17df499
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 49 deletions.
7 changes: 2 additions & 5 deletions cmd/thanos/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,10 @@ func (ac *alertMgrConfig) registerFlag(cmd extflag.FlagClause) *alertMgrConfig {
}

type ruleRWConfig struct {
remoteWrite bool
configPath *extflag.PathOrContent
configPath *extflag.PathOrContent
}

func (rc *ruleRWConfig) registerFlag(cmd extflag.FlagClause) *ruleRWConfig {
cmd.Flag("remote-write", "If true, directs ruler to remote-write evaluated samples to the server configured by 'remote-write.config'.").
BoolVar(&rc.remoteWrite)
rc.configPath = extflag.RegisterPathOrContent(cmd, "remote-write.config", "YAML config for the remote-write server where samples should be sent to. See https://thanos.io/tip/components/rule.md/#query-api", false)
rc.configPath = extflag.RegisterPathOrContent(cmd, "remote-write.config", "YAML config for the remote-write server where samples should be sent to. This automatically enables stateless mode for ruler and no series will be stored in the ruler's TSDB. See https://thanos.io/tip/components/rule.md/#query-api", false)
return rc
}
6 changes: 3 additions & 3 deletions cmd/thanos/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ func registerRule(app *extkingpin.App) {
return errors.New("--query/--query.sd-files and --query.config* parameters cannot be defined at the same time")
}

// Parse and check remote-write config if it's enabled
if conf.rwConfig.remoteWrite {
// Parse and check remote-write config and enable stateless mode for ruler.
if conf.rwConfig.configPath != nil {
conf.rwConfigYAML, err = conf.rwConfig.configPath.Content()
if err != nil {
return err
Expand Down Expand Up @@ -337,7 +337,7 @@ func runRule(
db *tsdb.DB
)

if conf.rwConfig.remoteWrite {
if conf.rwConfig.configPath != nil {
conf.rwConfigYAML, err = conf.rwConfig.configPath.Content()
if err != nil {
return err
Expand Down
11 changes: 7 additions & 4 deletions test/e2e/e2ethanos/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,14 @@ func NewReceiverWithConfigWatcher(sharedDir string, networkName string, name str
}

func NewTSDBRuler(sharedDir string, name string, ruleSubDir string, amCfg []alert.AlertmanagerConfig, queryCfg []query.Config) (*Service, error) {
return NewRuler(sharedDir, name, ruleSubDir, amCfg, queryCfg, false, remotewrite.Config{})
return newRuler(sharedDir, name, ruleSubDir, amCfg, queryCfg, nil)
}

func NewRuler(sharedDir string, name string, ruleSubDir string, amCfg []alert.AlertmanagerConfig, queryCfg []query.Config, remoteWrite bool, remoteWriteCfg remotewrite.Config) (*Service, error) {
func NewStatelessRuler(sharedDir string, name string, ruleSubDir string, amCfg []alert.AlertmanagerConfig, queryCfg []query.Config, remoteWriteCfg *remotewrite.Config) (*Service, error) {
return newRuler(sharedDir, name, ruleSubDir, amCfg, queryCfg, remoteWriteCfg)
}

func newRuler(sharedDir string, name string, ruleSubDir string, amCfg []alert.AlertmanagerConfig, queryCfg []query.Config, remoteWriteCfg *remotewrite.Config) (*Service, error) {
dir := filepath.Join(sharedDir, "data", "rule", name)
container := filepath.Join(e2e.ContainerSharedDir, "data", "rule", name)
if err := os.MkdirAll(dir, 0750); err != nil {
Expand Down Expand Up @@ -378,12 +382,11 @@ func NewRuler(sharedDir string, name string, ruleSubDir string, amCfg []alert.Al
"--query.sd-dns-interval": "1s",
"--resend-delay": "5s",
}
if remoteWrite {
if remoteWriteCfg != nil {
rwCfgBytes, err := yaml.Marshal(remoteWriteCfg)
if err != nil {
return nil, errors.Wrapf(err, "generate remote write config: %v", remoteWriteCfg)
}
ruleArgs["--remote-write"] = ""
ruleArgs["--remote-write.config"] = string(rwCfgBytes)
}

Expand Down
39 changes: 4 additions & 35 deletions test/e2e/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"testing"
"time"

commoncfg "github.com/prometheus/common/config"
common_cfg "github.com/prometheus/common/config"
"github.com/prometheus/prometheus/config"
"github.com/thanos-io/thanos/pkg/rules/remotewrite"

Expand Down Expand Up @@ -600,7 +600,7 @@ groups:
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(querier))

r, err := e2ethanos.NewRuler(s.SharedDir(), "1", rulesSubDir, []alert.AlertmanagerConfig{
r, err := e2ethanos.NewStatelessRuler(s.SharedDir(), "1", rulesSubDir, []alert.AlertmanagerConfig{
{
EndpointsConfig: http_util.EndpointsConfig{
StaticAddresses: []string{
Expand All @@ -620,47 +620,16 @@ groups:
Scheme: "http",
},
},
}, true, remotewrite.Config{
}, &remotewrite.Config{
Name: "ruler-rw-receivers",
RemoteStore: &config.RemoteWriteConfig{
URL: &commoncfg.URL{URL: rwURL},
URL: &common_cfg.URL{URL: rwURL},
Name: "thanos-receiver",
},
})
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(r))

t.Run("inject samples into receiver to reset its StoreAPI MinTime", func(t *testing.T) {
// inject data into receiver to reset its minTime (so it doesn't get filtered out by store)
// the sample is injected through a prometheus instance that remote_writes samples into the receiver node
prom, _, err := e2ethanos.NewPrometheus(s.SharedDir(), "1", defaultPromConfig("prom", 0, e2ethanos.RemoteWriteEndpoint(receiver.NetworkEndpoint(8081)), ""), e2ethanos.DefaultPrometheusImage())
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(prom))

queryAndAssertSeries(t, ctx, querier.HTTPEndpoint(), queryUpWithoutInstance, promclient.QueryOptions{
Deduplicate: false,
}, []model.Metric{
{
"job": "myself",
"prometheus": "prom",
"receive": "1",
"replica": "0",
"tenant_id": "default-tenant",
},
})
})

t.Run("query can contact from receiver", func(t *testing.T) {
testAbsentQuery := "absent(nonexistent{job='thanos-receive'})"
queryAndAssertSeries(t, ctx, querier.HTTPEndpoint(), testAbsentQuery, promclient.QueryOptions{
Deduplicate: false,
}, []model.Metric{
{
"job": "thanos-receive",
},
})
})

t.Run("can fetch remote-written samples from receiver", func(t *testing.T) {
testRecordedSamples := "test_absent_metric"
queryAndAssertSeries(t, ctx, querier.HTTPEndpoint(), testRecordedSamples, promclient.QueryOptions{
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/rules_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func TestRulesAPI_Fanout(t *testing.T) {
}

// Recreate rulers with the corresponding query config.
r1, err = e2ethanos.NewRuler(s.SharedDir(), "rule1", thanosRulesSubDir, nil, queryCfg)
r1, err = e2ethanos.NewTSDBRuler(s.SharedDir(), "rule1", thanosRulesSubDir, nil, queryCfg)
testutil.Ok(t, err)
r2, err = e2ethanos.NewRuler(s.SharedDir(), "rule2", thanosRulesSubDir, nil, queryCfg)
r2, err = e2ethanos.NewTSDBRuler(s.SharedDir(), "rule2", thanosRulesSubDir, nil, queryCfg)
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(r1, r2))

Expand Down

0 comments on commit 17df499

Please sign in to comment.