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"`
}
var defaultMetrics = []string{
"resources", "master", "system", "slaves", "frameworks",
"tasks", "messages", "evqueue", "messages", "registrar",
}
// SampleConfig returns a sample configuration block
func (m *Mesos) SampleConfig() string {
return sampleConfig
@ -66,6 +71,27 @@ func (m *Mesos) Gather(acc telegraf.Accumulator) error {
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 {
var m map[string][]string
@ -215,23 +241,20 @@ var sampleConfig = `
servers = ["localhost:5050"]
# Metrics groups to be collected.
# 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
func (m *Mesos) removeGroup(j *map[string]interface{}) {
var ok bool
u := map[string]bool{}
for _, v := range m.MetricsCol {
for _, k := range masterBlocks(v) {
u[k] = true
}
}
b := metricsDiff(m.MetricsCol)
for k, _ := range u {
if _, ok = (*j)[k]; ok {
delete((*j), k)
for _, k := range b {
for _, v := range masterBlocks(k) {
if _, ok = (*j)[v]; ok {
delete((*j), v)
}
}
}
}

View File

@ -89,29 +89,25 @@ func TestRemoveGroup(t *testing.T) {
//t.Skip("needs refactoring")
// FIXME: removeGroup() behavior is the opposite as it was,
// this test has to be refactored
j := []string{
"resources", "master",
"system", "slaves", "frameworks",
"tasks", "messages", "evqueue",
"messages", "registrar",
}
generateMetrics()
for _, v := range j {
m := Mesos{
MetricsCol: []string{v},
}
m.removeGroup(&mesosMetrics)
m := Mesos{
MetricsCol: []string{
"resources", "master", "registrar",
},
}
b := []string{
"system", "slaves", "frameworks",
"messages", "evqueue",
}
m.removeGroup(&mesosMetrics)
for _, v := range b {
for _, x := range masterBlocks(v) {
if _, ok := mesosMetrics[x]; ok {
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
}