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 <okokomichaels@outlook.com>
  • Loading branch information
idoqo committed Sep 17, 2021
1 parent 79b06d3 commit 86522b1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
10 changes: 6 additions & 4 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 @@ -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
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 86522b1

Please sign in to comment.