feat(whitelist): Converted black to whitelist

* Defined global var for holding default metric groups
* Refactor removeGroup() to work with the whitelist
* Refactor TestRemoveGroup()
This commit is contained in:
Sergio Jimenez 2016-02-04 02:46:20 +01:00 committed by Michele Fadda
parent 4b604d8d54
commit f7dda9742f
2 changed files with 46 additions and 27 deletions

View File

@ -21,6 +21,11 @@ type Mesos struct {
MetricsCol []string `toml:"metrics_collection"` MetricsCol []string `toml:"metrics_collection"`
} }
var defaultMetrics = []string{
"resources", "master", "system", "slaves", "frameworks",
"tasks", "messages", "evqueue", "messages", "registrar",
}
// SampleConfig returns a sample configuration block // SampleConfig returns a sample configuration block
func (m *Mesos) SampleConfig() string { func (m *Mesos) SampleConfig() string {
return sampleConfig return sampleConfig
@ -66,6 +71,27 @@ func (m *Mesos) Gather(acc telegraf.Accumulator) error {
return nil return nil
} }
func metricsDiff(w []string) []string {
b := []string{}
s := make(map[string]bool)
if len(w) == 0 {
return b
}
for _, v := range w {
s[v] = true
}
for _, d := range defaultMetrics {
if _, ok := s[d]; !ok {
b = append(b, d)
}
}
return b
}
func masterBlocks(g string) []string { func masterBlocks(g string) []string {
var m map[string][]string var m map[string][]string
@ -215,23 +241,20 @@ var sampleConfig = `
servers = ["localhost:5050"] servers = ["localhost:5050"]
# Metrics groups to be collected. # Metrics groups to be collected.
# Default, all enabled. # Default, all enabled.
metrics_collection = ["resources","master","system","slaves","frameworks","messages","evqueues","registrar"] metrics_collection = ["resources","master","system","slaves","frameworks","messages","evqueue","registrar"]
` `
// removeGroup(), remove blacklisted groups // removeGroup(), remove blacklisted groups
func (m *Mesos) removeGroup(j *map[string]interface{}) { func (m *Mesos) removeGroup(j *map[string]interface{}) {
var ok bool var ok bool
u := map[string]bool{}
for _, v := range m.MetricsCol { b := metricsDiff(m.MetricsCol)
for _, k := range masterBlocks(v) {
u[k] = true
}
}
for k, _ := range u { for _, k := range b {
if _, ok = (*j)[k]; ok { for _, v := range masterBlocks(k) {
delete((*j), k) if _, ok = (*j)[v]; ok {
delete((*j), v)
}
} }
} }
} }

View File

@ -89,29 +89,25 @@ func TestRemoveGroup(t *testing.T) {
//t.Skip("needs refactoring") //t.Skip("needs refactoring")
// FIXME: removeGroup() behavior is the opposite as it was, // FIXME: removeGroup() behavior is the opposite as it was,
// this test has to be refactored // this test has to be refactored
j := []string{
"resources", "master",
"system", "slaves", "frameworks",
"tasks", "messages", "evqueue",
"messages", "registrar",
}
generateMetrics() generateMetrics()
for _, v := range j {
m := Mesos{ m := Mesos{
MetricsCol: []string{v}, MetricsCol: []string{
"resources", "master", "registrar",
},
} }
b := []string{
"system", "slaves", "frameworks",
"messages", "evqueue",
}
m.removeGroup(&mesosMetrics) m.removeGroup(&mesosMetrics)
for _, v := range b {
for _, x := range masterBlocks(v) { for _, x := range masterBlocks(v) {
if _, ok := mesosMetrics[x]; ok { if _, ok := mesosMetrics[x]; ok {
t.Errorf("Found key %s, it should be gone.", x) t.Errorf("Found key %s, it should be gone.", x)
} }
} }
} }
if len(mesosMetrics) > 0 {
t.Error("Keys were left at slice sample")
}
//Test for wrong keys
} }