Skip to content
Snippets Groups Projects
Commit 97708029 authored by Sergio Jimenez's avatar Sergio Jimenez
Browse files

feat(whitelist): Converted black to whitelist

* Defined global var for holding default metric groups
* Refactor removeGroup() to work with the whitelist
* Refactor TestRemoveGroup()
parent 4c1e817b
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,11 @@ type Mesos struct { ...@@ -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 { ...@@ -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 = ` ...@@ -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)
}
} }
} }
} }
......
...@@ -89,29 +89,25 @@ func TestRemoveGroup(t *testing.T) { ...@@ -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{ generateMetrics()
"resources", "master",
m := Mesos{
MetricsCol: []string{
"resources", "master", "registrar",
},
}
b := []string{
"system", "slaves", "frameworks", "system", "slaves", "frameworks",
"tasks", "messages", "evqueue", "messages", "evqueue",
"messages", "registrar",
} }
generateMetrics() m.removeGroup(&mesosMetrics)
for _, v := range j { for _, v := range b {
m := Mesos{
MetricsCol: []string{v},
}
m.removeGroup(&mesosMetrics)
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
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment