Skip to content

Commit

Permalink
Generate docs for stateless ruler flags and fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Okoko <okokomichaels@outlook.com>
  • Loading branch information
idoqo committed Jun 7, 2021
1 parent f453beb commit c01154d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 40 deletions.
9 changes: 0 additions & 9 deletions cmd/thanos/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,3 @@ func (ac *alertMgrConfig) registerFlag(cmd extflag.FlagClause) *alertMgrConfig {
ac.alertRelabelConfigPath = extflag.RegisterPathOrContent(cmd, "alert.relabel-config", "YAML file that contains alert relabelling configuration.", false)
return ac
}

type ruleRWConfig struct {
configPath *extflag.PathOrContent
}

func (rc *ruleRWConfig) registerFlag(cmd extflag.FlagClause) *ruleRWConfig {
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
}
30 changes: 10 additions & 20 deletions cmd/thanos/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ type ruleConfig struct {
alertQueryURL *url.URL
alertRelabelConfigYAML []byte

rwConfig ruleRWConfig
rwConfigYAML []byte
rwConfig *extflag.PathOrContent

resendDelay time.Duration
evalInterval time.Duration
Expand All @@ -97,7 +96,6 @@ func (rc *ruleConfig) registerFlag(cmd extkingpin.FlagClause) {
rc.shipper.registerFlag(cmd)
rc.query.registerFlag(cmd)
rc.alertmgr.registerFlag(cmd)
rc.rwConfig.registerFlag(cmd)
}

// registerRule registers a rule command.
Expand Down Expand Up @@ -125,6 +123,8 @@ func registerRule(app *extkingpin.App) {
cmd.Flag("eval-interval", "The default evaluation interval to use.").
Default("30s").DurationVar(&conf.evalInterval)

conf.rwConfig = 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. If an empty config (or file) is provided, the flag is ignored and ruler is run with its own TSDB.", false)

reqLogDecision := cmd.Flag("log.request.decision", "Deprecation Warning - This flag would be soon deprecated, and replaced with `request.logging-config`. Request Logging for logging the start and end of requests. By default this flag is disabled. LogFinishCall: Logs the finish call of the requests. LogStartAndFinishCall: Logs the start and finish call of the requests. NoLogCall: Disable request logging.").Default("").Enum("NoLogCall", "LogFinishCall", "LogStartAndFinishCall", "")

conf.objStoreConfig = extkingpin.RegisterCommonObjStoreFlags(cmd, "", false)
Expand Down Expand Up @@ -172,14 +172,6 @@ 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 and enable stateless mode for ruler.
if conf.rwConfig.configPath != nil {
conf.rwConfigYAML, err = conf.rwConfig.configPath.Content()
if err != nil {
return err
}
}

// Parse and check alerting configuration.
conf.alertmgrsConfigYAML, err = conf.alertmgr.configPath.Content()
if err != nil {
Expand Down Expand Up @@ -337,16 +329,14 @@ func runRule(
db *tsdb.DB
)

if conf.rwConfig.configPath != nil {
conf.rwConfigYAML, err = conf.rwConfig.configPath.Content()
if err != nil {
return err
}
rwCfgYAML, err := conf.rwConfig.Content()
if err != nil {
return err
}

if len(rwCfgYAML) > 0 {
var rwCfg remotewrite.Config
if len(conf.rwConfigYAML) == 0 {
return errors.New("no --remote-write.config was given")
}
rwCfg, err = remotewrite.LoadRemoteWriteConfig(conf.rwConfigYAML)
rwCfg, err = remotewrite.LoadRemoteWriteConfig(rwCfgYAML)
if err != nil {
return err
}
Expand Down
17 changes: 17 additions & 0 deletions docs/components/rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,23 @@ Flags:
(repeatable).
--query.sd-interval=5m Refresh interval to re-read file SD files.
(used as a fallback)
--remote-write.config=<content>
Alternative to 'remote-write.config-file' flag
(mutually exclusive). Content of 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. If an empty config
(or file) is provided, the flag is ignored and
ruler is run with its own TSDB.
--remote-write.config-file=<file-path>
Path to 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. If an empty config (or file) is provided,
the flag is ignored and ruler is run with its
own TSDB.
--request.logging-config=<content>
Alternative to 'request.logging-config-file'
flag (mutually exclusive). Content of YAML file
Expand Down
9 changes: 7 additions & 2 deletions pkg/rules/remotewrite/remotewrite.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package remotewrite

import (
"fmt"
"time"

"github.com/pkg/errors"

"github.com/go-kit/kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/config"
Expand All @@ -12,11 +13,13 @@ import (
"gopkg.in/yaml.v2"
)

// Config represents a remote write configuration for Thanos stateless ruler.
type Config struct {
Name string `yaml:"name"`
RemoteStore *config.RemoteWriteConfig `yaml:"remote_write,omitempty"`
}

// LoadRemoteWriteConfig prepares a Config instance from a given YAML config.
func LoadRemoteWriteConfig(configYAML []byte) (Config, error) {
var cfg Config
if err := yaml.Unmarshal(configYAML, &cfg); err != nil {
Expand All @@ -25,6 +28,8 @@ func LoadRemoteWriteConfig(configYAML []byte) (Config, error) {
return cfg, nil
}

// NewFanoutStorage creates a storage that fans-out to both the WAL and a configured remote storage.
// The remote storage tails the WAL and sends the metrics it reads using Prometheus' remote_write.
func NewFanoutStorage(logger log.Logger, reg prometheus.Registerer, walDir string, rwConfig Config) (storage.Storage, error) {
walStore, err := NewStorage(logger, reg, walDir)
if err != nil {
Expand All @@ -35,7 +40,7 @@ func NewFanoutStorage(logger log.Logger, reg prometheus.Registerer, walDir strin
GlobalConfig: config.DefaultGlobalConfig,
RemoteWriteConfigs: []*config.RemoteWriteConfig{rwConfig.RemoteStore},
}); err != nil {
return nil, fmt.Errorf("failed applying config to remote storage: %w", err)
return nil, errors.Wrap(err, "applying config to remote storage")
}
return storage.NewFanout(logger, walStore, remoteStore), nil
}
5 changes: 5 additions & 0 deletions pkg/rules/remotewrite/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ func (c *walDataCollector) Append(samples []record.RefSample) bool {
return true
}

func (c *walDataCollector) AppendExemplars([]record.RefExemplar) bool {
// dummy implementation to make walDataCollector conform to the WriteTo interface
return true
}

func (c *walDataCollector) StoreSeries(series []record.RefSeries, _ int) {
c.mut.Lock()
defer c.mut.Unlock()
Expand Down
3 changes: 1 addition & 2 deletions scripts/quickstart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ if [ -n "${STATELESS_RULER_ENABLED}" ]; then
name: "receive-0"
EOF

REMOTE_WRITE_FLAGS="--remote-write --remote-write.config-file data/rule-remote-write.yaml
"
REMOTE_WRITE_FLAGS="--remote-write.config-file data/rule-remote-write.yaml"
fi

# Start Thanos Ruler.
Expand Down
15 changes: 8 additions & 7 deletions test/e2e/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
testAlertRuleAbortOnPartialResponse = `
groups:
- name: example_abort
interval: 100ms
interval: 1s
# Abort should be a default: partial_response_strategy: "ABORT"
rules:
- alert: TestAlert_AbortOnPartialResponse
Expand All @@ -56,7 +56,7 @@ groups:
testAlertRuleWarnOnPartialResponse = `
groups:
- name: example_warn
interval: 100ms
interval: 1s
partial_response_strategy: "WARN"
rules:
- alert: TestAlert_WarnOnPartialResponse
Expand All @@ -70,7 +70,7 @@ groups:
testAlertRuleAddedLaterWebHandler = `
groups:
- name: example
interval: 100ms
interval: 1s
partial_response_strategy: "WARN"
rules:
- alert: TestAlert_HasBeenLoadedViaWebHandler
Expand All @@ -81,6 +81,7 @@ groups:
annotations:
summary: "I always complain and I have been loaded via /-/reload."
`
amTimeout = model.Duration(10 * time.Second)
)

func createRuleFile(t *testing.T, path, content string) {
Expand Down Expand Up @@ -366,7 +367,7 @@ func TestRule(t *testing.T) {
},
Scheme: "http",
},
Timeout: model.Duration(time.Second),
Timeout: amTimeout,
APIVersion: alert.APIv1,
},
}, []query.Config{
Expand Down Expand Up @@ -569,7 +570,7 @@ func TestRule_CanRemoteWriteData(t *testing.T) {
testRuleRecordAbsentMetric := `
groups:
- name: example_record_rules
interval: 100ms
interval: 1s
rules:
- record: test_absent_metric
expr: absent(nonexistent{job='thanos-receive'})
Expand All @@ -596,7 +597,7 @@ groups:
testutil.Ok(t, s.StartAndWaitReady(receiver))
rwURL := mustURLParse(t, e2ethanos.RemoteWriteEndpoint(receiver.NetworkEndpoint(8081)))

querier, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{receiver.GRPCNetworkEndpoint()}, nil, nil, nil, nil, nil, "", "")
querier, err := e2ethanos.NewQuerierBuilder(s.SharedDir(), "1", []string{receiver.GRPCNetworkEndpoint()}).Build()
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(querier))

Expand All @@ -608,7 +609,7 @@ groups:
},
Scheme: "http",
},
Timeout: model.Duration(time.Second),
Timeout: amTimeout,
APIVersion: alert.APIv1,
},
}, []query.Config{
Expand Down

0 comments on commit c01154d

Please sign in to comment.