Skip to content

Commit

Permalink
Merge branch 'master' into refine-list-display-commands
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Feb 19, 2021
2 parents 19b7b85 + b769ff8 commit 5084f9e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/cluster/embed/autogen_pkger.go

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions pkg/cluster/spec/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ type PrometheusSpec struct {
DataDir string `yaml:"data_dir,omitempty"`
LogDir string `yaml:"log_dir,omitempty"`
NumaNode string `yaml:"numa_node,omitempty" validate:"numa_node:editable"`
RemoteConfig Remote `yaml:"remote_config,omitempty" validate:"remote_config:ignore"`
Retention string `yaml:"storage_retention,omitempty" validate:"storage_retention:editable"`
ResourceControl meta.ResourceControl `yaml:"resource_control,omitempty" validate:"resource_control:editable"`
Arch string `yaml:"arch,omitempty"`
OS string `yaml:"os,omitempty"`
RuleDir string `yaml:"rule_dir,omitempty" validate:"rule_dir:editable"`
}

// Remote prometheus remote config
type Remote struct {
RemoteWrite []map[string]interface{} `yaml:"remote_write,omitempty" validate:"remote_write:ignore"`
RemoteRead []map[string]interface{} `yaml:"remote_read,omitempty" validate:"remote_read:ignore"`
}

// Role returns the component role of the instance
func (s PrometheusSpec) Role() string {
return ComponentPrometheus
Expand Down Expand Up @@ -245,6 +252,7 @@ func (i *MonitorInstance) InitConfig(
cfig.AddDMWorker(host, uint64(port))
}
}

if monitoredOptions != nil {
for host := range uniqueHosts {
cfig.AddNodeExpoertor(host, uint64(monitoredOptions.NodeExporterPort))
Expand All @@ -253,6 +261,12 @@ func (i *MonitorInstance) InitConfig(
}
}

remoteCfg, err := encodeRemoteCfg2Yaml(spec.RemoteConfig)
if err != nil {
return err
}
cfig.SetRemoteConfig(string(remoteCfg))

if spec.RuleDir != "" {
filter := func(name string) bool { return strings.HasSuffix(name, ".rules.yml") }
err := i.IteratorLocalConfigDir(ctx, spec.RuleDir, filter, func(name string) error {
Expand Down
15 changes: 15 additions & 0 deletions pkg/cluster/spec/server_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pingcap/tiup/pkg/logger/log"
"github.com/pingcap/tiup/pkg/meta"
"github.com/pingcap/tiup/pkg/utils"
"gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -218,6 +219,20 @@ func merge2Toml(comp string, global, overwrite map[string]interface{}) ([]byte,
return buf.Bytes(), nil
}

func encodeRemoteCfg2Yaml(remote Remote) ([]byte, error) {
if len(remote.RemoteRead) == 0 && len(remote.RemoteWrite) == 0 {
return []byte{}, nil
}

buf := bytes.NewBufferString("")
enc := yaml.NewEncoder(buf)
err := enc.Encode(remote)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}

func mergeImported(importConfig []byte, specConfigs ...map[string]interface{}) (map[string]interface{}, error) {
var configData map[string]interface{}
if err := toml.Unmarshal(importConfig, &configData); err != nil {
Expand Down
39 changes: 39 additions & 0 deletions pkg/cluster/spec/server_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,42 @@ func (s *configSuite) TestFoldMap(c *check.C) {
},
})
}

func (s *configSuite) TestEncodeRemoteCfg(c *check.C) {
yamlData := []byte(`remote_write:
- queue_config:
batch_send_deadline: 5m
capacity: 100000
max_samples_per_send: 10000
max_shards: 300
url: http://127.0.0.1:/8086/write
remote_read:
- url: http://127.0.0.1:/8086/read
- url: http://127.0.0.1:/8087/read
`)

bs, err := encodeRemoteCfg2Yaml(Remote{
RemoteWrite: []map[string]interface{}{
{
"url": "http://127.0.0.1:/8086/write",
"queue_config": map[string]interface{}{
"batch_send_deadline": "5m",
"capacity": 100000,
"max_samples_per_send": 10000,
"max_shards": 300,
},
},
},
RemoteRead: []map[string]interface{}{
{
"url": "http://127.0.0.1:/8086/read",
},
{
"url": "http://127.0.0.1:/8087/read",
},
},
})

c.Assert(err, check.IsNil)
c.Assert(bs, check.BytesEquals, yamlData)
}
9 changes: 8 additions & 1 deletion pkg/cluster/template/config/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ type PrometheusConfig struct {
DMMasterAddrs []string
DMWorkerAddrs []string

LocalRules []string
LocalRules []string
RemoteConfig string
}

// NewPrometheusConfig returns a PrometheusConfig
Expand Down Expand Up @@ -193,6 +194,12 @@ func (c *PrometheusConfig) AddLocalRule(rule string) *PrometheusConfig {
return c
}

// SetRemoteConfig set remote read/write config
func (c *PrometheusConfig) SetRemoteConfig(cfg string) *PrometheusConfig {
c.RemoteConfig = cfg
return c
}

// Config generate the config file data.
func (c *PrometheusConfig) Config() ([]byte, error) {
fp := path.Join("/templates", "config", "prometheus.yml.tpl")
Expand Down
3 changes: 3 additions & 0 deletions pkg/repository/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ func (l *httpMirror) Download(resource, targetDir string) error {
return nil
}
return r.Close()
}, utils.RetryOption{
Timeout: time.Hour,
Attempts: 3,
})
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions templates/config/prometheus.yml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,7 @@ scrape_configs:
- '{{.}}'
{{- end}}
{{- end}}

{{- if .RemoteConfig}}
{{.RemoteConfig}}
{{- end}}

0 comments on commit 5084f9e

Please sign in to comment.