forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprometheus_config.go
98 lines (85 loc) · 2.46 KB
/
prometheus_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package prometheus
import (
"reflect"
"sort"
"strings"
"github.com/prometheus/prometheus/config"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
"gopkg.in/yaml.v2"
)
type PrometheusConfig struct {
Metadata core.Metadata `yaml:"metadata"`
Config `yaml:"config"`
Alerts string `yaml:"alerts"` // inline as a string, not processed by supergloo
Rules string `yaml:"rules"` // inline as a string, not processed by supergloo
}
func (c *PrometheusConfig) GetMetadata() core.Metadata {
return c.Metadata
}
func (c *PrometheusConfig) SetMetadata(meta core.Metadata) {
c.Metadata = meta
}
func (c *PrometheusConfig) Equal(that interface{}) bool {
return reflect.DeepEqual(c, that)
}
func (c *PrometheusConfig) Clone() *PrometheusConfig {
raw, err := yaml.Marshal(c)
if err != nil {
panic(err)
}
var newC PrometheusConfig
if err := yaml.Unmarshal(raw, &newC); err != nil {
panic(err)
}
return &newC
}
//type Config config.Config
type Config struct {
GlobalConfig *config.GlobalConfig `yaml:"global"`
AlertingConfig config.AlertingConfig `yaml:"alerting,omitempty"`
RuleFiles []string `yaml:"rule_files,omitempty"`
ScrapeConfigs []*config.ScrapeConfig `yaml:"scrape_configs,omitempty"`
RemoteWriteConfigs []*config.RemoteWriteConfig `yaml:"remote_write,omitempty"`
RemoteReadConfigs []*config.RemoteReadConfig `yaml:"remote_read,omitempty"`
}
func SortConfigs(scrapeConfigs []*config.ScrapeConfig) {
sort.SliceStable(scrapeConfigs, func(i, j int) bool {
return scrapeConfigs[i].JobName < scrapeConfigs[j].JobName
})
}
// returns number of added
func (cfg *Config) AddScrapeConfigs(scrapeConfigs []*config.ScrapeConfig) int {
var added int
for _, desiredScrapeConfig := range scrapeConfigs {
var found bool
for _, sc := range cfg.ScrapeConfigs {
if sc.JobName == desiredScrapeConfig.JobName {
found = true
break
}
}
if found {
continue
}
cfg.ScrapeConfigs = append(cfg.ScrapeConfigs, desiredScrapeConfig)
added++
}
SortConfigs(cfg.ScrapeConfigs)
return added
}
// returns number of removed
func (cfg *Config) RemoveScrapeConfigs(namePrefix string) int {
var removed int
// filter out jobs with the name prefix
var filteredJobs []*config.ScrapeConfig
for _, job := range cfg.ScrapeConfigs {
if strings.HasPrefix(job.JobName, namePrefix) {
removed++
continue
}
filteredJobs = append(filteredJobs, job)
}
cfg.ScrapeConfigs = filteredJobs
SortConfigs(cfg.ScrapeConfigs)
return removed
}