Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cluster: add additional prom scrape_configs #1641

Merged
merged 4 commits into from
Dec 6, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pkg/cluster/spec/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"crypto/tls"
"fmt"
"os"
"path"
"path/filepath"
"reflect"
Expand All @@ -29,6 +30,7 @@ import (
"github.com/pingcap/tiup/pkg/cluster/template/scripts"
"github.com/pingcap/tiup/pkg/meta"
"github.com/pingcap/tiup/pkg/set"
"gopkg.in/yaml.v3"
)

// PrometheusSpec represents the Prometheus Server topology specification in topology.yaml
Expand All @@ -51,6 +53,7 @@ type PrometheusSpec struct {
Arch string `yaml:"arch,omitempty"`
OS string `yaml:"os,omitempty"`
RuleDir string `yaml:"rule_dir,omitempty" validate:"rule_dir:editable"`
AdditionalScrapeConf map[string]interface{} `yaml:"additional_scrape_conf,omitempty" validate:"additional_scrape_conf:ignore"`
}

// Remote prometheus remote config
Expand Down Expand Up @@ -336,6 +339,12 @@ func (i *MonitorInstance) InitConfig(
if err := cfig.ConfigToFile(fp); err != nil {
return err
}
if spec.AdditionalScrapeConf != nil {
err = mergeAdditionalScrapeConf(fp, spec.AdditionalScrapeConf)
if err != nil {
return err
}
}
dst = filepath.Join(paths.Deploy, "conf", "prometheus.yml")
if err := e.Transfer(ctx, fp, dst, false, 0, false); err != nil {
return err
Expand Down Expand Up @@ -428,3 +437,26 @@ func (i *MonitorInstance) ScaleConfig(
i.topo = topo
return i.InitConfig(ctx, e, clusterName, clusterVersion, deployUser, paths)
}

func mergeAdditionalScrapeConf(source string, addition map[string]interface{}) error {
var result map[string]interface{}
bytes, err := os.ReadFile(source)
if err != nil {
return err
}
err = yaml.Unmarshal(bytes, &result)
if err != nil {
return err
}

for _, job := range result["scrape_configs"].([]interface{}) {
for k, v := range addition {
job.(map[string]interface{})[k] = v
}
}
bytes, err = yaml.Marshal(result)
if err != nil {
return err
}
return os.WriteFile(source, bytes, 0644)
}
nexustar marked this conversation as resolved.
Show resolved Hide resolved