Skip to content

Commit

Permalink
Use Prometheus' remote write config instead of rolling another
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Okoko <[email protected]>
  • Loading branch information
idoqo committed Sep 17, 2021
1 parent 79b06d3 commit 6b0612c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 37 deletions.
12 changes: 7 additions & 5 deletions cmd/thanos/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/common/model"
"github.com/prometheus/common/route"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/relabel"
"github.com/prometheus/prometheus/promql"
Expand Down Expand Up @@ -121,7 +122,7 @@ 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.", extflag.WithEnvSubstitution())
conf.rwConfig = extflag.RegisterPathOrContent(cmd, "remote-write.config", "YAML config for the remote-write server 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())

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", "")

Expand Down Expand Up @@ -337,20 +338,20 @@ func runRule(
}

if len(rwCfgYAML) > 0 {
var rwCfg remotewrite.Config
var rwCfg config.RemoteWriteConfig
rwCfg, err = remotewrite.LoadRemoteWriteConfig(rwCfgYAML)
if err != nil {
return err
}
walDir := filepath.Join(conf.dataDir, rwCfg.Name)
remoteStore, err := remotewrite.NewFanoutStorage(logger, reg, walDir, rwCfg)
remoteStore, err := remotewrite.NewFanoutStorage(logger, reg, walDir, &rwCfg)
if err != nil {
return errors.Wrap(err, "set up remote-write store for ruler")
}
appendable = remoteStore
queryable = remoteStore
} else {
db, err := tsdb.Open(conf.dataDir, log.With(logger, "component", "tsdb"), reg, tsdbOpts, nil)
db, err = tsdb.Open(conf.dataDir, log.With(logger, "component", "tsdb"), reg, tsdbOpts, nil)
if err != nil {
return errors.Wrap(err, "open TSDB")
}
Expand Down Expand Up @@ -555,7 +556,7 @@ func runRule(
)

// Start gRPC server.
{
if db != nil {
tsdbStore := store.NewTSDBStore(logger, db, component.Rule, conf.lset)

tlsCfg, err := tls.NewServerConfig(log.With(logger, "protocol", "gRPC"), conf.grpc.tlsSrvCert, conf.grpc.tlsSrvKey, conf.grpc.tlsSrvClientCA)
Expand All @@ -580,6 +581,7 @@ func runRule(
s.Shutdown(err)
})
}

// Start UI & metrics HTTP server.
{
router := route.New()
Expand Down
25 changes: 14 additions & 11 deletions docs/components/rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,19 +348,22 @@ Flags:
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.
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.
--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.
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.
--request.logging-config=<content>
Alternative to 'request.logging-config-file'
flag (mutually exclusive). Content of YAML file
Expand Down
16 changes: 5 additions & 11 deletions pkg/rules/remotewrite/remotewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@ 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
// LoadRemoteWriteConfig prepares a RemoteWriteConfig instance from a given YAML config.
func LoadRemoteWriteConfig(configYAML []byte) (config.RemoteWriteConfig, error) {
var cfg config.RemoteWriteConfig
if err := yaml.Unmarshal(configYAML, &cfg); err != nil {
return cfg, err
}
Expand All @@ -33,15 +27,15 @@ func LoadRemoteWriteConfig(configYAML []byte) (Config, error) {

// 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) {
func NewFanoutStorage(logger log.Logger, reg prometheus.Registerer, walDir string, rwConfig *config.RemoteWriteConfig) (storage.Storage, error) {
walStore, err := NewStorage(logger, reg, walDir)
if err != nil {
return nil, err
}
remoteStore := remote.NewStorage(logger, reg, walStore.StartTime, walStore.Directory(), 1*time.Minute, nil)
if err := remoteStore.ApplyConfig(&config.Config{
GlobalConfig: config.DefaultGlobalConfig,
RemoteWriteConfigs: []*config.RemoteWriteConfig{rwConfig.RemoteStore},
RemoteWriteConfigs: []*config.RemoteWriteConfig{rwConfig},
}); err != nil {
return nil, errors.Wrap(err, "applying config to remote storage")
}
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/e2ethanos/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/grafana/dskit/backoff"
"github.com/pkg/errors"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/pkg/relabel"
"gopkg.in/yaml.v2"
Expand All @@ -26,7 +27,6 @@ import (
"github.com/thanos-io/thanos/pkg/query"
"github.com/thanos-io/thanos/pkg/queryfrontend"
"github.com/thanos-io/thanos/pkg/receive"
"github.com/thanos-io/thanos/pkg/rules/remotewrite"
)

const infoLogLevel = "info"
Expand Down Expand Up @@ -465,11 +465,11 @@ func NewTSDBRuler(sharedDir string, name string, ruleSubDir string, amCfg []aler
return newRuler(sharedDir, name, ruleSubDir, amCfg, queryCfg, nil)
}

func NewStatelessRuler(sharedDir string, name string, ruleSubDir string, amCfg []alert.AlertmanagerConfig, queryCfg []query.Config, remoteWriteCfg *remotewrite.Config) (*Service, error) {
func NewStatelessRuler(sharedDir string, name string, ruleSubDir string, amCfg []alert.AlertmanagerConfig, queryCfg []query.Config, remoteWriteCfg *config.RemoteWriteConfig) (*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) {
func newRuler(sharedDir string, name string, ruleSubDir string, amCfg []alert.AlertmanagerConfig, queryCfg []query.Config, remoteWriteCfg *config.RemoteWriteConfig) (*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
10 changes: 3 additions & 7 deletions test/e2e/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
http_util "github.com/thanos-io/thanos/pkg/http"
"github.com/thanos-io/thanos/pkg/promclient"
"github.com/thanos-io/thanos/pkg/query"
"github.com/thanos-io/thanos/pkg/rules/remotewrite"
"github.com/thanos-io/thanos/pkg/rules/rulespb"
"github.com/thanos-io/thanos/pkg/testutil"
"github.com/thanos-io/thanos/test/e2e/e2ethanos"
Expand Down Expand Up @@ -500,12 +499,9 @@ func TestRule_CanRemoteWriteData(t *testing.T) {
Scheme: "http",
},
},
}, &remotewrite.Config{
Name: "ruler-rw-receivers",
RemoteStore: &config.RemoteWriteConfig{
URL: &common_cfg.URL{URL: rwURL},
Name: "thanos-receiver",
},
}, &config.RemoteWriteConfig{
URL: &common_cfg.URL{URL: rwURL},
Name: "thanos-receiver",
})
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(r))
Expand Down

0 comments on commit 6b0612c

Please sign in to comment.