Skip to content

Commit

Permalink
Feature/add prometheus local rules (#1092)
Browse files Browse the repository at this point in the history
* feat(cluster/spec): add local rules if given

* feat(cluster): upate autogen_pkger
  • Loading branch information
jsvisa authored Jan 25, 2021
1 parent d85cb88 commit c5c9f13
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/cluster/embed/autogen_pkger.go

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions pkg/cluster/spec/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,29 @@ func (i *BaseInstance) TransferLocalConfigFile(ctx context.Context, e ctxt.Execu
// TransferLocalConfigDir scp local config directory to remote
// Precondition: the user on remote have right to access & mkdir of dest files
func (i *BaseInstance) TransferLocalConfigDir(ctx context.Context, e ctxt.Executor, local, remote string, filter func(string) bool) error {
return i.IteratorLocalConfigDir(ctx, local, filter, func(fname string) error {
localPath := path.Join(local, fname)
remotePath := path.Join(remote, fname)
if err := i.TransferLocalConfigFile(ctx, e, localPath, remotePath); err != nil {
return errors.Annotatef(err, "transfer local config (%s -> %s) failed", localPath, remotePath)
}
return nil
})
}

// IteratorLocalConfigDir iterators the local dir with filter, then invoke f for each found fileName
func (i *BaseInstance) IteratorLocalConfigDir(ctx context.Context, local string, filter func(string) bool, f func(string) error) error {
files, err := ioutil.ReadDir(local)
if err != nil {
return errors.Annotatef(err, "read local directory %s failed", local)
}

for _, f := range files {
if filter != nil && !filter(f.Name()) {
for _, file := range files {
if filter != nil && !filter(file.Name()) {
continue
}
localPath := path.Join(local, f.Name())
remotePath := path.Join(remote, f.Name())
if err := i.TransferLocalConfigFile(ctx, e, localPath, remotePath); err != nil {
return errors.Annotatef(err, "transfer local config (%s -> %s) failed", localPath, remotePath)
if err := f(file.Name()); err != nil {
return err
}
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/cluster/spec/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ func (i *MonitorInstance) InitConfig(
}
}

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 {
cfig.AddLocalRule(name)
return nil
})
if err != nil {
return errors.Annotate(err, "add local rule")
}
}

if err := i.installRules(ctx, e, paths.Deploy, clusterVersion); err != nil {
return errors.Annotate(err, "install rules")
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/cluster/template/config/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type PrometheusConfig struct {

DMMasterAddrs []string
DMWorkerAddrs []string

LocalRules []string
}

// NewPrometheusConfig returns a PrometheusConfig
Expand Down Expand Up @@ -185,6 +187,12 @@ func (c *PrometheusConfig) AddDMWorker(ip string, port uint64) *PrometheusConfig
return c
}

// AddLocalRule add a local rule
func (c *PrometheusConfig) AddLocalRule(rule string) *PrometheusConfig {
c.LocalRules = append(c.LocalRules, rule)
return c
}

// Config generate the config file data.
func (c *PrometheusConfig) Config() ([]byte, error) {
fp := path.Join("/templates", "config", "prometheus.yml.tpl")
Expand Down
11 changes: 7 additions & 4 deletions templates/config/prometheus.yml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ rule_files:
- 'blacker.rules.yml'
- 'bypass.rules.yml'
{{- end}}
{{- range .LocalRules}}
- '{{.}}'
{{- end}}
{{- if .PDAddrs}}
- 'pd.rules.yml'
{{- end}}
Expand Down Expand Up @@ -48,11 +51,11 @@ rule_files:

{{- if .AlertmanagerAddrs}}
alerting:
alertmanagers:
- static_configs:
- targets:
alertmanagers:
- static_configs:
- targets:
{{- range .AlertmanagerAddrs}}
- '{{.}}'
- '{{.}}'
{{- end}}
{{- end}}

Expand Down

0 comments on commit c5c9f13

Please sign in to comment.